简体   繁体   English

从主方法调用时,子类中的 toString 方法返回零

[英]When called from my main method, my toString method in child class is returning zero

I have the assignment to use an abstract class as a superclass "Shape".我有任务使用抽象类作为超类“形状”。 It has three classes that extend that class, Triangle, Circle, and Square.它具有扩展该类的三个类,Triangle、Circle 和 Square。 Each class is supposed to compute the area and the perimeter of these shapes.每个类都应该计算这些形状的面积和周长。 I have things working when I System.out.println(shapeArray[i].computeArea());当我System.out.println(shapeArray[i].computeArea()); in a for loop.在 for 循环中。 But when I try and call the toString method from the child classes in that for loop everything returns as "0".但是当我尝试从该 for 循环中的子类调用 toString 方法时,一切都返回为“0”。 I appreciate anybody taking the time to help out, as I am pretty stuck here.我感谢任何人花时间提供帮助,因为我很困在这里。 I feel like I have read, and re-read threads, and my textbook all day.我觉得我一整天都在阅读和重读主题和教科书。 I feel like there is just some dumb mistake here that I cannot see.我觉得这里有一些我看不到的愚蠢错误。

Shape.java形状.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三角形.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圆环.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矩形.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());
        }
    }
}

Output:输出:

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

your class Triangle.computeArea method change the from area = this.area;你的类Triangle.computeArea方法改变了 from area = this.area; to this.area=area;this.area=area; Other class like this.其他班这样。

In you must call in for loop shapeArray[i].computeArea() and shapeArray[i].computePerimeter() before shapeArray[i].toString() .您必须在shapeArray[i].toString()之前调用 for 循环shapeArray[i].computeArea()shapeArray[i].computePerimeter() shapeArray[i].toString()

In ShapeArray.javaShapeArray.java 中

//other code

for (int i = 0; i <= 2; i++) {
    shapeArray[i].computeArea();
    shapeArray[i].computePerimeter()
    System.out.println(shapeArray[i].toString());
}

//other code

It prints 0 because you don't call calculation of perimeter and area for class instance before print it.它打印 0,因为您在打印之前没有调用类实例的周长和面积计算。 But I also recommend reviewing your code that it does not use unnecessary variables.但我也建议检查您的代码,它没有使用不必要的变量。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么从主方法登录时,我的ArrayList大小显示为零,而从内部方法调用时,为什么显示一定数目的大小? - Why does my ArrayList show size of zero when logged from main method but shows a size of a certain number when called from internal method? 从正确的类返回toString方法的问题 - Problem with toString method returning from proper class 从子类使用toString()方法 - Using toString() method from child class 我的另一个类的方法没有被调用? - My method from another class is not being called? 为什么我的主类没有运行我调用的方法? - Why doesnt my main class run the method i called? 我来自另一个类的方法,不会在我的 Main 类中调用输入 - My method from another class, will not call an input in my Main class 从另一个类调用时,实例“ Main”方法未运行 - Instance “Main” Method Not Running When Called from Another Class 如果一个方法属于另一个扩展Thread的类但从主线程调用,则该方法是由主线程还是子线程执行? (JAVA) - If a method belongs to another class that extends Thread but is called from the main thread, will it be executed by main or a child thread? (Java) 当main方法包含在一个类中时,我的程序会编译,但不会在main方法分开时编译 - When main method is contained within a class, my program compiles but not when main method is separate Mockito 正在调用主类的方法 - Method from main class is getting called for Mockito
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM