这是我对Java类的最后一个作业,我一直试图通过编译器运行它,但是我不明白代码有什么问题。
在阅读了有关如何解决返回类型问题的信息后,我尝试使用了void,但这使情况变得更糟,也许我将void放在了错误的位置。
public class Exercise09_01 {
private double width = 1;
private double height = 1;
public Rectangle() {
}
public Rectangle(double newWidth, double newHeight) {
width = newWidth;
height = newHeight;
}
public double getArea() {
return width * height;
}
public double getPerimeter() {
return 2 * (width + height);
}
public static void main(String[] args) {
Rectangle rectangle1 = new Rectangle(4, 40);
System.out.println("The area of a 4.0 x 40.0 Rectangle is " +
rectangle1.getArea());
System.out.println("The perimeter of a 4.0 x 40.0 Rectangle is " +
rectangle1.getPerimeter());
Rectangle rectangle2 = new Rectangle(3.5, 35.9);
System.out.println("The area of a 3.5 x 35.9 Rectangle is " +
rectangle2.getArea());
System.out.println("The perimeter of a 3.5 x 35.9 Rectangle is " +
rectangle2.getPerimeter());
}
}
这是我上这堂课的最后一个作业,我只想在任何帮助下结束这项工作。
答案 0 :(得分:1)
代码中的错误是您的类名和构造函数名不同。
您有两种选择,一种是将构造函数重命名为Exercise01_01
或将Rectangle
的返回类型设为空。
public class Exercise01_01 {
private double width = 1;
private double height = 1;
public Exercise01_01() {
}
public Exercise01_01(double newWidth, double newHeight) {
width = newWidth;
height = newHeight;
}
public double getArea() {
return width * height;
}
public double getPerimeter() {
return 2 * (width + height);
}
public static void main(String[] args) {
Exercise01_01 rectangle1 = new Exercise01_01(4, 40);
System.out.println("The area of a 4.0 x 40.0 Rectangle is " + rectangle1.getArea());
System.out.println("The perimeter of a 4.0 x 40.0 Rectangle is " + rectangle1.getPerimeter());
Exercise01_01 rectangle2 = new Exercise01_01(3.5, 35.9);
System.out.println("The area of a 3.5 x 35.9 Rectangle is " + rectangle2.getArea());
System.out.println("The perimeter of a 3.5 x 35.9 Rectangle is " + rectangle2.getPerimeter());
}
}
答案 1 :(得分:0)
public Rectangle() {
}
就Java而言,这是一种方法。并且所有方法都必须具有返回类型。
public Rectangle(double newWidth, double newHeight) {
width = newWidth;
height = newHeight;
}
这里也是。
除非确实需要在没有设置这些值的情况下制作第一个,否则您实际上不需要第一个。
您可以重命名它们,但是您可能只想重命名类Rectangle
答案 2 :(得分:0)
构造函数名称应与类名称相同
public class Exercise09_01 {
private double width = 1;
private double height = 1;
public Exercise09_01() {
}
public Exercise09_01(double newWidth, double newHeight) {
width = newWidth;
height = newHeight;
}
}
答案 3 :(得分:0)
public class Exercise09_01 {
private double width = 1;
private double height = 1;
public Exercise09_01() {
}
public Exercise09_01(double newWidth, double newHeight) {
width = newWidth;
height = newHeight;
}
public double getArea() {
return width * height;
}
public double getPerimeter() {
return 2 * (width + height);
}
public static void main(String[] args) {
Exercise09_01 rectangle1 = new Exercise09_01(4, 40);
System.out.println("The area of a 4.0 x 40.0 Rectangle is " + rectangle1.getArea());
System.out.println("The perimeter of a 4.0 x 40.0 Rectangle is " +
rectangle1.getPerimeter());
Exercise09_01 rectangle2 = new Exercise09_01(3.5, 35.9);
System.out.println("The area of a 3.5 x 35.9 Rectangle is " + rectangle2.getArea());
System.out.println("The perimeter of a 3.5 x 35.9 Rectangle is " +
rectangle2.getPerimeter());
}
}
感谢所有帮助,这是通过编译器的代码。只是留在这里供以后的访客使用。