简体   繁体   中英

Java Box program with multiple methods and constructors

I am a beginner in programming and am having trouble with using constructors, specifically. I have to write a program for one of my labs that must consist only of:

  1. Three instance variables – length, width and height (each of type double)
    1. One instance variables – input (type Scanner) initialized to System.in
    2. Default constructor (no-arg) – initialize all three instance variables to 1
    3. Initial constructor – initialize all three instance variables
    4. Copy constructor – copy Box
    5. inputWidth, inputLength, and inputHeight methods that set the instance variables based on user input have not parameters and do not return a value.
    6. a displayDimensions method that displays the length X Width X height (separated by “X”) and does not return a value.
    7. a calcVolume method that has no parameters and calculates the volume of the box

We also were given application BoxTest in which the output must exactly match the following:

  • Default dimensions are 1.0 X 1.0 X 1.0 with volume of 1.0
  • Initial dimensions are 8.5 X 11.0 X 1.0 with volume of 93.5
  • Copied dimensions are 8.5 X 11.0 X 1.0 with volume of 93.5
  • Update dimensions
  • Enter length: 1
  • Enter width: 2
  • Enter height: 3
  • Updated dimensions are 1.0 X 2.0 X 3.0 with volume of 6.0

Here's my code:

import java.util.Scanner;

public class Box {
public static void main(String args[]) {
    double length, width, height;

    Scanner input=new Scanner(System.in);

new Box() {     //  

Box defaultBox=new Box();
    double length = 1.0;
    double width = 1.0;
    double height = 1.0;
    System.out.print("Default dimensions are " + length + " X " + width + " X " + height);
    defaultBox.displayDimensions();
    System.out.println(" with volume of "+defaultBox.calcVolume());

Box initialBox=new Box(length, width, height);
    length = 8.5;
    width = 11.0;
    height = 1.0;
    System.out.print("Initial dimensions are " + length + " X " + width + " X " + height);
    initialBox.displayDimensions();
    System.out.println(" with volume of "+initialBox.calcVolume());

Box copyBox=new Box(initialBox);
    System.out.print("Copied dimensions are " + length + " X " + width + " X " + height);
    copyBox.displayDimensions();
    System.out.println(" with volume of "+copyBox.calcVolume());

    System.out.println("\nUpdate dimensions");
    initialBox.inputLength();
    initialBox.inputWidth();
    initialBox.inputHeight();
    System.out.print("Updated dimensions are ");
    initialBox.displayDimensions();
    System.out.println(" with volume of "+initialBox.calcVolume());
}
double inputLength() {
    Scanner input;
    double length = input.nextDouble(); 
    }
double inputWidth() {
    Scanner input;
    double width = input.nextDouble();
    }
double inputHeight() {
    Scanner input;
    double height = input.nextDouble();
    }

double displayDimensions(double length, double width, double height) {   
    Scanner input;
    }

double calcVolume() {
}

}

What am I missing? My program will not compile and gives the error message

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    Syntax error, insert "Identifier (" to complete MethodHeaderName
    Syntax error, insert ")" to complete MethodDeclaration
    Syntax error, insert ";" to complete MethodDeclaration
    Syntax error, insert "}" to complete ClassBody
at Box.main(Box.java:18)

As I said in the comments, you have put everything in main . Don't do that. As it is, your Box class is basically empty, and you are currently almost creating an anonymous sub-class in main . Your directions do not mention a main , but are pretty straight forward. You were supposed to write something like

public class Box {
    // Three instance variables – length, width and height (each of type double)
    private double length, width, height;
    // One instance variables – input (type Scanner) initialized to System.in
    private Scanner input = new Scanner(System.in);

    // Default constructor (no-arg) – initialize all three instance variables to 1
    public Box() {
        this.length = this.width = this.height = 1;
    }

    // Initial constructor – initialize all three instance variables
    public Box(double length, double width, double height) {
        this.length = length;
        this.width = width;
        this.height = height;
    }

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

    // inputWidth, inputLength, and inputHeight methods that set the instance
    // variables based on user input have not parameters and do not return a value.
    public void inputWidth() {
        this.width = input.nextDouble();
    }

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

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

    // a displayDimensions method that displays the length X Width X height
    // (separated by “X”) and does not return a value.
    public void displayDimensions() {
        System.out.printf("%.2fX%.2fX%.2f%n", length, width, height);
    }

    // a calcVolume method that has no parameters and calculates the volume of the
    // box
    public double calcVolume() {
        return length * width * height;
    }
}

Create a Java class named Package that contains the following: Package should have three private instance variables of type double named length, width, and height.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM