I fixed the errors..etc the program compiles and run
but I am not getting the correct out put. It looks like it is not combining the two fractions together
because the output only show.. the first number enter for the fraction 1
I under stand that the user enters in (num1 and den1) those get stored at
Rational rational1 = new Rational( num1, den1)
and (num2 and den2) get stored as rational2 so the math function between the two fractions can happen
and the result = rational1
but that doesn't seem to be happening I am not sure where at?
but I am not getting the correct out put. It looks like it is not combining the two fractions together
because the output only show.. the first number enter for the fraction 1
I under stand that the user enters in (num1 and den1) those get stored at
Rational rational1 = new Rational( num1, den1)
and (num2 and den2) get stored as rational2 so the math function between the two fractions can happen
and the result = rational1
but that doesn't seem to be happening I am not sure where at?
import java.util.Scanner;
public class RationalTest
{
public static void main (String args [])
{
int num1 = 0;
int den1 = 0;
int num2 = 0;
int den2 = 0;
// double num1 = 0;
// double den1 = 0;
// double num2 = 0;
// double den2 = 0;
int choice;
int dec;
// Rational foo = new Rational();
// ask user to enter the values for the fractions
Scanner input = new Scanner (System.in);
System.out.print("This program will do basic math with fractions\n");
// do{
System.out.print("please enter the numaortor for the first fraction\n");
num1 = input.nextInt();
System.out.print("Please enter the denominator for the frist fraction\n");
den1 = input.nextInt();
Rational rational1 = new Rational( num1, den1);
// takes num1 and num2 to create a rational
System.out.print("Please enter the numeraotor for the second fraction\n");
num2 = input.nextInt();
System.out.print("Please enter the denominator for the second fraction\n");
den2 = input.nextInt();
Rational rational2 = new Rational (num2, den2);
// takes num2 and num2 to creat a rational2
System.out.print ("please enter the precision\n");
dec = input.nextInt();
// Rational rational2 = new Rational ( num2, den2);
System.out.println("Choose what function you want the fractations to do?\n");
System.out.println("1 add fractions\n");
System.out.println("2 subtract fractions\n");
System.out.println("3 divide fractions\n");
System.out.println("4 muliply fractions\n");
System.out.println("0 end the program \n");
choice = input.nextInt();
Rational result;
switch( choice)
{
case 1:
result = rational1.getaddfract(rational2);
break;
case 2:
result = rational1.getsubfract(rational2);
break;
case 3:
result = rational1.gettimesfract(rational2);
break;
case 4:
result = rational1.getdividedfract(rational2);
break;
case 0:
System.out.print("Thank you for using this program");
break;
}
System.out.println("Result: " + rational1.toString());
System.out.println("Result: " + rational1.toString( dec));
// }
// while (choice !=0);
}
}
********* second part of the program
import java.text.DecimalFormat;
public class Rational
{
int numerator; //1
int denominator; //4
public Rational(int n, int d)
{
this. numerator = n;
this.denominator = d;
}
public Rational getaddfract (Rational r)
{
int num = (numerator * r.denominator)+(r.numerator * denominator);
// r. = rational from second rational
int den = (denominator*r.denominator);
Rational result = new Rational(num, den);
return result;
} // end add method
public Rational getsubfract( Rational r)
{
int num = (numerator * r.denominator)+(r.numerator * denominator);
int den = (denominator * r.denominator);
Rational result = new Rational(num,den);
return result;
} // end sutraction method
public Rational gettimesfract (Rational r)
{
int num = ( numerator * r.numerator);
int den = ( denominator * r.denominator);
Rational result = new Rational (num,den);
return result;
} // end times method
public Rational getdividedfract (Rational r)
{
int num = (numerator / r.denominator);
int den = (denominator / r.numerator);
Rational result = new Rational (num,den);
return result;
}// end divide method
public String toString()
{
return numerator+"/"+denominator+"\n";
}
public String toString(int dec)
{
DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits( dec);
df.setMinimumFractionDigits( dec);
return df.format(((int)numerator)/((int)denominator));
}
}