Let's Start

Now that you are ready to make your third program after installing Java and NetBeans, let's open NetBeans, now you will have to create a new project. There are a lot of project types you can choose from but we will start with the simplest and very basic one of them - the Java application.

File>>New Project

You will get a window similar to this (it may look different depending on the version of your NetBeans and other installed programs you have or have not).

Project Type

Choose Java for the Categories then Java Application for the Projects then Next>.

Project Name

For the name, type Calculator or whatever you like your program name to be. But in here, I will be using Calculator as the project name so it's a good practice to do the same so that names won't make you confused.

Now that you created the project, it's time to start writing some codes. This is how your code will look like when you open Calculator.java file.

Our program is very simple. All we want to do is to make our program ask you to insert a number, then second one and then add them and give you the result. To make the program read anything from the Output Window, we create an object from the Scanner Class we will also use variables in this program.

NOTE: Variables are containers that hold our data temporarily. It has many types as we may need to store text or numbers or anything else.

NOTE: Scanner Class is a simple text scanner which can parse primitive types and strings using regular expressions.

Now let's make our program ask for the first number. In the Main type System.out.Println("Please enter the first number");. Now we need to create an object of Scanner Class and name it input or whatever you want.

Scanner input= new Scanner(System.in);

Then we need to get the first number from the Output window, so we will declare a variable called num1 of type integer and put your first number there first. Here is how to declare a variable of type int.

int num1;

It's as easy as that but we need to tell our program to put your number in this variable because now we reserved an integer space in our memory and never used it and this is how to tell your program to read your number from the Output window and put it in the variable num1.

int num1 = input.nextInt();

We will do the same with the other number and create another variable called num2. Now all we need to do is to add the two numbers and display the result which is simple as we learned before how to display any text on the Output window but there is a little thing we will add there because we want the program to output num1 + num2 = result so you will notice the difference when we reach this step, but now we will make a function or method which are the same.

NOTE: Function is a block of code to be used in different places in the same code. This is how we declare the function:

privatestaticString add(int x, int y)
{
}

When we declare a function, we first set if it's going to be public or private which simply means if it is private, then we can't use it out of this code file. If it is public, then we can, then we declare that it's static which will be explained in later tutorials, then we set the return type which means that this function will return a text for me in our case, then we set the function name which can be whatever you want here I called it add, then between the brackets we put our arguments which are values we will be using in the function. Here, I put 2 variables of type int because we will send our 2 numbers to our function.

NOTE: If your function won't return any values, you can use void as the return type.

In our function, we simply declared another variable which is the result and added our 2 numbers and put the sum in the result variable then we returned the string and we used format to make our output in a specific format which is num1 + num2 = result.

Then, all we need to do is to call our function and display its return value and this is how: