Results 1 to 8 of 8
  1. #1
    darkgt is offline Member
    Join Date
    Nov 2007
    Posts
    5
    Rep Power
    0

    Default 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.

  2. #2
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    9

    Default

    Hello darkgt, Please learn to use [code] tag. Details are available on http://www.java-forums.org/misc.php?do=bbcode

  3. #3
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    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.

  4. #4
    darkgt is offline Member
    Join Date
    Nov 2007
    Posts
    5
    Rep Power
    0

    Default

    ok i redid it and this is what i got

    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;
    	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);
    	
    	
    	
    	}
    	}
    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 0

  5. #5
    darkgt is offline Member
    Join Date
    Nov 2007
    Posts
    5
    Rep Power
    0

    Default

    Java Code:
    public class testrectangle1{
    public static void main (String[] args){
    Rectangle1 testrectangle1 = new Rectangle(3.5,35.9);
    System.out.println(testrectangle1.getPerimeter);
    }
    }
    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.9

  6. #6
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    When you create the rectangle with this statement,
    Java Code:
    Rectangle1 rectangle = new Rectangle1();
    it takes on the default characteristics given in the class.
    Java 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.
    Java 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.

    Java 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.

    Java 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 05:54 PM.

  7. #7
    darkgt is offline Member
    Join Date
    Nov 2007
    Posts
    5
    Rep Power
    0

    Default

    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.

  8. #8
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    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

  1. Rectangle Intersection
    By Gwindow in forum Java 2D
    Replies: 1
    Last Post: 04-24-2008, 03:53 PM
  2. Replies: 0
    Last Post: 04-02-2008, 07:04 AM
  3. Inner class accessing outer class
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-17-2008, 08:59 AM
  4. Rectangle expands with applet frame
    By Godzilla in forum New To Java
    Replies: 1
    Last Post: 07-10-2007, 07:14 PM
  5. Rectangle with rounded edges??
    By orchid in forum Java 2D
    Replies: 1
    Last Post: 05-10-2007, 02:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •