gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 10:06:17 25 4
gpt4 key购买 nike

我是编程初学者,特别是在使用构造函数时遇到问题。我必须为我的一个实验室编写一个程序,该程序必须仅包含:

  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

这是我的代码:

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)

最佳答案

正如我在评论中所说,您已将所有内容放入 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;
}
}

关于具有多个方法和构造函数的 Java Box 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52883883/

25 4 0
文章推荐: python - @staff_member_required 正在抛出 "object has no attribute ' 用户'”
文章推荐: linux - 安装 MongoDB 时如何解决我的 Ubuntu 18.04 LTS 上的以下 "keyserver receive failed"问题?
文章推荐: jquery - 在多个
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com