Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

*** Please implement in Java **** Write a method that sums the areas of all the

ID: 3589460 • Letter: #

Question

*** Please implement in Java ****

Write a method that sums the areas of all the geometric objects in an array. The method signature is:

public static double totalArea(GeometricObject[] list)

The test program will create an array of four geometicObjects (two Rectangle and two Circle ) and call the totalArea method to compute the sum of the areas.

Note:

The radius, width and height are positive values and default value is 1.

Input format:

The first number is the width of the first rectangle follow by

the height of the first rectangle follow by

the width of the second rectangle

the height of the second rectangle

the radius of the first circle

the radius of the second circle

**** For main, use the following*****

**** Main may not be modified******

class DriverTest{

public static void main(String args[]){

Scanner input = new Scanner(System.in);

double w1 = input.nextDouble();

double h1 = input.nextDouble();

double w2 = input.nextDouble();

double h2 = input.nextDouble();

double r1 = input.nextDouble();

double r2 = input.nextDouble();

  

GeometricObject[] list = new GeometricObject[4];

list[0] = new Rectangle(w1, h1);

list[1] = new Rectangle(w2, h2);;

list[2] = new Circle(r1);

list[3] = new Circle(r2);

  

System.out.printf("%.2f",HW5_P2.totalArea(list));

}

}

**** The geometric object class is as follows ****

**** This class may not be modified*********

abstract class GeometricObject {

private String color = "white";

private boolean filled;

private java.util.Date dateCreated;

/** Construct a default geometric object */

protected GeometricObject() {

dateCreated = new java.util.Date();

}

/** Construct a geometric object with color and filled value */

protected GeometricObject(String color, boolean filled) {

dateCreated = new java.util.Date();

this.color = color;

this.filled = filled;

}

/** Return color */

public String getColor() {

return color;

}

/** Set a new color */

public void setColor(String color) {

this.color = color;

}

/** Return filled. Since filled is boolean,

* the get method is named isFilled */

public boolean isFilled() {

return filled;

}

/** Set a new filled */

public void setFilled(boolean filled) {

this.filled = filled;

}

/** Get dateCreated */

public java.util.Date getDateCreated() {

return dateCreated;

}

@Override

public String toString() {

return "created on " + dateCreated + " color: " + color +

" and filled: " + filled;

}

/** Abstract method getArea */

public abstract double getArea();

}

class Circle extends GeometricObject{

private double radius = 1;

  

public Circle(){

}

  

public Circle(double radius){

if(radius > 0)

this.setRadius(radius);

}

  

/** Return radius */

public double getRadius() {

return radius;

}

/** Set a new radius */

public void setRadius(double radius) {

if(radius > 0)

this.radius = radius;

}

  

public double getArea(){

return this.radius * this.radius * Math.PI;

}

  

}

class Rectangle extends GeometricObject{

private double width = 1;

private double height = 1;

  

public Rectangle() {

}

public Rectangle(double width, double height) {

if(width>0 && height>0){

this.width = width;

this.height = height;

}

}

/** Return width */

public double getWidth() {

return width;

}

/** Set a new width */

public void setWidth(double width) {

this.width = width;

}

/** Return height */

public double getHeight() {

return height;

}

/** Set a new height */

public void setHeight(double height) {

this.height = height;

}

  

public double getArea(){

return this.width * this.height;

}

  

}

***The HW4_P2 class follows***

*** Implementation is done in this class****

class HW5_P2{

public static double totalArea(GeometricObject[] list){

  

  

}

}

Explanation / Answer

GeometricObject.java

//**** The geometric object class is as follows ****
//**** This class may not be modified*********
abstract class GeometricObject {
private String color = "white";
private boolean filled;
private java.util.Date dateCreated;

/** Construct a default geometric object */
protected GeometricObject() {
dateCreated = new java.util.Date();
}
/** Construct a geometric object with color and filled value */
protected GeometricObject(String color, boolean filled) {
dateCreated = new java.util.Date();
this.color = color;
this.filled = filled;
}

/** Return color */
public String getColor() {
return color;
}

/** Set a new color */
public void setColor(String color) {
this.color = color;
}

/** Return filled. Since filled is boolean,
* the get method is named isFilled */
public boolean isFilled() {
return filled;
}

/** Set a new filled */
public void setFilled(boolean filled) {
this.filled = filled;
}

/** Get dateCreated */
public java.util.Date getDateCreated() {
return dateCreated;
}

@Override
public String toString() {
return "created on " + dateCreated + " color: " + color +
" and filled: " + filled;
}

/** Abstract method getArea */
public abstract double getArea();

}

_______________

Rectangle.java

class Rectangle extends GeometricObject{

private double width = 1;

private double height = 1;

  

public Rectangle() {

}

public Rectangle(double width, double height) {

if(width>0 && height>0){

this.width = width;

this.height = height;

}

}

/** Return width */

public double getWidth() {

return width;

}

/** Set a new width */

public void setWidth(double width) {

this.width = width;

}

/** Return height */

public double getHeight() {

return height;

}

/** Set a new height */

public void setHeight(double height) {

this.height = height;

}

  

public double getArea(){

return this.width * this.height;

}

  

}

_____________________

Circle.java

class Circle extends GeometricObject{
private double radius = 1;
  
public Circle(){
}
  
public Circle(double radius){
if(radius > 0)
this.setRadius(radius);
}
  
/** Return radius */
public double getRadius() {
return radius;
}

/** Set a new radius */
public void setRadius(double radius) {
if(radius > 0)
this.radius = radius;
}
  
public double getArea(){
return this.radius * this.radius * Math.PI;
}
  
}

__________________

HW5_P2.java

class HW5_P2 {

public static double totalArea(GeometricObject[] list) {

double total=0.0;

for(int i=0;i<list.length;i++)

{

if(list[i] instanceof Rectangle)

{

total+=list[i].getArea();

}

else if(list[i] instanceof Circle)

{

total+=list[i].getArea();

}

}

return total;

}

}

__________________

DriverTest.java

import java.util.Scanner;

public class DriverTest {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.println("** Rectangle#1 **");

System.out.println("Enter the Width of Rectangle :");

double w1 = input.nextDouble();

System.out.println("Enter the height of Rectangle :");

double h1 = input.nextDouble();

  

System.out.println("** Rectangle#2 **");

System.out.println("Enter the Width of Rectangle :");

double w2 = input.nextDouble();

System.out.println("Enter the height of Rectangle :");

double h2 = input.nextDouble();

  

System.out.println("** Circle#1 **");

System.out.println("Enter the radius :");

double r1 = input.nextDouble();

  

System.out.println("** Circle#2 **");

System.out.println("Enter the radius :");

double r2 = input.nextDouble();

  

GeometricObject[] list = new GeometricObject[4];

list[0] = new Rectangle(w1, h1);

list[1] = new Rectangle(w2, h2);;

list[2] = new Circle(r1);

list[3] = new Circle(r2);

  

System.out.printf("Total area of all Geometric Obejcts : %.2f ",HW5_P2.totalArea(list));

}

}

__________________

** Rectangle#1 **
Enter the Width of Rectangle :
2
Enter the height of Rectangle :
4
** Rectangle#2 **
Enter the Width of Rectangle :
4
Enter the height of Rectangle :
6
** Circle#1 **
Enter the radius :
2
** Circle#2 **
Enter the radius :
4
Total area of all Geometric Obejcts : 94.83


_____________Could you rate me well.Plz .Thank You