Advertisement
fosterbl

Rectangle.java

Feb 14th, 2020
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.05 KB | None | 0 0
  1. public class Rectangle{//class header
  2.    //instance variables - data
  3.    private int length, width;
  4.    
  5.    //Constructors
  6.    //Default
  7.    public Rectangle(){
  8.       length = 0;
  9.       width = 0;
  10.    }
  11.    
  12.    //Specified
  13.    public Rectangle(int l, int w){
  14.       length = l;
  15.       width = w;
  16.    }
  17.    
  18.    //Getters/Accessors
  19.    public int getLength(){
  20.       return length;
  21.    }
  22.    
  23.    public int getWidth(){
  24.       return width;
  25.    }
  26.    
  27.    //Setters/Mutators
  28.    public void setLength( int l ){
  29.       length = l;
  30.    }
  31.    
  32.    public void setWidth( int w ){
  33.       width = w;
  34.    }
  35.    
  36.    //toString
  37.    public String toString(){
  38.       return "Length: " + length + "\nWidth: " + width;
  39.    }
  40.    
  41.    //"special" methods
  42.    
  43.    //area
  44.    public int area(){
  45.       int theArea = length * width;
  46.       return theArea;
  47.    }
  48.    
  49.    //perimeter
  50.    public int perimeter(){
  51.       return 2 * length + 2 * width;
  52.    }
  53.    
  54.    //diagonal
  55.    public double diagonal(){
  56.       return Math.sqrt( Math.pow( length, 2 ) + Math.pow( width, 2 ) );
  57.    }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement