I had this original parent abstract class Geometric and i was extending it in Octagon, and also implementing Comparable and Cloneable. IDK why i keep getting the above error.Help with be appreciated.
class Octagon extends GeometricObject implements Cloneable, Comparable{
private double side;
public class Octagon(){
}
public class Octagon(double s){
side=s;
}
public double getArea(){
return (2+4/Math.sqrt(2))*side*side;
}
public double getPerimeter(){
return 8*side;
}
public int compareTo(Object o){
if (getArea()>((Octagon)o).getArea()){
return 1;
}
else if (getArea()<((Octagon)o).getArea()){
return -1;
}
else
return 0;
}
public Object clone() throws CloneNotSupportedException{
super.clone();
}
}
And this is my Geometric Class
public abstract class GeometricObject{
private String color="white";
private boolean filled;
private java.util.Date dateCreated;
protected GeometricObject(){
dateCreated=new java.util.Date();
}
protected GeometricObject(String color, boolean filled){
dateCreated=new java.util.Date();
this.color=color;
this.filled=filled;
}
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 java.util.Date getDateCreated(){
return dateCreated;
}
public String toString() {
return "created on "+dateCreated+"\ncolor: "+color;
}
public abstract double getArea();
public abstract double getPerimeter();
}
Got it – the error is within you “constructors”:
The
class“modifiers” are illegal. Simply remove them.Bonus advice – consider changing the implementation of
compareTo. You’re looking at the octagons area, which is perfectly OK for comparision. But it requires calculating the area each and every time when two octagons need to be compared.As the area only depends on the
sidevalue, it’s sufficient and much more efficient to compare octagons by theirsidelength: