# Array, fractions, etc

• April 17th, 2013, 07:34 PM
Kristenw17
Array, fractions, etc
So here is what I am trying to accomplish: I am creating a program with two classes: Fraction and FractionCounter. I am having some major problems (mostly because I am unsure of how to to the basic frame for the program). I am not posting the FractionCounter class because I am just worried about getting the Fraction class working at the moment.

Here is my Driver: I would appreciate any comments on how I can improve it because that is were most of my problems are.
1. I can't figure out the best way to do the driver so that it is possible to use the equals method from the Fraction class.

Code :

```public class Homework2Driver{   public static void main(String[] args) throws FileNotFoundException{ Scanner input = new Scanner(new File("fractions.txt"));   String [] fractions = new String[100]; int size = 0; int [] numerator = new int[100]; int [] denominator = new int[100]; String[]split = new String[2]; //ArrayList<FractionCounter> counter = new ArrayList<FractionCounter>();   //take a line at a time and put it into a string [] while(input.hasNextLine()){ fractions[size]= input.nextLine(); size++; }   //for loop to split fractions into numerator and denonimator for(int i = 0; i < size; i++){ split = fractions[i].split("/"); numerator[i]=Integer.valueOf(split[0]); denominator[i]=Integer.valueOf(split[1]); }   for(int i = 0; i < size; i++){ Fraction a = new Fraction(numerator[i],denominator[i]);   } } }```

Fraction Class: the only thing i'm really concerned with here is the boolean equals method. I know I did not do this right.
Code :

```public class Fraction{   private int numerator; private int denominator;   public Fraction(){   }   public Fraction(int n, int d){ setNumerator(n); setDenominator(d);   }   public String toString(){ return String.format("%d/%d", getNumerator(), getDenominator()); }   public boolean equals(Object otherObject){ if(otherObject == null){ return false; }else{ if(!getClass().equals(otherObject.getClass())){ return false; }else{ Fraction otherFraction = (Fraction) otherObject; return getNumerator()== otherFraction.getNumerator() && getDenominator()== otherFraction.getDenominator();   } } }   public int getNumerator(){ return numerator; }   public void setNumerator(int newNumerator){ numerator = newNumerator; }   public int getDenominator(){ return denominator; }   public void setDenominator(int newDenominator){ if(newDenominator == 0 ){ throw new IllegalStateException(String.format("Illegal denominator: %d", newDenominator)); } else{ denominator = newDenominator; } }   }```
• April 17th, 2013, 08:29 PM
pbrockway2
Re: Array, fractions, etc
Regarding the Fraction class, a couple of things: first fractions have numerators and (nonzero) denominators so I would get rid of the no argument constructor. (I would also point out - although it's not a big deal at this stage - that people are wary of calling nonprivate methods like your setXXX() from within the constructor. Things can go wrong if the Fraction class is ever subclassed by something that replaces the setter methods by other ones.)

A bigger problem is that your equals() insists on have both the numerators and denominators equal. But real fractions don't work that way. Suppose you have two fractions n1/d1 and n2/d2 then they are equal whenever n1*d2==n2*d1. For example 2/6 equals 1/3 because 2*3==1*6.

As for the driver...

Code :

```for(int i = 0; i < size; i++){ Fraction a = new Fraction(numerator[i],denominator[i]);   }```

This code does nothing with a, which I guess is where you're stuck. But you haven't told us what the driver is actually supposed to do. (Check for two equal fractions entered one after another? Check for *any* equal fractions? Something else?) So it's hard to know what to suggest.

(I prefer the idea you've commented out, of having an ArrayList<Fraction> in place of those parallel arrays of ints. Whatever you're supposed to do will, most likely, be more naturally done with a list: read a line, construct a fraction, and put the fraction into the list until you've finished the file. Then do whatever with the list of fractions. If the question asks you to do something like remove duplicate fractions then a Set is better than a List.)