mmayoub

School,24.09.2017,Box class

Sep 24th, 2017
114
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Box.java
  2. =========
  3.  
  4. public class Box {
  5.     double length;
  6.     double width;
  7.     double height;
  8.  
  9.     Box(double length, double width, double height) {
  10.         this.length = length;
  11.         this.width = width;
  12.         this.height = height;
  13.     }
  14.  
  15.     double getLength() {
  16.         return this.length;
  17.     }
  18.  
  19.     double getWidth() {
  20.         return this.width;
  21.     }
  22.  
  23.     double getHeight() {
  24.         return this.height;
  25.     }
  26.  
  27.     void setLenght(double length) {
  28.         this.length = length;
  29.     }
  30.  
  31.     void setWidth(double width) {
  32.         this.width = width;
  33.     }
  34.  
  35.     void setHeight(double height) {
  36.         this.height = height;
  37.     }
  38.  
  39.     double getVolume() {
  40.         return this.length * this.width * this.height;
  41.     }
  42.  
  43.     @Override
  44.     public String toString() {
  45.         return "Box [length=" + length + ", width=" + width + ", height=" + height + "]";
  46.     }
  47.  
  48. }
  49.  
  50.  
  51. MainClass.java
  52. ==============
  53. import java.util.Random;
  54. import java.util.Scanner;
  55.  
  56. public class MainClass {
  57.  
  58.     public static void main(String[] args) {
  59.         Scanner in = new Scanner(System.in);
  60.         Random rnd = new Random();
  61.  
  62.         /*
  63.          * double x ; System.out.print("Enter a number: "); x= in.nextInt();
  64.          *
  65.          * Box b1=new Box(x, x+1, x+2); System.out.println(b1);
  66.          *
  67.          * Box sondok = new Box(x, x+1, x+2); System.out.println(sondok);
  68.          *
  69.          * double y = 2*x; sondok.setLenght(y); System.out.println(sondok);
  70.          */
  71.         System.out.println("==================================");
  72.         double l, w, h;
  73.         l = 100 * rnd.nextDouble();
  74.         w = 80 * rnd.nextDouble();
  75.         h = 20 * rnd.nextDouble();
  76.  
  77.         l = ((int) (100 * l)) / 100.0;
  78.         w = ((int) (100 * w)) / 100.0;
  79.         h = ((int) (100 * h)) / 100.0;
  80.  
  81.         Box b2 = new Box(l, w, h);
  82.         System.out.println("First Random Box: " + b2);
  83.  
  84.         l = 100 * rnd.nextDouble();
  85.         w = 80 * rnd.nextDouble();
  86.         h = 20 * rnd.nextDouble();
  87.  
  88.         l = ((int) (100 * l)) / 100.0;
  89.         w = ((int) (100 * w)) / 100.0;
  90.         h = ((int) (100 * h)) / 100.0;
  91.  
  92.         Box b3 = new Box(l, w, h);
  93.         System.out.println("Other Random Box: " + b3);
  94.  
  95.         System.out.println("The box with max volume is");
  96.         if (b2.getVolume() > b3.getVolume()) {
  97.             System.out.println(b2);
  98.             System.out.println(b2.getVolume());
  99.         } else {
  100.             System.out.println(b3);
  101.             System.out.println(b3.getVolume());
  102.         }
  103.  
  104.         // make same volume
  105.  
  106.         // make the same length, width, height
  107.         b3.setLenght(b2.getLength());
  108.  
  109.         w = b2.getWidth();
  110.         b3.setWidth(w);
  111.         b3.setHeight(b2.getHeight());
  112.         System.out.println(b2);
  113.         System.out.println("volume 1 = " + b2.getVolume());
  114.         System.out.println(b3);
  115.         System.out.println("volume 2 = " + b3.getVolume());
  116.         // make a cube
  117.         double v = b2.getVolume();
  118.         double c = Math.pow(v, 1.0 / 3);
  119.  
  120.         b3.setLenght(c);
  121.         b3.setWidth(c);
  122.         b3.setHeight(c);
  123.  
  124.         System.out.println(b2);
  125.         System.out.println("volume 1 = " + b2.getVolume());
  126.         System.out.println(b3);
  127.         System.out.println("volume 2 = " + b3.getVolume());
  128.  
  129.     }
  130. }
RAW Paste Data