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.
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.
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; } } }