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?

[6101 byte] By [mchenja] at [2007-11-26 20:09:15]
# 1
Look again at the definitions of fields width, height and color. Notice anything interesting?
DrLaszloJamfa at 2007-7-9 23:12:19 > top of Java-index,Java Essentials,Java Programming...
# 2

I'm looking, but not seeing anything.

I believe I correctly created 2 separate objects, but can't figure out why only the color was changed when creating "rectangleB," but not the height or width. Does it have to do with color being static? If so, how does that change my approach to this problem?

mchenja at 2007-7-9 23:12:20 > top of Java-index,Java Essentials,Java Programming...
# 3
Do you know what static means in:private static String color = "White";
DrLaszloJamfa at 2007-7-9 23:12:20 > top of Java-index,Java Essentials,Java Programming...
# 4
From the tutorial: http://java.sun.com/docs/books/tutorial/java/javaOO/classvars.html
DrLaszloJamfa at 2007-7-9 23:12:20 > top of Java-index,Java Essentials,Java Programming...
# 5

You got me, I don't fully understand what "staic" means yet.

My grasp on it is limited to knowing that changing the variable itself will affect anything else using it. I thought that changing the color in the object rectangleB would leave A alone, but obviously, it didn't. The definition of static might be helpful here.

mchenja at 2007-7-9 23:12:20 > top of Java-index,Java Essentials,Java Programming...
# 6

Ok, after reading the link, static is a class variable, meaning that it encompasses the whole class, and any alteration to it sticks. Any reference to it after that will return the new value, as it is supposed to. So, how can I temporarily change the color without reassigning it to the whole class? The instructor intentionally made it static, so I think he meant for us to try to work around that.

Looking back at my code, I am, indeed, changing the variable itself, rather than a property of the current object. it seems to me the problem lies within this bit of code.

public static void setColor(String color)

{

Rectangle.color = color;

}

Message was edited by:

mchenj

mchenja at 2007-7-9 23:12:20 > top of Java-index,Java Essentials,Java Programming...
# 7
Here's a big clue: are height and width static?
floundera at 2007-7-9 23:12:20 > top of Java-index,Java Essentials,Java Programming...
# 8
> he instructor intentionally made it static, so I think he meant for us to try to work around that.Could he have done that to introduce a bug for you to discover and correct,rather than "work around"?
DrLaszloJamfa at 2007-7-9 23:12:20 > top of Java-index,Java Essentials,Java Programming...
# 9

No, they aren't, and I assume that's why they're working. The instructor intended for color to be static, so there's some lesson to learn here. Is there a way I can change only the property of rectangle B without changing the class variable? Or is this just some big game he's playing with us?

Could be, lazlo, that's what I was wondering. I could create a "temp" variable, but I think that would defeat the purpose of his code. I think it's just a lesson designed to help us fully understand what a static variable means.

Message was edited by:

mchenj

mchenja at 2007-7-9 23:12:20 > top of Java-index,Java Essentials,Java Programming...
# 10
Just because I wear a turban doesn't mean I'm a mind reader!Talk to your instructor. My best guess is he is testing who can think on their feet.
DrLaszloJamfa at 2007-7-9 23:12:20 > top of Java-index,Java Essentials,Java Programming...
# 11
Haha, that's probably just the case. I'll go in a little early today, just to make sure. Thanks a lot for the help guys, it's much appreciated!-James
mchenja at 2007-7-9 23:12:20 > top of Java-index,Java Essentials,Java Programming...
# 12
> Just because I wear a turban doesn't mean I'm a mind reader!I wear a Fez. All the other fish are jealous.
floundera at 2007-7-9 23:12:20 > top of Java-index,Java Essentials,Java Programming...
# 13
> I wear a Fez. All the other fish are jealous.Does that make you [url= http://www.crit.de/bunt/values450.GIF]Akbar or Jeff[/url]?
DrLaszloJamfa at 2007-7-9 23:12:20 > top of Java-index,Java Essentials,Java Programming...
# 14
That one gave me a good laugh.
mchenja at 2007-7-9 23:12:20 > top of Java-index,Java Essentials,Java Programming...
# 15
Alright, posted 9 stars to Lazlo, for the turban image :)And, well sry flounder, a fez just aint as big as a turban.Thanks again guys.
mchenja at 2007-7-21 17:52:19 > top of Java-index,Java Essentials,Java Programming...