Java "Sum the areas of geometric objects". Write a method that sums the areas of all the geometric objects in an array. The method signature is :
public static double sumArea(GeometricObject[] a)
Your main method should allow the user to enter 2 radius values for two circles and 2 sets of length and width for two rectangles. The final output should be like the following rounded to 5 decimal places:
The total area is 217.63715
Program:
import java.util.*;
abstract class GeometricObject
{
abstract double getArea();
}
class Circle extends GeometricObject
{
double radius;
Circle(double radius)
{
this.radius=radius;
}
double getArea()
{
return Math.PI*radius*radius;
}
}
class Rectangle extends GeometricObject
{
double length,width;
Rectangle(double length,double width)
{
this.length=length;
this.width=width;
}
double getArea()
{
return length*width;
}
}
class SumAreaDemo
{
public static double sumArea(GeometricObject[] a)
{
int i;
double sum;
sum=0;
for(i=0;i<a.length;i++)
{
sum=sum+a[i].getArea();
}
return sum;
}
public static void main(String args[])
{
double r1,r2,l1,l2,w1,w2;
Scanner sc=new Scanner(System.in);
System.out.print("\nEnter radius of a circle 1: ");
r1=sc.nextDouble();
System.out.print("\nEnter radius of a circle 2: ");
r2=sc.nextDouble();
System.out.print("\nEnter length and width of Rectangle1:
");
l1=sc.nextDouble();
w1=sc.nextDouble();
System.out.print("\nEnter length and width of Rectangle2:
");
l2=sc.nextDouble();
w2=sc.nextDouble();
GeometricObject[] a={new Circle(r1),new Circle(r2),new Rectangle(l1,w1), new Rectangle(l2,w2)};
double sum=sumArea(a);
System.out.printf("\nTotal Area is %.5f\n\n",sum);
}
}
Output:

Java "Sum the areas of geometric objects". Write a method that sums the areas of all...