Question
Hi can someone help me design a user menu for this fraction class? I am having a hard time of thinking of how to do
Hi can someone help me design a user menu for this fraction class? I am having a hard time of thinking of how to do it and could use some fresh eyes.
Here is my fraction Class in qoutes below, Remember I need help with designing the menu.
"public class Fraction {
private long numerator; private long denominator;
public Fraction( ) {
numerator = 1; denominator = 1;
} public Fraction( long numerator, long denominator ) {
this.numerator = numerator;
if ( denominator == 0 ) { System.out.println("WARNING: Fraction( long, long ) Constructor"); System.out.println(" A value of zero for denominator detected."); System.out.println(" The value has been changed to one."); this.denominator = 1;
}
else
this.denominator = denominator;
Reduce();
} public Fraction( Fraction fraction ) {
if (fraction == null) { System.out.println("Fraction Constructor: Deep Copy Error."); System.exit( 0 );
}
numerator = fraction.numerator; denominator = fraction.denominator;
Reduce();
} public void Add( Fraction that ) {
long a; long b; long c; long d;
a = this.numerator; b = this.denominator;
c = that.numerator;
d = that.denominator;
this.numerator = a*d + c*b; this.denominator = b*d; Reduce();
} public void Subtract( Fraction that ) {
long a; long b; long c; long d;
a = this.numerator; b = this.denominator;
c = that.numerator;
d = that.denominator;
this.numerator = a*d - c*b; this.denominator = b*d; Reduce();
} public void Divide( Fraction that ) {
long a; long b; long c; long d;
a = this.numerator; b = this.denominator;
c = that.numerator; d = that.denominator;
this.numerator = a*d; this.denominator = c*d; Reduce();
} public void Multiply( Fraction that ) {
long a; long b; long c; long d;
a = this.numerator; b = this.denominator;
c = that.numerator;
d = that.denominator;
this.numerator = a*c; this.denominator = b*d; Reduce();
} public void SetNumerator( long numerator ) {
this.numerator = numerator; Reduce();
} public void SetDenominatortor( long denominator ) {
if ( denominator == 0 ) { System.out.println("WARNING: SetDenominatortor( long ) Mutator Method."); System.out.println(" A value of zero for denominator detected."); System.out.println(" The value has been changed to one."); this.denominator = 1;
}
else
this.denominator = denominator;
Reduce();
} public void Set( Fraction fraction ) {
numerator = fraction.numerator; denominator = fraction.denominator; Reduce();
}
public double GetNumerator( ) {
return numerator;
}
public double GetDenominator( ) {
return denominator;
}
public Fraction Get( ) {
return new Fraction(numerator, denominator);
}
@Override public String toString( ) {
return numerator + "/" + denominator;
} @Override
public int hashCode( ) {
int hash = 7;
hash = 41 * hash + (int) (this.numerator ^ (this.numerator >>> 32)); hash = 41 * hash + (int) (this.denominator ^ (this.denominator >>> 32));
return hash;
} @Override
public boolean equals(Object otherObject ) {
if ( otherObject == null ) return false;
else if ( getClass() != otherObject.getClass() )
return false;
else {
Fraction otherFraction = (Fraction)otherObject;
return ( (numerator == otherFraction.numerator) &&
(denominator == otherFraction.denominator) );
}
} private long GreatestCommonDivisor( long a, long b ) {
if ( a%b == 0 ) return b; else return GreatestCommonDivisor(b, a%b);
} private long IterativeGCD( long a, long b ) {
if ( a == 0 ) return b;
while ( b != 0 ){
if ( a > b ) a = a - b; else b = b - a;
} // while return a;
}
private void Reduce( ) {
long gcd = GreatestCommonDivisor( numerator, denominator );
numerator = numerator/gcd;
denominator = denominator/gcd; }
}"
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