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 or 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 Im struggling, will thumbs up
private void doCalculationString operation, TextField tfA, TextField tfB TextField tfC
var output String.formats s s
tfA.getText operation, tfBgetText;
tfCsetTextoutput;
public class Rational extends Object implements Comparable
private final int num;
private final int den;
public final static Rational ZERO new Rational;
public final static Rational ONE new Rational;
public int getNumerator
return num;
public int getDenominator
return den;
public Rational
this;
public Rationalint num
thisnum;
public Rationalint fract
thisfract fract;
public Rationalint numerator, int denominator
if denominator
throw new ArithmeticExceptiondenominator is zero";
int g gcfnumerator denominator;
if denominator
this.num numerator g;
this.den denominator g;
else
this.num numerator g;
this.den denominator g;
public Rational timesRational b
Rational a this;
int num anum bnum;
int den aden bden;
return new Rationalnum den;
public Rational plusRational b
Rational a this;
if aequalsRationalZERO
return b;
if bcompareToRationalZERO
return a;
int num anum bden aden bnum;
int den aden bden;
return new Rationalnum den;
public Rational negate
return new Rationalthis.num, this.den;
public Rational minusRational b
Rational a this;
return aplusbnegate;
public Rational dividesRational b
Rational a this;
return atimesbreciprocal;
public Rational reciprocal
return new Rationalthisden, this.num;
public Rational abs
if thisnum
return this;
else
return this.negate;
public double toDouble
return doublethisnum this.den;
@Override
public boolean equalsObject obj
if this obj
return true;
if obj null
return false;
if thisgetClass 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
return num ;
else
return num den;
public int compareToRational b
Rational a this;
int lhs anum bden;
int rhs aden bnum;
if lhs rhs
return ;
if lhs rhs
return ;
return ;
private static int gcfint m int n
int retval ;
if m
m m;
if n
n n;
if n
retval m;
else
int rem;
while rem m n
m n;
n rem;
retval n;
return retval;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
