Rectangle class, how to calc perimeter and area

just downloaded eclipse! i wanna do this exersice

Page 1 of 1

2 Replies - 1646 Views - Last Post: 19 September 2009 - 02:56 PM Rate Topic: -----

#1 bigz008  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 38
  • Joined: 17-September 09

Rectangle class, how to calc perimeter and area

Post icon  Posted 19 September 2009 - 02:19 PM

Hi, my friend in India really got me interested in JAVA last time I went back home, so far I have this but how is it possible to Display and calc the peremiter and area of the rectangle? and are the get set methods correct so far?

package rectangle;

public class Rectangle {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

int length; //i declared attributes here
int width; // declared attributes here

public int getWidth(){return width;} //get and set method for display and changes??
public void setWidth (double w){ width = (w > 0 & w < 20)? w: 0;}

public int getLength(){return length;}
public void setLength (double l){ length = (l > 0 & l < 20)? l: 0;

}

}

}


THANKS in Advance

Is This A Good Question/Topic? 0
  • +

Replies To: Rectangle class, how to calc perimeter and area

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10562
  • View blog
  • Posts: 39,087
  • Joined: 27-December 08

Re: Rectangle class, how to calc perimeter and area

Posted 19 September 2009 - 02:38 PM

First, could you please post your code between code tags like so:
:code:

Next, I see you define all of your variables and methods within the main method. Don't do this. Declare every field and method outside of your main method. Also, add a constructor so that you can create instances of your Rectangle class. Let me illustrate:

public class Rectangle{
	private int width, heigth; //declare your width and height fields
	public Rectangle(int w, int h){ //allows you to set the width and height on instantiation
		 width = w;
		 height = h;

		 }
	 public Rectangle(){} 

	 public int getWidth(){return width;}
	 public int getHeight(){return height;}
	
	 public void setHeight(int h){height = h;}
	 public void setWidth(int w){width = w;}

	 public static void main(String[] args){
		 Rectangle r = new Rectangle();
		 Rectangle rect = new Rectangle(200,100);


		}
}



What happens at runtime is that the main method is instantiated by the JRE, so it is technically its own object if that makes sense. Also, methods cannot be declared within other methods. And remember, methods cannot access variables declared within other methods. Hope this makes some sense. It looks like you're on the right track if you understand the concept of methods as a beginner programmer. Good luck!

This post has been edited by macosxnerd101: 19 September 2009 - 03:08 PM

Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Rectangle class, how to calc perimeter and area

Posted 19 September 2009 - 02:56 PM

Please :code:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1