Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Box.java
- =========
- public class Box {
- double length;
- double width;
- double height;
- Box(double length, double width, double height) {
- this.length = length;
- this.width = width;
- this.height = height;
- }
- double getLength() {
- return this.length;
- }
- double getWidth() {
- return this.width;
- }
- double getHeight() {
- return this.height;
- }
- void setLenght(double length) {
- this.length = length;
- }
- void setWidth(double width) {
- this.width = width;
- }
- void setHeight(double height) {
- this.height = height;
- }
- double getVolume() {
- return this.length * this.width * this.height;
- }
- @Override
- public String toString() {
- return "Box [length=" + length + ", width=" + width + ", height=" + height + "]";
- }
- }
- MainClass.java
- ==============
- import java.util.Random;
- import java.util.Scanner;
- public class MainClass {
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- Random rnd = new Random();
- /*
- * double x ; System.out.print("Enter a number: "); x= in.nextInt();
- *
- * Box b1=new Box(x, x+1, x+2); System.out.println(b1);
- *
- * Box sondok = new Box(x, x+1, x+2); System.out.println(sondok);
- *
- * double y = 2*x; sondok.setLenght(y); System.out.println(sondok);
- */
- System.out.println("==================================");
- double l, w, h;
- l = 100 * rnd.nextDouble();
- w = 80 * rnd.nextDouble();
- h = 20 * rnd.nextDouble();
- l = ((int) (100 * l)) / 100.0;
- w = ((int) (100 * w)) / 100.0;
- h = ((int) (100 * h)) / 100.0;
- Box b2 = new Box(l, w, h);
- System.out.println("First Random Box: " + b2);
- l = 100 * rnd.nextDouble();
- w = 80 * rnd.nextDouble();
- h = 20 * rnd.nextDouble();
- l = ((int) (100 * l)) / 100.0;
- w = ((int) (100 * w)) / 100.0;
- h = ((int) (100 * h)) / 100.0;
- Box b3 = new Box(l, w, h);
- System.out.println("Other Random Box: " + b3);
- System.out.println("The box with max volume is");
- if (b2.getVolume() > b3.getVolume()) {
- System.out.println(b2);
- System.out.println(b2.getVolume());
- } else {
- System.out.println(b3);
- System.out.println(b3.getVolume());
- }
- // make same volume
- // make the same length, width, height
- b3.setLenght(b2.getLength());
- w = b2.getWidth();
- b3.setWidth(w);
- b3.setHeight(b2.getHeight());
- System.out.println(b2);
- System.out.println("volume 1 = " + b2.getVolume());
- System.out.println(b3);
- System.out.println("volume 2 = " + b3.getVolume());
- // make a cube
- double v = b2.getVolume();
- double c = Math.pow(v, 1.0 / 3);
- b3.setLenght(c);
- b3.setWidth(c);
- b3.setHeight(c);
- System.out.println(b2);
- System.out.println("volume 1 = " + b2.getVolume());
- System.out.println(b3);
- System.out.println("volume 2 = " + b3.getVolume());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement