(5/5)

Java :

The GeometricObject, Circle and Rectangle classes are given for this question. You may need to make necessary changes to the classes other than what is mentioned below. As a summary, the files in your submission for this question should at least include: GeometricObject.java,

Circle.java, Rectangle.java, Triangle.java, TestTriangleWithException.java, and TestLarger.java. Please bundle the above files and your class files in a folder named

Assignment3Question3.

PART I: 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 GeometricObject class. Override and implement getPerimeter() and getArea() in the subclasses Circle and Rectangle. Override and implement toString() method in Circle class as: return "Circle: radius = " + radius; and in Rectangle class as: return "Rectangle: width = " + width + ", height = " + height;

Use @Override annotation for all overridden methods.

PART II: Design and implement a class named Triangle that extends GeometricObject. The class contains:

- Three double data fields named side1, side2, and side3 with default values 1.0 to denote three sides of a triangle.

- A no-arg constructor that creates a default triangle.

- A constructor that creates a triangle with the specified side1, side2, and side3.

- Override and implement the abstract methods getPerimeter() and getArea()

in GeometricObject class.

(Hint: The area of a triangle is given by sqrt ( p( p- side1)(p-side2)(p-side3))

where p is half the perimeter, that is, p = (side1+side2+side3 ) /2

- Override and implement the toString() method as:

return "Triangle: side1 = " + side1 + ", side2 = " + side2 + "side3 = " + side3;

Use @Override annotation for all overridden methods.

In a triangle, the sum of any two sides is greater than the other side. The Triangle class must adhere to this rule. Create the IllegalTriangleException class and modify the constructor of the Triangle class to throw an IllegalTriangleException object if a triangle is created with sides that violate the rule. The constructor of IllegalTriangleException must encapsulate all three sides of the triangle and a string message, as follows:

public IllegalTriangleException(double side1, double side2, double side3, String message)

Write a test program TestTriangleWithException to test your Triangle class and IllegalTriangleException by creating two objects of Triangle with one of them violating the rule. Print the perimeter and area of the legal triangle. Print the sides and string message of the illegal triangle from the IllegalTriangleException caught. You may need additional methods in IllegalTriangleException other than what is mentioned above. Format your output to two decimal places. A sample run is as follows:

Legal triangle :

Perimeter : 6.50

Area : 10.25

Illegal triangle :

Side1 = 1.00

Side2 = 2.00

Side3 = 3.00

The sum of any two sides is greater than other side.

PART III:

Implement a static method called larger that takes two geometric objects of the same type as arguments and returns the object with larger area. If the two objects have the same area, the method returns null. If the two geometric objects are not of the same

type, the method throws DifferentTypeException. DifferentTypeException is a user-defined exception. Define it for your larger method. The method signature or larger is as follows:

static GeometricObject larger(GeometricObject g1, GeometricObject g2)

Write a test program called TestLarger. Implement larger method in TestLarger and test if your larger method works properly. Create different types of geometric objects to invoke the larger method. You should test your larger method with two circles, two rectangles, two triangles, and two different object types which should throw

DifferentTypeException. Print out appropriate details about the object returned by larger method such as radius, width, height, sides, perimeter, and area. Format your output to two decimal places. A sample output is as follows:

Testing two circles of same radius : Two objects have equal area Testing two circles of different radius:

The returned larger object is : Circle : radius = 3.0

The area is 28.27

The perimeter is 18.85

Testing two rectangles of different sizes :

The returned larger object is : rectangle : width = 3.0, height = 3.0

The area is 9.00

The perimeter is 12.00

Testing two triangles of different sizes :

The returned larger object is : Triangle, side1= 2.0, side2=3.0, side3=2.3

The area is 17.59

The perimeter is 7.30

Testing two different object types:

Two objects are of different type.

Please note that you are not allowed to overload larger method to accommodate Circle, Rectangle and Triangle. You must test whether the two geometric objects are of the same type in your larger method and throw DifferentTypeException in the case of different types.

(5/5)

DescriptionIn this final assignment, the students will demonstrate their ability to apply two ma

Path finding involves finding a path from A to B. Typically we want the path to have certain properties,such as being the shortest or to avoid going t

Develop a program to emulate a purchase transaction at a retail store. Thisprogram will have two classes, a LineItem class and a Transaction class. Th

1 Project 1 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of

1 Project 2 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of

Get Free Quote!

421 Experts Online