HarrJ

day 13 oop additional

Nov 22nd, 2023 (edited)
1,529
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.68 KB | None | 0 0
  1. public class Day13A {
  2.  
  3.     public static void main(String[] args) {
  4.         Rectangle callRect = new Rectangle(7, 4.32);
  5.  
  6.         System.out.println("length: 7");
  7.         System.out.println("width: 4.32");
  8.         System.out.printf("Area: " + callRect.getArea());
  9.  
  10.         Rectangle callRect2 = new Rectangle();
  11.         callRect2.setLen(8);
  12.         callRect2.setWid(6.44);
  13.  
  14.         System.out.println("length: 8");
  15.         System.out.println("width: 6.44");
  16.         System.out.printf("Area: " + callRect2.getArea());
  17.  
  18.        
  19.         Box callBox = new Box(4,7,3);
  20.         System.out.println("length: 4");
  21.         System.out.println("width: 7");
  22.         System.out.println("height: 3");
  23.         System.out.printf("Volume: " + callBox.getBoxVolume());
  24.         System.out.printf("%nSurface Area: " + callBox.getSurfaceArea());
  25.     }
  26.  
  27. }
  28.  
  29. class Rectangle {
  30.     private double len; //set
  31.     private double wid; //set
  32.     private double area; //get
  33.  
  34. // - Encapsulation
  35.     public void setLen(double len) {
  36.         this.len = len;
  37.     }
  38.  
  39.     public void setWid(double wid) {
  40.         this.wid = wid;
  41.     }
  42.  
  43.     public double getLen() {
  44.         return len;
  45.     }
  46.  
  47.     public double getWid() {
  48.         return wid;
  49.     }
  50.    
  51.     public double getArea() {
  52.         area = computeArea(getLen(), getWid());
  53.         return area;
  54.     }
  55.  
  56. // - Constructor
  57.     public Rectangle() {
  58.     }
  59.  
  60.     public Rectangle(double ln, double wd) {
  61.         this.len = ln;
  62.         this.wid = wd;
  63.     }
  64.  
  65.     double computeArea(double ln, double wd) {
  66.         double ar = ln * wd;
  67.  
  68.         return ar;
  69.     }
  70. }
  71.  
  72. class Box extends Rectangle {
  73.     private double hei; //set
  74.     private double boxVolume; //get
  75.     private double surfaceArea; //get
  76.  
  77. // - Encapsulation
  78.     public void setHei(double hei) {
  79.         this.hei = hei;
  80.     }
  81.    
  82.     public double getHei() {
  83.         return hei;
  84.     }
  85.  
  86.     public double getBoxVolume() {
  87.         boxVolume = computeVolume(getLen(), getWid(), getHei());
  88.         return boxVolume;
  89.     }
  90.  
  91.     public double getSurfaceArea() {
  92.         surfaceArea = computeSurfaceArea(getLen(), getWid(), getHei());
  93.         return surfaceArea;
  94.     }
  95.    
  96. // - Constructor
  97.     public Box() {
  98.     }
  99.  
  100.     public Box(double ln, double wd, double hi) {
  101.         super(ln, wd);
  102.         this.hei = hi;
  103.     }
  104.  
  105.     private double computeVolume(double ln, double wd, double hi) {
  106.         double vol = computeArea(ln, wd) * hi;
  107.  
  108.         return vol;
  109.     }
  110.  
  111.     private double computeSurfaceArea(double ln, double wd, double hi) {
  112.         double sArea = 2 * (computeArea(ln, wd) + computeArea(ln, hi) + computeArea(hi, wd));
  113.  
  114.         return sArea;
  115.     }
  116. }
  117.  
Advertisement
Add Comment
Please, Sign In to add comment