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

Hello, How can I set this menu to ask the user to enter the fractions they wish

ID: 3840648 • Letter: H

Question

Hello, How can I set this menu to ask the user to enter the fractions they wish to add, subtract, multiply, or divide instead of hard coding the fraction in. Thank you! im not looking for anything super complicated, I just need something simple.

"

public class Main {


public static void main(String[] args) {
Fraction fobj =new Fraction();
Fraction fobj1 =new Fraction(2,4);
int choice,ch;
Scanner scan=new Scanner(System.in);
do{
System.out.println("MENU");
System.out.println("Enter 1 to enter Fraction 1:");
System.out.println("Enter 2 to enter Fraction 2:");
System.out.println("Enter 3 for Addition.");
System.out.println("Enter 4 for Substraction.");
System.out.println("Enter 5 for Multiplication.");
System.out.println("Enter 6 for Division.");
System.out.println("Enter your choice: ");
choice=scan.nextInt();
switch(choice){
case 1:
  
break;
  
case 2:
  
break;
  
case 3:
fobj.Add(fobj1); //calling the Add function
break;
case 4:
fobj.Subtract(fobj1);
break;
case 5:
fobj.Divide(fobj1);
break;
case 6:
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.");
}
}
}

Explanation / Answer

You can ask user for num and den separately and pass them as arguments to the fraction object.

For example, ask user the value of num;

and then set the num by calling fobj.setNumerator(num);

public class Main {


public static void main(String[] args) {
Fraction fobj =new Fraction();
Fraction fobj1 =new Fraction();
int choice,ch,n,d;
Scanner scan=new Scanner(System.in);
do{
System.out.println("MENU");
System.out.println("Enter 1 to enter Fraction 1:");
System.out.println("Enter 2 to enter Fraction 2:");
System.out.println("Enter 3 for Addition.");
System.out.println("Enter 4 for Substraction.");
System.out.println("Enter 5 for Multiplication.");
System.out.println("Enter 6 for Division.");
System.out.println("Enter your choice: ");
choice=scan.nextInt();
switch(choice){
case 1:
System.out.println(“Enter the value of numerator: ”);

n=scan.nextInt();


System.out.println(“Enter the value of denominator: ”);

d=scan.nextInt();  

fobj.setNumerator(n);

fobj.setDenominator(d);
break;
  
case 2:


System.out.println(“Enter the value of numerator: ”);

n=scan.nextInt();


System.out.println(“Enter the value of denominator: ”);

d=scan.nextInt();  

fobj1.setNumerator(n);

fobj1.setDenominator(d);
  
break;
  
case 3:
fobj.Add(fobj1); //calling the Add function
break;
case 4:
fobj.Subtract(fobj1);
break;
case 5:
fobj.Divide(fobj1);
break;
case 6:
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.");
}
}
}

However, please make sure that your fraction class has some method like this

public void setNumerator(int num)

   {

numerator=num;

   }

public void setDenominator(int num)

   {

denominator=num;

   }

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