Question: JAVA Problem #2 Shapes First, add Relatable interface to your project. Code for the interface is provided. /** Relatable interface */ public interface Relatable {

JAVA

Problem #2 Shapes

First, add Relatable interface to your project. Code for the interface is provided.

/** Relatable interface */ public interface Relatable { public boolean equals(Object s); public boolean isGreater(Object s); public boolean isLess(Object s); }

Basic Shape Class

Start with BasicShape.java provided with assignment files. Class already has the field written in. It also extends HasState class, please do not change this hierarchy. HasState is provided with the assignment files.

In addition, BasicShape must be defined as an abstract that implements Relatable interface.

Class must have the following fields and methods:

Protected field - already there:

color, an array of 3 integers that are representing Red, Green, and Blue components in a color.

Public methods:

Non-argument constructor that sets color to white all three values 255

Constructor that takes 3 parameters (in order red, green, blue), and sets the array values in this same order. Constructor throws IllegalArgumantException when any one of three given values are not in the range [0, 255]

int[] getColor()accessor - returns an array of 3 integers. A deep copy of the field must be returned.

void setColor (int[]) throws IllegalArgumantException - mutator method for color field. The array must be validated and exception must be thrown in case the values passed are not valid

the array must be exactly 3 elements long

all values in the array must be in the range 0 255 as they represent colors.

Deep copy of the array must be made

double getArea(), abstract method.

Implement all interface methods. Shapes must be compared by comparing the areas of shapes. Although getArea() is defined as abstract, it can be used without limitations to implement the calculations needed to compare shapes. IMPORTANT note:

two shapes cannot be equal when they belong to different types even if their areas are equal.

isLess() and isGreater() can be used for two shapes of different types

Circle Class

Start with Circle.java provided with assignment files. Class already has all fields and toString() method written in.

Circle should be derived from the BasicShape class. It should have the following:

Private fields - already there:

centerX, integer to hold x coordinate of the circles center.

centerY, integer to hold y coordinate of the circles center.

radius, integer to hold the radius of the circle.

Public methods:

Non-argument constructor that sets all variables to 1 and color to white. Call on parent constructor to set the color field.

Constructor accepts values for centerX, centerY, radius, and color as an array of 3 integers. Call on parent constructor to set the color field. Constructor throws IllegalArgumentException when

radius is not a positive number or

the array is not exactly 3 elements long or

not all values in the array in the range 0 255

Make sure to call parent constructor first. This will take care of colors array validation

Accessor and mutator methods for all non-inherited fields.

Mutator for radius field must throw IllegalArgumentException when an argument passed to method is not positive

getArea calculates and returns the area of the circle

toString() - already there

Override clone() method. Use clone() method examples we worked on in class. When adding clone() method make sure to add implements Cloneable statement to the class header. Otherwise the override will not compile.

Dont forget to create deep copies whenever working with field of type Color.

Rectangle Class

Start with Rectangle.java provided with assignment files. Class already has all fields and toString() method written in.

Rectangle should also be derived from the BasicShape class. It must have the following fields and methods:

Private fields - already there:

cornerX integer to hold x coordinate of the rectangles left upper corner.

cornerY integer to hold y coordinate of the rectangles left upper corner.

width, integer to hold the width of the rectangle.

length, integer to hold the length of the rectangle.

Public methods:

Non-argument constructor that sets all variables to 1 and color to white.

Constructor accepts values for length, width, and color and sets fields. Throws IllegalArgumentException when

width or length are not positive numbers

the array is not exactly 3 elements long or

not all values in the array must be in the range 0 255

Make sure to call parent constructor first. This will take care of colors array validation

Accessor and mutator methods for all non-inherited fields.

Mutators for length and width fields must throw IllegalArgumentException when an argument passed to method is not positive

getArea calculates and returns the area of the rectangle

Override of toString() -- already there

Override clone() method

Testing

BasicShape class cannot be instantiated. Because of that it cannot be directly tested. I will be carefully reading your code to make sure all the fields and methods are written according to the specs. All methods will be tested later in the derived classes. BasicShape class implementation constitutes 10 pts. of the total assignment grade.

BasicShape - All methods of BasicShape class - 10 pts. Basic shape methods cannot be explicitly tested.

Circle methods and their point values:

Test Group #

Methods Tested

Point Value

1

No-argument constructor; accessors, mutators

10

2

constructor; clone(); getArea(); methods of Relatable interface

10

Total

20

Rectangle methods and their point values:

Test Group #

Methods Tested

Point Value

1

No-argument constructor; accessors, mutators

10

2

constructor; clone(); getArea()

10

3

methods of Relatable interface

10

Total

30

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!