Question: Java Programming use net beans. The GeometricObject, Circle and Rectangle classes are given for this exercise. You may need to make necessary changes to the

Java Programming use net beans.

The GeometricObject, Circle and Rectangle classes are given for this exercise. You may need to make necessary changes to the classes other than what is mentioned below. This exercise is about polymorphism.

Declare the getPerimeter() and getArea() methods in the GeometricObject class. These two methods should be declared as abstract because they cannot be implemented in the Geometric Object class. However, they are implemented in the subclasses. Change the toString() method of Triangle in Part (i) to return a string of Triangle Object. Implement toString() method in Circle and Rectangle class to return a string of Circle Object and Rectangle Object respectively. Implement a static method called larger in your test program. This method takes two geometric objects of the same type as arguments and returns the object with larger area. If two objects have the same area, the method returns null. If two geometric objects are not of the same type are used as arguments, the function throws DifferentTypeException. DifferentTypeException is a user defined exception. Define it for your larger method. The function signature of larger is as follow:

static GeometricObject larger(GeometricObject g1, GeometricObject g2) throws DifferentTypeException

Write a test program called TestLarger to test if your larger method works properly. Create different types of geometric objects to call the larger method. You should test your method with 2 Circles, 2 Rectangles, 2 Triangles, and 2 different object types should throw DifferentTypeException. Print appropriate details about the object returned by larger method such as description, radius, width, height, sides, perimeter and area. Format your output to two decimal places.

A sample output is as follow:

Testing 2 circles of same radius:

Two objects have equal area

Testing 2 circles of different radius:

The returned larger object is Circle Object

The area is 28.27

The radius is 3.00

The perimeter is 18.85

Testing 2 rectangles of different sizes:

The returned larger object is Rectangle Object

The area is 9.00

The width is 3.00

The height is 3.00

The perimeter is 12.00

Testing 2 triangles of different sizes:

The returned larger object is Triangle object

The area is 2.30

The side1 is 2.00

The side2 is 3.00

The side3 is 2.30

The perimeter is 7.30

Testing 2 different object types:

Two objects are of different type

Circle.java#

public class Circle extends GeometricObject {

private double radius;

public Circle() {

}

public Circle(double radius) {

this.radius = radius;

}

public Circle(double radius,

String color, boolean filled) {

this.radius = radius;

setColor(color);

setFilled(filled);

}

/** Return radius */

public double getRadius() {

return radius;

}

/** Set a new radius */

public void setRadius(double radius) {

this.radius = radius;

}

/** Return area */

public double getArea() {

return radius * radius * Math.PI;

}

/** Return diameter */

public double getDiameter() {

return 2 * radius;

}

/** Return perimeter */

public double getPerimeter() {

return 2 * radius * Math.PI;

}

/* Print the circle info */

public void printCircle() {

System.out.println("The circle is created " + getDateCreated() +

" and the radius is " + radius);

}

}

GeometricObject.java#

public class GeometricObject {

private String color = "white";

private boolean filled;

private java.util.Date dateCreated;

/** 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) {

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,

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 a string representation of this object */

public String toString() {

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

" and filled: " + filled;

}

}

Rectangle.java#

public class Rectangle extends SimpleGeometricObject {

private double width;

private double height;

public Rectangle() {

}

public Rectangle(

double width, double height) {

this.width = width;

this.height = height;

}

public Rectangle(

double width, double height, String color, boolean filled) {

this.width = width;

this.height = height;

setColor(color);

setFilled(filled);

}

/** 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;

}

/** Return area */

public double getArea() {

return width * height;

}

/** Return perimeter */

public double getPerimeter() {

return 2 * (width + height);

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!