May 20, 2024

Part 2: How to Build a Simple Calculator in Java Using Netbeans – Step by Step with Screenshots

This is the Part 2 of How to Build a Simple Calculator in Java using Netbeans IDE. This part follows from the first part. If you have not gone through the first part, you can find it in the link below:

Part 1: How to Build a Simple Calculator in Java Using Netbeans – Step by Step with Screenshots 

Question raised in the first part includes:
How do I arrange the buttons on the form
How to I modify the program to handle any number
How do I handle errors in case of bad input

Let’s start with the first one

Arrangement of the Buttons
Step 1: Right click inside the panel where the buttons are placed. Choose Set Layout > Select Absolute Layout. This is shown in the figure below:

If you have done this correctly, then the buttons will no longer snap.

Step 2: Take time to resize and arrange your buttons as shown in the screenshot

Step 3(Optional): To get a very uniform look, click a buttons and look at the properties. Find the Layout section as shown.

  • make sure all the buttons except the equals button, has the same width and height
  • Make sure all buttons in the same row have the same Y
  • All buttons on the in the same column have the same X

If you have done everything correctly, your form would be looking really good.
Lets now look at the next topic.

How to Handle any Number
To do this you need to replace the codes for the buttons with a slightly modified code.
Modify Code for Button 0 – 9 with the following codes:

For Button 1
        String res = txtResult.getText();
        if(txtResult.getText().isEmpty())
            txtResult.setText(“1”);
        else if(res.contains(“+”) || res.contains(“-“) || res.contains(“*”) || res.contains(“/”))
        {
            txtResult.setText(txtResult.getText() + “1”);
            value2 = Integer.parseInt(value2 + “1”);
        }
        else
            txtResult.setText(txtResult.getText()+ “1”);

For Button 2
        String res = txtResult.getText();
        if(txtResult.getText().isEmpty())
            txtResult.setText(“2”);
        else if(res.contains(“+”) || res.contains(“-“) || res.contains(“*”) || res.contains(“/”))
        {
            txtResult.setText(txtResult.getText() + “2”);
            value2 = Integer.parseInt(value2 + “2”);
        }
        else
            txtResult.setText(txtResult.getText()+ “2”);

Continue with the same trend for buttons 3, 4, 5, 6, 7,  9, 0. You can do this by replacing the places you have the numbers

Code for +, -, * and / Buttons
Also modify the codes for the plus, minus, division and multiplication buttons as follows:

For plus button, use the code:
         if(txtResult.getText().isEmpty())
           return;
        else
            value1 = Integer.parseInt(txtResult.getText());
            txtResult.setText( txtResult.getText() + ” ” + btnPlus.getText() );
            operator = “plus”;

For the minus button, use the code:
        if(txtResult.getText().isEmpty())
           return;
        else
            value1 = Integer.parseInt(txtResult.getText());
            txtResult.setText( txtResult.getText() + ” ” + btnMinus.getText() );
            operator = “minus”;

For the division button, use the code
        if(txtResult.getText().isEmpty())
           return;
        else
            value1 = Integer.parseInt(txtResult.getText());
            txtResult.setText( txtResult.getText() + ” ” + btnDivide.getText() );
            operator = “division”;

For the multiplication button, use the code:
        if(txtResult.getText().isEmpty())
           return;
        else
            value1 = Integer.parseInt(txtResult.getText());
            txtResult.setText( txtResult.getText() + ” ” + btnMultiplication.getText() );
            operator = “multiplication”;

Test the Program
Now run the program and perform some operation to see the result. Your program should be working well by now. But there are some questions that are yet to be answered. For example:

  • How to we handle decimal points
  • How to we handle wrong inputs
  • If you have any more questions, leave it in the comment box below.

We would answer these questions in Part 3.

3.5 6 votes
Article Rating
Subscribe
Notify of
guest

6 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Srishti
Srishti
3 years ago

When are you going to publish part 3????

Eric
Eric
2 years ago

What about the plus/minus sign?

nishu
nishu
2 years ago

Division operator is not working properly

Pedro
Pedro
1 year ago

INCREDIBOOOL!

Prathmesh Patil
Prathmesh Patil
1 year ago

Part 3?