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?
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!
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.
And that is confusing, isn't it. I think it means it creates a Point object at (x, y) 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.
public void add(int newx,
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);
newx - the X coordinate of the new point
newy - the Y coordinate of the new point
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.
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.
@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.
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.