• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help understanding a method  RSS feed

 
A Arnold
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everyone!

I'm just starting out on Java with an old textbook that I have had lying around and doing the exercises to get me familiar with Java.
So I am working on a problem in this book that is talking about the Rectangle class and locating this method in the API:



What the book is asking is I read through the API to figure out what this code would do and create a program if I don't understand:



I didn't quite understand what the API was talking about, so I wrote a little program:



What I saw was the x and y were now 0,0.
So does this mean that my rectangle shrank in size to accommodate this new point or did it increase in size to accommodate this new point?
Could someone let me know?

Thanks!
 
Naziru Gelajo
Ranch Hand
Posts: 175
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Arnold,

The void add (int newx, int newy) is a method (judging by its name) that has 2 integer parameters, newx and newy. The add function adds both newx and newy together. However, That's what I assume, I can't tell you for sure without the source code. However, since it's a void method, it does not return any value.

Rectangle box = new Rectangle(5, 10, 20, 30) creates a new rectangle object known as box using a constructor with 4 parameters (x, y, width, height) if I'm not mistaken so x = 5, y = 10, these are the positional requirements and 20 is the width and 30 is the height. I hope this helps. Please let me know if this is not clear. Thanks!
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would tend to agree with Naziru's assumption, but like he/she said, it's just an assumption. The API documentation is there so you don't have to make assumptions, it should tell you what the method does.

But you said you already read it, and didn't quite understand it. That's fine, it happens to all of us. And trying an example is often a useful thing to do. But if you wanted somebody to help clarify your understanding, it would help if you told us (a) what the documentation said, and (b) what you thought was confusing or over your head or vague or whatever.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That does sound like an old book if it uses the Rectangle class. I think most poeple nowadays would use this instead. You can find out more about it in Java™ Tutorials, especially this section. But that doesn't answer your current question. If you look for the add method, it says this:-
public void add(int newx,
int newy)

Adds a point, specified by the integer arguments newx,newy to the bounds of this Rectangle.

If this Rectangle has any dimension less than zero, the rules for non-existant rectangles apply. In that case, the new bounds of this Rectangle will have a location equal to the specified coordinates and width and height equal to zero.

After adding a point, a call to contains with the added point as an argument does not necessarily return true. The contains method does not return true for points on the right or bottom edges of a Rectangle. Therefore, if the added point falls on the right or bottom edge of the enlarged Rectangle, contains returns false for that point. If the specified point must be contained within the new Rectangle, a 1x1 rectangle should be added instead:

r.add(newx, newy, 1, 1);


Parameters:
newx - the X coordinate of the new point
newy - the Y coordinate of the new point
And that is confusing, isn't it. I think it means it creates a Point object at (xy) and changes the size of the Rectangle object to include that Point. But I am not certain. The Point is not considered as included if it is on the lower or right edges. I don't think you are going to get a good explanation of that method anywhere; you might be able to paint that Rectangle object and add points to it. But why? It is old AWT code which has been superseded by Swing and Oracle seem to think that Swing has been superseded by JavaFX, so they are probably no longer updating either.

How old is your book? If it is old enough to talk about JDK1.4.2 or older, I think it should be consigned to the archives as of historic interest only (I have a Java1.3.1 book somewhere). There were so many changes in Java5 that anything older is obsolete.
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The add(newX,newY) method of Rectangle may or may not alter any of the 4 fields. If the point (newX,newY) is already inside the rectangle, nothing happens.
If newX > x+width, then width is increased such that newX becomes the right side of the rectangle. If newX < x, then x is set to newX and width is set to width + (x-newX).
The same idea applies to newY. Thus the rectangle is never shrunk, but it can get larger.
 
A Arnold
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you everyone for your thoughts on this!
@Campbell Ritchie Thanks for the advise! I think the book I am using is too old. I just looked at the copyright date and it said 2005! I'm going to go to Amazon and see if I can find a newer version of this book so I get educated on the latest version of Java.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which book is it? 2005 means it probably uses Java5, even though that Rectangle method is rather out of date. Most people would use Rectangle2D instead. Does the book give decent coverage of
  • generics
  • threading with executor service and locks
  • enumerated types (keyword enum, not interface Enumeration)
  • for-each loops
  • variable argument lists
  • and annotations (though annotations were just beginning their use in 2005)?
  • If yes, then I suggest you keep that book and update yourself to Java8 with a book like Java 8 for the really impatient (or similar) by Cay Horstmann. Just a suggestion; you may prefer to do something different.
     
    A Arnold
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Campbell!

    The book that I have actually is Java Concepts by Cay Horsmann 4th edition.
    It is actually a good book and I like it. It looks like it does have some of the things you are talking about, but I think I might check out that book you suggested since it is also Cay's work. I seem to connect with this material quite a bit.
    I did also buy Intro to Java Programming by Liang 9th edition yesterday since I liked his book in an Amazon preview and it came highly recommended on Reddit.
    Thanks for the suggestions! I can use all of them that I can get on this journey.
     
    Campbell Ritchie
    Marshal
    Posts: 56518
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I have always liked Horstmann's book myself. I have Core JavaII 2005 edition which does cover Java5. My suggestion about his Java8 for the impatient book might still be a good one.
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!