Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-14-2007, 05:30 PM
Member
 
Join Date: Nov 2007
Posts: 5
Rep Power: 0
darkgt is on a distinguished road
Default help with rectangle class
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 05:36 PM.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 11-14-2007, 05:38 PM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
Rep Power: 10
JavaBean is on a distinguished road
Default
Hello darkgt, Please learn to use [code] tag. Details are available on http://www.java-forums.org/misc.php?do=bbcode
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-14-2007, 05:44 PM
ShoeNinja's Avatar
Senior Member
 
Join Date: Oct 2007
Posts: 124
Rep Power: 0
ShoeNinja is on a distinguished road
Send a message via AIM to ShoeNinja
Default
You don't seem to have any setters defined. Just like the get commands, you need to define setters.

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 05:47 PM.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-14-2007, 05:52 PM
Member
 
Join Date: Nov 2007
Posts: 5
Rep Power: 0
darkgt is on a distinguished road
Default
ok i redid it and this is what i got

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
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-14-2007, 06:13 PM
Member
 
Join Date: Nov 2007
Posts: 5
Rep Power: 0
darkgt is on a distinguished road
Default
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
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 11-14-2007, 06:52 PM
ShoeNinja's Avatar
Senior Member
 
Join Date: Oct 2007
Posts: 124
Rep Power: 0
ShoeNinja is on a distinguished road
Send a message via AIM to ShoeNinja
Default
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.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 11-14-2007, 07:13 PM
Member
 
Join Date: Nov 2007
Posts: 5
Rep Power: 0
darkgt is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 11-14-2007, 07:19 PM
ShoeNinja's Avatar
Senior Member
 
Join Date: Oct 2007
Posts: 124
Rep Power: 0
ShoeNinja is on a distinguished road
Send a message via AIM to ShoeNinja
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.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Rectangle Intersection Gwindow Java 2D 1 04-24-2008 04:53 PM
what is the Priority for execution of Interface class and a Abstract class Santoshbk Advanced Java 0 04-02-2008 08:04 AM
Inner class accessing outer class Java Tip Java Tips 0 02-17-2008 09:59 AM
Rectangle expands with applet frame Godzilla New To Java 1 07-10-2007 08:14 PM
Rectangle with rounded edges?? orchid Java 2D 1 05-10-2007 03:31 AM


All times are GMT +2. The time now is 07:02 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org