Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Create this gui on java that supports rational calculations using this code as the logic, make it look like the picture below. Will thumbs up
Create this gui on java that supports rational calculations using this code as the logic, make it look like the picture below. Will thumbs up
.
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"
)
;
}
/
/
reduce fraction
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;
}
/
/
return
{
-
,
0
,
+
}
if a
<
b
,
a
=
b
,
or a
>
b
/
/
a
.
compareTo
(
b
)
<
0
if a
<
b
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;
}
student submitted image, transcription available
.
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"
)
;
}
/
/
reduce fraction
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;
}
/
/
return
{
-
,
0
,
+
}
if a
<
b
,
a
=
b
,
or a
>
b
/
/
a
.
compareTo
(
b
)
<
0
if a
<
b
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;
}
student submitted image, transcription available
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started