具有多个方法和构造函数的Java Box程序

时间:2018-10-18 23:25:01

标签: java methods constructor

我是编程的初学者,尤其是在使用构造函数时遇到麻烦。我必须为我的一个实验室编写一个程序,该程序必须仅包含以下内容:

  
      
  1. 三个实例变量–长度,宽度和高度(每个都是double类型)      
        
    1. 一个实例变量–输入(类型为Scanner)初始化为System.in
    2.   
    3. 默认构造函数(no-arg)–将所有三个实例变量初始化为1
    4.   
    5. 初始构造函数–初始化所有三个实例变量
    6.   
    7. 复制构造函数–复制框
    8.   根据用户输入设置实例变量的
    9. inputWidth,inputLength和inputHeight方法没有参数,也没有参数   返回一个值。
    10.   
    11. 一个displayDimensions方法,该方法显示长度X宽度X高度(用“ X”分隔),并且不返回任何值。
    12.   
    13. 无参数的calcVolume方法,不会计算盒子的体积
    14.   
  2.   
     

我们还获得了BoxTest应用程序,其中的输出必须   完全符合以下条件:

     
      
  • 默认尺寸为1.0 X 1.0 X 1.0,体积为1.0
  •   
  • 初始尺寸为8.5 X 11.0 X 1.0,体积为93.5
  •   
  • 复制的尺寸为8.5 X 11.0 X 1.0,体积为93.5
  •   
  • 更新尺寸
  •   
  • 输入长度:1
  •   
  • 输入宽度:2
  •   
  • 输入高度:3
  •   
  • 更新后的尺寸为1.0 X 2.0 X 3.0,体积为6.0
  •   

这是我的代码:

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() {
}

}

我想念什么?我的程序无法编译并给出错误消息

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)

1 个答案:

答案 0 :(得分:0)

正如我在评论中所说,您已将所有内容放入main中。不要那样做实际上,您的Box类基本上是空的,并且当前您几乎在main中创建一个匿名子类。您的指示并未提及main,但很简单。你应该写类似

的东西
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;
    }
}