Question: For Java Fraction calculator: You will start by prompting the user for two fractions. The inputs should be two strings in the form #/#. You
For Java
Fraction calculator: You will start by prompting the user for two fractions. The inputs should be two strings in the form #/#. You must account for both positive and negative numerators. Therefor (-#/#) is also a valid input. In a separate function create a Fraction Class that will create two instances of an Object then calculate the following. Addition, Subtraction, Multiplication and Division of the two fractions. Then display the results in a formatted table. All outputted fractions must be in lowest terms or whole numbers if possible. You may make as many additional function as needed.
This is my code so far, I'm not quite sure how to get it to print though when object oriented java
class Fraction:
public class Fraction { int num; //numerator int den; // denominator int results; public static int lowestTerms(int a, int b) { if (b == 0) { return a; } return lowestTerms(b, a%b); } //end of lowestTerms public void add (Fraction f1, Fraction f2) { den = f1.num*f2.den; num = (f1.num*f2.den) + (f2.num*f1.den); int l = lowestTerms(num, den); num = num/l; den = den/l;
} //end of add
public void subtract (Fraction f1, Fraction f2) { den = f1.den*f2.den; num = (f1.num*f2.den) - (f2.num*f1.den); int l = lowestTerms(num, den); num = num/l; den = den/l; } //end of subtract public void multiply (Fraction f1, Fraction f2) { den = f1.den*f2.den; num = f1.num*f2.num; int l = lowestTerms(num, den); num = num/l; den = den/l; } // end of multiply
public void divide (Fraction f1, Fraction f2) { den = f1.den*f2.num; num = f2.num*f1.den; int l = lowestTerms(num, den); num = num/l; den = den/l; } // end of divide public static void printFraction(){ }
} // end of class
Main class:
public static void fraction () { int num, den, num2, den2; Scanner s = new Scanner(System.in); System.out.println("Please enter a fraction: "); String fr = s.nextLine(); String[] frac = fr.split("/"); num = Integer.parseInt(frac[0]); // numerator of the first fraction den = Integer.parseInt(frac[1]); // denominator of the first fraction // second fraction System.out.println("Please enter another fraction: "); String fr2 = s.nextLine(); String[] frac2 = fr2.split("/"); num2 = Integer.parseInt(frac2[3]); // numerator of the second fraction den2 = Integer.parseInt(frac2[4]); // denominator of the second fraction System.out.printf("Fraction 1\t Fraction 2\tAddition\tSubtraction\tMuliplication\tDivision "); Fraction f1; // declaring the first fraction f1 = new Fraction(); //instantiate the first fraction f1.num = num; f1.den = den; Fraction f2; f2 = new Fraction(); f2.num = num2; f2.den = den2;
Fraction result = new Fraction(); result.add(f1, f2); // addition result.printFraction(); // prints the fractions result.subtract(f1, f2); // subtraction result.printFraction(); result.multiply(f1, f2); // multiplication result.printFraction(); result.divide(f1, f2); // division result.printFraction(); } //end of fraction
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
