Block Image

Let's create a first simple program in Java, in a procedural way, that realizes the following algorithm: provided in input 2 integers a and b, return the sum of a and b.

Prerequisites

  • JDK 8 installed
  • An IDE installed(in the guide we will use Intellij Community Edition)

Step 1: Open Intellij

Open the Intellij IDE and click on New Project:

Block Image

The following screen will open:

Block Image

Click on Next. On the next screen, check Create project from template:

Block Image

In this way we already create a skeleton for our program. We give as project name "Sum" and as package: en.enzoracca.somma. When we illustrate Java with object-oriented programming (and therefore we will really use Java for its entirety), we will explain the concept of package. For now you just need to know that each word preceded by a dot represents a folder. There will be a folder called "it", inside it there will be a folder called "enzoracca" and inside it there will be a folder called "somma".

Block Image

Click on Finish. In the folder "sum" will be created a file from Intellij called Main.java, which represents our source file. The structure of the project is represented by the following image:

Block Image

Note 1: The public static void main(String[] args) method indicates an executable method.
Note 2: All text written after the double slash(//) in Java indicates a comment, i.e. it is not considered by the compiler as a java instruction to be filled in and will be ignored.

Step 2: Write the variables

The problem we are asked consists of 2 integers a and b input data and a sum returned in output. We will then have 3 variables: a, b and sum. In Java each variable must have a type. For example a variable can be numeric or alphanumeric. Below are the types, also called "primitive types", made available to Java:

Integer numbers
  1. byte: an integer number that can represent values from -128 to 127
  2. short: an integer number that can represent values from -32768 to 32767
  3. int: an integer number that can represent values from -2147483648 to 2147483647
  4. long: an integer number that can represent values from -92233720364775808 to 92233720364775807
Floating point numbers
  1. float: single precision floating point numbers
  2. double: double precision floating point numbers
Booleans (true/false)
  1. boolean(can have as true or false values)
Alphanumeric characters
  1. char

For our program we will use int type variables, which is the most used type to represent integers. If we needed to handle very large digits, we would use long type variables.

In Java each instruction ends with a semicolon. We declare the 3 variables:

package it.enzoracca.somma;

public class Main {

    public static void main(String[] args) {
	// write your code here
		int a;
		int b;
		int sum;
    }
}

Variables in Java always have a default value; for int the default value is zero. However, if we want to initialize a variable, for example sum (it makes no sense to initialize a and b since they will be input by the user), we can do in this way:

package it.enzoracca.somma;

public class Main {

    public static void main(String[] args) {
	// write your code here
		int a;
		int b;
		int sum = 0; //I initialized to zero the sum variable
    }
}
Note 3: Initialize means to assign a value to a variable already during its declaration. What would happen if we initialize a and b?

Step 3: We give a and b in input

Even if we have to input the 2 integers, first, for a matter of cleanliness, let's output such a writing: "Write the value of a: ". To output the text, we use the following instruction:

System.out.println("Write the value of a: ");

To allow the user to input data, which will be written using the keyboard for example, we use the following instruction:

Scanner scanner = new Scanner(System.in);

As we will see in the next lessons, in reality Scanner is a java class that allows us to read values. In this case writing new Scanner(System.in), we tell it to read the standard input (keyboard). Now we only have to indicate to Scanner that 2 integers must be read.

a = scanner.nextInt();

Here with scanner.nextInt() we are saying that an integer will be input and assign the supplied integer to the variable. For completeness here is the entire program written so far:

package it.enzoracca.somma;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
	// write your code here
		int a;
		int b;
		int sum;
		System.out.println("Write the value of a: ");
		Scanner scanner = new Scanner(System.in);
		a = scanner.nextInt();
		System.out.println("Write the value of b: ");
		b = scanner.nextInt();
    }
}

Step 4: calculate the sum and return it to output

At this point to the sum variable we can associate the value of a+b and then we can print it in output:

sum = a + b;
System.out.println("The sum between a and b is: " + sum);

In System.out.println, after the double quotes, we have added a + that serves to concatenate a variable to the written text. Then in output we will have the message "The sum between a and b is: " followed by the content of the sum variable.
Here is the complete program:

package it.enzoracca.somma;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
	// write your code here
		int a;
		int b;
		int sum;
		System.out.println("Write the value of a: ");
		Scanner scanner = new Scanner(System.in);
		a = scanner.nextInt();
		System.out.println("Write the value of b: ");
		b = scanner.nextInt();
		sum = a + b;
		System.out.println("The sum between a and b is: " + sum);
    }
}

To run the program just click on the play icon to the left of public class Main

Block Image

A drop-down menu will open at the bottom: click Run 'Main'. The Run tab of Intellij will open:

Block Image

Insert a number and press the Enter key on the keyboard. The message "Type the value of b" will appear. Insert b and press Enter. Finally, in output we will be given the message containing the sum:

Block Image

If we enter the Sum project folder, we will see that in addition to the src folder, where the source program resides (it/enzoracca/omma/Main.java) there will be another folder called target (or out) where the object program will reside generated by the IDE (containing Main.class).

Exercise: Give 2 numbers a and b (not necessarily integers), return the difference between a and b.