Learn Java basics with a simple calculator

In this tutorial, we will create a simple text-based calculator. It’s meant to practice basic Java syntax, like println(), if- and basic arithmetic. The resulting calculator can only do the 4 basic operations and it does not recognize fractions.

It is advised to first go over a basic tutorial like on w3schools.com/java before attempting this tutorial.

Nonetheless in case of division, it will always round down to the previous integer. Yet it is able to recognize negative numbers.

1. Setup the project

We will use Eclipse as the main IDE for this. So you must install that first, if you do not have that yet.

Start a new project (File > New > Java Project). Name it “SimpleCalculator”. Leave all options default. At the bottom, uncheck “Create module-info.java…”. Press Finish.

Add a Class (File > New > Class), name it “Calculator” (with a capital ‘C’). The package name can be “calculator” (with a lowercase ‘C’). Add a main method (under “Which method stubs would you like to create?”): check the option “public static main (String[] args)”. Check the option to generate comments also (last one). Press Finish.

Now the project looks like:

2. Design using pseudo code

Now, think of what this app must do. And translate that into some steps that need to be taken. For me, that resulted in these steps in pseudo code (I already put comments, to make it easy to copy into the code):

// declare variables
// read numbers

// read operator

// what is the operator? (+ - / *)
// perform operation
// show result

3. Declare variables

Now, inside the main method, declare variables for the two numbers (the two values we will do the calculation for) and the operator. The two numbers will be integers, the operator a String.

This could be a start. Add the other variables yourself:

4. Read values

To read input from the keyboard, we can use the Scanner class. Coding it might look like:

Scanner input = new Scanner(System.in);

This requires the import of that class from the util library. Add the line below at the top of the code, right after the line with the package definition.

import java.util.Scanner;

Importing can also be done automatically in Eclipse: selecte the unknown Class (with the red line underneath) and press Ctrl + Shift + o (Command + Shift + o on a Mac).

Now we can add code to read the value from the user. But first tell the user what should be done:

System.out.println("Enter first number:");

Now we can add the code that reads the value. We will use the nextInt() method of the Scanner class for that:

number1 = input.nextInt();

To show the value to the user (for us: to test if it works), we can add this println() statement:

System.out.println("First number is: "+number1);

5. A first try: run the App

Now its time to test if this works. Run the app and try to enter a value in the Console:

If all went well, input should work, and after that shows the entered number properly.

6. Read second number and operator

Add println() lines and read the second number. For the operator, we can use the method next() from the Scanner class.

The complete code could look like:

Test if it works.

7. Check the operator

To check the contents of the operator variable, we can use a conditional statement, like an if-statement, in combination with the equals() method of a String, which checks if the String is equal to a given text:

// what is the operator? (+ - / *)
if (operator.equals("+")) // if the operator is a '+'
	System.out.println("Its an addition");

8. Give the result

What’s left is calculating the result and showing that. This can be done on one line of code:

System.out.println("Result is: " + (number1 + number2));

Replace the line that prints “Its an addition” with this line. And test again.

9. Add other operations

Make it work for the other operations also. For this, you can use an else if … statement:

Try to add that for the remaining operators.

To help you a bit, this would look like:

Finalize this yourself.

Maybe add a final else … statement to give a message if the operator was none of the 4 basic ones. It is always good to let your user know if something went wrong.

Learn more