首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >具有多个方法和构造函数的Java Box程序

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

Stack Overflow用户
提问于 2018-10-19 07:25:01
回答 1查看 2.1K关注 0票数 1

我是编程的初学者,特别是在使用构造函数方面遇到了麻烦。我必须为我的一个实验编写一个程序,它必须只包含以下内容:

复制三个实例变量-长度、宽度和高度(每个都是双精度类型)复制一个实例变量-输入(类型扫描器)初始化为System.in

  • Default构造函数(no-arg) -将所有三个实例变量初始化为1个variables

  • Copy初始构造函数-初始化所有三个实例Box

  • inputWidth,构造函数-

  • inputLength,基于用户输入设置实例变量的inputHeight方法没有参数并且不返回值。

  • 显示长度X宽度X高度(用“X”分隔)并且不返回值的displayDimensions方法。

  • 没有参数并且计算box

体积的displayDimensions方法

我们还获得了应用程序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

<>H133输入宽度:2<代码>H234<代码>H135输入高度:3<代码>H137更新后的尺寸为1.0 X 2.0 X 3.0,体积为6.0<代码>F239

下面是我的代码:

代码语言:javascript
复制
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() {
}

}

我遗漏了什么?我的程序将无法编译,并给出错误消息

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

发布于 2018-10-19 08:16:19

正如我在评论中所说的,您已经将所有内容都放在了main中。别干那事。实际上,您的Box类基本上是空的,并且您当前几乎是在main中创建一个匿名子类。您的说明没有提到main,但是非常直接。你应该写下这样的东西

代码语言:javascript
复制
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;
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52883883

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档