dm6801

171123 - Class 07 - Rectangle

Nov 26th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.35 KB | None | 0 0
  1. package myApp;
  2.  
  3. public class Rectangle {
  4.     /* Properties */
  5.     private double  width, height;
  6.    
  7.    
  8.     /* Get, Set Methods */
  9.     public double getWidth() {
  10.         return this.width;
  11.     }
  12.     public void setWidth(double width) {
  13.         this.width = width<0?(width*(-1)):width;
  14.     }
  15.    
  16.     public double getHeight() {
  17.         return this.height;
  18.     }
  19.     public void setHeight(double height) {
  20.         this.height = height<0?(height*(-1)):height;
  21.     }
  22.    
  23.    
  24.     /* Constructors */
  25.     public Rectangle() {
  26.         this(10, 10); //default properties
  27.     }
  28.     public Rectangle(double width, double height) {
  29.         this.setWidth(width);
  30.         this.setHeight(height);
  31.     }
  32.     public Rectangle(Rectangle rectObj) {
  33.         this.width = rectObj.width;
  34.         this.height = rectObj.height;
  35.     }
  36.    
  37.    
  38.     /* Methods */
  39.     public double getPerimeter() {
  40.         return 2*(width+height);
  41.     }
  42.    
  43.     public double getArea() {
  44.         return width*height;
  45.     }
  46.    
  47.     public void details(String prefix) {
  48.         System.out.printf("(%s): %s Width=%.2f, Height=%.2f, Perimeter=%.2f, Area=%.2f      //Rectangle object%n",
  49.                             this, prefix, width, height, this.getPerimeter(), this.getArea());
  50.     }
  51.     public void details() { //default case
  52.         details("   ");
  53.     }
  54.  
  55.    
  56.     public void print(char chr) {
  57.         for (int line=0; line<(int)height; line+=1) {
  58.             for (int index=0; index<(int)width; index+=1) {
  59.                 System.out.print(chr);
  60.                
  61.             }
  62.             System.out.print("\n");
  63.         }
  64.     }
  65.    
  66.    
  67. }
Advertisement
Add Comment
Please, Sign In to add comment