This is a Java Program to Perform Menu Driven Arithmetic Operations Using Switch Case
We have to write a program in Java such that it creates two input fields for two numbers, and contains four buttons for the operations – Addition, Subtraction, Multiplication and Division. When the user clicks on any button after entering the value in the two input field, the respective operation is performed and the value is displayed in an output text field.
For performing arithmetic operations, we have the following different sets of input and output.
1. When Addition is Selected :
If the user selects the Add option, it is expected that the sum of the numbers is displayed. For example, if we are entering the first number 10.5 and the second number is 20. then addition of 2 numbers is 10.5 + 20 = 30.5
2. When Subtraction is Selected :
If the user selects the Subtract option, it is expected that the difference of the numbers is displayed. For example, if we are entering first number 10.5 and the second number is 20. then Subtraction of 2 numbers is 10.5 - 20 = -9.5
3. When Multiplication is Selected :
If the user selects the Multiply option, it is expected that the product of the numbers is displayed. For example, if we are entering the first number 10.5 and the second number is 20. then product of 2 numbers is 10.5 * 20 = 210
4. When Division is Selected :
If the user selects the Divide option, it is expected that the quotient of the numbers when divided is displayed. For example, if we are entering the first number 10.5 and the second number is 20. then division of 2 numbers is 10.5/20 = 0.525
1. Create a frame with two input fields for the two numbers.
2. Create four buttons – Add, Subtract, Multiply and Divide.
3. When any button is clicked, perform the operation using switch case.
4. Display the result in an output text field.
Here is source code of the Java Program to perform arithmetic operations. The program is successfully compiled and tested using javac compiler on Fedora 30. The program output is also shown below.
/*Java Program to Perform Arithmetic Operations Using Switch Case*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Arithmetic_Operations implements ActionListener
{
static JFrame frame;
static JTextField text1,text2;
static JTextField out;
//Driver function
public static void main(String args[])
{
//Create a frame
frame = new JFrame("Aritmetic Operations");
frame.setSize(500,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setBackground(Color.white);
frame.setLayout(null);
//Create an object
Arithmetic_Operations obj = new Arithmetic_Operations();
//Input field for first number
JLabel label1 = new JLabel("First Number : ");
label1.setBounds(100,50,150,40);
frame.add(label1);
text1 = new JTextField(5);
text1.setBounds(250,50,100,40);
frame.add(text1);
//Input field for second number
JLabel label2 = new JLabel("Second Number : ");
label2.setBounds(100,150,150,40);
frame.add(label2);
text2 = new JTextField(5);
text2.setBounds(250,150,100,40);
frame.add(text2);
//Create four buttons
JButton add = new JButton("Add");
add.setBounds(30,250,100,50);
JButton sub = new JButton("Subtract");
sub.setBounds(140,250,100,50);
JButton mul = new JButton("Multiply");
mul.setBounds(250,250,100,50);
JButton div = new JButton("Divide");
div.setBounds(360,250,100,50);
//Add ActionListener to all buttons
add.addActionListener(obj);
sub.addActionListener(obj);
mul.addActionListener(obj);
div.addActionListener(obj);
//Add the buttons to frame
frame.add(add);
frame.add(sub);
frame.add(mul);
frame.add(div);
//Create output text field
out = new JTextField();
out.setBounds(150,350,200,100);
frame.add(out);
//Display frame
frame.setVisible(true);
}
//Perform the respective operation
public void actionPerformed(ActionEvent e)
{
//Get the button
String button = e.getActionCommand();
//Get the numbers
double num1=Double.valueOf(text1.getText());
double num2=Double.valueOf(text2.getText());
switch(button)
{
case "Add" :
out.setText(num1+" + "+num2+" = "+(num1+num2));
break;
case "Subtract" :
out.setText(num1+" - "+num2+" = "+(num1-num2));
break;
case "Multiply" :
out.setText(num1+" * "+num2+" = "+(num1*num2));
break;
case "Divide" :
out.setText(num1+" / "+num2+" = "+(num1/num2));
}
}
}
1. Add the components to the frame at appropriate position.
2. To get the label of button clicked, use getActionCommand.
3. Obtain the text entered in the input fields and convert to double data type using
Double.valueOf.
Here’s the run time test cases to perform arithmetic operations.
Test case 1 – To perform Addition.
Test case 2 – To perform Subtraction.
Test case 3 – To perform Multiplication.
Test case 4 – To perform Division.
Sanfoundry Global Education & Learning Series – Java Programs.
If you find any mistake above, kindly email to [email protected]- Practice Programming MCQs
- Apply for Computer Science Internship
- Apply for Java Internship
- Check Programming Books
- Practice BCA MCQs