Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

public class GeometricObject { private String color = white; private boolean filled; private java.util.Date dateCreated; /** Construct a default geometric object */ public GeometricObject() {

image text in transcribedimage text in transcribed

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; } }

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); } }

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); } }

The GeometricObject, Circle and Rectangle classes are given for this exercise. You may need to make necessarv changes to the classes other than what is mentioned below. This exercise is about polymorphism Declare the getPerimeter) and getArea0 methods in the GeometricObject class. These two methods should be declared as abstract because thev 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 DifferentTvpeException. DifferentTvpeException 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 vour 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 decimnal places

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Microsoft Visual Basic 2008 Comprehensive Concepts And Techniques

Authors: Gary B. Shelly, Corinne Hoisington

1st Edition

1423927168, 978-1423927167

More Books

Students also viewed these Databases questions

Question

=+ Are there additional forms of employee representation?

Answered: 1 week ago

Question

=+What is the nature of their impact?

Answered: 1 week ago