View Single Post
  #6 (permalink)  
Old 11-14-2007, 06:52 PM
ShoeNinja's Avatar
ShoeNinja ShoeNinja is offline
Senior Member
 
Join Date: Oct 2007
Posts: 112
ShoeNinja is on a distinguished road
Send a message via AIM to ShoeNinja
When you create the rectangle with this statement,
Code:
Rectangle1 rectangle = new Rectangle1();
it takes on the default characteristics given in the class.
Code:
private String color = "red"; private double height = 40; private double width = 4; private double area; private double perimeter;
But after that, you call the setters to change the characteristics.
Code:
rectangle.setColor ("white"); rectangle.setHeight(1); rectangle.setWidth(1);
This is why you end up with white instead of red. To keep the default (red), remove the setColor call.

As far as the perimeter and area go, I'm not sure why you are getting 0. A place to start would be to add parentheses in your equations to make sure that operations are being performed in the right order. You might also look into performing operations on Doubles. I never use the class so I'm not sure what to tell you.

As far as your last question goes, it looks like you need some constructor methods. Constructors are what actually create the objects. So if you want to be able to declare the width and height of a rectangle while you are creating it (like in your example,
Rectangle1 testrectangle1 = new Rectangle(3.5,35.9) you need to have a constructor that can handle it. Since you are passing two doubles, you need to create a constructor to handle two doubles.

Code:
public Rectangle(Double h, Double w){ this.height = h; this.width = w; }
If you want to be able to declare height, width and color at time of object creation, you need to have a constructor to handle that case as well.

Code:
public Rectangle(Double w, Double h, String hue){ this.width = w; this.height = h; this.color = hue; }
Your class can have multiple constructors. The one with the correct signature will be the one that is used.

Hope this helps.

Last edited by ShoeNinja : 11-14-2007 at 06:54 PM.
Reply With Quote