简体   繁体   中英

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. 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()); in a for loop. But when I try and call the toString method from the child classes in that for loop everything returns as "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

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());
        }
    }
}

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; to 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() .

In 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

It prints 0 because you don't call calculation of perimeter and area for class instance before print it. But I also recommend reviewing your code that it does not use unnecessary variables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM