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 :

advertisement
advertisement
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.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
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.

advertisement
Runtime Test Cases

Here’s the run time test cases to perform arithmetic operations.

Test case 1 – To perform Addition.
java-program-arithmetic-operations-add

Test case 2 – To perform Subtraction.
java-program-arithmetic-operations-subtract

Test case 3 – To perform Multiplication.
java-program-arithmetic-operations-multiply

advertisement

Test case 4 – To perform Division.
java-program-arithmetic-operations-divide

Sanfoundry Global Education & Learning Series – Java Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.