# Java Fraction Calculator Help

Page 1 of 1

## 2 Replies - 2454 Views - Last Post: 22 April 2010 - 07:22 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=169677&amp;s=1d526c1fad3e4e07f0f0d7f96313bd23&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Venport

Reputation: 0
• Posts: 26
• Joined: 23-September 09

# Java Fraction Calculator Help

Posted 22 April 2010 - 05:12 PM

Hi all,

I'm having troubles with my Java Fraction Calculator. Here is the output from my driver Program followed by all my code.

Any Help would be amazing

Thank you

*Driver Output*
```\$ java FractionDriver
Testing
(1, 2)
(1, 2)+(1, 2)=(3, 4)
(1, 2)-(1, 2)=(1, 4)
(1, 2)*(1, 2)=(1, 4)
(1, 2)/(1, 2)=(2, 0)
(1, 2)As a Decimal = (0, 0)
```

*Driver Code*
```public class FractionDriver
{
public static void main(String[] args)
{
FractionNumber c1 = new FractionNumber(1 ,2);
FractionNumber c2 = new FractionNumber(1 ,2);
FractionOperations fractionSum = new FractionOperations(c1, c2);
FractionOperations fractionDiff = new FractionOperations(c1, c2);
FractionOperations fractionProd = new FractionOperations(c1, c2);
FractionOperations fractionQuot = new FractionOperations(c1, c2);
FractionOperations fractionDeci = new FractionOperations(c1, c2);

System.out.println("Testing");
System.out.println(c1);
System.out.println(c1 + "+" + c2 + "=" + fractionSum.getResult());

fractionDiff.subtract();
System.out.println(c1 + "-" + c2 + "=" + fractionDiff.getResult());

fractionProd.multiply();
System.out.println(c1 + "*" + c2 + "=" + fractionProd.getResult());

fractionQuot.divide();
System.out.println(c1 + "/" + c2 + "=" + fractionQuot.getResult());

fractionDeci.decimal();
System.out.println(c1 + "As a Decimal = " + fractionDeci.getResult());
}
}

```

*Fraction Number Object*
```import java.util.*;

public class FractionNumber
{
private int num, den;

public FractionNumber()
{
num = 0;
den = 1;
}//Default Constructor

public FractionNumber(int userNum, int userDen)
{
if(userDen !=0)
{
num = userNum;
den = userDen;
}
else
System.exit(0);
}//Creates a user defined fraction that protects against an undefined Fraction

public FractionNumber(FractionNumber z)
{
num = z.getNum();
den = z.getDen();
}//Copy Constuctor
public int getNum()
{
return num;
}
public int getDen()
{
return den;
}

public void setNum(int n)
{
num = n;
}

public void setDen(int d)
{
den = d;
}

public String toString()
{
return "(" + num + ", " + den + ")";
}
}

```

*Operations Object*
```import java.math.*;

public class FractionOperations
{
private FractionNumber n1 = new FractionNumber();
private FractionNumber n2 = new FractionNumber();
private FractionNumber result = new FractionNumber();

public FractionOperations() //Default Constcutor
{
n1.setNum(0);
n1.setDen(0);
n2.setNum(0);
n2.setDen(0);
result.setNum(0);
result.setDen(0);
}

public FractionOperations(FractionNumber num1) //One value is user defined others are zero
{
n1.setNum(num1.getNum());
n1.setDen(num1.getDen());
n2.setNum(0);
n2.setDen(0);
result.setNum(0);
result.setDen(0);
}

public FractionOperations(FractionNumber num1, FractionNumber num2) //Two values are user defined others are zero
{
n1.setNum(num1.getNum());
n1.setDen(num1.getDen());
n2.setNum(num2.getNum());
n2.setDen(num2.getDen());
result.setNum(0);
result.setDen(0);
}

public FractionOperations(FractionOperations cpy)
{
n1 = cpy.getN1();
n2 = cpy.getN2();
result = cpy.getResult();
}

public FractionNumber getN1()
{
return n1;
}

public FractionNumber getN2()
{
return n2;
}

public FractionNumber getResult()
{
return result;
}

/*private static int gcd(int x, int y)
{
//Ggreatest Common Divisor
int mod;
if(x < y)
{
mod = x;
x = y;
y = mod;
}
int r = x % y;
while (r !=0)
{
x = y;
y = r;
r = x % y;
}
return y;
} */ //Now working the way i want it to

{
result.setNum((n1.getNum() * n2.getDen()) + n2.getNum() * n1.getNum());
result.setDen(n1.getDen() * n2.getDen());
//((a * d) +(c * B)/>)/(b * d)
}

public void subtract()
{
result.setNum((n1.getNum() * n2.getDen()) - n2.getNum() * n1.getNum());
result.setDen(n1.getDen() * n2.getDen());
//((a * d) -(c * B)/>)/(b * d)
}

public void multiply()
{
result.setNum(n1.getNum() * n2.getNum());
result.setDen(n1.getDen() * n2.getDen());
//(a * c) / (b * d)
}

public void divide()
{
if(n2.getNum() !=0)
{
result.setNum(n1.getNum() * n2.getDen());
result.setNum(n1.getDen() * n2.getNum());
}
else
{
System.out.println("Undefined");
}
//If Because the Fraction class does not allow 0 to be a demoninator the only way to get a undfiend anser with real numbers is if the numerator of the second number is 0 i've protected against this.
//(a * d) / (b * c)
}

public void decimal()
{
result.setNum(n1.getNum() / n1.getDen());
}

}//End

```

Is This A Good Question/Topic? 0

## Replies To: Java Fraction Calculator Help

### #2 nick2price

• D.I.C Lover

Reputation: 563
• Posts: 2,826
• Joined: 23-November 07

## Re: Java Fraction Calculator Help

Posted 22 April 2010 - 06:37 PM

Firslty, you need to find out which of the outputs are correct and which are wrong. Then the ones that are wrong, you need to re look at your formulas. Heres an example. When your adding 1/2 with 1/2, your getting 1/4 when it should be 4/4 or 1. So, you know you add is wrong. look at the forumla
```result.setNum((n1.getNum() * n2.getDen()) + n2.getNum() * n1.getNum());

```

Well, I can see this is wrong straight away because three of the method calls are too getNum, when it should be 2 calling getNum and 2 calling getDen. So, using your formula
//((a * d) +(c * )/(b * d)

Seperate it properly.

a = n1.getNum()
b = n1.getDen()
c = n2.getNum()
d = n2.getDen()

Now, place these into your formula in the correct place
```result.setNum((n1.getNum() * n2.getDen()) + (n2.getNum() * n1.getDen()));
```

New result, 4/4.

Now you need to sort out the other ones which are wrong.

### #3 Venport

Reputation: 0
• Posts: 26
• Joined: 23-September 09

## Re: Java Fraction Calculator Help

Posted 22 April 2010 - 07:22 PM

Hey Thanks I've been staring at my addition equation for 30min now and did not see it until you pointed it out.

I've fixed Subtraction as well (same error as Addition) however division is still alluding me. It's the same as above but I'm adding it here as well.

``` public void divide()
{
if(n2.getNum() !=0)
{
result.setNum(n1.getNum() * n2.getDen());
result.setNum(n1.getDen() * n2.getNum());
}
else
{
System.out.println("Undefined");
}
//(a * d) / (b * c)
}

```

a = n1.getNum
b = n1.getDen
c = n2.getNum
d = n2.getDen

so the Numerator of the new fraction should be n1.getNum * n2.getDen
and the denominator of the new fration should be n1.getDen * n2.getNum

Is there something else I'm doing wrong?? Or have i been stairing at this one too long as well and there is a simple error hiding from me?