# How do I reduce Fractions in this program?

• 12-07-2008, 08:25 PM
How do I reduce Fractions in this program?
I have been working on htis program for a few weeks, but I can't get the fractions to work correctly. Can someone please help me?
Code:

```import javax.swing.JOptionPane; public class Lab08bst {         public static void main (String args[])         {                  String strNum1 = JOptionPane.showInputDialog("Enter Numerator 1");                 String strDen1 = JOptionPane.showInputDialog("Enter Denominator 1");                 String strNum2 = JOptionPane.showInputDialog("Enter Numerator 2");                 String strDen2 = JOptionPane.showInputDialog("Enter Denominator 2");                                 int num1 = Integer.parseInt(strNum1);                 int den1 = Integer.parseInt(strDen1);                 int num2 = Integer.parseInt(strNum2);                 int den2 = Integer.parseInt(strDen2);                 Rational r1 = new Rational(num1,den1);                 Rational r2 = new Rational(num2,den2);                 Rational r3 = new Rational();                                 r3.multiply(r1,r2);                 String mul = r1.getOriginal() + " * " + r2.getOriginal() + "  =  " + r3.getRational();                 r3.divide(r1,r2);                 String div = r1.getOriginal() + " / " + r2.getOriginal() + "  =  " + r3.getRational();                                r3.add(r1,r2);                 String add = r1.getOriginal() + " + " + r2.getOriginal() + "  =  " + r3.getRational();                 r3.subtract(r1,r2);                 String sub = r1.getOriginal() + " - " + r2.getOriginal() + "  =  " + r3.getRational();                                 String output = mul + "\n" + div + "\n" + add + "\n" + sub;                                 JOptionPane.showMessageDialog(null,output);                           System.exit(0);         } } class Rational {                 private int firstNum;        // entered numerator         private int firstDen;        // entered denominator         private int num;                // reduced numerator         private int den;                // reduced denominator                 public Rational()         {                 firstNum = 0;                 firstDen = 0;                 num = 0;                 den = 0;         }         public Rational(int n, int d)         {                 firstNum = n;                 firstDen = d;                 num = 0;                 den = 0;         }                 private int getGCF(int n1,int n2)         {                 int rem = 0;                 int gcf = 0;                 do                 {                         rem = n1 % n2;                         if (rem == 0)                                 gcf = n2;                         else                         {                                 n1 = n2;                                 n2 = rem;                         }                 }                 while (rem != 0);                 return gcf;         }         private void reduce()         {                 int gcf = getGCF(num,den);                                         }                 public double getDecimal()         {                 return 0;         }         public String getRational()         {                 return (" " + num + "/" + den);                         }                 public String getOriginal()         {                 return (" " + firstNum + "/" + firstDen);                         }                 public int getNum()         {                         return num;         }         public int getDen()         {                 return den;                         }                 public void multiply(Rational r1, Rational r2)         {                 this.num = r1.firstNum * r2.firstNum;                 this.den = r1.firstDen * r2.firstDen;         }         public void divide(Rational r1, Rational r2)         {                 this.num = r1.firstNum * r2.firstDen;                 this.den = r1.firstDen * r2.firstNum;                         }                 public void add(Rational r1, Rational r2)         {                 this.num = r1.firstNum + r2.firstNum;                 this.den = r1.firstDen + r2.firstDen;         }                 public void subtract(Rational r1, Rational r2)         {                 this.num = r1.firstNum - r2.firstNum;                 this.den = r1.firstDen - r2.firstDen;         }   }```
• 12-07-2008, 08:58 PM
Fubarable
What problems are you having?

Oh, also, when posting your code, please use code tags so that your code will retain its formatting and be readable. To do this, you will need to paste already formatted code into the forum, highlight this code, and then press the "code" button at the top of the forum Message editor prior to posting the message. Another way is to place the tag &#91;code] at the top of your block of code and the tag &#91;/code] at the bottom, like so:

Code:

```&#91;code]   // your code block goes here.   // note the differences between the tag at the top vs the bottom. &#91;/code]```
Doing this will help increase the number of folks willing to read your code, and that is definitely what you desire.

Good luck.
• 12-07-2008, 09:03 PM
Quote:

Originally Posted by Fubarable
What problems are you having?

Oh, also, when posting your code, please use code tags so that your code will retain its formatting and be readable. To do this, you will need to paste already formatted code into the forum, highlight this code, and then press the "code" button at the top of the forum Message editor prior to posting the message. Another way is to place the tag
Code:

` at the top of your block of code and the tag`
at the bottom, like so:

Code:

```[code]   // your code block goes here.   // note the differences between the tag at the top vs the bottom.```
[/code]

Doing this will help increase the number of folks willing to read your code, and that is definitely what you desire.

Good luck.

Thanks. It's edited. I'm trying to reduce the fractions and make the addition and subtraction show correct results.
• 12-07-2008, 09:27 PM
Fubarable
Quote:

Thanks. It's edited.

Great, thanks.
Quote:

I'm trying to reduce the fractions and make the addition and subtraction show correct results.
And what is happening now when you run it? How is it not working right?
• 12-07-2008, 09:32 PM
Quote:

Originally Posted by Fubarable
Great, thanks.

And what is happening now when you run it? How is it not working right?

I input the numbers and the box shows this:
3/4 * 1/2 = 3/8
3/4 / 1/2 = 6/4
3/4 + 1/2 = 4/6
3/4 - 1/2 = 2/2

Not only are the answers for addition and subtraction incorrect, but I need the answers for all of them to be able to be reduced.
• 12-07-2008, 10:39 PM
Fubarable
Well, at least the non-reduced math is correct of the multiply and divide portion. The addition and subtraction math is all wrong but is doing just what the code tells it to do.

For instance this is the add code:
Code:

```  public void add(Rational r1, Rational r2)   {     this.num = r1.firstNum + r2.firstNum;     this.den = r1.firstDen + r2.firstDen;   }```
Can you describe in non-Java terms how addition of fractions is supposed to be done? If you understand this, we can work on the Java representation of this.

As for the reduction of fractions, this is more of a math issue than a Java issue and involves finding the greatest common divisor or gcd. Here's a good article on how to do this non-programmatically: Greatest common divisor - Wikipedia, the free encyclopedia
• 12-07-2008, 10:46 PM
Quote:

Originally Posted by Fubarable
Well, at least the non-reduced math is correct of the multiply and divide portion. The addition and subtraction math is all wrong but is doing just what the code tells it to do.

For instance this is the add code:
Code:

```  public void add(Rational r1, Rational r2)   {     this.num = r1.firstNum + r2.firstNum;     this.den = r1.firstDen + r2.firstDen;   }```
Can you describe in non-Java terms how addition of fractions is supposed to be done? If you understand this, we can work on the Java representation of this.

You change the denonimators to the least common multiple, then add or subtract the numerators. If the numerator and denomorator share a greatest Common Factor, then it can be reduced by dividing both by the GCF.
• 12-07-2008, 11:37 PM
Fubarable
So the first thing I would do would be to create a greatest common factor method:
Code:

```int gcf(int a, int b) {   // your code in here }```
One nice quick and dirty way to do this is via Euclid's algorithm, and you can find more about this here:
Euclidean algorithm - Wikipedia, the free encyclopedia
New To Java - reduce fraction??? (prometheuzz' post)
• 12-08-2008, 01:28 AM
```int gcf(int a, int b) {   // your code in here }```
```private void reduce()         {                 int gcf = getGCF(num,den);                                         }```