autokustomizer
10-13-2009, 10:38 PM
I am writing these programs, and I am stuck. The instructions are:
-Write a class named Octagon that extends GeometricObject and implements the Comparable and Cloneable interfaces
-Assume all eight sides are of equal length
-Area is computed by : area = (2+4/sqrt(2))side*side
-The test program creates an Octagon with side value of 5 and displays its area and perimeter.
-Create a new object using the clone method and compare the two objects using the compareTo method.
Here is my GeometricObject program:
public abstract class GeometricObject
{
//data fields
private String color = "white";
private boolean filled;
private java.util.Date dateCreated;
//default constructor
protected GeometricObject()
{
dateCreated = new java.util.Date();
}
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 + " and filled: " + filled;
}
public abstract double getArea();
public abstract double getPerimeter();
}
That compiles fine, and here is my Octagon program...
public abstract class Octagon extends GeometricObject implements Comparable, Cloneable
{
private double side = 1.0;
protected native Object clone() throws CloneNotSupportedException;
public Octagon()
{
}
public Octagon(double side)
{
super();
this.side = side;
}
public void setSide(double side)
{
this.side = side;
}
public double getSide(double side)
{
return side;
}
public double getArea()
{
return (2 + (4 / (Math.sqrt(2))) * side * side);
}
public double getPerimeter()
{
return side * 8;
}
public String toString()
{
return "The length of each side is: " + side;
}
public int compareTo(Octagon octagon1)
{
if (getArea() >= ((Octagon)octagon1).getArea())
return 1;
else if (getArea() < ((Octagon)octagon1).getArea())
return -1;
else
return 0;
}
public interface Cloneable
{
}
}
And here is what I have for my tester program, but I have no idea how to create a tester for abstract methods when I can't use the 'new' operator... Can anyone help me?
import java.util.*;
public class OctagonTester
{
public static void main(String[] args) throws CloneNotSupportedException
{
Octagon octagon1 = (Octagon)octagon1.clone();
}
}
And that throws an error...
----jGRASP exec: javac -g C:\Users\Christopher\Documents\GeometricObject.java
----jGRASP: operation complete.
----jGRASP exec: javac -g C:\Users\Christopher\Documents\Octagon.java
----jGRASP: operation complete.
----jGRASP exec: javac -g C:\Users\Christopher\Documents\OctagonTester.java
OctagonTester.java:13: variable octagon1 might not have been initialized
Octagon octagon1 = (Octagon)octagon1.clone();
^
1 error
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
Please help...TY in advance...
-Write a class named Octagon that extends GeometricObject and implements the Comparable and Cloneable interfaces
-Assume all eight sides are of equal length
-Area is computed by : area = (2+4/sqrt(2))side*side
-The test program creates an Octagon with side value of 5 and displays its area and perimeter.
-Create a new object using the clone method and compare the two objects using the compareTo method.
Here is my GeometricObject program:
public abstract class GeometricObject
{
//data fields
private String color = "white";
private boolean filled;
private java.util.Date dateCreated;
//default constructor
protected GeometricObject()
{
dateCreated = new java.util.Date();
}
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 + " and filled: " + filled;
}
public abstract double getArea();
public abstract double getPerimeter();
}
That compiles fine, and here is my Octagon program...
public abstract class Octagon extends GeometricObject implements Comparable, Cloneable
{
private double side = 1.0;
protected native Object clone() throws CloneNotSupportedException;
public Octagon()
{
}
public Octagon(double side)
{
super();
this.side = side;
}
public void setSide(double side)
{
this.side = side;
}
public double getSide(double side)
{
return side;
}
public double getArea()
{
return (2 + (4 / (Math.sqrt(2))) * side * side);
}
public double getPerimeter()
{
return side * 8;
}
public String toString()
{
return "The length of each side is: " + side;
}
public int compareTo(Octagon octagon1)
{
if (getArea() >= ((Octagon)octagon1).getArea())
return 1;
else if (getArea() < ((Octagon)octagon1).getArea())
return -1;
else
return 0;
}
public interface Cloneable
{
}
}
And here is what I have for my tester program, but I have no idea how to create a tester for abstract methods when I can't use the 'new' operator... Can anyone help me?
import java.util.*;
public class OctagonTester
{
public static void main(String[] args) throws CloneNotSupportedException
{
Octagon octagon1 = (Octagon)octagon1.clone();
}
}
And that throws an error...
----jGRASP exec: javac -g C:\Users\Christopher\Documents\GeometricObject.java
----jGRASP: operation complete.
----jGRASP exec: javac -g C:\Users\Christopher\Documents\Octagon.java
----jGRASP: operation complete.
----jGRASP exec: javac -g C:\Users\Christopher\Documents\OctagonTester.java
OctagonTester.java:13: variable octagon1 might not have been initialized
Octagon octagon1 = (Octagon)octagon1.clone();
^
1 error
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
Please help...TY in advance...