Results 1 to 9 of 9
 12072008, 08:25 PM #1Member
 Join Date
 Dec 2008
 Posts
 6
 Rep Power
 0
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?
Java 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; } }
Last edited by Popedreadlock; 12072008 at 09:02 PM.

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 [/code] at the bottom, like so:
Java Code:[code] // your code block goes here. // note the differences between the tag at the top vs the bottom. [/code]
Good luck.
 12072008, 09:03 PM #3Member
 Join Date
 Dec 2008
 Posts
 6
 Rep Power
 0

 12072008, 09:32 PM #5Member
 Join Date
 Dec 2008
 Posts
 6
 Rep Power
 0

Well, at least the nonreduced 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:
Java Code:public void add(Rational r1, Rational r2) { this.num = r1.firstNum + r2.firstNum; this.den = r1.firstDen + r2.firstDen; }
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 nonprogrammatically: Greatest common divisor  Wikipedia, the free encyclopedia
 12072008, 10:46 PM #7Member
 Join Date
 Dec 2008
 Posts
 6
 Rep Power
 0

So the first thing I would do would be to create a greatest common factor method:
Java Code:int gcf(int a, int b) { // your code in here }
Euclidean algorithm  Wikipedia, the free encyclopedia
New To Java  reduce fraction??? (prometheuzz' post)
 12082008, 01:28 AM #9Member
 Join Date
 Dec 2008
 Posts
 6
 Rep Power
 0
Similar Threads

Executing a program within a program
By gibsonrocker800 in forum New To JavaReplies: 5Last Post: 05122008, 08:24 AM 
How to use Java's compression classes to reduce the amount of data sent over a socket
By Java Tip in forum java.netReplies: 0Last Post: 04072008, 07:56 PM 
How to execute an External Program through Java program
By Java Tip in forum java.ioReplies: 0Last Post: 04042008, 02:40 PM 
How to execute an External Program through Java program
By JavaBean in forum Java TipReplies: 0Last Post: 10042007, 09:33 PM 
How to reduce the size or avoiding out of memory error?
By rajeshkumarmsc in forum Advanced JavaReplies: 3Last Post: 08112007, 10:15 PM
Bookmarks