Question: Please implement regex into this GUI calculation code snippet so that the operation actually calculates and recognizes the inputs for each textfield as a rational

Please implement regex into this GUI calculation code snippet so that the operation actually calculates and recognizes the inputs for each textfield as a rational number (things like 21/2 or 1/2). First code is what to edit and implement regex into, second is where all the calculations are, and the picture is what my gui currently does. Apparently the best way is to "write a method that takes a string as input which is supposed to represent a rational number and return a two element arrary of ints." But I'm struggling, will thumbs up!
private void doCalculation(String operation, TextField tfA, TextField tfB, TextField tfC){
var output = String.format("%s %s %s =?
", tfA.getText(), operation, tfB.getText());
tfC.setText(output);
}
__________________________________________________________________________________________________
public class Rational extends Object implements Comparable {
private final int num;
private final int den;
public final static Rational ZERO = new Rational(0);
public final static Rational ONE = new Rational(1);
public int getNumerator(){
return num;
}
public int getDenominator(){
return den;
}
public Rational(){
this(0,1);
}
public Rational(int num){
this(num,1);
}
public Rational(int[] fract){
this(fract[0], fract[1]);
}
public Rational(int numerator, int denominator){
if (denominator ==0){
throw new ArithmeticException("denominator is zero");
}
int g = gcf(numerator, denominator);
if (denominator >0){
this.num = numerator / g;
this.den = denominator / g;
} else {
this.num =-numerator / g;
this.den =-denominator / g;
}
}
public Rational times(Rational b){
Rational a = this;
int num = a.num * b.num;
int den = a.den * b.den;
return new Rational(num, den);
}
public Rational plus(Rational b){
Rational a = this;
if (a.equals(Rational.ZERO))
return b;
if (b.compareTo(Rational.ZERO)==0)
return a;
int num = a.num * b.den + a.den * b.num;
int den = a.den * b.den;
return new Rational(num, den);
}
public Rational negate(){
return new Rational(-this.num, this.den);
}
public Rational minus(Rational b){
Rational a = this;
return a.plus(b.negate());
}
public Rational divides(Rational b){
Rational a = this;
return a.times(b.reciprocal());
}
public Rational reciprocal(){
return new Rational(this.den, this.num);
}
public Rational abs(){
if (this.num >=0)
return this;
else
return this.negate();
}
public double toDouble(){
return (double)this.num / this.den;
}
@Override
public boolean equals(Object obj){
if (this == obj)
return true;
if (obj == null)
return false;
if (this.getClass()!= obj.getClass())
return false;
Rational other =(Rational) obj;
if (den != other.den)
return false;
if (num != other.num)
return false;
return true;
}
public String toString(){
if (den ==1)
return num +"";
else
return num +"/"+ den;
}
public int compareTo(Rational b){
Rational a = this;
int lhs = a.num * b.den;
int rhs = a.den * b.num;
if (lhs rhs)
return -1;
if (lhs > rhs)
return 1;
return 0;
}
private static int gcf(int m, int n){
int retval =0;
if (m 0)
m =-m;
if (n 0)
n =-n;
if (n ==0)
retval = m;
else {
int rem;
while ((rem = m % n)!=0){
m = n;
n = rem;
}
retval = n;
}
return retval;
}
}
 Please implement regex into this GUI calculation code snippet so that

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!