mardi 30 octobre 2018

Java -- Classes and if statements - calculated multiples

I am writing a program for a class that compares the volumes of two boxes based on user input. It must calculate the volume difference, and give the following:

  • if there is no difference, display that the volumes are the same
  • if the volume of one is less than twice the other, display that it is slightly bigger
  • if the volume of one is less than three times the other, display that it is twice the size
  • if the volume of one is less than four times the other, display that it is triple the size
  • if the volume of one is less than five the other, display that it is quadruple the size
  • otherwise, display that it is the calculated multiple ***

The program must indicate the larger box (if not the same volume) and by how much. It also must NOT display values less than one (i.e. "the first box is 0.5 times the size of the second box" is wrong)

We have a specific structure to follow which I have done. I'm almost finished, but I'm stuck on the way in which to display the calculated multiple. It doesn't make sense to overload my code with if else statements for every number a user might enter, so there must be a particular way to command the program to do the math for me. I'm also thinking I may have made an error in the display() method of my Size Calculator... I have warnings for my volume variable and I'm not sure where to call that from (I know I put volume1 and volume2 and then have just volume, I'm playing around with it to see which one works).

Here is my main class:

import java.util.Scanner;
public class Assign2 {          // main to do box volume difference 
calculation
/*
 * Author: 
 * CST 8110 
 * This program compares box volumes
 */
    public static void main(String[] args) {    // program main

        System.out.println("Size calculator - we speak volumes\n");

        Box firstBox;
        Box secondBox;
        firstBox = new Box();
        secondBox = new Box();

        System.out.println("Enter first box dimensions");

        System.out.print("Enter length: ");
        firstBox.inputLength();

        System.out.print("Enter width: ");
        firstBox.inputWidth();

        System.out.print("Enter height: ");
        firstBox.inputHeight();

        System.out.println("\nEnter second box dimensions");

        System.out.print("\nEnter length: ");
        secondBox.inputLength();

        System.out.print("Enter width: ");
        secondBox.inputWidth();

        System.out.print("Enter height: ");
        secondBox.inputHeight();


        System.out.println("\nFirst box: ");
        firstBox.displayDimensions();
        double volume1 = firstBox.calcVolume();
        System.out.println(" Volume = " + volume1);


        System.out.println("\nSecond box: ");
        secondBox.displayDimensions();
        double volume2 = secondBox.calcVolume();
        System.out.println(" Volume = " + volume2);
    }


}

Here is my Box class:

import java.util.Scanner;
public class Box {              // store a single box

    private double length;      // length value
    private double width;       // width value
    private double height;      // height value

    private Scanner input = new Scanner(System.in); // scanner for all input

public Box() {              // default (no-arg) constructor             
    this.length = 0;
    this.width = 0;
    this.height = 0;
}                           

public Box(double length, double width, double height) {    // initial constructor
    this.length = length;
    this.width = width;
    this.height = height;
}

public Box(Box copyBox) {               // copy constructor
    this(copyBox.length, copyBox.width, copyBox.height);
}

public void inputLength() {     // input length value
    this.length = input.nextDouble();       
}                                   

public void inputWidth() {      // input width value
    this.width = input.nextDouble();
}

public void inputHeight() {     // input height value
    this.height = input.nextDouble();
}

public void displayDimensions() {   // display the box in proper format
    System.out.print(this.length + " X " + this.width + " X " + this.height);
    //System.out.println("First box: " + getLength() + "X" + width + "X" + height);
    //System.out.println("Second box: " + getLength() + "X" + width + "X" + height);
    //System.out.println("");
}

public double calcVolume() {        // calculate the box volume
    double volume = this.length * this.width * this.height;
    return volume;
}

}

And my Size Calculator class:

import java.text.DecimalFormat;

public class SizeCalculator {   // calculate the difference between two box volumes

    private Box firstBox = new Box();       // first box to compare

    private Box secondBox = new Box();      // second box to compare

    private String message = new String();  // message to be displayed

    private DecimalFormat df = new DecimalFormat("0.00");   // decimal format to be used

public SizeCalculator() {           // default (no-arg) constructor
}

public void inputBoxes() {          // input the two boxes
    double length, width, height;

}

//  public void inputBox(Box){          optional method to simplify box 
//  }   

public void calculateSizes() {      // calculate difference between two boxes and store the value in message
    double length, width, height;
}

public void display() {             // display the message
    double length, width, height;

    Box firstBox;
    Box secondBox;
    firstBox = new Box();
    secondBox = new Box();

    // this.firstBox = length * width * height;

    double volume1 = firstBox.length * firstBox.width * firstBox.height;
    double volume2 = secondBox.length * secondBox.width * secondBox.height;


    if (firstBox.volume1 == secondBox.volume2) {
        System.out.println("The volumes are the same.");

    }else if (firstBox.volume < 2 * secondBox.volume) {
        System.out.println("The volume of the second box is slightly bigger than the first box");
    }else if (secondBox.volume < 2 * firstBox.volume ) {
        System.out.println("The volume of the first box is slightly bigger than the second box");

    }else if (firstBox.volume < 3 * secondBox.volume) {
        System.out.println("The volume of the second box is twice the size of the second box");
    }else if (secondBox.volume < 3 * firstBox.volume) {
        System.out.println("The volume of the second box is twice the size of the first box" );

    }else if (firstBox.volume < 4 * secondBox.volume) {
        System.out.println("The volume of the second box is triple the size of the first box");
    }else if (secondBox.volume < 4 * secondBox.volume) {
        System.out.println("The volume of the first box is triple the size of the second box");

    }else if (firstBox.volume < 5 * secondBox.volume) {
        System.out.println("The volume of the second box is quadruple the size of the first box");
    }else if (secondBox.volume < 5 * firstBox.volume) {
        System.out.println("The volume of the first box is quadruple the size of the second box");


    }else if (firstBox.volume < 6 * secondBox.volume) {     // how to write this so it displays the multiple
        System.out.println("The volume of the second box is 5 times the size of the first box");
    }else if (secondBox.volume < 6 * secondBox.volume) {
        System.out.println("The volume of the first box is 5 times the size of the second box");


}
}

}

Aucun commentaire:

Enregistrer un commentaire