469,134 Members | 1,315 Online 74
I am having trouble figuring out how to add, subtract, multiply, and divide fractions in my Java program. First off I will tell you what I already did. At first, the assignment was to:

Define a class called Rational. This class is used to represent a ratio of two integers. Include mutator functions that allow the user to set the numerator or denominator as a double. Include additional member function that outputs the value of the fraction reduced to lowest terms. This will require finding the greatest common divisor for the numerator and denominator, then dividing both by that number. Embed your class in a test program.

Okay so I did all of that fine. Everything worked and it was all good, but then, my teacher wanted us to insert another fraction to the mix to do the math operations on it. I am having trouble figuring out how to do that. I keep getting errors upon errors. Also, I am having trouble reducing the fractions now since a new fraction is now in the program.

So, I am asking anyone to help assist me in fixing this problem. Any feedback whatsoever would be greatly appreciated. If you need any more information on the program, please let me know! Thanks!

P.S. Here is my program:

/**
Description: Program to represent a ratio of two integers.
Author:
Last Changed: September 25, 2006
**/

import java.util.Scanner;
import java.text.DecimalFormat;

public class Rational1
{
public static int num1; //num1 = numerator of first fraction
public static int den1; //den1 = denominator of first fraction
public static int num2; //num2 = numerator of second fraction
public static int den2; //den2 = denominator of second fraction

public Rational1()
{
num1 = 0;
num2 = 0;
den1 = 0;
den2 = 0;

}

public Rational1 (int n1, int d1, int n2, int d2)
{
num1 = n1;
den1 = d1;
num2 = n2;
den2 = d2;

if((d1 == 0) || (d2 == 0))
{
System.out.println("Error - denominator cannot be zero!");
}
}

public static void main(String[] args)
{
int lowestNum1 = 0;
int lowestDen1 = 0;
int lowestNum2 = 0;
int lowestDen2 = 0;
int gcd = 0;

System.out.println("Welcome to the world of ratios!");
Rational1 yourRatio = new Rational1();
yourRatio.setRatio();
System.out.println("Your first fraction: " + num1 + "/" + den1);
System.out.println("Your numerator as a double: ");
yourRatio.toDouble(num1, num2);
System.out.println("Your denominator as a double: ");
yourRatio.toDouble(den1, den2);
gcd = yourRatio.gcd(num1, den1);
System.out.println("Your first fraction's GCD: " + gcd);
lowestNum1 = yourRatio.reduceNum(num1, gcd);
lowestDen1 = yourRatio.reduceDen(den1, gcd);
System.out.println("Your reduced fraction:" + lowestNum1 + "/" + lowestDen1);
System.out.println("Your second fraction: " + num2 + "/" + den2);
System.out.println("Your numerator as a double: ");
// yourRatio.toDouble(num2);
System.out.println("Your denominator as a double: ");
// yourRatio.toDouble(den2);
gcd = yourRatio.gcd(num2, den2);
System.out.println("Your second fraction's GCD: " + gcd);
lowestNum2 = yourRatio.reduceNum(num2, gcd);
lowestDen2 = yourRatio.reduceDen(den2, gcd);
System.out.println("Your reduced fraction:" + lowestNum2 + "/" + lowestDen2);
System.out.println("Thank you for visiting!");
}

public void setRatio()
{
System.out.println("Enter an integer for the numerator followed by another integer for the denominator to make a ratio.");
System.out.println("(Do not use the / sign to separate the numbers, just insert a space.)");
Scanner ratio = new Scanner(System.in);
num1 = ratio.nextInt();
den1 = ratio.nextInt();
num2 = ratio.nextInt();
den2 = ratio.nextInt();
}

public void toDouble(int n1, int n2)
{
double dubN1;
double dubN2;
dubN1 = n1;
dubN2 = n2;

System.out.println(dubN1);
System.out.println(dubN2);
}

/* public static int abs(int n, int d)
{
n = abs(n); //n = numerator
d = abs(d); //d = denominator

while(n !=d)
{
if(n > d)
{
n = n - d;
t = n;
}
else
{
n = d - n;
d = t;
}
g = n;
}
} */

/* public static int abs(int n)
{
return n > 0 ? n : - n;
}*/

public static int gcd(int a, int b)
{
/* int t; //t = temp
n = abs(n);
d = abs(d);

while(n != d)
{
if(n > d)
{
n = n - d;
t = n;
}
else
{
n = n - d;
d = t;
}
return n;
*/
if(b == 0)
{
return a;
}
else
{
return gcd(b, a % b);
}

}

public void reduce()
{
int n1 = num1;
int d1 = den1;
int n2 = num2;
int d2 = den2;
int g = gcd(n, m);

num1 = num1 / g;
den1 = den1 / g;
num2 = num2 / g;
den2 = den2 / g;
}

public int reduceNum(int a, int gcd)
{
int rNum1 = a / gcd;
int rNum2 = a / gcd;

return rNum1;
return rNum2;
}

public int reduceDen(int b, int gcd)
{
int rNum1 = b / gcd;
int rNum2 = b / gcd;

return rNum1;
return rNum2;
}

{
Rational1 R;
int den = den1 * den2;
int num = den2 * num1 + den1 * num2;
R = new Rational1(n, d);
R = R.reduce();
return R;
}

public Rational1 multiply(Rational1 Q)
{
Rational1 R;
int den = den1 * den2;
int num = num1 * num2;
R = new Rational1(n,d);
R = R.reduce();
return R;
}

public Rational1 subtract(Rational1 Q)
{
Rational1 R;
int den = den1 * den2;
int num = den2 * num1 = den1 * num2;
R = R.reduce();
return R;
}

public Rational1 divide(Rational1 Q)
{
Rational1 R;
int den = den1 * den2;
int num = num1 / num2;
R = R.reduce();
return R;
}

public static void mathOp(int num1, int den1, int num2, int den2, char op)
{
Rational1 result = new Rational1(); //just in case

f1.write();
prt(op);
f2.write();
prt( " = " );
switch(op)
{
case '+':
result = num1 * den2 + num2 * den1;
break;
case '-':
result = num1 * den2 - num2 * den1;
break;
case '*':
result = num1 * num2;
result = den1 * den2;
break;
case '/':
result = num1 / num2;
result = den1 / den2;
break;
default:
prtln( "Operator!? ..." );
}

result.write( );
DecimalFormat threeDigits = new DecimalFormat("#0.000");
prtln( " = " + (threeDigits.format(Result.toDouble( ))));
}
}
Sep 25 '06 #1
1 8360 r035198x
13,262 8TB
Next time you want to post code, please use code tags.
You could have given your variables names like numerator, denominator, etc.
You do not need to define the other fraction inside your Rational class. It is in fact a Rational itself. Declare it in the main method where you want to test it.
The switch construct should be avoided where possible. In this case, I would suggest you define methods inside your Rational class to perform the arithmetic operations.(If you do not understand this point, have a look at the definition of the java.math.BigDecimal class.
Sep 25 '06 #2