Question: Convert to an if loop sFract.replaceAll(o, 0); sFract.replaceAll(l, 1); int pos = sFract.indexOf(' '); int pos = sFract.indexOf('/'); sNum = sNum.trim(); int iNum = Integer.parseInt(sNum);
Convert to an if loop
sFract.replaceAll("o", "0"); sFract.replaceAll("l", "1");
int pos = sFract.indexOf(' '); int pos = sFract.indexOf('/');
sNum = sNum.trim(); int iNum = Integer.parseInt(sNum);
String sNum = sFract.substring(0, pos); sNum = sNum.trim(); String sDen = sFract.substring(pos + 1); int iDen = Integer.parseInt(sDen);
////////////////////////////////////////////////////////////////////////////////
Code:
import java.util.Scanner;
class Utility{
static Fraction inputFraction(){ System.out.println("Enter a fraction > ");
Fraction f = new Fraction(); Scanner myObj = new Scanner(System.in); String sFract = myobj.nextLine();
sFract = sFract.trim();
sFract.replaceAll("o", "0"); sFract.replaceAll("l", "1");
int pos = sFract.indexOf(' '); int pos = sFract.indexOf('/');
sNum = sNum.trim(); int iNum = Integer.parseInt(sNum);
String sNum = sFract.substring(0, pos); sNum = sNum.trim(); String sDen = sFract.substring(pos + 1); int iDen = Integer.parseInt(sDen);
}
static int findGCD(int i, int j){ // find gcd of 2 numbers i and j // the larger number is reduced by the smaller // repeat until they are the same while (i != j){ if (i > j) i = i - j; else j = j - i; } //while return i; }//findGCD
static Fraction reduce(int m, int n){ int g = findGCD(m, n); m = m / g; n = n / g;
Fraction f = new Fraction(m, n); return f; }
static Fraction inputReduce(int n, int d){ int m; int g = findGCD(n, d); n = n / g; d = d / g; m = n / d; n = n % d;
Fraction = new Fraction(m, n, d); return f; } static Fraction mixedReduce(int n, int d, int m){ int g = findGCD(n, d); n = n / g; d = d / g;
Fraction f = new Fraction(n, d, m); return f; }
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
