Professional Documents
Culture Documents
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.
The dialog in Figure 2 appears, do not select anything, and proceed by pressing Next.
1
APROG – Algoritmia e Programação
In the next step, the dialog in Figure 3 is presented to define the project name and location
(project root directory).
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
The “Ex1” class file is then presented to the user (Figure 6).
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.
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;
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