Assignment A2 (35 marks) Focus: Abstract Classes and Interfaces, Standard Java I
ID: 3880063 • Letter: A
Question
Assignment A2 (35 marks) Focus: Abstract Classes and Interfaces, Standard Java Interfaces: Comparable, Cloneable, For this assignment, you need to download from Connect the Shape class shown below and include in your project. public abstract class Shape //attributes private String color; private boolean filled; //constructors protected Shape ()(this ( "White", true);} protected Shape(String color, boolean filled){ setColor (color); setFilled (filled) //methods public String getColorO(return color;) public void setColor(string color){this.color = color:) public boolean isFilled ()(return filled;) public void set Filled (boolean f)(filled f;} public String toString() 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 int compareTo (Shape shp) 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 mainmethod in your test program. You will learn more about exception handling in next lecture and labExplanation / Answer
Hello, I have a solution for you. Implemented everything as per the requirements. Defined following things in this answer.
//Shape.java
public abstract class Shape {
private String color;
private boolean filled;
//constructors
protected Shape(){
this("White",true);
}
protected Shape(String color,boolean filled){
setColor(color);
setFilled(filled);
}
//methods
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public boolean isFilled() {
return filled;
}
public void setFilled(boolean filled) {
this.filled = filled;
}
public String toString() {
return "Color: "+color+". "+(filled ? "Filled. ":"Not filled. ");
}
public abstract double getArea();
public abstract double getPerimeter();
}
//Hexagon.java
public class Hexagon extends Shape implements Cloneable, Comparable<Shape> {
// variable to represent the side length
private double sideLength;
// constructors
public Hexagon() {
super();
setSideLength(0);
}
public Hexagon(String color, boolean filled, double sideLength) {
super(color, filled);
setSideLength(sideLength);
}
public double getSideLength() {
return sideLength;
}
public void setSideLength(double sideLength) {
this.sideLength = sideLength;
}
@Override
public String toString() {
return super.toString() + " Side: " + sideLength + ". Area: "
+ getArea() + ". Perimeter: " + getPerimeter();
}
/**
* method to compare two shapes by their areas
*/
public int compareTo(Shape shp) {
if (this.getArea() > shp.getArea()) {
return 1;
} else if (this.getArea() < shp.getArea()) {
return -1;
} else {
return 0;
}
}
/**
* method to return the area
*/
@Override
public double getArea() {
/**
* Area of a hexagon: 3*squareroot(3)*side*side /2
*/
double area = 3.0 * Math.sqrt(3) * sideLength * sideLength / 2;
return area;
}
/**
* method to return the perimeter
*/
@Override
public double getPerimeter() {
/**
* perimeter of hexagon= 6*side
*/
return 6.0 * sideLength;
}
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
//Rectangle.java
public class Rectangle extends Shape implements Cloneable, Comparable<Shape> {
private double width;
private double height;
//constructors
public Rectangle() {
super();
setWidth(0);
setHeight(0);
}
public Rectangle(String color, boolean filled,double width,double height) {
super(color, filled);
setWidth(width);
setHeight(height);
}
//getters and setters
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
/**
* method to compare two shapes by their areas
*/
public int compareTo(Shape shp) {
if (this.getArea() > shp.getArea()) {
return 1;
} else if (this.getArea() < shp.getArea()) {
return -1;
} else {
return 0;
}
}
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
/**
* method to return the area
*/
@Override
public double getArea() {
return width*height;
}
/**
* method to return the perimeter
*/
@Override
public double getPerimeter() {
return 2.0*(height+width);
}
@Override
public String toString() {
return super.toString()+" Width: "+width + ". Height: "+height +". Area: " + getArea() + ". Perimeter: "
+ getPerimeter();
}
}
//Test.java
import java.util.Arrays;
import java.util.Scanner;
public class Test {
public static void main(String[] args) throws CloneNotSupportedException {
Scanner scanner = new Scanner(System.in);
/**
* Answer for question 1. Prompting the user to enter details about a
* Hexagon, creating an object from the input details, displaying the
* hexagon details, cloning it, and displaying the cloned hexagon and
* verifying the compareTo() method
*/
System.out.println("Hexagon:");
System.out.println("Enter Color: ");
String color = scanner.nextLine();
System.out.println("Filled?(YES/NO): ");
String choice = scanner.nextLine();
boolean filled = false;
if (choice.equalsIgnoreCase("YES")) {
filled = true;
} else {
filled = false;
}
System.out.println("Side length: ");
double side = Double.parseDouble(scanner.nextLine());
Hexagon hexagon = new Hexagon(color, filled, side);
System.out.println("First hexagon: " + hexagon);
Hexagon cloned = (Hexagon) hexagon.clone();
System.out.println("Cloned hexagon: " + cloned);
if (hexagon.compareTo(cloned) == 0) {
System.out.println("Both hexagons are identical");
} else {
System.out.println("Both hexagons are not identical");
}
/**
* Answer for question 2. Prompting the user to enter details about a
* Rectangle, creating a Rectangle object from the input details,
* displaying the details, cloning it, and displaying the cloned
* rectangle and verifying the compareTo() method
*/
System.out.println(" Rectangle");
System.out.println("Enter Color: ");
color = scanner.nextLine();
System.out.println("Filled?(YES/NO): ");
choice = scanner.nextLine();
filled = false;
if (choice.equalsIgnoreCase("YES")) {
filled = true;
} else {
filled = false;
}
System.out.println("Width: ");
double width = Double.parseDouble(scanner.nextLine());
System.out.println("Height: ");
double height = Double.parseDouble(scanner.nextLine());
Rectangle rectangle = new Rectangle(color, filled, width, height);
System.out.println("First rectangle: " + rectangle);
Rectangle clonedRectangle = (Rectangle) rectangle.clone();
System.out.println("Cloned rectangle: " + rectangle);
if (rectangle.compareTo(clonedRectangle) == 0) {
System.out.println("Both rectangles are identical");
} else {
System.out.println("Both rectangles are not identical");
}
/**
* Answer for question 3. Creating an array of Shape objects, creating 3
* rectangles and 2 hexagons and adding to the array, prints the total
* area, clones the array to create another array, sorting the second
* array, and displaying areas of shapes in both arrays
*/
System.out.println(" Shapes array");
Shape[] shapes1=new Shape[5];
/**
* Creating shapes with some random values
*/
shapes1[0]=new Rectangle("green", true, 10, 5);
shapes1[1]=new Rectangle("red", false, 20, 3);
shapes1[2]=new Rectangle("violet", true, 2, 2);
shapes1[3]=new Hexagon("blue", true, 20);
shapes1[4]=new Hexagon("white", true, 14.5);
double totalArea=0;
/**
* Calculating the total area
*/
for(int i=0;i<shapes1.length;i++){
totalArea+=shapes1[i].getArea();
}
System.out.println("Total area of all shapes in shapes1: "+totalArea);
/**
* Cloning the array
*/
Shape[] shapes2=shapes1.clone();
/**
* Sorting the array
*/
Arrays.sort(shapes2);
/**
* Displaying the areas of shapes in two arrays
*/
System.out.printf("%18s %18s ","Areas in shapes1","Areas in shapes2");
for(int i=0;i<shapes1.length;i++){
System.out.printf("%18.2f %18.2f ",shapes1[i].getArea(),shapes2[i].getArea());
}
}
}
/*OUTPUT*/
Hexagon:
Enter Color:
RED
Filled?(YES/NO):
YES
Side length:
5
First hexagon:
Color: RED. Filled.
Side: 5.0. Area: 64.9519052838329. Perimeter: 30.0
Cloned hexagon:
Color: RED. Filled.
Side: 5.0. Area: 64.9519052838329. Perimeter: 30.0
Both hexagons are identical
Rectangle
Enter Color:
BLUE
Filled?(YES/NO):
NO
Width:
4
Height:
5
First rectangle:
Color: BLUE. Not filled.
Width: 4.0. Height: 5.0. Area: 20.0. Perimeter: 18.0
Cloned rectangle:
Color: BLUE. Not filled.
Width: 4.0. Height: 5.0. Area: 20.0. Perimeter: 18.0
Both rectangles are identical
Shapes array
Total area of all shapes in shapes1: 1699.476007978361
Areas in shapes1 Areas in shapes2
50.00 4.00
60.00 50.00
4.00 60.00
1039.23 546.25
546.25 1039.23
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.