{"id":243,"date":"2017-12-04T02:41:00","date_gmt":"2017-12-04T02:41:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/2017\/12\/04\/how-to-build-a-simple-calculator-in-java-using-netbeans-step-by-step-with-screenshots\/"},"modified":"2020-11-05T12:59:12","modified_gmt":"2020-11-05T11:59:12","slug":"how-to-build-a-simple-calculator-in-java-using-netbeans-step-by-step-with-screenshots","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/how-to-build-a-simple-calculator-in-java-using-netbeans-step-by-step-with-screenshots\/","title":{"rendered":"How to Build a Simple Calculator in Java Using Netbeans &#8211; Step by Step with Screenshots"},"content":{"rendered":"<div style=\"color: #555555; font-size: 18px; line-height: 30px; text-align: justify;\">\n<div style=\"font-family: 'segoe ui';\">This tutorial would take you through the procedure of building a simple calculator in Java. So I would advice, you print it out and follow the instructions.&nbsp; If you are completely new to Java and Netbeans you can take these two lessons:<\/p>\n<ul>\n<li><a href=\"https:\/\/kindsonthegenius.com\/blog\/java-progamming-for-beginners-lesson-1-introduction-to-java-and-intallation-of-netbeans\/\">Java Programming for Beginners &#8211; Lesson 1: Introduction to Java Netbeans Installation<\/a><\/li>\n<li><a href=\"https:\/\/kindsonthegenius.com\/blog\/java-progamming-for-beginners-lesson-2-write-your-first-java-program\/\">Java Programming for Beginners &#8211; Lesson 2: Write Your First Program<\/a><\/li>\n<\/ul>\n<p>You can print out this page so that you can easily follow along (you can also open it in a different display)<br \/><ins data-ad-client=\"ca-pub-7041870931346451\" data-ad-format=\"fluid\" data-ad-layout=\"in-article\" data-ad-slot=\"8227894917\" style=\"display: block; text-align: center;\"><\/ins> &nbsp; <\/p>\n<div style=\"clear: both; text-align: center;\"><a href=\"https:\/\/4.bp.blogspot.com\/-zuRztnFRnBk\/WiS1mgly_3I\/AAAAAAAAAVM\/sas2_5hwZE4klSoHc6HQ1RJxv4uT4v7SACLcBGAs\/s1600\/Build-a-Simple-Calculator-In-Java-Using-Netbeans.jpg\" style=\"margin-left: 1em; margin-right: 1em;\"><img decoding=\"async\" loading=\"lazy\" border=\"0\" data-original-height=\"918\" data-original-width=\"1512\" height=\"388\" src=\"https:\/\/4.bp.blogspot.com\/-zuRztnFRnBk\/WiS1mgly_3I\/AAAAAAAAAVM\/sas2_5hwZE4klSoHc6HQ1RJxv4uT4v7SACLcBGAs\/s640\/Build-a-Simple-Calculator-In-Java-Using-Netbeans.jpg\" width=\"640\" \/><\/a><\/div>\n<p><b>What you would need<\/b><\/p>\n<ul>\n<li>Netbeans IDE<\/li>\n<li>A pen and a paper(optional)<\/li>\n<\/ul>\n<p><b>Step 1<\/b>: Create an Application in Netbeans. Name the application <i>CalculatorProgram<\/i>.<br \/>If you don&#8217;t know how to create new application. You can get it here: <a href=\"https:\/\/kindsonthegenius.com\/blog\/java-progamming-for-beginners-lesson-2-write-your-first-java-program\/\">Your First Java Program<\/a>.<\/p>\n<p><b>Step 2<\/b>: Add a JFrame Form to the Application<br \/>Right-click on the name of the application in the Projects tab by the left of the window and Choose New &gt; JFrame Form<br \/>Enter the name pnlCalculator next to the Class Name and click on Finish. You can see that the form is added as shown below<\/p>\n<div style=\"clear: both;\"><a href=\"https:\/\/3.bp.blogspot.com\/-bu3pJ5HRQKg\/WiSeVAg7A6I\/AAAAAAAAAUI\/HTrsXYgKFCkryLoGMS20_JP0G_1KbD6FwCLcBGAs\/s1600\/Adding%2Ba%2BJFrame%2BForm.JPG\" style=\"margin-left: 1em; margin-right: 1em;\"><img decoding=\"async\" loading=\"lazy\" border=\"0\" data-original-height=\"728\" data-original-width=\"1114\" height=\"418\" src=\"https:\/\/3.bp.blogspot.com\/-bu3pJ5HRQKg\/WiSeVAg7A6I\/AAAAAAAAAUI\/HTrsXYgKFCkryLoGMS20_JP0G_1KbD6FwCLcBGAs\/s640\/Adding%2Ba%2BJFrame%2BForm.JPG\" width=\"640\" \/><\/a><\/div>\n<p><ins data-ad-client=\"ca-pub-7041870931346451\" data-ad-format=\"fluid\" data-ad-layout=\"in-article\" data-ad-slot=\"8227894917\" style=\"display: block; text-align: center;\"><\/ins> <br \/><b>Step 3<\/b>: Add two JPanels to the form and then resize and position them as shown. You can get Panels at the right hand side in the palettes tab.<\/p>\n<div style=\"clear: both;\"><a href=\"https:\/\/3.bp.blogspot.com\/-b0duSz4QG2w\/WiSeU5dDXYI\/AAAAAAAAAUE\/akZmY524biQOhxIhzvZJQG7hDeeBLc_0wCEwYBhgL\/s1600\/Adding%2Btwo%2BJPanels.JPG\" style=\"margin-left: 1em; margin-right: 1em;\"><img decoding=\"async\" loading=\"lazy\" border=\"0\" data-original-height=\"728\" data-original-width=\"1114\" height=\"418\" src=\"https:\/\/3.bp.blogspot.com\/-b0duSz4QG2w\/WiSeU5dDXYI\/AAAAAAAAAUE\/akZmY524biQOhxIhzvZJQG7hDeeBLc_0wCEwYBhgL\/s640\/Adding%2Btwo%2BJPanels.JPG\" width=\"640\" \/><\/a><\/div>\n<p><ins data-ad-client=\"ca-pub-7041870931346451\" data-ad-format=\"fluid\" data-ad-layout=\"in-article\" data-ad-slot=\"8227894917\" style=\"display: block; text-align: center;\"><\/ins> <b>Step 4a<\/b>: Place a TextField in the Form<br \/><i>See Step 4b to delete the jTextField1. <\/i><br \/>You need to place a TextField on the form, by clicking on TextField from the Palette and clicking in the upper part of the form. Drag to position it as shown in the figure below.<\/p>\n<div style=\"clear: both;\"><a href=\"https:\/\/3.bp.blogspot.com\/-72geiVb5VNM\/WiSeUmImUyI\/AAAAAAAAAUA\/rrqZ12y8AaYFPmOxunP6xrl1IZqseKz6wCEwYBhgL\/s1600\/Adding%2Ba%2BTextField.JPG\" style=\"margin-left: 1em; margin-right: 1em;\"><img decoding=\"async\" loading=\"lazy\" border=\"0\" data-original-height=\"728\" data-original-width=\"1114\" height=\"418\" src=\"https:\/\/3.bp.blogspot.com\/-72geiVb5VNM\/WiSeUmImUyI\/AAAAAAAAAUA\/rrqZ12y8AaYFPmOxunP6xrl1IZqseKz6wCEwYBhgL\/s640\/Adding%2Ba%2BTextField.JPG\" width=\"640\" \/><\/a><\/div>\n<p><b>Step 4b: Modify TextField<\/b><br \/>Right-click on the TextField and click on Edit Text. Delete the text there.<br \/>Right-click again and click on &#8216;Change Variable Name&#8230;&#8217;.<br \/>Delete what is there and enter txtResult<\/p>\n<p><ins data-ad-client=\"ca-pub-7041870931346451\" data-ad-format=\"fluid\" data-ad-layout=\"in-article\" data-ad-slot=\"8227894917\" style=\"display: block; text-align: center;\"><\/ins> <b>Step 5<\/b>: Place and&nbsp; Buttons on the Form. (Don&#8217;t change the name here) You can find buttons on the palette tab. Just add the button, resize them until you have enough buttons on the screen<\/p>\n<p><b>Step 6:<\/b> Change the text of the buttons<br \/>To change the name of a button, right click on the button and choose &#8220;Edit Text&#8221;. For button 1, just type 1, for button 2, type 2 and so on exactly as it appears below.<br \/>At the end of this step, your form would look like this<\/p>\n<div style=\"clear: both;\"><a href=\"https:\/\/4.bp.blogspot.com\/-VXX7Ov9wT2s\/WiSeWn5Q1YI\/AAAAAAAAAUs\/BtKbfQLSd7A7AdxTYW-MZxwOt0B74JldACEwYBhgL\/s1600\/Completed%2BForm%2BDesign.JPG\" style=\"margin-left: 1em; margin-right: 1em;\"><img decoding=\"async\" loading=\"lazy\" border=\"0\" data-original-height=\"728\" data-original-width=\"1114\" height=\"418\" src=\"https:\/\/4.bp.blogspot.com\/-VXX7Ov9wT2s\/WiSeWn5Q1YI\/AAAAAAAAAUs\/BtKbfQLSd7A7AdxTYW-MZxwOt0B74JldACEwYBhgL\/s640\/Completed%2BForm%2BDesign.JPG\" width=\"640\" \/><\/a><\/div>\n<p><ins data-ad-client=\"ca-pub-7041870931346451\" data-ad-format=\"fluid\" data-ad-layout=\"in-article\" data-ad-slot=\"8227894917\" style=\"display: block; text-align: center;\"><\/ins> <b>Step 7<\/b>: Change the Variable Names of the buttons.<br \/>To do that, right-click on a button and click &#8220;<i>Change Variable Name<\/i>&#8220;. Change the names according to the outline below. You need to carefully enter the names correctly, otherwise the program may have issues<\/p>\n<p>1-&nbsp; btn1<br \/>2 &#8211; btn2<br \/>3 &#8211; btn3<br \/>4 &#8211; btn4<br \/>5 &#8211; btn5<br \/>6 &#8211; btn6<br \/>7 &#8211; btn7<br \/>8 &#8211; btn8<br \/>9 &#8211; btn9<br \/>0 &#8211; btn0<br \/>+\/-&nbsp; btnPlusMinus<br \/>CE &#8211; btnClear<br \/>+ &#8211;&nbsp; btnPlus<br \/>&#8211;&nbsp;&nbsp;&nbsp;&nbsp; btnMinus<br \/>\/&nbsp;&nbsp; &nbsp; btnDivision<br \/>*&nbsp;&nbsp; &nbsp; btnMultiplication<br \/><ins data-ad-client=\"ca-pub-7041870931346451\" data-ad-format=\"fluid\" data-ad-layout=\"in-article\" data-ad-slot=\"8227894917\" style=\"display: block; text-align: center;\"><\/ins> <b>Step 8: Preview your design<\/b><br \/>Click on the Projects tab, locate the name of the Form. In this case it is <i>pnlCalculator.java<\/i>. Right-click on it and click on <i>Run File.<\/i><\/p>\n<div style=\"clear: both; text-align: center;\"><a href=\"https:\/\/3.bp.blogspot.com\/-uzZdN3DAwOA\/WiStZD4RwnI\/AAAAAAAAAU4\/3duTxV7O0E4mVTBJ3b6aZjJtMBTYGELjACLcBGAs\/s1600\/RunFile.jpg\" style=\"margin-left: 1em; margin-right: 1em;\"><img decoding=\"async\" loading=\"lazy\" border=\"0\" data-original-height=\"768\" data-original-width=\"1366\" height=\"358\" src=\"https:\/\/3.bp.blogspot.com\/-uzZdN3DAwOA\/WiStZD4RwnI\/AAAAAAAAAU4\/3duTxV7O0E4mVTBJ3b6aZjJtMBTYGELjACLcBGAs\/s640\/RunFile.jpg\" width=\"640\" \/><\/a><\/div>\n<p>You wait for a couple of seconds and if you did everything correctly, the form below would be displayed.<\/p>\n<div style=\"clear: both;\"><a href=\"https:\/\/2.bp.blogspot.com\/-pbmhjEWb_UA\/WiSeYRzHTTI\/AAAAAAAAAUs\/eyEfnwyPsYsfjqB7j126rUZKgp3vMM6zQCEwYBhgL\/s1600\/Finished-Form-Run.JPG\" style=\"margin-left: 1em; margin-right: 1em;\"><img decoding=\"async\" loading=\"lazy\" border=\"0\" data-original-height=\"462\" data-original-width=\"537\" height=\"550\" src=\"https:\/\/2.bp.blogspot.com\/-pbmhjEWb_UA\/WiSeYRzHTTI\/AAAAAAAAAUs\/eyEfnwyPsYsfjqB7j126rUZKgp3vMM6zQCEwYBhgL\/s640\/Finished-Form-Run.JPG\" width=\"640\" \/><\/a><\/div>\n<p>You can then close the preview form.<\/p>\n<p><b>Step 9<\/b>: View your source code and locate where you will write your first code. Once in the code view, you can scroll up a little to find the position where you would write your first code.<\/p>\n<p><a href=\"https:\/\/1.bp.blogspot.com\/-Edu9meRB1os\/WiSeZcyWyPI\/AAAAAAAAAUs\/43V1AN3UvcwoSAwFuIwbJ54oMxipYlVowCEwYBhgL\/s1600\/Where%2Bto%2BWrite%2Bthe%2Bfirst%2Bcode.JPG\" style=\"clear: left; float: left; margin-bottom: 1em; margin-right: 1em;\"><img decoding=\"async\" loading=\"lazy\" border=\"0\" data-original-height=\"736\" data-original-width=\"1366\" height=\"344\" src=\"https:\/\/1.bp.blogspot.com\/-Edu9meRB1os\/WiSeZcyWyPI\/AAAAAAAAAUs\/43V1AN3UvcwoSAwFuIwbJ54oMxipYlVowCEwYBhgL\/s640\/Where%2Bto%2BWrite%2Bthe%2Bfirst%2Bcode.JPG\" width=\"640\" \/><\/a><\/p>\n<p><ins data-ad-client=\"ca-pub-7041870931346451\" data-ad-format=\"fluid\" data-ad-layout=\"in-article\" data-ad-slot=\"8227894917\" style=\"display: block; text-align: center;\"><\/ins> <br \/><b>Step 10<\/b>: Type the code below in the position you identified in Step 9. (You can also copy and paste it there)<\/p>\n<p><span style=\"color: #0b5394;\">static int value1;<\/span><br \/><span style=\"color: #0b5394;\">static int value2;<\/span><br \/><span style=\"color: #0b5394;\">static String operator; <\/span><\/div>\n<div style=\"font-family: 'segoe ui';\"><\/div>\n<div style=\"font-family: 'segoe ui';\"><\/div>\n<div style=\"font-family: 'segoe ui';\"><\/div>\n<div style=\"font-family: 'segoe ui';\"><\/div>\n<div style=\"font-family: 'segoe ui';\"><\/div>\n<div style=\"font-family: 'segoe ui';\"><b>Step 11:<\/b>&nbsp; Write the code for Button 1(btn1). Right-click on button 1 &gt; Choose Events &gt; Choose Mouse, Choose MouseClicked.<br \/>This takes you to where you would write the code for button 1<br \/>&nbsp;<a href=\"https:\/\/4.bp.blogspot.com\/-aXCnfQp3p2Y\/WiSeYwKy6SI\/AAAAAAAAAUs\/LYyi7pVK3okfETdRdV1xh9SfuAt9hUYAACEwYBhgL\/s1600\/Go%2Bto%2BButton1%2BAction.jpg\" style=\"margin-left: 1em; margin-right: 1em;\"><img decoding=\"async\" loading=\"lazy\" border=\"0\" data-original-height=\"768\" data-original-width=\"1366\" height=\"358\" src=\"https:\/\/4.bp.blogspot.com\/-aXCnfQp3p2Y\/WiSeYwKy6SI\/AAAAAAAAAUs\/LYyi7pVK3okfETdRdV1xh9SfuAt9hUYAACEwYBhgL\/s640\/Go%2Bto%2BButton1%2BAction.jpg\" width=\"640\" \/><\/a><\/p>\n<p>I have highlighted the position to help you find it(remember you may have to scroll up)<br \/>You would see something like:<br \/><i>Private void btn1MouseClicked&#8230;&#8230;<\/i><br \/>Below the next line that says&#8230;\/\/TODO, you can write your code.<br \/>&nbsp; <\/p>\n<div style=\"clear: both;\"><a href=\"https:\/\/4.bp.blogspot.com\/-CbWZuBRvCbg\/WiSeadm30KI\/AAAAAAAAAUs\/fcIuiawhA9QgeQ-3tjWs-vYdmL5I1UBEwCEwYBhgL\/s1600\/Where-to-write-button-1-code.JPG\" style=\"margin-left: 1em; margin-right: 1em;\"><img decoding=\"async\" loading=\"lazy\" border=\"0\" data-original-height=\"683\" data-original-width=\"1124\" height=\"388\" src=\"https:\/\/4.bp.blogspot.com\/-CbWZuBRvCbg\/WiSeadm30KI\/AAAAAAAAAUs\/fcIuiawhA9QgeQ-3tjWs-vYdmL5I1UBEwCEwYBhgL\/s640\/Where-to-write-button-1-code.JPG\" width=\"640\" \/><\/a><\/div>\n<p><ins data-ad-client=\"ca-pub-7041870931346451\" data-ad-format=\"fluid\" data-ad-layout=\"in-article\" data-ad-slot=\"8227894917\" style=\"display: block; text-align: center;\"><\/ins> <br \/><b>Step 12:<\/b> Now Write the code for button 1. This means that we need to write a code to specify what happens when 1 is clicked. To to that:<br \/>Write the code below in the position as shown in the figure( you can also copy and paste)<br \/><span style=\"color: #0b5394;\"><span style=\"background-color: white;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(txtResult.getText().isEmpty())<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; txtResult.setText(btn1.getText());<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; value1 = 1;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } else {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; txtResult.setText(txtResult.getText()+ &#8221; &#8221; + btn1.getText());<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; value2 = 1; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } <\/span><\/span><br \/>Your code would now look like the one in the figure below:<\/p>\n<div style=\"clear: both;\"><a href=\"https:\/\/2.bp.blogspot.com\/-VBxmnzjeK5M\/WiSeaPUJb0I\/AAAAAAAAAUs\/3IZBzX150IkE3jLsEJh3w4PFcW6qlztEgCEwYBhgL\/s1600\/Where-to-write-button-1-code-written.JPG\" style=\"margin-left: 1em; margin-right: 1em;\"><img decoding=\"async\" loading=\"lazy\" border=\"0\" data-original-height=\"683\" data-original-width=\"1112\" height=\"392\" src=\"https:\/\/2.bp.blogspot.com\/-VBxmnzjeK5M\/WiSeaPUJb0I\/AAAAAAAAAUs\/3IZBzX150IkE3jLsEJh3w4PFcW6qlztEgCEwYBhgL\/s640\/Where-to-write-button-1-code-written.JPG\" width=\"640\" \/><\/a><\/div>\n<p><b>Step 13: Test Your Program<\/b><br \/>You can test your program using the procedure you applied in Step 8. When the form displays, Click on 1 to see what happens. If you did everything right, 1 would appear in the display.<br \/>Close the form<\/p>\n<p><b>Step 14:&nbsp;<\/b> Write the code for Button 2(btn2). Right-click on button 2, Choose Events, Choose Mouse, Choose MouseClicked.<br \/>This takes you to where you would write the code for button 2<\/p>\n<p><b>Step 15: <\/b>Write the code below in the position as shown in the figure( you can also copy and paste)<\/p>\n<p><span style=\"color: #073763;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(txtResult.getText().isEmpty())<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; txtResult.setText(btn2.getText());<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; value1 = 2;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } else {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; txtResult.setText(txtResult.getText()+ &#8221; &#8221; + btn2.getText());<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; value2 = 2; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<\/span><\/p>\n<p>If you have written it in the right position, your code would be as shown in the figure below:<\/p>\n<div style=\"clear: both;\"><a href=\"https:\/\/1.bp.blogspot.com\/--145Uf8TVXQ\/WiSeWeOVIuI\/AAAAAAAAAUs\/5C1Sk_GGr28ul-pGgzGn0wA7ghLeonn_gCEwYBhgL\/s1600\/Button1-and-2-Code-Written.JPG\" style=\"margin-left: 1em; margin-right: 1em;\"><img decoding=\"async\" loading=\"lazy\" border=\"0\" data-original-height=\"728\" data-original-width=\"1114\" height=\"418\" src=\"https:\/\/1.bp.blogspot.com\/--145Uf8TVXQ\/WiSeWeOVIuI\/AAAAAAAAAUs\/5C1Sk_GGr28ul-pGgzGn0wA7ghLeonn_gCEwYBhgL\/s640\/Button1-and-2-Code-Written.JPG\" width=\"640\" \/><\/a><\/div>\n<p><b>Step 16:<\/b> Do the same for buttons 3, 4, 5, 6, 7, 8, 9 and 0<\/p>\n<p><b>Step 17<\/b>: Take some time to look through the codes to make sure you got it right. And also get used to various parts of the code. But don&#8217;t modify anything!<\/p>\n<p><b>Step 18: Test the program again<\/b><\/p>\n<p><ins data-ad-client=\"ca-pub-7041870931346451\" data-ad-format=\"fluid\" data-ad-layout=\"in-article\" data-ad-slot=\"8227894917\" style=\"display: block; text-align: center;\"><\/ins> <b>Step 19: Code for the CE (Clear) button<\/b><br \/>Right-click on CE button, choose events, choose Mouse, choose MouseClick. This takes you to where you would write the code for this button. Copy and paste the code below in the position<\/p>\n<p><span style=\"color: #073763;\">&nbsp; txtResult.setText(&#8220;&#8221;);<\/span><\/p>\n<p>This is the code that would clear the display when the CE button is clicked. Very simple, right?<\/p>\n<p><b>Step 20:<\/b> Code for the +(plus) and -(minus), \/(division) and multiplication buttons<\/p>\n<p>For the plus button, write the code below:<\/p>\n<p><span style=\"color: #0b5394;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(!(txtResult.getText().isEmpty())){<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; operator = &#8220;plus&#8221;;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; txtResult.setText(txtResult.getText()+ &#8221; +&#8221;);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<\/span><\/p>\n<p>For the minus write the code below:<\/p>\n<p><span style=\"color: #0b5394;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(!(txtResult.getText().isEmpty())){<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; operator = &#8220;minus&#8221;;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; txtResult.setText(txtResult.getText()+ &#8221; -&#8220;);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<\/span><\/p>\n<p>For the division button, write the code below<\/p>\n<p><span style=\"color: #0b5394;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(!(txtResult.getText().isEmpty())){<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; operator = &#8220;division&#8221;;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; txtResult.setText(txtResult.getText()+ &#8221; \/&#8221;);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<\/span><\/p>\n<p>For the multiplication button, write the code below:<\/p>\n<p><span style=\"color: #0b5394;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(!(txtResult.getText().isEmpty())){<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; operator = &#8220;multiplication&#8221;;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; txtResult.setText(txtResult.getText()+ &#8221; *&#8221;);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<\/span><br \/><ins data-ad-client=\"ca-pub-7041870931346451\" data-ad-format=\"fluid\" data-ad-layout=\"in-article\" data-ad-slot=\"8227894917\" style=\"display: block; text-align: center;\"><\/ins> <b>Step 21: Test your program. <\/b>Run the program, test the 0 &#8211; 9 buttons as well as the operations. Also test the clear button<br \/>Attempt to enter:<br \/>4 + 5 and click (=)<br \/>Note that nothing happens when you click the equals button.<br \/><b>Note<\/b>: you can only work with 1 to 9 at this time <br \/>Now let&#8217;s write code for the equality sign to perform the calculation.<\/p>\n<p><b>Step 22: Write code for the equality button<\/b><br \/>This is the code for the equality button(you already know how to find where to place this code)<\/p>\n<p><span style=\"color: #660000;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; double answer = 0;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(operator == &#8220;plus&#8221;) <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; answer = value1 + value2;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else if(operator==&#8221;minus&#8221;)<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; answer = value1 &#8211; value2;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else if (operator ==&#8221;multiplication&#8221;)<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; answer = value1 * value2;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else if(operator == &#8220;division&#8221;)<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; answer = value1\/value2;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String Result = Double.toString(answer);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; txtResult.setText(Result);<\/span><\/p>\n<p><b>Step 23: Test the program (Congratulations!)<\/b><br \/>Run the program and try a few calculations. What are your observations. You can leave a comment in the comment box below to let me know what you observe. Are there some questions, leave it in the comment box below.<\/p>\n<p><b>Get Ready for the Next Steps<\/b><br \/>In the next tutorials we would tidy up by doing the following:<\/p>\n<ul>\n<li>Modify this program to handle any number (not just 0 to 9)<\/li>\n<li>Answer your questions<\/li>\n<li>Handle errors, in case user enters wrong inputs<\/li>\n<\/ul>\n<p>I would publish it in a couple of days. Just click follow in the button under my name to get notified when I publish the next step. You can also subscribe to the YouTube channel to watch step by step video<\/p>\n<div style=\"text-align: center;\"><a href=\"https:\/\/kindsonthegenius.com\/blog\/part-2-how-to-build-a-simple-calculator-in-java-using-netbeans-step-by-step-with-screenshots\/\">&nbsp;Continue to Part II <\/a><\/div>\n<\/div>\n<\/div>\n<p><ins data-ad-client=\"ca-pub-7041870931346451\" data-ad-format=\"fluid\" data-ad-layout=\"in-article\" data-ad-slot=\"8227894917\" style=\"display: block; text-align: center;\"><\/ins> <br \/><ins data-ad-client=\"ca-pub-7041870931346451\" data-ad-format=\"fluid\" data-ad-layout=\"in-article\" data-ad-slot=\"8227894917\" style=\"display: block; text-align: center;\"><\/ins><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial would take you through the procedure of building a simple calculator in Java. So I would advice, you print it out and follow &hellip; <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[85],"tags":[],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/243"}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/comments?post=243"}],"version-history":[{"count":3,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/243\/revisions"}],"predecessor-version":[{"id":1689,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/243\/revisions\/1689"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=243"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}