rectangle constructor problem
Ok, i've seen a couple posts on this in other threads, but it seemed like people were asking for you guys to complete their homework for them. This is an assignment calling for us to construct 2 rectangles, modify the second, then verify that the first remained unaltered. This is my main code here:
publicstaticvoid main(String[] args)
{
// Creates object "rectangle A"
Rectangle rectangleA =new Rectangle();
// Tests rectangle A's components
System.out.println("Rectangle A's componenets are as follows:");
System.out.println("Height: " + rectangleA.getHeight());
System.out.println("Width: " + rectangleA.getWidth());
System.out.println("Color: " + rectangleA.getColor());
// Creates object "rectangle B"
Rectangle rectangleB =new Rectangle(2, 2,"Blue");
// Tests rectangle B's components
System.out.println('\n' +"Rectangle B's components are as follows:");
System.out.println("Height: " + rectangleB.getHeight());
System.out.println("Width: " + rectangleB.getWidth());
System.out.println("Width: " + rectangleB.getColor());
// Checks setComponent methods on rectangle B for functionality
rectangleB.setHeight(3);
rectangleB.setWidth(4);
rectangleB.setColor("Azul");
// Checks the changes to rectangle B
System.out.println('\n' +"Rectangle B's new components are as follows:");
System.out.println("Height: " + rectangleB.getHeight());
System.out.println("Width: " + rectangleB.getWidth());
System.out.println("Color: " + rectangleB.getColor());
// Prints components of rectangle A to verify that it has remained unaltered
System.out.println('\n' +"Rectangle A's components are still:");
System.out.println("Height: " + rectangleA.getHeight());
System.out.println("Width: " + rectangleA.getWidth());
System.out.println("Color: " + rectangleA.getColor());
}
}
Here is the second class, including the completed (as correct as I could come haha) rectangle constructors and methods:
publicclass Rectangle
{
privatedouble width = 1;
privatedouble height = 1;
privatestatic String color ="White";
public Rectangle()
{
}
public Rectangle(double width,double height, String color)
{
this.width = width;
this.height = height;
this.color = color;
}
publicdouble getWidth()
{
return width;
}
publicvoid setWidth(double width)
{
this.width = width;
}
publicdouble getHeight()
{
return height;
}
publicvoid setHeight(double height)
{
this.height = height;
}
publicstatic String getColor()
{
return color;
}
publicstaticvoid setColor(String color)
{
Rectangle.color = color;
}
publicdouble findArea()
{
return (this.getHeight() * this.getWidth());
}
}
We are supposed to use each rectangle constructor once, then use a series of print statements to check each method/constructor in the Rectangle class. I will add findArea to the main class later.
So... my problem is that the second rectangle object's "color" property is messing with the first rectangle's color as well, when I run the program, rectangle A ends up with a color of "Azul" instead of "White."
Any ideas?

