[JAVA-2d] Building a Simple Calculator: Making Comparisons and Performing Calculations

Let’s get straight into it and answer the question from last time.

Jack Boyuan Xu
The Startup
Published in
3 min readJan 7, 2020

--

Caching

We have already saved the user’s arithmetic operation of choice into a variable. Let’s choose to only perform the operation after the user has entered the second number. This is a very simple form of caching.

It’s time to ask the user for the second number. Since we are not performing the actual operation at the moment, we want to put aside the conditional statements from the previous article. Therefore, the code for asking the second number should go before the conditional statements:

System.out.print(“Please enter the second number: “);
userInput = s.nextLine();
double number2 = Double.parseDouble(userInput);
System.out.println();

Nice! We now have the first number, second number, and the operation of choice. Time to do some MATH.

Comparing Apples to Apples

Recall the conditional statements we wrote out last time (pseudocode):

if (arithmeticChoice is 1) {
do addition;
} else if (arithmeticChoice is 2) {
do subtraction;
} else if (arithmeticChoice is 3) {
do multiplication;
} else if (arithmeticChoice is 4) {
do division;
} else {
print “Invalid arithmetic choice! Aborting…”;
end program;
}

Before we can perform any actions, we need to check if the arithmeticChoice match any preset value we’ve defined (1, 2, 3, or 4). In other words, we need to compare arithmeticChoice to the integers 1, 2, 3, and 4 and check if they are equal to each other. Making comparisons in Java is very simple. For example, if we want to check if arithmeticChoice and 1 is equal:

if (arithmeticChoice == 1) { … }

In Java and the majority of programming languages, two equal signs indicate a comparison operator. Comparison operators return a boolean value indicating if the relationship is true or false. For example:

1 == 1 // True
1 == 2 // False
1 > 2 // False. > indicates “greater than”.
1 < 2 // True. < indicates “less than”.
1 >= 2 // False. >= indicates “greater than or equal to”.
1 <= 2 // True. <= indicates “less than or equal to”.
1 != 2 // True. != indicates inequality.

Try typing 1 == 1 by itself and IntelliJ will tell us it isn’t a statement. Comparisons are meaningless on their own — they are conditions that should be used with conditional statements such as if. We can now modify our pseudocode to the following:

if (arithmeticChoice == 1) {
do addition;
} else if (arithmeticChoice == 2) {
do subtraction;
} else if (arithmeticChoice == 3) {
do multiplication;
} else if (arithmeticChoice == 4) {
do division;
} else {
print “Invalid arithmetic choice! Aborting…”;
end program;
}

Comparison only works when both sides are of the same type. Just like how we cannot compare apples to oranges, we cannot compare an integer to a string either.

Doing Math and Printing the Result

Time to do some math. It is quite straightforward so here’s the pseudocode:

double result = 0; // We declare a new variable to store the result. It must be a double because we want to keep the precision and try not to truncate.
if (arithmeticChoice == 1) {
result = number1 + number2;
} else if (arithmeticChoice == 2) {
result = number1 — number2;
} else if (arithmeticChoice == 3) {
result = number1 * number2;
} else if (arithmeticChoice == 4) {
result = number1 / number2;
} else {
System.out.println(“Invalid input! Aborting…”);
System.exit(7); // Immediately terminates the program with a custom exit code (that you can determine).
}
System.out.println(“The result of your calculation is “ + result); // You can put strings and numbers together for output using ‘+’.

Voila! We are now finished with our first actual Java application! Run this calculator and watch your code in action with pride:

For reference, here’s my complete code:

Next Steps

Although this simple calculator is now up and running, our work is not yet done. Remember all those foreign terms we have encountered so far? Not all of them have been explained yet.

In the next few articles, we shall refactor this calculator and properly dissect those seemingly complicated terms. They don’t bite, I promise.

--

--

Jack Boyuan Xu
The Startup

Co-founder & Tech Lead @ EthSign. Blockchain Lecturer @ USC.