从我的主要方法调用时,我在子 class 中的 toString 方法返回零
When called from my main method, my toString method in child class is returning zero
我的作业是使用抽象 class 作为超级 class "Shape"。它有三个 class 扩展 class、三角形、圆形和方形。每个 class 都应该计算这些形状的面积和周长。当我在 for 循环中 System.out.println(shapeArray[i].computeArea());
时,我的工作正常。但是当我尝试从那个 for 循环中的子 classes 调用 toString 方法时,所有 returns 都为“0”。我感谢任何人花时间帮忙,因为我很困在这里。我觉得我整天都在阅读和重新阅读线程和我的教科书。我觉得这里有一些我看不到的愚蠢错误。
Shape.java
public class Circle extends Shape {
private double radius;
final private double PI = 3.14159;
private double circumfrence;
private double area;
Circle(double radius) {
this.radius = radius;
}
@Override
public double computeArea() {
double radSquared = radius * radius;
double area = PI * radSquared;
return area;
}
@Override
public double computePerimeter() {
circumfrence = 2 * PI * radius;
return circumfrence;
}
@Override
public String toString() {
return "Area of Circle: " +area + "\nCircumfrence of Circle: " + circumfrence;
}
}
Triangle.java
public class Triangle extends Shape {
private double side1;
private double side2;
private double side3;
private double area;
private double perimeter;
Triangle (double side1, double side2, double side3) {
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
@Override
public double computeArea() {
double s =(side1 + side2 + side3) / 2;
double area= Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
area = this.area;
return area;
}
@Override
public double computePerimeter() {
perimeter = side1 + side2 + side3;
return perimeter;
}
@Override
public String toString() {
return "Area of Triangle: " + area+ "\nPerimeter of Triangle: " + perimeter;
}
}
Circle.java
public class Circle extends Shape {
private double radius;
final private double PI = 3.14159;
private double circumfrence;
private double area;
Circle(double radius) {
this.radius = radius;
}
@Override
public double computeArea() {
double radSquared = radius * radius;
double area = PI * radSquared;
return area;
}
@Override
public double computePerimeter() {
circumfrence = 2 * PI * radius;
return circumfrence;
}
@Override
public String toString() {
return "Area of Circle: " +area + "\nCircumfrence of Circle: " + circumfrence;
}
}
Rectangle.java
public class Rectangle extends Shape {
private double length;
private double height;
private double sumOfLength;
private double sumOfHeight;
private double area;
private double perimeter;
Rectangle(double length, double height) {
this.height = height;
this.length = length;
}
@Override
public double computeArea() {
area = (length * height);
return area;
}
@Override
public double computePerimeter() {
sumOfLength = length * 2;
sumOfHeight = height * 2;
perimeter = sumOfLength + sumOfHeight;
return perimeter;
}
@Override
public String toString() {
return "Area of Rectangle: " +area + "\nPerimeter of Rectangle: " + perimeter;
}
}
ShapeArray.java
public class ShapeArray {
public static void main(String [] args) {
Shape shapeArray[] = new Shape[3];
Circle circ = new Circle(9);
Rectangle rect = new Rectangle(10, 6);
Triangle tri = new Triangle(6, 8, 12);
shapeArray[0] = circ;
shapeArray[1] = rect;
shapeArray[2] = tri;
for (int i = 0; i <= 2; i++) {
System.out.println(shapeArray[i].toString());
}
}
}
输出:
Area of Circle: 0.0
Circumfrence of Circle: 0.0
Area of Rectangle: 0.0
Perimeter of Rectangle: 0.0
Area of Triangle: 0.0
Perimeter of Triangle: 0.0
您的 class Triangle.computeArea
方法将 area = this.area;
更改为 this.area=area;
其他class这样的
您必须在 shapeArray[i].toString()
之前调用 for 循环 shapeArray[i].computeArea()
和 shapeArray[i].computePerimeter()
。
在ShapeArray.java
//other code
for (int i = 0; i <= 2; i++) {
shapeArray[i].computeArea();
shapeArray[i].computePerimeter()
System.out.println(shapeArray[i].toString());
}
//other code
它打印 0 是因为您在打印它之前没有调用 class 实例的周长和面积计算。但我也建议检查您的代码,确保它没有使用不必要的变量。
我的作业是使用抽象 class 作为超级 class "Shape"。它有三个 class 扩展 class、三角形、圆形和方形。每个 class 都应该计算这些形状的面积和周长。当我在 for 循环中 System.out.println(shapeArray[i].computeArea());
时,我的工作正常。但是当我尝试从那个 for 循环中的子 classes 调用 toString 方法时,所有 returns 都为“0”。我感谢任何人花时间帮忙,因为我很困在这里。我觉得我整天都在阅读和重新阅读线程和我的教科书。我觉得这里有一些我看不到的愚蠢错误。
Shape.java
public class Circle extends Shape {
private double radius;
final private double PI = 3.14159;
private double circumfrence;
private double area;
Circle(double radius) {
this.radius = radius;
}
@Override
public double computeArea() {
double radSquared = radius * radius;
double area = PI * radSquared;
return area;
}
@Override
public double computePerimeter() {
circumfrence = 2 * PI * radius;
return circumfrence;
}
@Override
public String toString() {
return "Area of Circle: " +area + "\nCircumfrence of Circle: " + circumfrence;
}
}
Triangle.java
public class Triangle extends Shape {
private double side1;
private double side2;
private double side3;
private double area;
private double perimeter;
Triangle (double side1, double side2, double side3) {
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
@Override
public double computeArea() {
double s =(side1 + side2 + side3) / 2;
double area= Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
area = this.area;
return area;
}
@Override
public double computePerimeter() {
perimeter = side1 + side2 + side3;
return perimeter;
}
@Override
public String toString() {
return "Area of Triangle: " + area+ "\nPerimeter of Triangle: " + perimeter;
}
}
Circle.java
public class Circle extends Shape {
private double radius;
final private double PI = 3.14159;
private double circumfrence;
private double area;
Circle(double radius) {
this.radius = radius;
}
@Override
public double computeArea() {
double radSquared = radius * radius;
double area = PI * radSquared;
return area;
}
@Override
public double computePerimeter() {
circumfrence = 2 * PI * radius;
return circumfrence;
}
@Override
public String toString() {
return "Area of Circle: " +area + "\nCircumfrence of Circle: " + circumfrence;
}
}
Rectangle.java
public class Rectangle extends Shape {
private double length;
private double height;
private double sumOfLength;
private double sumOfHeight;
private double area;
private double perimeter;
Rectangle(double length, double height) {
this.height = height;
this.length = length;
}
@Override
public double computeArea() {
area = (length * height);
return area;
}
@Override
public double computePerimeter() {
sumOfLength = length * 2;
sumOfHeight = height * 2;
perimeter = sumOfLength + sumOfHeight;
return perimeter;
}
@Override
public String toString() {
return "Area of Rectangle: " +area + "\nPerimeter of Rectangle: " + perimeter;
}
}
ShapeArray.java
public class ShapeArray {
public static void main(String [] args) {
Shape shapeArray[] = new Shape[3];
Circle circ = new Circle(9);
Rectangle rect = new Rectangle(10, 6);
Triangle tri = new Triangle(6, 8, 12);
shapeArray[0] = circ;
shapeArray[1] = rect;
shapeArray[2] = tri;
for (int i = 0; i <= 2; i++) {
System.out.println(shapeArray[i].toString());
}
}
}
输出:
Area of Circle: 0.0
Circumfrence of Circle: 0.0
Area of Rectangle: 0.0
Perimeter of Rectangle: 0.0
Area of Triangle: 0.0
Perimeter of Triangle: 0.0
您的 class Triangle.computeArea
方法将 area = this.area;
更改为 this.area=area;
其他class这样的
您必须在 shapeArray[i].toString()
之前调用 for 循环 shapeArray[i].computeArea()
和 shapeArray[i].computePerimeter()
。
在ShapeArray.java
//other code
for (int i = 0; i <= 2; i++) {
shapeArray[i].computeArea();
shapeArray[i].computePerimeter()
System.out.println(shapeArray[i].toString());
}
//other code
它打印 0 是因为您在打印它之前没有调用 class 实例的周长和面积计算。但我也建议检查您的代码,确保它没有使用不必要的变量。