title: Java fraction counter with classes
body: <p>I am very stuck on an assignment for some reason. I have a program that reads fractions from an input file, assigns each line to a string, and then saves the contents to the class Fraction (numerator and denominator). The fraction object is then saved into an array called <code>fractionList</code>.</p>
  
  <p>I have another class <code>FractionCounter</code> that counts the occurrences of fractions in their reduced form, and if there are any repeats then the program will simply add to the integer "counter" of that object. The <code>FractionCounter</code> objects are stored in an object list called <code>fracCount</code>.</p>
  
  <p>My problem is when I am adding an object to the <code>fracCount</code> list of <code>FractionCounter</code> objects. I am using a branching statement to determine whether I should add a new fraction element (one that doesn't match any of the reduced form of the other fractions in the array <code>fracCount</code>) to <code>fracCount</code>, or if I should add a value to the <code>counter</code> variable in an element that shares the same reduced fraction value.</p>
  
  <p>Everything works except when I am trying to determine whether the faction object is a copy or not. With the code I have, my <code>fracCount</code> array is empty.</p>
  
  <p>Here is my code</p>
  
  <pre><code>public class Fraction {
      private int numerator;
      private int denominator;
  
      Fraction(){ // default, no arg constructor
      }
      // constructor that initializes data
      Fraction(int numerator, int denominator){
          this.numerator = numerator;
          if(denominator &gt; 1000){
              this.denominator = 1;
          }else if(numerator == denominator){
              this.numerator = 1;
              this.denominator = 1;
          }else if(denominator == 0){
              System.out.println("Zero is not a valid denominator");
          }else{
              this.denominator = denominator;
          }
      }
      // compares this fraction object to 'other' fraction object
      boolean equals(Fraction other){
          if(this.numerator == other.numerator &amp;&amp; 
                  this.denominator == other.denominator){
              return true;
          }else if((this.numerator / this.denominator) ==
                  (other.numerator / other.denominator)){
              return true;
          }else{
              return false;
          }
      }
      // gives user access to the numerator and denominator
      public int getNumerator(){
          return this.numerator;
      }
      public int getDenominator(){
          return this.denominator;
      }   
  }
  
  public class FractionCounter extends Fraction{
      private Fraction theFraction;
      private int counter = 1;
  
      FractionCounter(Fraction theFraction){
          this.theFraction = theFraction;
      }
      public boolean compareAndIncrement(Fraction newFraction){
          if((theFraction.getNumerator() / theFraction.getDenominator() == 
                  newFraction.getNumerator() / newFraction.getDenominator())){
              this.counter++;
              return true;
          }else if(theFraction.getDenominator() == 0 || 
                  newFraction.getDenominator() == 0){
              return false;
          }else{
              return false;
          }
      }
  }
  
  public class ObjectList {
      private int N;
      private Fraction[] fractionList;
      private int numElements = 0;
  
      public ObjectList(int n){
          this.N = n;
          this.fractionList = new Fraction[N];
      }
      public void add(Fraction next){
          fractionList[numElements] = next;
          numElements++;
      }
      public int size(){
          return this.numElements;
      }
      public Fraction getFraction(int i){
          return fractionList[i];
      }
  }
  
      import java.util.Scanner;
      import java.awt.List;
      import java.io.*;
      import java.util.Arrays;
  
      public class FractionDriver {
      public static void main(String[] args){
  
          // creates scanner object
          Scanner fractions = null;
  
          // uses scanner to import fractions file and read how many lines 
          try{
              fractions = new Scanner(new FileInputStream("fractions.txt"));
          }catch(FileNotFoundException e){
              System.out.println("Cannot find fractions.txt");
              System.exit(0);
          }
  
          // creates a large array that stores the text file
          String[] input = new String[100];
  
          int numLines = 0; // counts the number of fractions
          int numElement = 0; // counts the current index element
          while(fractions.hasNextLine()){
              input[numElement] = fractions.next();
              numElement++;
              numLines++;
          }
          fractions.close(); // closes the input stream
  
          // create object list of fractions
          ObjectList fractionList = new ObjectList(numLines);
          ObjectList fractCount = new ObjectList(numLines);
  
          int totalFractions = 0;
  
          for(int i = 0; i &lt; numLines; i++){
  
              totalFractions++; // adds one on every new line
  
              // creates an array for each fraction where frac[0] is the 
              // numerator and frac[1] is the denominator
              String[] fract = input[i].split("/"); 
  
              // converts the string values to integers
              int numerator = Integer.parseInt(fract[0]);
              int denom = Integer.parseInt(fract[1]);
  
              // creates a fraction object and assigns instance variables
              Fraction f = new Fraction(numerator, denom);
              FractionCounter count = new FractionCounter(f);
  
              // adds the fraction to the array if the denominator
              // is not zero
              if(f.getDenominator() != 0){
                  fractionList.add(f);
                  for(int j = 0; j &lt; totalFractions; j++){
                      if(fractCount.getFraction(j) != null){
                          if(!f.equals(fractCount.getFraction(j))){
                              fractCount.add(count);
                          }else{
                              count.compareAndIncrement(f);
                          }
                      }
                  }
              }   
          }
      }
  }
  </code></pre>