I think this is a typecast issue, still trying to figure out how to make that work in java. Any help would be great!
******************
Hi-
I got some great help on some dumb I made errors on my addition, subtractions and division last night, however i'm still having issues getting my fraction to decimal conversion to work.
I'm getting this for the Fraction to dec conversion....
(1, 2)As a Decimal = (0, 0)
I'm really not sure what i'm doing wrong, but maybe it's because i've been staring at this way to long.
Thanks!
Here comes to code.
*Fraction Operations Object*
import java.math.*;
public class FractionOperations
{
private FractionNumber n1 = new FractionNumber();
private FractionNumber n2 = new FractionNumber();
private FractionNumber result = new FractionNumber();
private double deci;
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;
}
public void add()
{
result.setNum((n1.getNum() * n2.getDen()) + n2.getNum() * n1.getDen());
result.setDen(n1.getDen() * n2.getDen());
//((a * d) +(c * B)/>)/(b * d)
}
public void subtract()
{
result.setNum((n1.getNum() * n2.getDen()) - n2.getNum() * n1.getDen());
result.setDen(n1.getDen() * n2.getDen());
//((a * d) -(c * B)/>)/(b * d)
}
public void multiply()
{
result.setNum(n1.getNum() * n2.getNum());
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.setDen(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 double decimal()
{
deci = (n1.getNum() / n1.getDen());
return deci;
}
}//End
*Fraction 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 + ")";
}
}
*Driver*
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);
System.out.println("Testing");
System.out.println(c1);
fractionSum.add();
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());
}
}
This post has been edited by Venport: 23 April 2010 - 03:24 PM

New Topic/Question
Reply



MultiQuote



|