Part 1 Abstract Class You have been provided the abstract class GeometricThing.
ID: 3816956 • Letter: P
Question
Part 1 Abstract Class
You have been provided the abstract class GeometricThing. Create the concrete class Square, according to the UML, below. You may use TestGeometricThing.java to run the Square class. GeometricThing.java and TestGeometricThings.java have been provided.
/* Create a class Square that is a child of the abstract class GeometricThing.
Square
- side : double //default value of 1.0
+Square() //sets side to default of 1.0 +Square(side : double) //sets property side to parameter side.
+getSide() : double //returns the property side.
+toString() : String //returns a string that describes the side value. */
Part 2 Add to TestGeometricThing.java. Declare and ArrayList of GeometricThing. Add geoThing1, geoThing2, and geoThing3 to the ArrayList. Create a method public static double sumArea(ArrayList list) that will return the summed area of every object in list.
Part 3 Add three more objects to the ArrayList. These can be any of the children of GeometricThing. Create a method public static GeometricThing findBiggestThing(ArrayList list) that will return the largest GeometricThing in terms of area. Display the area of the returned object.
Square.java
TestGeometricThing.Java
GeometricThing.java
Circle.Java
public class Circle extends GeometricThing {
Rectangle.java
public class Rectangle extends GeometricThing {
Explanation / Answer
//Part 1
// Abstract and square class...
package com.geometry;
public class Square extends GeometricThing{
double side = 1.0; ////default value of 1.0
Square(double side){
this.side=side;//specific side
}
@Override
public double getArea() {
return side*side; //returns area of square
}
@Override
public double getPerimeter() {
return 4*side; //returns peremeter of square
}
public double getSide() {
return side; //returns length of side
}
public String toString(){
return "side of sqare is "+side; //display side
}
}
//code of GeometricThing class
package com.geometry;
public abstract class GeometricThing {
private String color = "white";
private boolean filled;
private java.util.Date dateCreated;
/** Construct a default geometric object */
protected GeometricThing() {
dateCreated = new java.util.Date();
}
/** Construct a geometric object with color and filled value */
protected GeometricThing(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();
/** Abstract method getPerimeter */
public abstract double getPerimeter();
}
//part2 and part3
package com.geometry;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class TestGeometricThing {
static double sumOfArea = 0;//sum of area in list
//creating list of GeometricThing objects
static List<GeometricThing> list = new ArrayList<GeometricThing>();
/** Main method */
public static void main(String[] args) {
// Declare and initialize two geometric objects
GeometricThing geoThing1 = new Circle(5);
GeometricThing geoThing2 = new Rectangle(5, 3);
GeometricThing geoThing3 = new Square(5);
System.out.println("The two objects have the same area? " +
equalArea(geoThing1, geoThing2));
// Display circle
System.out.println("Circle");
displayGeometricThing(geoThing1);
// Display rectangle
System.out.println("Rectangle");
displayGeometricThing(geoThing2);
// Display square
System.out.println("Square");
displayGeometricThing(geoThing3);
System.out.println("Do circle and rectangle have same area? " +
equalArea(geoThing1, geoThing3));
list.add(geoThing1);
list.add(geoThing2);
list.add(geoThing3);
//Get and display sum of all objects in list
double sum = sumArea(list);
System.out.println("Sum of area: "+sum);
//Get and display biggest area of thing
GeometricThing obj = findBiggestThing(list);
System.out.println("Biggest area of thing is: "+obj.getArea());
}
/** A method for comparing the areas of two geometric objects */
public static boolean equalArea(GeometricThing object1,GeometricThing object2) {
return object1.getArea() == object2.getArea();
}
/** A method for displaying a geometric object */
public static void displayGeometricThing(GeometricThing object) {
System.out.println("The area is " + object.getArea());
System.out.println("The perimeter is " + object.getPerimeter());
System.out.println();
}
//This method returns sum of area of all ojects in list
public static double sumArea(List<GeometricThing> list){
for(int i=0;i<list.size();i++){
sumOfArea+=list.get(i).getArea();
}
return sumOfArea;
}
//Returns largest area object in the list
public static GeometricThing findBiggestThing(List<GeometricThing> list){
List list1=new ArrayList();
for(int i=0;i<list.size();i++){
list1.add(list.get(i).getArea());
}
GeometricThing obj = (GeometricThing)Collections.max(list1);
return obj;
}
}