Advertisement
advictoriam

Untitled

Jan 25th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.24 KB | None | 0 0
  1. /**
  2.    A rectangle with height and width.
  3. */
  4. public class Rectangle
  5. {
  6.    private int height;
  7.    private int width;
  8.  
  9.    /**
  10.       Constructs a rectangle.
  11.       @param aHeight: the height of the rectangle
  12.       @param aWidth: the width of the rectangle
  13.    */
  14.    public Rectangle(int aHeight, int aWidth)
  15.    {
  16.       height = aHeight;
  17.       width = aWidth;
  18.    }
  19.  
  20.    /**
  21.       Constructs a rectangle that is a square.
  22.       @param aSide: the length of a side of the square
  23.    */
  24.    public Rectangle(int aSide)
  25.    {
  26.       height = aSide;
  27.       width = aSide;
  28.    }
  29.  
  30.    /**
  31.       Computes the perimeter.
  32.       @return the perimeter of the rectangle
  33.    */
  34.    public int perimeter()
  35.    {
  36.       return 2 * height + 2 * width;
  37.    }
  38.  
  39.    /**
  40.       Computes the area.
  41.       @return the area of the rectangle
  42.    */
  43.    public int area()
  44.    {
  45.       return height * width;
  46.    }
  47.    
  48.    // This method is used for checking your work. Do not modify it
  49.  
  50.    public static String check(int h, int w, int sideSq)
  51.    {
  52.       Rectangle aRect = new Rectangle(h, w);
  53.       Rectangle aSquare = new Rectangle(sideSq);
  54.       return aRect.perimeter() + " " + aRect.area() + " "
  55.            + aSquare.perimeter() + " " + aSquare.area();
  56.    }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement