Results 1 to 8 of 8
Thread: help with rectangle class
- 11-14-2007, 04:30 PM #1
Member
- Join Date
- Nov 2007
- Posts
- 5
- Rep Power
- 0
help with rectangle class
Java Code:public class Rectangle{ public static void main (String[]args){ Rectangle1 rectangle = new Rectangle1(); rectangle.setColor ("white"); rectangle.setHeight(1); rectangle.setWidth(1); System.out.println(rectangle.toString()); } } class Rectangle1 { private String color = "red"; private double height = 40; private double width = 4; Rectangle1 () { } public double getHeight(){ return height; } public double getWidth(){ return width; } public String getColor(){ return color; } //area and perimeter double getArea(){ return height* width; } double getPerimeter(){ return height*2 + width*2; } public String toString(){ return System.out.println("the area is" + area + "the perimeter is" + perimeter + "\n" + "color is" + color); } }
having a problem with the cannot find symbol area for the set commands. any ideas?Last edited by JavaBean; 11-14-2007 at 04:36 PM.
- 11-14-2007, 04:38 PM #2
Hello darkgt, Please learn to use [code] tag. Details are available on http://www.java-forums.org/misc.php?do=bbcode
- 11-14-2007, 04:44 PM #3
You don't seem to have any setters defined. Just like the get commands, you need to define setters.
Java Code:public void setHeight(double i){ this.height = i; } public void setWidth(double i){ this.width = i; } public void setColor(String color){ this.color = color; }Last edited by ShoeNinja; 11-14-2007 at 04:47 PM.
- 11-14-2007, 04:52 PM #4
Member
- Join Date
- Nov 2007
- Posts
- 5
- Rep Power
- 0
ok i redid it and this is what i got
but i cant get the answers to come out right now susposed to have color set as red but comes out as white. and the area and perimeter equal 0Java Code:public class Rectangle{ public static void main (String[]args){ Rectangle1 rectangle = new Rectangle1(); rectangle.setColor ("white"); rectangle.setHeight(1); rectangle.setWidth(1); System.out.println(rectangle.toString()); } } class Rectangle1 { private String color = "red"; private double height = 40; private double width = 4; private double area; private double perimeter; Rectangle1 () { } public double getHeight(){ return height; } public void setHeight(double height) { this.height = height; } public double getWidth(){ return width; } public void setWidth(double width){ this.width = width; } public String getColor(){ return color; } public void setColor(String color){ this.color = color; } //area and perimeter double getArea(){ return height * width; } double getPerimeter(){ return height* 2 + width* 2; } public String toString(){ return("the area is" + area + "the perimeter is" + perimeter + "\n" + "color is" + color); } }
- 11-14-2007, 05:13 PM #5
Member
- Join Date
- Nov 2007
- Posts
- 5
- Rep Power
- 0
also don't know how to exactly get the test file compiled right either trying to enter a width of 3.5 and a height of 35.9Java Code:public class testrectangle1{ public static void main (String[] args){ Rectangle1 testrectangle1 = new Rectangle(3.5,35.9); System.out.println(testrectangle1.getPerimeter); } }
- 11-14-2007, 05:52 PM #6
When you create the rectangle with this statement,
it takes on the default characteristics given in the class.Java Code:Rectangle1 rectangle = new Rectangle1();
But after that, you call the setters to change the characteristics.Java Code:private String color = "red"; private double height = 40; private double width = 4; private double area; private double perimeter;
This is why you end up with white instead of red. To keep the default (red), remove the setColor call.Java Code:rectangle.setColor ("white"); rectangle.setHeight(1); rectangle.setWidth(1);
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.
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.Java Code:public Rectangle(Double h, Double w){ this.height = h; this.width = w; }
Your class can have multiple constructors. The one with the correct signature will be the one that is used.Java Code:public Rectangle(Double w, Double h, String hue){ this.width = w; this.height = h; this.color = hue; }
Hope this helps.Last edited by ShoeNinja; 11-14-2007 at 05:54 PM.
- 11-14-2007, 06:13 PM #7
Member
- Join Date
- Nov 2007
- Posts
- 5
- Rep Power
- 0
yeah i think it will help i just gotta go home and read it and try to figure this stuff out, i'm not good at all in programming. I prefer working with hardware and lines and cables and stuff like that, just this degree requires this class. I have a's in my other 3 classes and i put more time in this class and I'm still failing. The book is completely useless and the teacher doesn't help out that much.
- 11-14-2007, 06:19 PM #8
I know exactly what you mean. I just finished computer and electrical engineering degrees in May. My school was so research focused that it would let anyone come teach as long as their research would bring in money. I took classes from guys that didn't have a good enough grasp on the English language to order a cheeseburger let alone explain JK Flip Flops.
Anyway, work is a lot better. You get to continue to learn and you take home a pay check. Hang in there!
Feel free to send me a PM anytime. All this stuff isn't that tough. You just need that light bulb in your head to turn on.
Similar Threads
-
Rectangle Intersection
By Gwindow in forum Java 2DReplies: 1Last Post: 04-24-2008, 03:53 PM -
what is the Priority for execution of Interface class and a Abstract class
By Santoshbk in forum Advanced JavaReplies: 0Last Post: 04-02-2008, 07:04 AM -
Inner class accessing outer class
By Java Tip in forum Java TipReplies: 0Last Post: 02-17-2008, 08:59 AM -
Rectangle expands with applet frame
By Godzilla in forum New To JavaReplies: 1Last Post: 07-10-2007, 07:14 PM -
Rectangle with rounded edges??
By orchid in forum Java 2DReplies: 1Last Post: 05-10-2007, 02:31 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks