Java:如何从给定的形状中获取平均面积

时间:2016-10-28 03:25:31

标签: java shape

我正在编写一个Java程序,我必须从给定的形状中获取平均面积。我需要从askAreaProblem问题中得到平均面积,这里是askAreaProblem和getAverageArea方法的方法。 askAreaProblem方法工作正常,但getAverageArea方法的单元测试失败。这是我的两种方法的代码。

public class Assignment9 {

    public static void askAreaProblem(IShape s, double acceptableVariance)
    {
        System.out.println("What is the area of a "+s.forProblemStatement());
        double answer=new Scanner(System.in).nextDouble();
        double area=s.getArea();
        if(Math.abs(answer-area)<=acceptableVariance) {
            System.out.println("Yay ! your answer, "+answer+" is correct");
        } else {
            System.out.println("Sorry :( your answer, "+answer+" is not correct.");
            System.out.println("The correct answer is "+area);
        }
    }

public static double getAverageArea(IShape[] shapes)
    { 
        double sum = 0;
        double avg = sum/shapes.length;
        for(IShape shape: shapes) {
            sum += shape.getArea();
        }
        return avg;
    }

这是单元测试。如果它通过我得到20分。

public void testAverageArea()
    {       
        IShape[] shapes={new Rectangle(2,3), new Circle(1)};
        Assert.assertEquals(4.57,Assignment9.getAverageArea(shapes),0.01);

        IShape[] shapes2={new Rectangle(2,3)};
        Assert.assertEquals(6,Assignment9.getAverageArea(shapes2),0.01);
    }   

    @Grade(points=20)
    @Test

以下是我编写的程序的其余代码:

package assignment;

import java.util.Scanner;
import java.util.Random;

interface IShape  {
    double getArea();
    double getPerimeter();
    String forProblemStatement(); 
}

class Rectangle implements IShape {
    private double width, height;
    public Rectangle(double width, double height) {
        this.width=width;
        this.height=height;
    }

    @Override
    public double getArea() { return width*height; }

    @Override
    public double getPerimeter() { return 2*(width+height); } 

    @Override
    public String toString(){ return "Rectangle[width:"+width+" height:"+height+"]"; }

    @Override
    public String forProblemStatement() {
        return "rectangle with width "+ width +" and height "+height+".";
    }
}

class Circle implements IShape {
    private double radius;
    public Circle(double radius) {
        this.radius=radius;
    }

    @Override public double getArea() { return Math.PI*radius*radius;}
    @Override public double getPerimeter() {return 2*radius*Math.PI;} 
    @Override public String toString(){ return "Circle[radius:"+radius+"]"; }
    @Override public String forProblemStatement() {
        return "circle with radius "+radius;
    }
}

class Square implements IShape {
    private double side;
    public Square(double side) {
        this.side=side;
    }

    double getSide(){return side;}

    @Override
    public double getArea() { return side * side; }

    @Override
    public double getPerimeter() { return 4 * side; } 

    @Override
    public String toString(){ return "Square"; }

    @Override
    public String forProblemStatement() {
        return "square.";
    }
}

class RegularPolygon implements IShape {
    private double sides;
    private double size;
    private double apothem;
    public RegularPolygon(double sides, double size, double apothem) {
        this.sides=sides;
        this.size=size;
        this.apothem=apothem;
    }

    double getSides(){
        return sides;
    }

    @Override
    public double getArea() {
        return .5 * apothem * size * sides;
    }

    @Override
    public double getPerimeter() {
        return size * sides;
    }

    @Override
    public String toString() {
        return "RegularPolygon";
    }

    @Override
    public String forProblemStatement() {
        return "RegularPolygon";
    }

}

class ShapeFactory {
    public static Random randomGenerator=new Random();
    public static IShape getShapeInstance(String kindOfShape, double param1, double param2, double param3) {
        kindOfShape=kindOfShape.toLowerCase();
        if(kindOfShape.equals("circle")) {
            return new Circle(param1);
        } else if (kindOfShape.equals("rectangle")) {
            return new Rectangle(param1, param2);
        }   
        else if (kindOfShape.equals("square")) {
            return new Square(param1);
    } else if (kindOfShape.equals("RegularPolygon")) {
        return new RegularPolygon(param1, param2, param3);
    }
        return null;
}   
    public static IShape getRandomShape() {
        String[] shapeNames={"circle", "rectangle", "square","RegularPolygon"};
        int shape=randomGenerator.nextInt(shapeNames.length);
        int param1=randomGenerator.nextInt(10)+1;
        int param2=randomGenerator.nextInt(10)+1;
        int param3=randomGenerator.nextInt(10)+1;

        return getShapeInstance(shapeNames[shape],param1,param2,param3);
    }
}

2 个答案:

答案 0 :(得分:2)

avg = sum/shapes.length的计算应该在累积sum的循环之后,而不是之前。

答案 1 :(得分:0)

逐行查看代码。

第一行你声明了sum,其值为零。 第二行,您已声明avg,其值为sum/shapes.length

这意味着avg将等于零,因此您的getAverageArea方法将始终返回零。

我建议:

public static double getAverageArea(IShape[] shapes)
    { 
        double sum = 0;
        for(IShape shape: shapes) {
            sum += shape.getArea();
        }
        return sum/shapes.length;
    }