You are on page 1of 6

APROG – Algoritmia e Programação

Guide to start coding a program using the Intellij IDE.

This document briefly presents the essential steps for creating a Java language program
using the Intellij IDEA development environment.
In a simplistic way, in Intellij a project is a general structure that can contain a set of
classes, that is, the exercises that will be solved in class.
In this sense, it is enough to create a single project and then, create the resolutions of the
exercises within that project.

1. Create a new project


Run Intellij. If the initial screen appears, click on Create New Project, otherwise, from
the main menu select File | New | Project. which opens the dialog in Figure 1.
If suggested, select / configure the Project SDK. To develop Java applications, it is
necessary to use the JDK (Java Development Kit). Then, click Next.

Figure 1 –Create a new project screen

The dialog in Figure 2 appears, do not select anything, and proceed by pressing Next.

1
APROG – Algoritmia e Programação

Figure 2 –New Project dialog

In the next step, the dialog in Figure 3 is presented to define the project name and location
(project root directory).

Figure 3 - Filling in the project data (name and location)

After clicking Finish, the project is created, and the screen shown in Figure 4 is
presented. Intellij creates a complex directory structure below the project's root directory
and controls file changes. The user should not change files or any of these directories
directly.

The “src” folder will contain all the Java source code files for the project.

2
APROG – Algoritmia e Programação

Figure 4 – Screen with the project structure

2. Creating a new Java exercise/program


Select the “src” folder and, from the main menu select File | New | Java Class.
Another way is to select the folder with the right mouse button and New | Java Class.
The dialog in Figure 5 is presented to define the class name (the exercise name). In the
example it is “Ex1”.
The class name MUST start with a capital letter and must NOT contain spaces or special
characters.

Figure 5 – Screen to define the class name (exercise name)

The “Ex1” class file is then presented to the user (Figure 6).

class source code


class file

Figure 6 – Screen with the project structure and the class file

3
APROG – Algoritmia e Programação

To execute a program, it is necessary to have the main() method, which is the starting
point of the program. The easiest way to include main() is to write inside the braces of
the class “Ex1”: psvm and press TAB. The following code is automatically generated:
public static void main (String [] args) { }, as shown in Figure 7.

Figure 7 – Screen with the minimum base code for solving an exercise

2. Program development
It is intended to develop a program that presents the digits of a number, starting at least
significant. The algorithm is presented in Figure 8.

DS: num, alg INTEGER

BEGIN
READ( num )
REPEAT
alg  num % 10
num  num / 10
WRITE( alg )
WHILE(num ≠ 0)
END
Figure 8 – Algorithm for listing digits of a number

The user can now start entering the program code. The IDE has a set of aids that make
the task easier for the user, from error detection to using mnemonics to insert common
instructions.
Figure 9 shows the algorithm already coded in Java and inserted in the main() method.

4
APROG – Algoritmia e Programação

classes in use:
import java.util.Scanner;

insert the code into


the method main()

Figure 9 - Algorithm coding in Java language

To read values we used the Scanner class that is included in the base Java distribution.
For this reason, it is necessary to indicate the location of the Scanner class through the
import command. This location can be filled automatically following the IDE’s help.

After writing the program (and correcting any errors reported by IDE), it is time to test
the program. The simplest way to test a program is to run it with a set of predefined input
values and see if the result is as expected. This is not enough to guarantee that the
program is correct, but it is a good start.
In our case, we will use the number “14352”, and it is expected that the result is the
display of the digits in reverse order, i.e. “2 5 3 4 1”, one digit in each line. To run the
program simply click the button shown in Figure 10 (or Alt+Shift+F10).
Because our program is what is commonly referred to as a “console program”, all user
interaction is focused on one window (Output window) that the IDE opens at the bottom
of the screen (Figure 10). It is in this area that data is entered, and results are displayed.

5
APROG – Algoritmia e Programação

run program

console window

input

output

Figure 10 – Program execution

You might also like