# Java Program to Perform Menu Driven Arithmetic Operations Using Switch Case

«
»

This is a Java Program to Perform Menu Driven Arithmetic Operations Using Switch Case

Problem Description

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.

Expected Input and Output

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```
Problem Solution

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.

Program/Source Code

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.

1. `/*Java Program to Perform Arithmetic Operations Using Switch Case*/`
2. `import javax.swing.*;`
3. `import java.awt.*;`
4. `import java.awt.event.*;`
5. `class Arithmetic_Operations implements ActionListener`
6. `{`
7. `    static JFrame frame;`
8. `    static JTextField text1,text2;`
9. `    static JTextField out; `
10. `    //Driver function`
11. `    public static void main(String args[])`
12. `    {`
13. `	//Create a frame`
14. `	frame = new JFrame("Aritmetic Operations");`
15. `	frame.setSize(500,500);`
16. `	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);`
17. `	frame.getContentPane().setBackground(Color.white);`
18. `	frame.setLayout(null);`
19. `	//Create an object`
20. `	Arithmetic_Operations obj = new Arithmetic_Operations();`
21. `	//Input field for first number`
22. `	JLabel label1 = new JLabel("First Number : ");`
23. `	label1.setBounds(100,50,150,40);`
24. `	frame.add(label1);`
25. `	text1 = new JTextField(5);`
26. `	text1.setBounds(250,50,100,40);`
27. `	frame.add(text1);`
28. `	//Input field for second number`
29. `	JLabel label2 = new JLabel("Second Number : ");`
30. `	label2.setBounds(100,150,150,40);`
31. `	frame.add(label2);`
32. `	text2 = new JTextField(5);`
33. `	text2.setBounds(250,150,100,40);`
34. `	frame.add(text2);`
35. `	//Create four buttons`
36. `	JButton add = new JButton("Add");`
37. `	add.setBounds(30,250,100,50);`
38. `	JButton sub = new JButton("Subtract");`
39. `	sub.setBounds(140,250,100,50);`
40. `	JButton mul = new JButton("Multiply");`
41. `	mul.setBounds(250,250,100,50);`
42. `	JButton div = new JButton("Divide");`
43. `	div.setBounds(360,250,100,50);`
44. `	//Add ActionListener to all buttons`
45. `	add.addActionListener(obj);`
46. `	sub.addActionListener(obj);`
47. `	mul.addActionListener(obj);`
48. `	div.addActionListener(obj);`
49. `	//Add the buttons to frame`
50. `	frame.add(add);`
51. `	frame.add(sub);`
52. `	frame.add(mul);`
53. `	frame.add(div);`
54. `	//Create output text field`
55. `	out = new JTextField();`
56. `	out.setBounds(150,350,200,100);`
57. `	frame.add(out);`
58. `	//Display frame`
59. `	frame.setVisible(true);`
60. `    }`
61. `    //Perform the respective operation`
62. `    public void actionPerformed(ActionEvent e)`
63. `    {`
64. `	//Get the button`
65. `	String button = e.getActionCommand();`
66. `	//Get the numbers`
67. `	double num1=Double.valueOf(text1.getText());`
68. `	double num2=Double.valueOf(text2.getText());`
69. `	switch(button)`
70. `	{`
71. `	    case "Add" :`
72. `	        out.setText(num1+" + "+num2+" = "+(num1+num2));`
73. `		break;`
74. `	    case "Subtract" :`
75. `		out.setText(num1+" - "+num2+" = "+(num1-num2));`
76. `		break;`
77. `	    case "Multiply" :`
78. `		out.setText(num1+" * "+num2+" = "+(num1*num2));`
79. `		break;`
80. `	    case "Divide" :`
81. `		out.setText(num1+" / "+num2+" = "+(num1/num2));`
82. `	}`
83. `    }`
84. `}`
Program Explanation

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.

Runtime Test Cases

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. 