title: Setting up the compareTo() method in Java
body: <p><strong>Please Note:</strong>
  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.</p>
  <p>I'm working on a program with the following instructions:</p>
  <blockquote>
  <p>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</p>
  <pre><code>area = (2 + 4/square root of 2) * side * side 
  </code></pre>
  <p>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.</p>
  </blockquote>
  <p>Here is the code I have so far, This is my <code>GeometricObject</code> class:</p>
  <pre><code>public abstract class GeometricObject {
      
      public abstract double getArea();
      public abstract double getPerimeter(); 
  }
  </code></pre>
  <p>My <code>Octagon</code> class:</p>
  <pre><code>public class Octagon extends GeometricObject implements Comparable&lt;Octagon&gt; {
      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 &quot;Octagon &quot; + numCreated + &quot;: Area: &quot; + getArea() + &quot;\nPerimeter: &quot; 
              + getPerimeter() + &quot;\nClone Compare: &quot; + Cloneable + &quot;\nFirst Compare: &quot;
              + comparisson;
      }
      
      public int compareTo(Octagon octagon) {
          if(getArea() &gt; octagon.getArea()) 
              return 1;
          else if(getArea() &lt; octagon.getArea()) 
                  return -1;
          else
              return 0;
      }
  
      public interface Cloneable {
      } 
  }
  </code></pre>
  <p>And my <code>Driver</code> or tester class: (this is where I need the most help)</p>
  <pre><code>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(&quot;prog7.dat&quot;);
          Scanner fin = new Scanner(file);
          
          while(fin.hasNext()) {
              double side = fin.nextDouble();
                  if(side &lt; 0.0) break;
              Octagon first = new Octagon(side);
              System.out.println(&quot;Octagon 1: &quot; + first);
          
          }
          
      }
  }
  </code></pre>
  <p>And here is the file being used to get the input. Each line is one octagon:</p>
  <pre><code>5.0
  7.5
  3.26
  0.0
  -1.0
  </code></pre>
  <p>I'm having trouble setting up the <code>compareTo()</code> 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.</p>
  <p>Along these same lines, I know that the <code>compareTo()</code> 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&quot;m supposed to display something like &quot;The first is smaller&quot;. 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?</p>