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

Hi can someone help me design a user menu for this fraction class? I am having a

ID: 3826030 • Letter: H

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 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;
}

}"

Explanation / Answer

public static void main(String[] args)throws IOException{
        Fraction fobj =new Fraction();//calling default constructor;
  Fraction fobj1 =new Fraction(2,4);//calling paramenterised constructior
        int choice,ch;
        Scanner scan=new Scanner(System.in);
        do{
            System.out.println("MENU");
            System.out.println("Enter 1 for Addition.");
            System.out.println("Enter 2 for Substraction.");
            System.out.println("Enter 3 for Multiplication.");
            System.out.println("Enter 4 for Division.");
            System.out.println("Enter your choice: ");
            choice=scan.nextInt();
            switch(choice){
                case 1:
    fobj.Add(fobj1); //calling the Add function
    ;break;
                case 2:
    fobj.Subtract(fobj1);
    break;
                case 3:
     fobj.Divide(fobj1);
     break;
                case 4:
     fobj.Multiply(fobj1);
     break;
                default: System.out.println("Wrong choice.");
            }
            System.out.println("Enter 0 to exit: ");
            ch=scan.nextInt();
        }
        while(ch!=0);
        if(ch==0){
            System.out.println("Exit!!Thank You.");
        }
  }

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