package aresfour; class Rectangle{ double width; double height; public Rectangle(double width, double height) { this.width = width; this.height = height; } public String getLength() { return "周长:"+2 * (width + height); } public String getArea() { System.out.println("面积Area: " + (width * height)); } } public class four { public static void main(String[] args) { Rectangle re=new Rectangle(7.8,6.4); System.out.println(re.getArea()); } }怎么改正
时间: 2023-07-16 12:16:38 浏览: 37
您可以将 `getArea()` 方法的返回类型改为 `String` 并返回一个字符串表示面积,像这样:
```
package aresfour;
class Rectangle {
double width;
double height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
public String getLength() {
return "周长:" + 2 * (width + height);
}
public String getArea() {
return "面积Area: " + (width * height);
}
}
public class Four {
public static void main(String[] args) {
Rectangle re = new Rectangle(7.8, 6.4);
System.out.println(re.getArea());
}
}
```
注意,类名应该遵循 Java 命名规范,即首字母大写。
相关问题
package b; public class yx { public static void main(String[] args) { Cuboid v = new Cuboid(); v.volume(); } } class Rectangle{ double length; double width; } class Cuboid extends Rectangle{ double height; public Cuboid(double length,double width,double height) { this.length=length; this.width=width; this.height=height; } void volume() { length=10; width=5; height=2; System.out.println("体积为:" +length*width*height); } }
你的代码中存在一些问题:
1. `yx` 类和 `Cuboid` 类不在同一个包中,需要将 `Cuboid` 类声明为 `public` 类型,或将两个类放在同一个包中。
2. 在 `yx` 类的 `main` 方法中,创建了一个没有参数的 `Cuboid` 对象 `v`,但是你的 `Cuboid` 类中只定义了一个有参数的构造方法,因此需要在创建对象时传入三个参数。
3. `volume` 方法中的计算公式有误,应该是长乘以宽乘以高。
下面是修改后的示例代码:
```java
package b;
public class yx {
public static void main(String[] args) {
Cuboid v = new Cuboid(10, 5, 2);
v.volume();
}
}
class Rectangle {
double length;
double width;
}
public class Cuboid extends Rectangle {
double height;
public Cuboid(double length, double width, double height) {
this.length = length;
this.width = width;
this.height = height;
}
void volume() {
System.out.println("体积为:" + length * width * height);
}
}
```
希望能够帮助到你。
补全代码测试输入:9 8 7 6 5 4 3 2 1 预期输出: Circle.Area=254.34 Circle.Circumference=56.52 Rectangle.Area=56.0 Rectangle.Circumference=30.0 Triangle.Area=9.921567416492215 Triangle.Circumference=15.0 Triangle.Area=0.0 Triangle.Circumference=6.0package module3; import java.util.Scanner; public class IShapeTest { public static void main(String[] args) throws Exception { Scanner scanner = new Scanner(System.in); Double radius = scanner.nextDouble(); Circle circle = new Circle(radius); System.out.println("Circle.Area=" + circle.getArea()); System.out.println("Circle.Circumference=" + circle.getCircumference()); …} /********** End *********/
/********** Start *********/
Double[] sides = new Double[3];
for (int i = 0; i < 3; i++) {
sides[i] = scanner.nextDouble();
}
Triangle triangle = new Triangle(sides[0], sides[1], sides[2]);
System.out.println("Triangle.Area=" + triangle.getArea());
System.out.println("Triangle.Circumference=" + triangle.getCircumference());
Double width = scanner.nextDouble();
Double height = scanner.nextDouble();
Rectangle rectangle = new Rectangle(width, height);
System.out.println("Rectangle.Area=" + rectangle.getArea());
System.out.println("Rectangle.Circumference=" + rectangle.getCircumference());
/********** End **********/