Question

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

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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:

Add a comment
Know the answer?
Add Answer to:
Java "Sum the areas of geometric objects". Write a method that sums the areas of all...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT