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: 3840271 • 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

THE MENU CAN BE CREATED USING JAVA SWING

import javax.swing.*;

import java.awt.FlowLayout;

import java.awt.event.*;

import javax.swing.JComboBox;

import javax.swing.JFrame;

import javax.swing.JOptionPane;

public class FractionFrame extends JFrame {

    Fractionc calculator = new Fraction();

    JTextField frac1 = new JTextField(3);

    JTextField frac2 = new JTextField(3);

    JTextField opp = new JTextField(1);

    JTextField results = new JTextField(40);

                // create a list of operations

    String[] operationList = {"+", "-", "*", "/"};

    //create a combo-box

    JComboBox operactionSelection = new JComboBox(operationList);

   

    JButton but = new JButton("Calculate");

   

   

    public FractionFrame(){

      setLayout(new FlowLayout(FlowLayout.LEFT, 10,20));

        setTitle("Javier Chavez Fraction Calculator GUI - P3");

        setSize(640,200); //width height

        setLocationRelativeTo(null); //center on screen

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //exit otherwise cmd-c

        //add elements to the container

        add(new JLabel(" Fraction1"));

        add(frac1);

        add(new JLabel(" Fraction 2"));

        add(frac2);

        add(new JLabel("choice"));

        add(operactionSelection);

        add(but);

        add(new JLabel("Results"));

        //add results

        add(results);

       

      //add a listener to the ok btn

        btnOk.addActionListener(

            new ActionListener(){

                public void actionPerformed(ActionEvent e){

                    //method

                    press();

                }

            }

        );

    }//end constructor

    //when the button is clicked

    void press(){

        String resultOut, opperation, fraction1, fraction2;

        //

        fraction1 = frac1.getText();

        fraction2 = frac2.getText();

        opperation = (String)operactionSelection.getSelectedItem();

        //check for errors and handle them

        try{

            //set user input

            calculator.setInputData(fraction1, fraction2, opperation);

            resultOut = calculator.toString();

            //get results

            results.setText(resultOut);

            //catch division / 0

        }catch(ArithmeticException ex){

            results.setText("Sorry, you cannot divide by zero.");

            //show message

             JOptionPane.showMessageDialog(null,"Sorry dividing by 0 is not allowed", "Error", JOptionPane.OK_OPTION);

        }

    }

import javax.swing.JComboBox;

import javax.swing.JFrame;

import javax.swing.JOptionPane;

public class FractionFrame extends JFrame {

    Fractionc calculator = new Fraction();

    JTextField frac1 = new JTextField(3);

    JTextField frac2 = new JTextField(3);

    JTextField opp = new JTextField(1);

    JTextField results = new JTextField(40);

                // create a list of operations

    String[] operationList = {"+", "-", "*", "/"};

    //create a combo-box

    JComboBox operactionSelection = new JComboBox(operationList);

   

    JButton but = new JButton("Calculate");

   

   

    public FractionFrame(){

      setLayout(new FlowLayout(FlowLayout.LEFT, 10,20));

        setTitle("Javier Chavez Fraction Calculator GUI - P3");

        setSize(640,200); //width height

        setLocationRelativeTo(null); //center on screen

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //exit otherwise cmd-c

        //add elements to the container

        add(new JLabel(" Fraction1"));

        add(frac1);

        add(new JLabel(" Fraction 2"));

        add(frac2);

        add(new JLabel("choice"));

        add(operactionSelection);

        add(but);

        add(new JLabel("Results"));

        //add results

        add(results);

       

      //add a listener to the ok btn

        btnOk.addActionListener(

            new ActionListener(){

                public void actionPerformed(ActionEvent e){

                    //method

                    press();

                }

            }

        );

    }//end constructor

    //when the button is clicked

    void press(){

        String resultOut, opperation, fraction1, fraction2;

        //

        fraction1 = frac1.getText();

        fraction2 = frac2.getText();

        opperation = (String)operactionSelection.getSelectedItem();

        //check for errors and handle them

        try{

            //set user input

            calculator.setInputData(fraction1, fraction2, opperation);

            resultOut = calculator.toString();

            //get results

            results.setText(resultOut);

            //catch division / 0

        }catch(ArithmeticException ex){

            results.setText("Sorry, you cannot divide by zero.");

            //show message

             JOptionPane.showMessageDialog(null,"Sorry dividing by 0 is not allowed", "Error", JOptionPane.OK_OPTION);

        }

    }

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