Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Array, fractions, etc

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Thanked 1 Time in 1 Post

    Default 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.

    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 []
                fractions[size]= input.nextLine();
            //for loop to split fractions into numerator and denonimator
            for(int i = 0; i < size; i++){
             split = fractions[i].split("/");
            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){
        public String toString(){
            return String.format("%d/%d", getNumerator(), getDenominator());
        public boolean equals(Object otherObject){
            if(otherObject == null){
                return false;
                    return false;
                    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;

  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Thanked 206 Times in 182 Posts

    Default 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...

    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.)

Similar Threads

  1. Fractions
    By ps3lover3 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 29th, 2013, 09:10 PM
  2. Question about for loop to print the sum of series of fractions
    By _K_ in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 17th, 2012, 08:08 AM
  3. [SOLVED] Keeping Negative Fractions
    By Kerrigan in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 10th, 2011, 05:03 PM
  4. Fractions Code help
    By SOK in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 9th, 2010, 11:07 AM
  5. Fractions
    By debug in forum Java Theory & Questions
    Replies: 1
    Last Post: March 8th, 2010, 11:29 PM

Tags for this Thread