package tasks; class GeometricObject { private String color = \"white\"; private
ID: 3589516 • Letter: P
Question
package tasks;
class GeometricObject
{
private String color = "white";
private boolean filled;
private java.util.Date dateCreated;
private String name = "noname";
/** Construct a default geometric object */
public GeometricObject()
{
dateCreated = new java.util.Date();
}
/** Construct a geometric object with the specified color
* and filled value */
public GeometricObject(String color, boolean filled, String name)
{
dateCreated = new java.util.Date();
this.color = color;
this.filled = filled;
this.name = name;
}
/** 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,
its 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;
}
/** Return name */
public String getName()
{
return name;
}
/** Set a new name */
public void setName(String name)
{
this.name = name;
}
/** Return a string representation of this object */
public String toString()
{
return "created on " + dateCreated + " color: " + color + " and filled: " + filled;
}
}
class Circle //Create a Circle class
{
double radius;
public Circle()
{
radius=1.0;
}
}
class Rectangle //Creates a Rectangle class
{
double width;
double height;
public Rectangle()
{
width=1.0;
height=1.0;
}
}
public class Triangle extends GeometricObject //Creates Triangle class That Extends Geometric object
{
double side1=1.0;
double side2=1.0;
double side3=1.0;
double permiter;
double area,value;
public Triangle()
{
}
public Triangle(double side1,double side2,double side3)
{
this.side1=side1;
this.side2=side2;
this.side3=side3;
}
public double getPerimeter()
{
permiter=(side1+side2+side3);
return permiter;
}
public double getArea()
{
value= permiter*((permiter-side1)*(permiter-side2)*(permiter-side3));
area=Math.sqrt(value);
return area;
}
/** Return a string representation of this object */
public String toString()
{
return "created For Finding Perimeter " + permiter + " And Area: "+ area;
}
public static void main(String[] args)
{
Circle obj=new Circle();
Rectangle obj2=new Rectangle();
Triangle obj3=new Triangle(2.0,3.0,4.0);
System.out.println("The Permeter of a Triangle is:"+obj3.getPerimeter());
System.out.println("The Area of Triangle is:"+obj3.getArea());
System.out.println(obj3.toString());
}
}
Based on the code above Modify the GeometricObject class to implement the Comparable interface, and define a static max method in the GeometricObject class for finding the larger of two GeometricObject objects.
Write a test program that uses the max() method to find the larger of two circles and the larger of two rectangles. Use the appropriate methods to determine if two GeometricObjects are the same.
Add all your objects to an ArrayList, sort it, then print out the sorted ArrayList.
Explanation / Answer
PLease find my implementation.
###########
// GeometricObject Interface
// putting all common methods and constants
public interface GeometricObject {
double PI = 3.14;
double getArea();
double getPerimeter();
}
// Circle class that implements GeometricObject interface
class Circle implements GeometricObject{
private double radius;
public Circle(double radius) {
this.radius = radius;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
@Override
public double getArea() {
return PI*radius*radius;
}
@Override
public double getPerimeter() {
return 2*PI*radius;
}
@Override
public String toString() {
return "Circle("+radius+")";
}
}
//Rectangle class that implements GeometricObject interface
class Rectangle implements GeometricObject{
private double height;
private double width;
// constructor
public Rectangle(double height, double width) {
this.height = height;
this.width = width;
}
//getter and setters
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
@Override
public double getArea() {
return height*width;
}
@Override
public double getPerimeter() {
return 2*(height+width);
}
@Override
public String toString() {
return "Rectangle("+height+", "+width+")";
}
}
###############
// comparator Class
import java.util.Comparator;
public class GeometricObjectComparator implements Comparator<GeometricObject>, java.io.Serializable {
public int compare(GeometricObject o1, GeometricObject o2) {
double area1 = o1.getArea();
double area2 = o2.getArea();
if (area1 < area2)
return -1;
else if (area1 == area2)
return 0;
else
return 1;
}
}
//Test class
class TestGeometric{
// selection sort
public static <E> void selectionSort(E[] list, Comparator<E> comparator)
{
for(int i=0; i<list.length -1; i++)
{
int iSmallest = i;
for(int j=i+1; j<list.length; j++)
{
if(comparator.compare(list[iSmallest], list[j]) > 0 )
{
iSmallest = j;
}
}
E iSwap = list[iSmallest];
list[iSmallest] = list[i];
list[i] = iSwap;
}
}
// method to print
public static <E> void printArray(E[] list)
{
for(int i=0; i<list.length; i++)
{
System.out.print(list[i] + ", ");
}
}
public static void main(String[] args) {
GeometricObject[] list = {new Circle(5), new Rectangle(4, 5),
new Circle(5.5), new Rectangle(2.4, 5), new Circle(0.5),
new Rectangle(4, 65), new Circle(4.5), new Rectangle(4.4, 1),
new Circle(6.5), new Rectangle(4, 5)};
// printing before sorting
System.out.println("Before sorting ");
printArray(list);
// calling sort methos
GeometricObjectComparator comparator = new GeometricObjectComparator();
selectionSort(list, comparator);
System.out.println();
System.out.println("After Sorting ");
printArray(list);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.