Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Assignment A2 (35 marks) Focus: Abstract Classes and Interfaces, Standard Java I

ID: 3879540 • 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 lab

Explanation / Answer

Shape.java

public abstract class Shape {

private String color;

private boolean filled;

  

protected Shape() {

this("White",true);

}

protected Shape(String color,boolean filled) {

setColor(color);

setFilled(filled);

}

  

public String getColor() {

return color;

}

public void setColor(String color) {

this.color = color;

}

public boolean isFilled() {

return filled;

}

public void setFilled(boolean f) {

filled = f;

}

public String toString() {

return "Color: " + color + ". " + (filled? "Filled. ":"Not Filled. ");

}

  

public abstract double getArea();

public abstract double getPerimeter();

}

Q1

Hexagon.java

public class Hexagon extends Shape implements Cloneable,Comparable {

@Override

public int compareTo(Object o) {

return 0;

}

private String color;

private boolean filled;

private int side_length;

  

protected Hexagon() {

this("Red",false,5);

}

protected Hexagon(String color,boolean filled,int side_length) {

setColor(color);

setFilled(filled);

setLength(side_length);

}

  

public String getColor() {

return color;

}

public void setColor(String color) {

this.color = color;

}

public boolean isFilled() {

return filled;

}

public void setFilled(boolean f) {

filled = f;

}

public void setLength(int side_length) {

this.side_length = side_length;  

}

public int getLength() {

return side_length;

}

public String toString() {

return "Color: " + color + ". " + (filled? "Filled. ":"Not Filled. " + ". " + "Side Length" + side_length);

}

@Override

public double getArea() {

return ((3*1.732)/2)*(side_length*side_length);

}

@Override

public double getPerimeter() {

return 6*side_length;

}

public int compareTo(Shape shp) {

if(this.getArea() == shp.getArea()) {

return 1;

}

return 0;

}

@Override

public Hexagon clone() {

try {

return (Hexagon) super.clone();

} catch (CloneNotSupportedException e) {

e.printStackTrace();

throw new RuntimeException();

}

}

}

HexagonTester.java

public class HexagonTester {

public static void main(String[] args) {

Hexagon h1 = new Hexagon();

System.out.println("First Hexagon");

System.out.println(h1);

System.out.println("Cloned Hexagon");

Hexagon h2 = h1.clone();

System.out.println(h2);

if(h1.compareTo(h2) == 0) {

System.out.println("Hexagons are not identical");

} else {

System.out.println("Hexagons are identical");

}

}

}

Q2

Rectangle.java

public class Rectangle extends Shape implements Cloneable,Comparable{

@Override

public int compareTo(Object o) {

return 0;

}

private String color;

private boolean filled;

private int width;

private int height;

  

protected Rectangle() {

this("Red",true,5,2);

}

protected Rectangle(String color,boolean filled,int width,int height) {

setColor(color);

setFilled(filled);

setWidth(width);

setHeight(height);

}

  

public String getColor() {

return color;

}

public void setColor(String color) {

this.color = color;

}

public boolean isFilled() {

return filled;

}

public void setFilled(boolean f) {

filled = f;

}

public void setWidth(int width) {

this.width = width;  

}

public int getWidth() {

return width;

}

public void setHeight(int height) {

this.height = height;  

}

public int getheight() {

return height;

}

public String toString() {

return "Color: " + color + ". " + (filled? "Filled. ":"Not Filled. " + ". " + "Width:" + width + ". " + "Height" + height + ".");

}

@Override

public double getArea() {

return width*height;

}

@Override

public double getPerimeter() {

return 2*(width+height);

}

public int compareTo(Shape shp) {

if(this.getArea() == shp.getArea()) {

return 1;

}

return 0;

}

@Override

public Rectangle clone() {

try {

return (Rectangle) super.clone();

} catch (CloneNotSupportedException e) {

e.printStackTrace();

throw new RuntimeException();

}

}

}

RectangleTester.java

public class RectangleTester {

public static void main(String[] args) {

Rectangle r1 = new Rectangle();

System.out.println("First Rectangle");

System.out.println(r1);

System.out.println("Cloned Rectangle");

Rectangle r2 = r1.clone();

System.out.println(r2);

if(r1.compareTo(r2) == 0) {

System.out.println("Rectangles are not identical");

} else {

System.out.println("Rectangles are identical");

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote