rls1982 rls1982 - 1 month ago 10
Java Question

Java: How to get the Average Area from the given shape

I'm writing a Java Program in which I have to get the Average Area from the given shape. I'm needing to get the average area from the askAreaProblem problem and here is the methods for the askAreaProblem and the getAverageArea method. The askAreaProblem method works fine, but the unit test for the getAverageArea method are failing. Here is my code for the two methods.

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


Here is the Unit test. If it passes I get 20 points.

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


Here is the rest of my code for the program that I'm writing:

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

Answer

The computation of avg = sum/shapes.length should be after the loop of accumulating sum, not before.

Comments