How To Make A Basic Calculator With Java

Calculators make life so much easier when working with math. Whether you're working with fractions, exponents, or even pi; all these tasks can be done with just a click of a few buttons. 
But what if you could make your own digital calculator and not ever have to worry about running to the store to get a few batteries? That's is where Java comes in and by the end of this post you'll have grasp at least some knowledge of how calculator works behind the scenes. 

What Is Java?

Java is another programming language developed by Sun Microsystems on January 23, 1996 and over the years it has been made open source. With it we can create apps for Android systems and use it for web development to make webpages. 
That's not all though, Java is very broad in terms of its usability and we can even create our own game with it! But for now, we need to get an IDE text editor (Integrated Development Environment) that will allow us to make or input our code.

Getting Started With NetBeans

To get started, we need to install NetBeans on our computer to store all of our Java code. NetBeans is one of the IDE text editors we can use and there are others such as Eclipse and Aptana Studio 3. What makes NetBeans different from the others is that we can use it for not just Java, but for other programming languages as well. 
Before we get NetBeans you need to install Java JDK (Java Development Kit) SE (Standard Edition). We need to install this first because it includes the necessary tools needed for Java development. Another thing to make note of is make sure you download the correct version of JDK. 
For example, I'm using NetBeans 10.0, which is the latest version of NetBeans and I would have to download Java JDK 11. To get NetBeans, go to and download NetBeans 10.0. You'll want to get the installer that has everything included and keep in mind that this for Windows users. For Mac users, you can go to the NetBeans article or any tutorial for Mac OS X. 
Locate the netbeans folder on computer and copy/paste it into your Programs File folder. Then once there, go to netbeans/bin and open the netbeans64 application and from there it's smooth sailing. At the load screen, create a new Java project and name it you can name it whatever you want, but for this case I named the project Calculator since that's what were going to building.

Understanding Java Basics

Ok, now that we're up and running you should see a few code lines loaded upon launch. The first thing you should is the package, which is useful for storing classes of the similar type. Below that we can see that we have a class named Calculator as well.
Classes are templates for creating objects in Java and within the class we can define methods to describe what the object will do. Methods have a set parenthesis that immediately follow it and these are referred to as parameters in Java. With parameters you can specify what variables or values the method will accept when accessed. 
Since we're going to be using two numbers, our method should have two parameters and here we're using two variables named a and b. The public keyword in front of our class name makes our class accessible from anywhere in the package. 
First, we need to create a method using the public keyword and in this case we're dealing with numbers so we have to declare the method using a datatype called int.

Methods and Datatypes

Datatypes are values that identifies our method, for example numbers are identified by the int datatype and words of text are identified by a datatype called string. Int is the short hand word for integer, which is a type of number in math. 
Next, we just name the name method and in this sense we are trying to find the sum of two numbers which is why I named the method is called getSum. Whenever we create a method, we must always enclose them in opening and closing curly braces because this shows when a method will start and end in our code editor. 
Now you may be wondering what the line System.out.println means and here we're basically telling the computer to print or input our numbers on screen. On the next line, the keyword return stores the numbers in the method and returns those numbers when the getSum method is called upon. 
The same thing occurs in the other methods, with the only difference being the addition, subtraction, multiplication, and division signs being used on the numbers.
Another thing to make note of is that in the getDivision and getRemainder methods we are checking if the number being divided equals zero, then return an error message saying that a number cannot be divided by zero. 
If and else methods checks if a condition is true or not and if not; triggers a statement or condition by default. Public static void main is the main method and everything that we want to run on the computer must be in this method.  

Java Scanner

We then imported the scanner package; it is already built into Java and every time its used we must import it. The scanner waits for a user to input the first number and then the second number. Next, the scanner performs all of the operations used in each of the addition, subtraction, multiplication, and division methods. 
A scanner object was created so that it could be accessed directly by our methods and it could later be used by our scanner. In addition, before a user inputs a number, a message pops up telling the user to input a number using a string of text. The integer variable "a" awaits a number that is expected to be an integer and the same thing occurs with the other variable. 
Finally, we activate our methods on the calculator object so every time we want to find the solution of two numbers, all arithmetic operations are executed too upon selecting any two numbers.
Event Handling

All calculators use a process called event handling to find the solutions to whatever a user inputs in the calculator. An event handler waits on a user to interact with an application or website and then perform an action based on what the user clicked on.
For example, let's you order a product from a website. Whenever purchase the item, you'll usually receive an email confirmation thanking you for the purchase. This is an example of an event handler because the thank you email is automatically sent to you after you perform an action.
As always thank you for reading.
Powered by Blogger.