Please Note: I created a post earlier that had this question along with several others, but was told that since I was asking so many questions in the same post, it'd be better to break it up into individual questions. So please do not mark this as a duplicate, yes the instructions are the same and yes the same code is being used, but the question itself is different. Thanks.
I'm working on a program with the following instructions:
Write a class named Octagon that extends GeometricObject and implements the Comparable and Cloneable interfaces. Assume that all 8 sides of the octagon are of equal size. The area can be computed using the following formula
area = (2 + 4/square root of 2) * side * side
Write a program (driver) to read in a series of values from a file, display the area and perimeter, create a clone and compare the object and its clone (based on the area). In addition, your program should compare the current object (just read in) with the first object read in. The program ends when a negative number is read from the file.
Here is the code I have so far, This is my GeometricObject
class:
public abstract class GeometricObject {
public abstract double getArea();
public abstract double getPerimeter();
}
My Octagon
class:
public class Octagon extends GeometricObject implements Comparable<Octagon> {
private double side = 1.0;
protected native Object clone() throws CloneNotSupportedException;
private static int numberOfObjects = 0;
public Octagon() {
}
public Octagon(double side) {
this.side = side;
numberOfObjects++;
}
public double getSide() {
return side;
}
public void setSide(double side) {
this.side = side;
}
public double getArea() {
return (2 + (4 / (Math.sqrt(2))) * side * side);
}
public double getPerimeter() {
return side * 8;
}
public String toString() {
return "Octagon " + numCreated + ": Area: " + getArea() + "\nPerimeter: "
+ getPerimeter() + "\nClone Compare: " + Cloneable + "\nFirst Compare: "
+ comparisson;
}
public int compareTo(Octagon octagon) {
if(getArea() > octagon.getArea())
return 1;
else if(getArea() < octagon.getArea())
return -1;
else
return 0;
}
public interface Cloneable {
}
}
And my Driver
or tester class: (this is where I need the most help)
import java.util.*;
import java.io.*;
public class Driver {
public static void main(String[] args) {
int comparisson = compareTo(octagon);
java.io.File file = new java.io.File("prog7.dat");
Scanner fin = new Scanner(file);
while(fin.hasNext()) {
double side = fin.nextDouble();
if(side < 0.0) break;
Octagon first = new Octagon(side);
System.out.println("Octagon 1: " + first);
}
}
}
And here is the file being used to get the input. Each line is one octagon:
5.0
7.5
3.26
0.0
-1.0
I'm having trouble setting up the compareTo()
method. I've never used this before and while I've read the documentation and have a general idea of how it works, I'm having trouble figuring out how to implement it to my specific program.
Along these same lines, I know that the compareTo()
method returns an integer value of either -1, 0, or 1 based on whether the current object is smaller, larger, or equal to the object it's being compared with. However, as you can see from the example output, rather than displaying this integer value, I"m supposed to display something like "The first is smaller". I'm pretty sure to do this I would need to assign the return value to a variable and then use if statements to determine what should be printed, but I'm having trouble figuring out how and at what point in my code this should be done?
Based on this line of your requirement: In addition, your program should compare the current object (just read in) with the first object read in.
Here's some code to do that. Basically this reads Octagon
s until a negative number is read, and compares each one with the first Octagon
read in.
Octagon first = null;
int i = 0;
while(fin.hasNext())
{
double side = fin.nextDouble();
if(side < 0.0)
break;
Octagon oct = new Octagon(side);
System.out.print("Octagon " + i + ": \"" + oct.toString() + "\"");
if (first == null)
first = oct;
else
{
// Here is where we compare the current Octagon to the first one
int comparison = oct.compareTo(first);
if (comparison < 0)
System.out.print(", less than first");
else if (comparison > 0)
System.out.print(", greater than first");
else System.out.print(", equal to first");
}
Octagon first = new Octagon(side);
System.out.println();
}