Question
Create a new test program with a main method that does the following: Creates an array named shapes1 that has five objects: three of the
Create a new test program with a main method that does the following:
Creates an array named shapes1 that has five objects: three of the type Rectangle and two of the type Hexagon. Assume any values for the object attributes.
Calculates and prints the total area of all objects in shapes1.
Creates a clone of shapes1. Name your clone shapes2.
Sorts the elements in shapes2. Hint: use a method from java.util.Arrays class.
Displays the areas of all shapes in both arrays. Hint: use printf() to have your output formatted similarly to the sample run below.
*Any help is appreciated, I am having some trouble forming the code for the last question
public abstract class Shape { //attributes private String color; private boolean filled; //constructors protected Shape ()fthis ("White", true);} protected Shape(String color, boolean filled) setColor (color); setFilled(filled) //methods public String getColor ()freturn color;} public void setColor (String color)(this.color - color;) public boolean isFi1led)freturn filled;) public void setFilled (boolean f)(filled -f;} public String toString()i return "Color: " + color + "." + (filled?"Filled. ": "Not filled. "); public abstract double getArea(); public abstract double getPerimeter(); Q1. [15 marks] Create a class named Hexagon that extends Shape and implements the Cloneable and Comparable interfaces. The comparison should be based on the area of two shapes. Use the following method header: public lnt comparelo (Shape snp Assume that all six sides of a hexagon are of equal length. Create appropriate setter and getter method(s) and constructor(s), and override the toString method to return a string representation of all attribute values as well as the area and perimeter of a hexagon object Write a test program that asks the use for the attributes of a Hexagon object (i.e., side length, color, and filled), creates that object, and then displays its state (side, color, area, etc.). Assume users enter valid inputs. Create a new object using the clone method and compare the two objects using the compareTo method. Hint: Add "throws CloneNotSupportedException" to the header of the clone method as well as the main method in your test program. You will learn more about exception handling in next lecture and lab. Q2. [10 Marks] Repeat Q1 for a Rectangle class (note: width is not always equal to height). Sample run Color:Blue Filled (Yes/No)? Yes Width and Height: 10 5 First Rectangle Color: Blue. Filled. Width: 10.0, Height: 5.0 Area: 50.0. Perimeter: 30.0 Clonned Rectangle Color: Blue. Filled. Width: 10.0, Height: 5.0 Area: 50.0. Perimeter: 30.0 Both rectangles are identical. Q3. [10 marks] Create a new test program with a main method that does the following: * Creates an array named shapesl that has five objects: three of the type Rectangle and two of the type Hexagon. Assume any values for the object attributes. . Calculates and prints the total area of all objects in shapes1. * Creates a clone of shapes1. Name your clone shapes2. * Sorts the elements in shapes2. Hint: use a method from java.util.Arrays class. . Displays the areas of all shapes in both arrays. Hint: use printf () to have your output formatted similarly to the sample run below Sample run otal area of all shapes in 'shapes1' is 322.82685902179844 shapes1 has been cloned to shapes2. shapes2 has been reordered! Areas in shapes1 35.00 24.00 30.00 210.44 23.38 Areas in shapes2 23.38 24.00 30.00 35.00 210.44
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started