Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I'm currently working on a fraction class. I've successfully figured out how to create a fraction object as well as check for errors but I've blanked and cannot figure out how to add together the two values of two different objects. I'm stumped. I have a feeling it's something simple and I'm over thinking things.

My issue is in the add method. The error is: " bad operand type for binary operator '+' first type: RationalNumber second type: RationalNumber

import java.util.*;
public class RationalNumber{
private int numerator;
private int denominator;
//Prompts user to set his/her own values for the numerator and denominator

public RationalNumber(){
    Scanner s = new Scanner(System.in);
    System.out.println("Enter in the value you want for your numerator");
    numerator = s.nextInt();
    System.out.println("Enter in the value you want for your denominator");
    denominator = s.nextInt();
    do{
        //makes sure 0 doesnt exist in the denominator
        if(denominator == 0){
            System.out.println("You cannot enter zero in the denonimator");
            System.out.println("Enter in the value you want for your denominator");
            denominator = s.nextInt();
        }
    }while(denominator == 0);
    RationalNumber r1 = new RationalNumber(numerator, denominator);
}
//creates a fraction based on passed inputs
public RationalNumber(int r1, int r2){
    numerator = r1;
    denominator = r2;
    double wholeNum = 0;
    Scanner s = new Scanner(System.in);
    do{
        if(denominator == 0){
            System.out.println("You cannot enter zero in the denonimator");
            System.out.println("Enter in the value you want for your denominator");
            denominator = s.nextInt();
        }
    }while(denominator == 0);
    int gcf = 0;
    boolean cont = true;
    do{
        if(Math.abs(numerator) > Math.abs(denominator)){
            //will calculate the value of the numerator dividided by the denominator
            if(Math.abs(numerator)%Math.abs(denominator) != 0){
                wholeNum = (double)numerator/denominator;
                cont = false;
                break;
            }
            //if numerator is equal to 1, it goes no further and will go ahead and calculate the value
            else if(Math.abs(numerator) == 1){
                gcf = 1;
                cont = false;
                break;
            }
            else{
            wholeNum = (numerator / denominator);
            cont = false;
            break;
            }
        }
        //anything divisible by 0 is 0
        else if(numerator == 0){
            wholeNum = 0;
            cont = false;
            break;
        }
        //finds the greatest common denominator
        else if(Math.abs(numerator) < Math.abs(denominator)){
            gcf = gcd(numerator, denominator);

            //System.out.println(gcf);
            cont = false;
            break;
        }
    }while(cont != true);
    //if numerator = 0, only 0 returned
    if(numerator == 0){
        System.out.println(wholeNum);
        //return wholeNum;
    }
    else if(Math.abs(numerator) == 1){
    System.out.println("The simplified fraction is: " + numerator +"/" +       denominator + ".");
    }
    else if(Math.abs(numerator) > Math.abs(denominator)){
        System.out.println("The simplified fraction is: " + numerator +"/" + denominator + ".");
        //return wholeNum;
    }
    else if(numerator !=1){
        numerator = numerator/gcf;
        denominator = denominator/gcf;
        System.out.println("The simplified fraction is: " + numerator +"/" + denominator + ".");
        //return(numerator/denominator);
    }

    }
//calculates greatest common denominator
public int gcd(int num, int denom){
    if(denom == 0)
        return num;
    else
        return gcd(denom, num%denom);
}
//adds together two 'fractions' and returns their value
public RationalNumber add(RationalNumber r2){
 double sum;
 sum = this + r2;
 return sum;
}

public static void main(String [] args){
   RationalNumber r1 = new RationalNumber(9,5);
   RationalNumber r2 = new RationalNumber(3,2);
   System.out.println(r1.add(r2));

    //RationalNumber r1 = new RationalNumber(4,25);

}
}
share|improve this question

closed as off-topic by Jamal, palacsint, 200_success, Brian Reichle, Aseem Bansal Sep 15 '13 at 12:02

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions must contain working code for us to review it here. For questions regarding specific problems encountered while coding, try Stack Overflow. After your code is working you can edit this question for reviewing your working code." – Jamal, palacsint, 200_success, Brian Reichle, Aseem Bansal
If this question can be reworded to fit the rules in the help center, please edit the question.

1  
your code must compile and work before we can do anything about it. If you need help getting the code to work, go to stackoverflow –  Olayinka Sep 15 '13 at 2:58

1 Answer 1

The problem in your code is that you haven't taught it how to add two rational numbers. Here you go:

public RationalNumber add(RationalNumber r2) {
    return new RationalNumber(
        this.numerator * r2.denominator + r2.numerator * this.denominator,
        //////////////////////////////////////////////////////////////////
                       this.denominator * r2.denominator
    );
}

There are also some problems with your code organization. You don't want anything other than arithmetic in RationalNumber. You have mixed in a lot of input/output code in your RationalNumber class, which prevents you from reusing that class in, say, graphical user interfaces or web apps. You should expunge all mention of System.in and System.out from RationalNumber (except maybe in a main() function that you use as a test case). At most, RationalNumber should have a toString() method:

public String toString() {
    return this.numerator + "/" + this.denominator;
}

If the RationalNumber constructor detects a 0 denominator, it should throw an ArithmeticException instead of printing a message.

The signature of your gcd() function should be private static int gcd(int a, int b). It should be private unless you deliberately want to let other code use it as a utility function. It should be static because it is purely a function of its two input parameters, and has nothing to do with the instance variables numerator and denominator. Finally, I would rename the parameters since gcd() is a generic operation on any two integers, not necessarily representing a numerator and denominator.

Your do-while loops are awkward. do { if (denominator == 0) { ... } } while (denominator == 0) would be better written as a normal while loop:

while (denominator == 0) {
    ...
}

Also,

boolean cont = true;
do {
    if (...) {
        cont = false;
        break;
    }
} while (cont != true);

should just be

while (true) {
    if (...) {
        break;
    }
}
share|improve this answer
1  
+1, yet I would say gcd() should be private. If intended as a utility method then RationalNumber is simply not the right place for it. –  bowmore Sep 15 '13 at 11:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.