0

The following code is what I have so far. I know there are errors in some parts, because i'm currently still workin on them. However, I am having trouble invoking the reduce() method. The purpose of the program when im done is to be able to add and multiply fractions or rational numbers and then show them in their reduced form. Is my reduce method correct? If so, what am I doing wrong when I try to invoke the method when I am trying to write the other methods? Thanks if you can help.

public class RationalNumber {
int numerator;
int denominator;
public RationalNumber(int num, int denom){
num = numerator;
denom = denominator;
if(denom == 0){
denom = 1;
}
//not finished yet//


}
public int getNumerator(){
return numerator;
}
public int getDenominator(){
return denominator;
}
public RationalNumber multiply(RationalNumber other){
int num1 = this.getNumerator();
int den1 = this.getDenominator();
int num2 = other.getNumerator();
int den2 = other.getNumerator();
int numFinal = num1 * num2;
int denFinal = den1 * den2;
RationalNumber result = RationalNumber(numFinal, denFinal);
return result;
}
public RationalNumber add(RationalNumber other){
int num1 = this.getNumerator();
int den1 = this.getDenominator();
int num2 = other.getNumerator();
int den2 = other.getDenominator();
int lcm = lcm(den1, den2);
if(den1 != den2){
int newDen1 = lcm;
int newDen2 = lcm;
int newNum1 = num1 * den2;
int newNum2 = num2 * den1;
RationalNumber result = RationalNumber((newNum1 + newNum2), lcm);
}
RationalNumber result = RationalNumber((num1 + num2), den1);
return result;
}
public double toDouble(){
double number = (double) numerator / denominator;
return number;
}
public String toString(){
return;
}private void reduce(){
while(denominator % numerator == 0){
numerator = numerator / gcd(numerator, denominator);
denominator = denominator / gcd(numerator, denominator);
}
return;
}
private int lcm(int a, int b)  {
return a * b / gcd(a, b);
}


private int gcd(int a, int b)  {
int remainder;
do {
remainder = a % b;
a = b;
b = remainder;
} while (remainder > 0);


return a;
}


}

Edited by happygeek: fixed formatting

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by jinternalframe
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.