Learn To Create A Basic Calculator In Java Using Netbeans

12
42180
Basic Calculator Program In Java

In this article, we will be learning how to create a Basic Calculator Program in Java using the NetBeans. We will start by installing NetBeans.

Installation of NetBeans
The Java SE Development Kit(JDK) 7 or JDK 8 is required to install the NetBeans IDE. Now download the latest NetBeans IDE from this link.

Download the whole IDE bundle version depending upon your processor architecture. The user can add or remove the packs using the IDE’s Plugin Manager.

download-netbeans

Assume that you have successfully installed NetBeans on your machine. Start NetBeans from the Window. The NetBeans main window will appear as shown below.

install-netbeans

The NetBeans main window contains menus, toolbars, project pane, files pane, runtime pane, navigator pane, and other similar panes. The NetBeans main window is the command center for the IDE.

Creating a Blank Project
To create and run a program, you have to first create a project. Here are the steps to create a demo project.

Step 1: – Choose File, New Project to display the New Project Dialog box.

new-project

The new project box enables you to specify the project type.
Step 2: – Choose the Java type from the given list.
Step 3: – From the New Application Tab, enter the project name and unselect the main class from it.

Java Calculator
Step 1: – Create a blank Java project and name it Java Calculator. Uncheck the Main class option from the project tab.
Step 2: – To add a Jframe, right click the project name in the Project window.

project-name

Step 3: – To the right of the Jframe, there is a Palette with a set of controls on it. Click the Text Field and apply it on the blank Field form. Now click Properties, then change the text field to blank. Click on the Code and change the Variable Name to txtDisplay.

palette

text-field

Step 4: – Select the Button and drag it to the Jframe form. Change the text and the Variable Name as we did for the text field.

jframe-form

Step5: – Change the Variable Name to btnOne as we did for the text Field. Enter the value 1 for the text by selecting the properties. Do the same for all the buttons by changing the text and the Variable Name.
The Final Jframe structure will be formed.

final-jframe-structure

Step6: – Double click on any of the button. This will take the user to the source section where the major coding of the program is done. The image below depicts the Source section.

depicts-the-source

Step7: – Declare the following variables inside the main class.

declare

The firstnum term is to accept the first number entered, whereas the secondnum is to accept the second number stored. The variable result is used to store the output value.

Writing Code for the Number Button on Java Calculator
To get a text from a form object, you can use the getText method of the object (if it has one). So to get the text from btnOne we could use this code:

Similarly, for the btnTwo, we could use the similar code:

btnone-and-btntwo-action

Arithmetic Codes for the Button
The code for the Addition operation is as shown below. The parse statement is used in order to convert the string argument as a signed decimal integer.

The number entered into the EditText textbox is taken from the EditText and stored temporarily in a variable.

Therefore the getText() method can be used to retrieve the data, and the setText() method to display the data. The elswhere.Integer.parseInt() method will throw a “NumberFormatException” error if the String provided is not a proper number.

btnplus-action

Similarly, do this for all the other logics to be performed. For each logic, the operation to be performed is clearly stated.

btns-action

The Equal action button performs according to the required logic. For this, we make use of the if statements. If the user prefers addition, the firstnum and the secondnum will be added and the answer will be stored in the result.

btnequal

The Clear button is used to remove the previously used values from the text Display. For this the secondnum will be set to 0 and the textField must be set to blank.

btnclear-action

Now, Save the file and click the Run button as shown below.

run-button

The Jframe panel will appear as shown.

jframe-panel

Enter the first number, and second number followed by the logic to be applied. Now click the Equal action button. The result will be displayed on the text Field.
The first number is entered.

first-number

The operation to be performed is applied.

operation-to-be-performed

Enter the second number.

second-number

Now click the Equal sign button, which gives you the result.

equal-sign-button

The addition operation has been performed successfully. The rest of the arithmetic operations can be performed in a similar manner.

Conclusion: – We hope you enjoyed this informative article on how to create a calculator in Java using Netbeans and will check out our other articles such as Create A Login Page Class Form In Java Using Netbeans or about how to install Netbeans on your computer in detail.

Also, if you would like to learn more about Java, You can try out the “Java Programming Course for Beginner From Scratch” for FREE! The course comes with over 12 hours of video that covers 12 major topics such as Java basics, polymorphism, streams, concurrency and much more.

Also, you can learn to make projects in Java with the “Learn Projects in Java for Beginners from Scratch” online course. The course has 5 hours of video where you will learn to build a media player, games, messenger application and much more in Java.

Leave a comment in case you face any issues and have a code filled day!

12 COMMENTS

  1. This is now become very easy and simple to calculate your life plans and objectives by using the free online calculator, which provides you the basic 5 calculating function of your life, by using this calculator you can calculate your loan, credit, and dues amount which are going to be payable by you in future to settle or make anything in your current life.

  2. This help me some how but
    Try to add a page of source code and some time leave your different account like YouTube where you can add that video

  3. Thank you very much for sharing such valuable and informative blog post about how to learn java programming language. I have read many blogs but here i found something new so great explanation keep posting.

LEAVE A REPLY

Please enter your comment!
Please enter your name here