Hi all!
I have moved on to learning about classes.....they don't seem as bad as the past things I have done so far. I have, however, hit a few snags along the way.
Here is my code for my Rectangle class

``````public class Rectangle
{
private int x;
private int y;
private int height;
private int width;

public Rectangle(int initialX, int initialY, int initialWidth, int initialHeight)
{
x = initialX;
y = initialY;
width = initialWidth;
height = initialHeight;
}
public int getHeight()
{
return height;
}
public int getWidth()
{
return width;
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
public boolean equals(Rectangle o)
{
return (x == o.x
&& y == o.y
&& width == o.width
&& height == o.height);
}
}``````

And here is the code for my client class.

``````public class testRectangle
{
public static void main(String[] args)
{
Rectangle r1 = new Rectangle(10,10,100,100);
Rectangle r2 = new Rectangle(5,5,50,50);

System.out.println("Coordinate X is: (" + r1.getX() + ")");
System.out.println("Coordinate Y is: (" + r1.getY() + ")");
System.out.println("The Width is: (" + r1.getWidth() + ")");
System.out.println("The Length is: (" + r1.getHeight() + ")");

if(r1.equals(r2))
System.out.println("Both Rectangles Are Equal!");
else
System.out.println("Both Rectangles Are Not Equal!");
}
}``````

Ok now here are my issues....I know most of you have seen this exact same method done to death BUT what is the point of the coordinates? Why are they needed and what do they do? Did I do the width and height incorrectly and the coordinates are meant to determine these 2 things? I am confused as to why the coordinates x and y are even needed. And also, is my boolean method in my rectangle class correct?
And for my way of checking if both rectangles are equal in my client...is this the correct way of doing it?
I value all and any input please.....criticize my code as harshly as you can so that I can better improve my skills in programming. positive comments are also welcome as the boost morale LOL =)

Oh ok and now I remember another issue.....within my Rectangle class with the public Rectangle(int x, int y, int width, int height)
How do I fix it so that it throws an illegal exception if the height or width are negative?

## peter_budo 2,516

Ok now here are my issues....I know most of you have seen this exact same method done to death BUT what is the point of the coordinates? Why are they needed and what do they do? Did I do the width and height incorrectly and the coordinates are meant to determine these 2 things? I am confused as to why the coordinates x and y are even needed. And also, is my boolean method in my rectangle class correct?
And for my way of checking if both rectangles are equal in my client...is this the correct way of doing it?
I value all and any input please.....criticize my code as harshly as you can so that I can better improve my skills in programming. positive comments are also welcome as the boost morale LOL =)

Coordinates are for giving starting position, without that you wouldn't know where to place the object on the screen

With your equal I think that just with and height comparison would do. I do not see reason to compare it with coordinates if you are not trying to find out if a object is the object you looking for given x,y coordinates with width and height (you may want to ask your tutor on more details for it)

To prevent negative value entry just run a validation check before you assigned values to class variables something like

``````public Rectangle(int initialX, int initialY, int initialWidth, int initialHeight)
{
if(validateValues(width, height)){
x = initialX;
y = initialY;
width = initialWidth;
height = initialHeight;
}
else{
throw new Exception();
}
}``````

validateValues a method you will have to write...

OH! So that is how I do the negative entry thing.
I was thinking it had to be something like when you are using files and you have to place that throws FileNotFoundException in the program. I was searching everywhere and still could not find it. Stupid me it is something I have to create.

Ok I am getting an error with the exception part

``````public class Rectangle
{
//the private variables
private int x;
private int y;
private int height;
private int width;
//default constructor that sets values to 0
public Rectangle()
{
setRectangle(0,0,0,0);
}
//non-default constructor that sets values based on input
public void setRectangle(int initialX, int initialY, int initialWidth, int initialHeight)
{
if(isValid(width,height))
{
x = initialX;
y = initialY;
width = initialWidth;
height = initialHeight;
}
else
{
throw new Exception();
}
}
//method for returning the height of the rectangle
public int getHeight()
{
return height;
}
//method for returning the width of the rectangle
public int getWidth()
{
return width;
}
//method for returning the X coordinate
public int getX()
{
return x;
}
//method for returning the Y coordinate
public int getY()
{
return y;
}
//boolean method for comparing rectangles and seeing if the values are equal
public boolean equals(Rectangle o)
{
return (x == o.x
&& y == o.y
&& width == o.width
&& height == o.height);
}
public boolean isValid(int width, int height)
{
return (width >= 0
&& height >= 0);
}
}``````

It says unreported exception java.lang.Exception; must be caught or declared to be thrown.

## quuba 81

unreported exception java.lang.Exception;
TWO WAYS
must be caught //handling error in place
-->

``````else {
try {
throw new Exception();
} catch (Exception ex) {
// here your reaction on error
}
}``````

or
declared to be thrown.

``public void setRectangle(int initialX, int initialY, int initialWidth, int initialHeight) throws Exception {``

//propagate error

commented: Yay, replied faster then me. Good one +15

## peter_budo 2,516

Did you put your initialization in try - catch statement?

``````try{
Rectangle rec = new Rectangle(0,0,10,10);
/*
* REST OF CODE TO PROCESS
*/
}catch(Exception e){
e.printStackTrace();
}``````

## quuba 81

Tankadin, another remark, you changed second constructor to method!

Ok now I tried to make fixes....I needed to change everything to accept user input for the values as well. I am still having an issue with my exception portion. I can enter all negative numbers for each input and it will still just print out everything normally with the output just having negative numbers in it.
Here is my updated Rectangle class:

``````public class Rectangle
{
//the private variables
private int x;
private int y;
private int height;
private int width;
//default constructor that sets values to 0
public Rectangle()
{
int x = 0;
int y = 0;
int height = 0;

}
//non-default constructor that sets values based on input
public Rectangle(int x, int y, int width, int height)
{
setRectangle(x,y,width,height);
}
public void setRectangle(int initialX, int initialY, int initialWidth, int initialHeight)
{
if(isValid(x,y,width,height))
{
x = initialX;
y = initialY;
width = initialWidth;
height = initialHeight;
}
else
{
try
{
throw new Exception();
}
catch (Exception ex)
{
System.out.println("Invalid form of input!");
}
}
}

//method for returning the height of the rectangle
public int getHeight()
{
return height;
}
//method for returning the width of the rectangle
public int getWidth()
{
return width;
}
//method for returning the X coordinate
public int getX()
{
return x;
}
//method for returning the Y coordinate
public int getY()
{
return y;
}
//boolean method for comparing rectangles and seeing if the values are equal
public boolean equals(Rectangle o)
{
return (x == o.x
&& y == o.y
&& width == o.width
&& height == o.height);
}
public boolean isValid(int x, int y, int width, int height)
{
return (x >=0
&& y >= 0
&&width >= 0
&& height >= 0);
}
}``````

Here is my updated client class

``````import java.util.*;
public class testRectangle
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
int x;
int y;
int width;
int height;
//declaring my two separate rectangles
Rectangle r1 = new Rectangle();
Rectangle r2 = new Rectangle();
//asking for input on rectangle one
System.out.println("Enter Coordinate X for Rectangle One");
x = console.nextInt();
System.out.println("Enter Coordinate Y for Rectangle One");
y = console.nextInt();
System.out.println("Enter Width for Rectangle One");
width = console.nextInt();
System.out.println("Enter Height for Rectangle One");
height = console.nextInt();
//sets the input values into the rectangle one
r1.setRectangle(x,y,width,height);
//asking for input on rectangle two
System.out.println("Enter Coordinate X for Rectangle Two");
x = console.nextInt();
System.out.println("Enter Coordinate Y for Rectangle Two");
y = console.nextInt();
System.out.println("Enter Width for Rectangle Two");
width = console.nextInt();
System.out.println("Enter Height for Rectangle Two");
height = console.nextInt();
//sets the input values into the rectangle two
r2.setRectangle(x,y,width,height);
//print statements for getting all of the values needed.
System.out.println("Rectangle One: Coordinate X is: (" + r1.getX() + ")");
System.out.println("Rectangle One: Coordinate Y is: (" + r1.getY() + ")");
System.out.println("Rectangle One: The Width is: (" + r1.getWidth() + ")");
System.out.println("Rectangle One: The Height is: (" + r1.getHeight() + ")");
System.out.println();
System.out.println("Rectangle Two: Coordinate X is: (" + r2.getX() + ")");
System.out.println("Rectangle Two: Coordinate Y is: (" + r2.getY() + ")");
System.out.println("Rectangle Two: The Width is: (" + r2.getWidth() + ")");
System.out.println("Rectangle Two: The Height is: (" + r2.getHeight() + ")");
//comparing the two rectangles and attempting to find if they are equal or not
if(r1.equals(r2))
System.out.println("Both Rectangles Are Equal!");
else
System.out.println("Both Rectangles Are Not Equal!");
}
}``````

## firstPerson 761

For you exception handling, it would make sense if you propagate the exception to the client instead of throwing it yourself and catching it. If thats the case then you need not to throw any exception, you can simply use a boolean statement. And to further extend your exception handling, create your own exception class call it NegativeDimensionException which extends from the java standard exception. Then you can throw a more meaningful exception which lets the user know what to specifically to expect. Its just an idea.

Bleh I'd like to keep the exception portion simple like I have...just working.
But a new problem has arisen. Apparently I am not supposed to have the setRectangle method in my Rectangle class. My mistake of course for misreading instructions. I need the default and non default constructor of course. How do I have everything in my non-default constructor....way to accept input and do the exceptions? I can't figure out how to do this in conjunction with using the client class. I figured I needed the method to set the values based on the user input.

So far the code is looking good for my rectangle class but is there anyway other than what you guys have told me that is simpler to do the exception thing?
I have googled like crazy and found nothing but a lot of high lvl stuff.

``````public class Rectangle
{
//the private variables
private int x;
private int y;
private int height;
private int width;
//default constructor that sets values to 0
public Rectangle()
{
int x = 0;
int y = 0;
int height = 0;
int width = 0;

}
//non-default constructor that sets values based on input
public Rectangle(int x, int y, int width, int height)
{
setX(x);
setY(y);
setWidth(width);
setHeight(height);
}
//method for returning the height of the rectangle
public int getHeight()
{
return height;
}
//method for returning the width of the rectangle
public int getWidth()
{
return width;
}
//method for returning the X coordinate
public int getX()
{
return x;
}
//method for returning the Y coordinate
public int getY()
{
return y;
}
//boolean method for comparing rectangles and seeing if the values are equal
public boolean equals(Rectangle o)
{
return (x == o.x
&& y == o.y
&& width == o.width
&& height == o.height);
}

public void setX(int newX)
{
if (newX >= 0)
{
x = newX;
}
else
{
System.out.println("Invalid Number!");
}

}
public void setY(int newY)
{
y = newY;
}
public void setWidth(int newWidth)
{
width = newWidth;
}
public void setHeight(int newHeight)
{
height = newHeight;
}
}``````

My system.out.println in my setX method just prints the message and moves on to the next input in client class.

## quuba 81

are rectangles the same?
Rectangle(0, 0, -10, 9)
Rectangle(-10, 0, 10, 9);

``````public class Rectangle {

private int x;
private int y;
private int width;
private int height;

public Rectangle() {
this(0, 0, 0, 0);
}

public Rectangle(int initialX, int initialY, int initialWidth, int initialHeight) {
x = initialX;
y = initialY;
width = initialWidth;
height = initialHeight;
}

public int getHeight() {
return height;
}

public int getWidth() {
return width;
}

public int getX() {
return x;
}

public int getY() {
return y;
}
/*
public boolean equals(Rectangle r) {
System.out.println("//used method equals(Rectangle r)");
return (x == r.getX()
&& y == r.getY()
&& width == r.getWidth()
&& height == r.getHeight());
}
*/

public Rectangle getNormalized() {
int bx = x;
int bwidth = width;
if (width < 0) {
bwidth = -width;
bx -= bwidth;
}
//TODO: not fully implemented, check height
return new Rectangle(bx, y, bwidth, height);
}

//Overrides method from: java.lang.Object
@Override
public boolean equals(Object o) {
System.out.println("//used method equals(Object o)");
if (o instanceof Rectangle) {
Rectangle r = (Rectangle) o;
return (x == r.getX()
&& y == r.getY()
&& width == r.getWidth()
&& height == r.getHeight());
} else {
return false;
}
}

@Override
public int hashCode() {
int hash = 3;
hash = 37 * hash + this.x;
hash = 37 * hash + this.y;
hash = 37 * hash + this.height;
hash = 37 * hash + this.width;
return hash;
}

public static boolean isValid(int width0, int height0) {
return (width0 >= 0 && height0 >= 0);
}

public boolean isValid() {
return (width >= 0 && height >= 0);
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("Rectangle[");
sb.append("Coordinate X is:(" + getX() + "), ");
sb.append("Coordinate Y is:(" + getY() + "), ");
sb.append("The Width is:(" + getWidth() + "), ");
sb.append("The Length is:(" + getHeight() + "), ");
sb.append("Is Valid:(" + isValid() + ") ");
sb.append("]");
return sb.toString();
}

public static void main(String[] args) {
Rectangle r1 = new Rectangle(10, 10, 100, 100);
System.out.println(r1);

Rectangle r2 = new Rectangle(5, 5, 50, 50);
System.out.println(r2);
if (r2.equals(r2)) {
System.out.println("Both Rectangles Are Equal!");
} else {
System.out.println("Both Rectangles Are Not Equal!");
}
//
System.out.println();
//

Rectangle e1 = new Rectangle(0, 0, -10, 9);
System.out.println(e1);
Rectangle e1bis = e1.getNormalized();
System.out.println(e1bis);

Rectangle e2 = new Rectangle(-10, 0, 10, 9);
System.out.println(e2);
if (e1.equals(e2)) {
System.out.println("Both e1,e2 Rectangles Are Equal!");
} else {
System.out.println("Both e1,e2 Rectangles Are Not Equal!");
}

if (e1bis.equals(e2)) {
System.out.println("Both e1bis,e2 Rectangles Are Equal!");
} else {
System.out.println("Both e1bis,e2 Rectangles Are Not Equal!");
}
}
}``````

Try draw on paper e1 and e2.

Wow what did you do? LOL
The only issue with my program is me trying to do something for throwing exceptions when the user in the client class enters a negative value. I have the exception I was trying out in one of my set methods as to see what happens.

Well it is all solved for any that might find it useful.

``````public class Rectangle
{
//the private variables
private int x;
private int y;
private int height;
private int width;
//default constructor that sets values to 0
public Rectangle()
{
int x = 0;
int y = 0;
int height = 0;
int width = 0;

}
//non-default constructor that sets values based on input
public Rectangle(int x, int y, int width, int height)
{
setX(x);
setY(y);
setWidth(width);
setHeight(height);
}
//method for returning the height of the rectangle
public int getHeight()
{
return height;
}
//method for returning the width of the rectangle
public int getWidth()
{
return width;
}
//method for returning the X coordinate
public int getX()
{
return x;
}
//method for returning the Y coordinate
public int getY()
{
return y;
}
//boolean method for comparing rectangles and seeing if the values are equal
public boolean equals(Rectangle o)
{
return (x == o.x
&& y == o.y
&& width == o.width
&& height == o.height);
}

public void setX(int newX)
{
if (newX < 0)
{
throw new IllegalArgumentException("Invalid Data Entered!");
}
else
{
x = newX;
}

}
public void setY(int newY)
{
if (newY < 0)
{
throw new IllegalArgumentException("Invalid Data Entered!");
}
else
{
y = newY;
}
}
public void setWidth(int newWidth)
{
if (newWidth < 0)
{
throw new IllegalArgumentException("Invalid Data Entered!");
}
else
{
width = newWidth;
}
}
public void setHeight(int newHeight)
{
if (newHeight < 0)
{
throw new IllegalArgumentException("Invalid Data Entered!");
}
else
{
height = newHeight;
}
}
}``````

## JamesCherrill 3,661

I can see why width or height <0 would be an error, but what's wrong with negative x or y? Haven't you even moved a window so its top left corner is just off the screen?

You use IllegalArgumentException as your exception. This is a kind of RuntimeException which means it is unchecked - ie you are not required to catch or declare it as thrown; the program can just exit without any diagnostics.
This kind of exception was never intended for ordinary user data input errors, which should always be handled explicitly in your code. You should use an ordinary checked exception (see firstPerson's post), declare your methods as throwing that exception, and thus ensure that the calling code has to deal with the user error

## quuba 81

``````import java.util.Scanner;
public class Rectangle {
//the private variables

private int x;
private int y;
private int height;
private int width;
//default constructor that sets values to 0

public Rectangle() {
int x = 0;
int y = 0;
int height = 0;
int width = 0;

}
//non-default constructor that sets values based on input

public Rectangle(int x, int y, int width, int height) {
setX(x);
setY(y);
setWidth(width);
setHeight(height);
}
//method for returning the height of the rectangle

public int getHeight() {
return height;
}
//method for returning the width of the rectangle

public int getWidth() {
return width;
}
//method for returning the X coordinate

public int getX() {
return x;
}
//method for returning the Y coordinate

public int getY() {
return y;
}
//boolean method for comparing rectangles and seeing if the values are equal

public boolean equals(Rectangle o) {
return (x == o.x
&& y == o.y
&& width == o.width
&& height == o.height);
}

public void setX(int newX) {
if (newX < 0) {
throw new IllegalArgumentException("Invalid Data Entered! negative x = " + newX);
} else {
x = newX;
}

}

public void setY(int newY) {
if (newY < 0) {
throw new IllegalArgumentException("Invalid Data Entered! negative y = " + newY);
} else {
y = newY;
}
}

public void setWidth(int newWidth) {
if (newWidth < 0) {
throw new IllegalArgumentException("Invalid Data Entered! negative width = " + newWidth);
} else {
width = newWidth;
}
}

public void setHeight(int newHeight) {
if (newHeight < 0) {
throw new IllegalArgumentException("Invalid Data Entered! negative height = " + newHeight);
} else {
height = newHeight;
}
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("Rectangle[");
sb.append("Coordinate X is:(" + getX() + "), ");
sb.append("Coordinate Y is:(" + getY() + "), ");
sb.append("The Width is:(" + getWidth() + "), ");
sb.append("The Length is:(" + getHeight() + ") ");
sb.append("]");
return sb.toString();
}

public static void main(String[] args) {
Scanner console = new Scanner(System.in);
Rectangle e1 = null;
while (e1 == null) {
try {
System.out.println("Enter Width for Rectangle Two");
int width = console.nextInt();
e1 = new Rectangle(0, 0, width, 9);
} catch (IllegalArgumentException iae) {
System.err.println(iae);
}
}
System.out.println(e1);
Rectangle e2 = new Rectangle(0, 0, 10, 9);
System.out.println(e2);
if (e1.equals(e2)) {
System.out.println("Both e1,e2 Rectangles Are Equal!");
} else {
System.out.println("Both e1,e2 Rectangles Are Not Equal!");
}
}
}``````

## JamesCherrill 3,661

Yes, of course you can catch it, but you don't have to. Even worse, unless you study the complete source code of the public method you are calling, you have no way to know that this exception may be thrown and are therefore very unlikely to write a try/catch for it.

## quuba 81

JamesCherrill you're right.I have read java doc at this point again, some basic information escaped my mind.

Java doc:

RuntimeException is the superclass of those exceptions that can be thrown during the normal operation of the Java Virtual Machine.

A method is not required to declare in its throws clause any subclasses of RuntimeException that might be thrown during the execution of the method but not caught.

Tandakin you is just write a custom class NegativeDimensionException