Hello, How can I set this menu to ask the user to enter the fractions they wish
ID: 3840049 • 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!
"
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
PROGRAM:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Menu
{
//main method
public static void main(String[] args) {
//declaring integer variables choice and ch
int choice=0;
int ch=0;
//to read the user input
InputStreamReader st=new InputStreamReader(System.in);
BufferedReader scan=new BufferedReader(st);
do{
try
{
System.out.println("Enter 1 to enter Fraction 1:");
int fraction1=Integer.parseInt(scan.readLine());
System.out.println("Enter 2 to enter Fraction 2:");
int fraction2=Integer.parseInt(scan.readLine());
Fraction fobj1 =new Fraction(fraction1,fraction2);
//menus with different functions to perform
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: ");
//getting the user choice
//if the user input a value other than integer,it will throw an exception
choice=Integer.parseInt(scan.readLine());
switch(choice)
{
case 1:
fobj1.add();// add funtion calling
break;
case 2:
fobj1.substract();// substract funtion calling
break;
case 3:
fobj1.divide();// divide funtion calling
break;
case 4:
fobj1.multiply(fobj1);// multiply function calling
break;
default:
System.out.println("Wrong choice.");
}
}
catch(Exception e)
{
System.out.println("Enter a valid choice");
}
System.out.println("Enter 0 to exit / press any other number to continue: ");
//if the user input a value other than integer,it will throw an exception
try
{
ch=Integer.parseInt(scan.readLine());
}
catch(Exception e)
{
ch=1;
System.out.println("Restarting the menu");
}
}while(ch!=0);
System.out.println("Exit!!Thank You.");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.