Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Rectangle{//class header
- //instance variables - data
- private int length, width;
- //Constructors
- //Default
- public Rectangle(){
- length = 0;
- width = 0;
- }
- //Specified
- public Rectangle(int l, int w){
- length = l;
- width = w;
- }
- //Getters/Accessors
- public int getLength(){
- return length;
- }
- public int getWidth(){
- return width;
- }
- //Setters/Mutators
- public void setLength( int l ){
- length = l;
- }
- public void setWidth( int w ){
- width = w;
- }
- //toString
- public String toString(){
- return "Length: " + length + "\nWidth: " + width;
- }
- //"special" methods
- //area
- public int area(){
- int theArea = length * width;
- return theArea;
- }
- //perimeter
- public int perimeter(){
- return 2 * length + 2 * width;
- }
- //diagonal
- public double diagonal(){
- return Math.sqrt( Math.pow( length, 2 ) + Math.pow( width, 2 ) );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement