Calculator with Java (for beginners)

Calculator with Java (for beginners)

Online Compiler

I use the online compiler JDoodle in this article. At this point I would also like to recommend JDoodle. The web application has the ability to compile and execute code in many different languages. Projects can be saved in your own account, you can download your source code as a file and the whole thing is free. Especially for smaller tasks where console output is sufficient a very practical tool, which can also be accessed and used from anywhere.

No surface area

The article is aimed completely at beginners and covers basic topics. It does not develop an interface but only works with console output and input.

The Main

C++ and Java have a lot in common, including the Main function. It is the heart of every Java application. Our smart devices always jump first into the main and work line by line through their content. This is an important piece of information that beginners should keep in mind. It means that no matter how complex the application may be (even if it's 100 files and a thousand lines), everything starts in the Main function.

In Java, the simplest file looks like this:

public class One class
{
public static void main(String args[])
{

}
}

The first version of the calculator should work with two numbers and be able to perform the basic operations. So all we need are two variables and a few conditions.

// ...in the Main
int x = 0;
int y = 0;

Everything still pretty unspectacular, or?

Console output and input

To output something in the console we need access to the operating system which also provides the console. If you understand this logic, you can remember the spelling better. In general, sometimes the idea behind the keywords, classes and methods is enough to remember them. Some development environments even help you to remember them, so sometimes it's enough to just remember parts, like System.

// println = print line = print line
System.out.println;

However, we need more meaningful text output for our calculator, how about the following.

System.out.println("Please enter first number:");
System.out.println("Please enter second number:");
System.out.println("Result:");

Now we have to wait for input and pick it up, so we complete the whole thing.

**Scanner scanner = new Scanner(System.in);

System.out.println("Please enter first number:");
**x = scanner.nextLine();
System.out.println("Please enter second number:");
**y = scanner.nextLine();
System.out.println("Result:");

The scanner object takes care of the input stream of the console (System.in). If you do the whole thing, you get two error messages, wieso?

import .*

Java doesn't know everything. To be honest, Java, like many other programming languages, is quite limited. The core has mainly learned to communicate with systems. It would also remain limited if there weren't these incomprehensible import statements. These are the things that make Java more functional. One can imagine that Java only becomes smarter and understands things when it has the relevant books ready. If it is about neuronal systems, it needs a book about neuronal systems, if it is about banking, if it needs a book about banking etc.

import java.util.*;

With this import statement Java understands many new things. Among other things what a "Scanner" is.

character strings to numbers

When we write x = 50 on a sheet, everyone knows that 50 is a number. The computer, however, is never 100% sure what it is about and exactly what it wants. The x could also be a number for the computer. Therefore the computer always wants to know relatively strictly what string and what number is. Therefore there are also different types of variables. And again therefore a variable of type int, which is intended for numbers, cannot accept a string without further ado.

Java is a very comfortable programming language and offers many internal solutions for such problems.

x = Integer.parseInt(scanner.nextLine());

The string is converted to an integer and then passed to variable x.

Conditions (if, else if, else)

As we don't only want to include one arithmetic operation, but several, we need two things: A further console input to define which operator to calculate with and conditions to tell the computer what this entered operator should trigger. Sounds more complicated than it is.

System.out.println("Please enter operator:");
String operator = scanner.nextLine();

if(operator.equals("+"))
{
System.out.println(x+y);
}
else if(operator.equals("-"))
{
System.out.println(x-y);
}
else if(operator.equals("*"))
{
System.out.println(x*y);
}
else
{
System.out.println(x/y);
}

The input is retrieved with scanner.nextLine(); and passed on to a string object. Strings have the great method equals to check if another string x is identical. If no suitable case is found, a division is always executed.

You could also output the formula now:

System.out.println(x+operator+y+"=");

1 divided by 2 = 0?

Who has tried to divide 1 by 2 with our current calculator will get 0. Why? Very simple, our variables x and y are both of type integer. So they are integers. As mentioned before, the computer never really knows what you want from it. He doesn't know that we program a calculator and 1/2 = 0 is an unexpected and wrong result for us in this case... Unless you tell him. And this is done with the variable type. So we also want to get decimal/comma numbers as a result for our calculator, so we have to change our variables from type integer to float.

float x = 0;
float y = 0;

Total source code

Last but not least, the whole calculator.

import java.util.*;

public class MyClass 
{
public static void main(String args[])
{
Scanner scanner = new Scanner(System.in);
int x = 0;
int y = 0;

System.out.println("Enter first number:");
x = Integer.parseInt(scanner.nextLine());
System.out.println("Enter second number:");
y = Integer.parseInt(scanner.nextLine());
System.out.println("Please enter operator:");
String operator = scanner.nextLine();
System.out.println(x+operator+y+"=");

if(operator.equals("+"))
{
System.out.println(x+y);
}
else if(operator.equals("-"))
{
System.out.println(x-y);
}
else if(operator.equals("*"))
{
System.out.println(x*y);
}
else
{
System.out.println(x/y);
}
}
}

In order to simulate console inputs in JDoodle, you must enter the inputs in the stdin inputs text field in the correct order and separated by line breaks.

Next site
Task (Exercise)
Leave a like or comment (~‾▿‾)~
Name Text