fill in the code below Import java. util. ArrayList; public class ObjectCalculat
ID: 3686918 • Letter: F
Question
fill in the code below
Import java. util. ArrayList; public class ObjectCalculator { public static void main(String args[]) { ArrayList objects = new ArrayList();//Create a Scanner to read input from the keyboard//[Add code here]//Prompt the user for a radius, get the radius from//the keyboard, and add the circle to the list of objects.//[Add code here]//Prompt the user for a side length for an equilateral triangle.//Create an equilateral triangle with that side length and//add it to the list of objects.//[Add code here]//Prompt the user for the width and height of a rectangle.//Create a rectangle with that width and height and add it//to the list of objects.//[Add code here]//Write a for loop that will print out all//in the list, along with their attributes//width and height for rectangle, and side//equilateral triangle).//[Add code here]//Write code that will calculate and print the total perimeter//and total area of all the objects in the list.//[Add code here]Explanation / Answer
ObjectCalculator.java
import java.util.ArrayList;
public class ObjectCalculator {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<GeometricObject> objects = new ArrayList<GeometricObject>();
//Create a Scanner to read input from the keyboard
java.util.Scanner scanner = new java.util.Scanner(System.in);
//Prompt the user for a radius, get the radius from the keyboard, and add circle to the list of objects
System.out.println("Enter the radius for Circle");
double radius = scanner.nextDouble();
Circle c = new Circle(radius);
objects.add(c);
//Prompt the user for a side length for a equilateral triangle.
System.out.println("Enter the side length for Equilateral Triangle");
double sideLength = scanner.nextDouble();
EquilateralTriangle e = new EquilateralTriangle(sideLength);
objects.add(e);
//Prompt the user for a width and height for a Rectangle.
System.out.println("Enter the width for Ractangle");
double width = scanner.nextDouble();
System.out.println("Enter the height for Ractangle");
double height = scanner.nextDouble();
Rectangle r = new Rectangle(width, height);
objects.add(r);
//Write a for loop that will print out all of the objects in the list, along with their attributes
for(int i=0; i<objects.size(); i++){
GeometricObject obj = objects.get(i);
if(obj instanceof Circle){
Circle cc = (Circle) obj;
System.out.println("Circle Radius :"+cc.getRadius());
}
else if(obj instanceof Rectangle){
Rectangle rr = (Rectangle) obj;
System.out.println("Rectangle Width :"+rr.getWidth()+" Height : "+rr.getHeight());
}
else if(obj instanceof EquilateralTriangle){{
EquilateralTriangle ee = (EquilateralTriangle) obj;
System.out.println("EquilateralTriangle Side Length :"+ee.getSideLength());
}
}
}
//Write a for loop that will calculate and print total perimeter and area
for(int i=0; i<objects.size(); i++){
GeometricObject obj = objects.get(i);
System.out.println(obj.toString());
}
}
}
Circle.java
/*
* Circle Area formula : PI * R * R
* Circle Perimeter formula: 2 * PI * R
*
* */
public class Circle extends GeometricObject{
private double radius;
private final double PI = 3.14;
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public Circle(double radius){
this.radius = radius;
}
public double getArea(){
return PI * (radius * radius);
}
public double getPerimeter(){
return 2 * PI * radius;
}
public String toString(){
return String.format("Circle: Area = %.2f , Perimeter = %.2f ",getArea(),getPerimeter());
}
}
Rectangle.java
/*
* Rectangle Area formula : width * height
* Rectangle Perimeter formula: 2 * (height + width)
*
* */
public class Rectangle extends GeometricObject{
private double width;
private double height;
private final double PI = 3.14;
public Rectangle(double width, double height){
this.width = width;
this.height = height;
}
public double getArea(){
return width * height;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getPerimeter(){
return 2 * (height + width);
}
public String toString(){
return String.format("Rectangle: Area = %.2f , Perimeter = %.2f ",getArea(),getPerimeter());
}
}
EquilateralTriangle.java
/*
* EquilateralTriangle Area formula : (Math.sqrt(3) / 4) * a * a
* EquilateralTriangle Perimeter formula: 3 * a
*
* */
public class EquilateralTriangle extends GeometricObject {
private double sideLength;
private final double PI = 3.14;
public EquilateralTriangle(double sideLength){
this.sideLength = sideLength;
}
public double getArea(){
return (Math.sqrt(3) / 4) * sideLength * sideLength ;
}
public double getPerimeter(){
return 3 * sideLength;
}
public String toString(){
return String.format("EquilateralTriangle: Area = %.2f , Perimeter = %.2f ",getArea(),getPerimeter());
}
public double getSideLength() {
return sideLength;
}
public void setSideLength(double sideLength) {
this.sideLength = sideLength;
}
}
GeometricObject.java
/*
* GeometricObject abstract class
*
* */
public abstract class GeometricObject {
public abstract double getArea();
public abstract double getPerimeter();
public String toString(){
return String.format("Circle: Area = %.2f , Perimeter = %.2f ",getArea(),getPerimeter());
}
}
Output:
Enter the radius for Circle
2.4
Enter the side length for Equilateral Triangle
2
Enter the width for Ractangle
3
Enter the height for Ractangle
4
Circle Radius :2.4
EquilateralTriangle Side Length :2.0
Rectangle Width :3.0 Height : 4.0
Circle: Area = 18.09 , Perimeter = 15.07
EquilateralTriangle: Area = 1.73 , Perimeter = 6.00
Rectangle: Area = 12.00 , Perimeter = 14.00
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.