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>, Cloneable {
private double side;
public Octagon() {
}
public Octagon(double side) {
this.side = side;
}
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 "Area: " + getArea() + "\nPerimeter: "
+ getPerimeter() + "\nClone Compare: " + "\nFirst Compare: ";
}
public int compareTo(Octagon octagon) {
if(getArea() > octagon.getArea())
return 1;
else if(getArea() < octagon.getArea())
return -1;
else
return 0;
}
@Override
public Octagon clone() {
return new Octagon(this.side);
}
}
And my Driver or tester class: (This is where I need the most help):
import java.util.*;
public class Driver {
public static void main(String[] args) throws Exception {
java.io.File file = new java.io.File("prog7.dat");
Scanner fin = new Scanner(file);
Octagon first = null;
int i = 1;
Octagon older;
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;
System.out.print("Equal");
}
else {
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");
}
String cloneComparison = "Clone Compare: ";
older = oct;
Octagon clone = oct.clone();
if ( older.getArea() == clone.getArea() ){
cloneComparison = cloneComparison + "Equal";
} else {
cloneComparison = cloneComparison + "Not Equal";
}
//System.out.println(cloneComparison);
i++;
first = new Octagon(side);
System.out.println();
}
fin.close();
}
}
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
program is doing the comparison between the original object and its clone correctly, however, I'm having trouble getting it to print in the way I'd like it to.
As the above code is written, here is my output:
Octagon 1: "Area: 72.71067811865474
Perimeter: 40.0
Clone Compare:
First Compare: "EqualClone Comparision: Equal
Octagon 2: "Area: 161.09902576697317
Perimeter: 60.0
Clone Compare:
First Compare: "greater than firstClone Comparision: Equal
Octagon 3: "Area: 32.0593921109526
Perimeter: 26.08
Clone Compare:
First Compare: "less than firstClone Comparision: Equal
Octagon 4: "Area: 2.0
Perimeter: 0.0
Clone Compare:
First Compare: "less than firstClone Comparision: Equal
But I need it to look something like this:
Octagon 1: Area: 2.0
Perimeter: 0.0
Clone Compare: Equal
First Compare: less than first
I feel like this is probably something wrong with my toString() method in my Octagon class since that's where I'm printing the getArea() and getPerimeter() from, but I can not get it to compile with cloneComparison in that return statement.
Some of the things I've tried with my toString() method's return statement include:
"\nClone Compare: " + cloneComparison + ...
"\nClone Compare: " + oct.cloneComparison + ...
"\nClone Compare: " + Driver.cloneComparison + ...
And
"\nClone Compare: " + Octagon.cloneComparison + ...
Any input is greatly appreciated.
Your toString()
method is returning too much. Some of the output you want will be output by your main method, so it doesn't belong in toString()
. Which part of the code is responsible for what? If your main program is printing comparison information, let it print the comparison information (along with the labels like Clone compare:)
, and don't put it in toString()
.
In general, it is helpful to define what the responsibilities of each class are. One way to think about it is: "If there were some other class, other than Driver
, using my Octagon
class, what would that class expect if it were to call toString
on an Octagon
object?" It certainly wouldn't expect the result to include strings like "Clone compare"
, since that has nothing to do with an Octagon
; it has more to do with what the Driver
wants to do. Separating responsibilities into their logical places like this is one of the most important things to learn about object-oriented programming--and, I'd say, is one of the main reasons object-oriented programming was invented.
Also, the output you say you want doesn't have quote marks in it, so you shouldn't include them in the string you're printing (where you have backslash followed by "
).