Advertisement
mmayoub

Constructors, ex02, slide 50

Jul 11th, 2017
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.46 KB | None | 0 0
  1. /*
  2.  * Constructors, exercise 02, slide no 50
  3.  */
  4.  
  5. /*
  6.  * Rectangle class
  7.  */
  8. public class Rectangle {
  9.     // class final variables
  10.     private static final int DEFAULT_WIDTH = 10;
  11.     private static final int DEFAULT_HEIGHT = 10;
  12.  
  13.     // Rectangle object properties
  14.     private int width = Rectangle.DEFAULT_WIDTH;
  15.     private int height = Rectangle.DEFAULT_HEIGHT;
  16.  
  17.     // constructors
  18.     public Rectangle() {
  19.         this(DEFAULT_WIDTH, DEFAULT_HEIGHT);
  20.     }
  21.  
  22.     public Rectangle(int width, int height) {
  23.         this.setWidth(width);
  24.         this.setHeight(height);
  25.     }
  26.  
  27.     public Rectangle(Rectangle sourceRect) {
  28.         this.width = sourceRect.width;
  29.         this.height = sourceRect.height;
  30.     }
  31.  
  32.     // getter and setter for width property
  33.     public int getWidth() {
  34.         return this.width;
  35.     }
  36.  
  37.     public boolean setWidth(int width) {
  38.         if (width < 0) {
  39.             return false;
  40.         }
  41.         this.width = width;
  42.         return true;
  43.     }
  44.  
  45.     // getter and setter for height property
  46.     public int getHeight() {
  47.         return this.height;
  48.     }
  49.  
  50.     public boolean setHeight(int height) {
  51.         if (height < 0) {
  52.             return false;
  53.         }
  54.  
  55.         this.height = height;
  56.         return true;
  57.     }
  58.  
  59.     // get rectangle area
  60.     public int getArea() {
  61.         return this.width * this.height;
  62.     }
  63.  
  64.     // get rectangle perimeter
  65.     public int getPerimeter() {
  66.         return 2 * (this.width + this.height);
  67.     }
  68.  
  69.     // print a rectangle
  70.     public void print() {
  71.         System.out.println(this.toShape('*'));
  72.     }
  73.  
  74.     public void print(char fillChar) {
  75.         System.out.println(this.toShape(fillChar));
  76.     }
  77.  
  78.     // get rectangle shape
  79.     private String toShape(char fillChar) {
  80.         String aRow = oneRow(fillChar) + "\n";
  81.         String result = "";
  82.         for (int i = 0; i < this.height; i += 1) {
  83.             result += aRow;
  84.         }
  85.         return result;
  86.     }
  87.  
  88.     private String oneRow(char fillChar) {
  89.         String row = "";
  90.         for (int i = 0; i < this.width; i += 1)
  91.             row += fillChar;
  92.         return row;
  93.     }
  94.  
  95.     @Override
  96.     public String toString() {
  97.         return "Rectangle [width=" + width + ", height=" + height + "]";
  98.     }
  99.  
  100. }
  101.  
  102.  
  103. /*
  104.  * RectangleTester class, including main
  105.  */
  106.  
  107. public class RectangleTester {
  108.  
  109.     public static void main(String[] args) {
  110.         // TODO Auto-generated method stub
  111.         Rectangle r1 = new Rectangle(3, 5);
  112.         Rectangle r2 = new Rectangle();
  113.  
  114.         System.out.println("r1\n" + r1.toString());
  115.         r1.print();
  116.  
  117.         System.out.println("r2\n" + r2.toString());
  118.         r2.print('$');
  119.  
  120.         r2.setWidth(7);
  121.         r2.setHeight(r1.getHeight() + 1);
  122.  
  123.         System.out.println("r2\n" + r2.toString());
  124.         r2.print('#');
  125.     }
  126.  
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement