# Rectangle class, how to calc perimeter and area

Page 1 of 1

## 2 Replies - 1813 Views - Last Post: 19 September 2009 - 02:56 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=126810&amp;s=bdc6853af1bb0980c2ba35d2339252e8&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 bigz008

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

# Rectangle class, how to calc perimeter and area

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;

}

}

}

Is This A Good Question/Topic? 0

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

### #2 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11449
• Posts: 43,170
• 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:

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

### #3 pbl

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

Reputation: 8365
• Posts: 31,956
• Joined: 06-March 08

## Re: Rectangle class, how to calc perimeter and area

Posted 19 September 2009 - 02:56 PM