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