Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.71 KB | None | 0 0
  1. class Shape {
  2.     private double width;
  3.     private double height;
  4.    
  5.     double getWidth() {
  6.         return width;
  7.     }
  8.    
  9.     double getHeight() {
  10.         return height;
  11.     }
  12.    
  13.     void setWidth(double w) {
  14.         width = w;
  15.     }
  16.    
  17.     void setHeight(double h) {
  18.         height = h;
  19.     }
  20.    
  21.    
  22.     void showDimensions() {
  23.         System.out.println("Weight of an object: " +width + "\nHeight of an object: " +height);
  24.     }
  25. }
  26.  
  27.  
  28.  
  29. class Rectangle extends Shape {
  30.    
  31.         boolean isSquare() {
  32.             if (getWidth() == getHeight())
  33.                 return true;
  34.             else
  35.                 return false;
  36.         }
  37.    
  38.         double calculateArea() {
  39.             return getWidth() * getHeight();
  40.         }
  41.        
  42.     }
  43.  
  44. public class Hermetiq {
  45.     public static void main(String args[]) {
  46.         Rectangle r1 = new Rectangle();
  47.         Rectangle r2 = new Rectangle();
  48.         Rectangle r3 = new Rectangle();
  49.        
  50.         r1.setWidth(2.0);
  51.         r1.setHeight(2.5);
  52.        
  53.         r2.setWidth(12.0);
  54.         r2.setHeight(1.2);
  55.        
  56.         r3.setWidth(5.5);
  57.         r3.setHeight(5.5);
  58.        
  59.         System.out.println("Information about dimensions of an rectangle object r1: ");
  60.         r1.showDimensions();
  61.         System.out.println("The area of an rectangle object r1: " + r1.calculateArea());
  62.         System.out.println("If the rectangle is a square? " +r1.isSquare());
  63.        
  64.         System.out.println("Information about dimensions of an rectangle object r2: ");
  65.         r2.showDimensions();
  66.         System.out.println("The area of an rectangle object r2: " + r2.calculateArea());
  67.         System.out.println("If the rectangle is a square? " +r2.isSquare());
  68.        
  69.         System.out.println("Information about dimensions of an rectangle object r3: ");
  70.         r3.showDimensions();
  71.         System.out.println("The area of an rectangle object r3: " + r3.calculateArea());
  72.         System.out.println("If the rectangle is a square? " +r3.isSquare());
  73.        
  74.        
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement