Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m creating a program in Java that performs fraction math. First, I prompt the

ID: 3786776 • Letter: I

Question

I'm creating a program in Java that performs fraction math. First, I prompt the user to enter an mathematical expression that contains fractions. The expressions can look like any of the following (and more):

-6 / 2 + -3 / 4 (With spaces)

-6/2*-3/4 (Without spaces)

-6 / 2 / -3 / 4 (With spaces and fraction division)

I've already created methods to perform the fraction math. However, I'm confused on how to parse or split the string and save/set each numerator, denominator and mathematical operator (+, -, *, /) as variables. I've tried methods such as StringTokenizer and .split(), but I keep using them incorrectly. Again, all of my methods to perform the fraction math are already created. Here's some of my code:

import java.util.*;
import java.util.Scanner;

public static void main(String[] args) {
   String expression1;
   int numerator1;
   int numerator2;
   int denominator1;
   int denominator2;
   char operator;
  
   Scanner scanner = new Scanner(System.in);
   System.out.print("Please enter an expression that contains fractions: ");   // -6/2*-3/4
   expression1 = scanner.nextLine();
}

Explanation / Answer

you can use the below program,

public class FractionSimplifier {

private int numer, denom;

//constructors
public FractionSimplifier(){
int num = 1;
int den = 2;
reduce();
}
public FractionSimplifier(int num, int den){
numer = num;
denom = den;
reduce();
}
public FractionSimplifier(FractionSimplifier x){
numer = x.numer;
denom = x.denom;
reduce();
}

//setters
public void setNumer(int num){
numer = num;
reduce();
}
public void setDenom(int den){
denom = den;
reduce();
}
public void setFractionSimplifier(int num, int den){
numer = num;
denom = den;
reduce();
}

//getters
public int getNumer(){
return numer;
}
public int getDenom(){
return denom;
}

//Copy method
public void copyFrom(FractionSimplifier x){
numer = x.numer;
denom = x.denom;
reduce();
}

//Equals method
public boolean equals(FractionSimplifier x){
if (numer / denom == x.numer / x.denom){
return(true);
}
else {
return(false);
}
}

//Compare to method
public int compareTo(FractionSimplifier x){
if (numer / denom == x.numer / x.denom){
return (0);
}
else if (numer / denom < x.numer / x.denom){
return (-1);
}
else{
return (1);
}
}

//Find greatest common divisor
static int gcd(int x, int y){
int r;
while (y != 0) {
r = x % y;
x = y;
y = r;
}
return x;
}

//FractionSimplifier Addition
public void plus(FractionSimplifier x){
int greatdenom = x.denom * denom;   
int multx = greatdenom / x.denom;
int mult = greatdenom / denom;
denom = x.denom * denom;
numer = (x.numer * multx) + (numer * mult);
reduce();
}

//FractionSimplifier Subtraction
public void minus(FractionSimplifier x){
int greatdenom = x.denom * denom;   
int multx = greatdenom / x.denom;
int mult = greatdenom / denom;
denom = x.denom * denom;
if (x.numer > numer){
numer = (x.numer * multx) - (numer * mult);
}
else {
numer = (numer * mult) - (x.numer * multx);
}
reduce();
}

//Multiplication   
public void times(FractionSimplifier x){
numer = numer * x.numer;
denom = denom * x.denom;
reduce();
}

//Division
public void divBy(FractionSimplifier x){
numer = numer / x.numer;
denom = denom / x.denom;
reduce();
}

//Fraction simplifier
private void reduce(){
int divisor;
divisor = FractionSimplifier.gcd(numer, denom);
numer = numer / divisor;
denom = denom / divisor;
}

@Override
public String toString(){
if (denom == 1){
return numer + "";
}
else{
return numer + " / " + denom;
}   
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote