public abstract class Shape // attributes private String color; private boolean
ID: 3861047 • Letter: P
Question
public abstract class Shape // attributes private String color; private boolean filled; : @param color * @param filled public Shape(String color, boolean filled) ( super(); this.color # color; this.filled = filled; public String getColorO ( public void setcolor (String color) f public boolean isfilled) ( public void setFilled(boolean filled) return color; this.color color: return filled; this.filled = filled; @override public String tostring) f return color color +" filled-" + filled public abstract double getArea(); public abstract double getPerimeter);Explanation / Answer
/**
*
* @author Sam
*/
public class Hexagon extends Shape implements Cloneable, Comparable<Shape>{
private double sideLength;
public Hexagon(double sideLength, String color, boolean filled) {
super(color, filled);
this.sideLength = sideLength;
}
public double getSideLength() {
return sideLength;
}
public void setSideLength(double sideLength) {
this.sideLength = sideLength;
}
@Override
public double getArea() {
return 1.5 * Math.sqrt(3) * sideLength * sideLength;
}
@Override
public double getPerimeter() {
return 6 * sideLength;
}
@Override
public int compareTo(Shape shp) {
double diff = getArea() - shp.getArea();
if (diff > 0)
return (int) Math.ceil(diff);
else
return (int) Math.floor(diff);
}
@Override
public String toString() {
String string = super.toString() + " ";
string = string + "side= " + sideLength + "area= " + getArea() + "perimeter= " + getPerimeter();
return string;
}
}
This is the class that you need. The main method is given below:
public static void main(String[] args) throws IOException, CloneNotSupportedException {
Hexagon h1;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Color: ");
String color = br.readLine();
System.out.print("Filled");
String tmp = br.readLine();
boolean filled = tmp.charAt(0) == 'y' || tmp.charAt(0) == 'Y';
System.out.print("Side: ");
double side = Double.parseDouble(br.readLine());
h1 = new Hexagon(side, color, filled);
System.out.println("First hexagon");
System.out.println(h1);
Hexagon h2 = (Hexagon) h1.clone();
System.out.println("Cloned hexagon");
System.out.println(h2);
if (h1.compareTo(h2) == 0)
System.out.println("Both hexagon are identical");
else
System.out.println("Two hexagons are different");
System.out.println("Modify the cloned hexagon");
h2.setSideLength(side + 10);
System.out.println(h2);
if (h1.compareTo(h2) == 0)
System.out.println("Both hexagon are identical");
else
System.out.println("Two hexagons are different");
}
Please post 1 question at a time. Class rectange is very same. still am providing you the rectangle class. I belive you can code teh main
class Rectangle extends Shape implements Cloneable, Comparable<Shape> {
private double length;
private double width;
public Rectangle(double length, double width, String color, boolean filled) {
super(color, filled);
this.length = length;
this.width = width;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
@Override
public double getArea() {
return length*width;
}
@Override
public double getPerimeter() {
return 2 * (length + width);
}
@Override
public int compareTo(Shape shp) {
double diff = getArea() - shp.getArea();
if (diff > 0)
return (int) Math.ceil(diff);
else
return (int) Math.floor(diff);
}
@Override
public String toString() {
String string = super.toString() + " ";
string = string + "length= " + length + "width= " + width + "area= " + getArea() + "perimeter= " + getPerimeter();
return string;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.