Advertisement
prprice16

RectangleClass

Feb 11th, 2020
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.74 KB | None | 0 0
  1. class Rectangle
  2. {
  3.     //attributes must be private
  4.     private double length, width;
  5.  
  6.     //default constructor
  7.     public Rectangle()
  8.     {
  9.         //assign default values to attributes
  10.         length = 1;
  11.         width = 1;
  12.     }
  13.  
  14.     //parameterized constructor
  15.     public Rectangle(double len, double wid)
  16.     {
  17.         //assign parameter values to attributes
  18.         length = len;
  19.         width = wid;
  20.     }
  21.  
  22.     //sets and gets for length and width
  23.     public void setLength(double len)
  24.     {
  25.         length = len;
  26.     }
  27.  
  28.     public double getLength()
  29.     {
  30.         return length;
  31.     }
  32.  
  33.     public void setWidth(double wid)
  34.     {
  35.         width = wid;
  36.     }
  37.  
  38.     public double getWidth()
  39.     {
  40.         return width;
  41.     }
  42.  
  43.     //area is calculated, only needs get
  44.     public double getArea()
  45.     {
  46.         return length * width;
  47.     }
  48. }//end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement