Search code examples
javainterfacecloneable

Implementing the Cloneable Interface


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

I'm having a hard time figuring out how I would implement the Cloneable Interface so that when I print out the results, they will say Clone Comparison: Equal (or not equal).

Any input is greatly appreciated.


Solution

  • What I would do is first implement the method Octagon.clone() by creating an Octagon with the same side length

    Public Octagon clone(){
        return new Octagon(this.side);
    }
    

    In Driver have it clone your newly created Octagon and compare the two (older and newer) with something like this:

    String cloneComparison = "Clone Comparision: ";
    if ( older.getArea() == newer.getArea() ){
        cloneComparison = cloneComparison + "Equal";
    } else {
        cloneComparison = cloneComparison + "Not Equal";
    }
    System.println(cloneComparison);
    

    Which also returns the correct string depending on the result.