in Informatica

Java programming lesson 2 Our first Java program

Java programming course lesson 2:
Our first Java program

The aim of this lesson is to show you a few lines of Java code in order to get familiar with this language. Don’t worry if some points are not clear, we will explain every single aspect in the next lessons.

import java.util.Scanner; // loads the Scanner class from the package(library) java.util

public class FirstProgram { // name of the Class

	public static void main(String[] args) {
		System.out.println("Hi Guys! \nDigit 2 numbers and I will sum them!");


		int n1, n2; // 2 variables containing integer numbers

		Scanner keyboard = new Scanner(System.in);  // it enables the ability to
													// read from the keyboard

		n1 = keyboard.nextInt(); // reads a number from the keyboard
		n2 = keyboard.nextInt();

		System.out.println("The sum of the numbers is: " + (n1+n2));
	}
}
FirstProgram.java

The first row:

import java.util.Scanner

informs the compiler that our Class is going to use the Scanner Class. For the moment we can think of a class like a code fragment to be used in a program. The Scanner class is defined in the package java.util which stands for “Java utilities”. A package is a library of classes that have already been defined.

Then it follows the class definition:

public class FirstProgram {
    ...
}

FirstProgram is the name of our class. Every Java class definition starts with an open curly bracket { and ends with the corresponding closed one }.
Enclosed by the brackets we can find the definition of the methods. Every Java Class has a method called main and it often contains the definition of other methods.

The definition of the method main starts from the second opened bracket till the correspondent closed parenthesis:

public static void main(String[] args){
    ...
}

The words public static are necessary but we will not talk about them now. Every instruction (statement) in a method defines a specific task; the set of instructions inside a method constitute the body of the method. The first instruction of our method starts with System.out.println, this instructions allow to print on the screen what is contained between the round brackets (i.e. (“Hi guys! …”). The \n is a special keyword that allows to start a new line. In order to execute actions, Java programs use Objects. The actions are defined by methods. System.out is an object used to send an output to the screen; println is the method that executes this action for the object System.out. In other words, println sends to the screen what is specified inside the round brackets. The element/s between brackets are called arguments and they feed the method with the necessary input to fulfill its action. In this case the argument is a string of characters. An object executes an action when one of its methods gets called. In a Java program the method call is obtained writing the object name followed by a dot followed by the name of the method and ended by a couple of round brackets. Inside the brackets there could be specified one or more arguments. The line:

int n1, n2;

says that n1 and n2 are the names of two variables.
A variable can store data. The word int means that the variable can contain an integer number. integer is an example of data type. A data type (or type) specifies the set of possible values and the actions defined for these values. The values of a particular data type are stored in memory in the same format.

The next line:

Scanner tastiera = new Scanner(System.in);

enables the program to accept, or read, the data that the user digits using the keyboard.
This line of code will be explained better in the future.

The next line:

n1 = tastiera.nextInt();

reads the number typed with the keyboard and stores it in the variable n1.

The last (interesting) line:

System.out.println("The sum of the numbers is: " + (n1+n2));

Corresponds to the execution of the following actions:

  1. The expression n1+n2 is evalued
  2. The initial string (up to the : character) is concatenated to the result of n1+n2 using the + sign
  3. The concatenation of the initial string with the integer result obtained from the evaluation we obtained a new string that acts as the argument for the println method
  4. the resulting string is printed on the screen

N.B. note that the expression (n1+n2) is evalued only because we put n1+n2 inside the round brackets; if we simply do String + n1 + n2 we obtain the concatenation of the string with the two variables (e.g. String=”Hello”, n1 = 2, n2 = 3, String + n1 + n2 = “Hello23”).

Now let’s talk about the semicolon, yeah…. it is mandatory to place a semicolon to the end of each instruction. Basically you are telling the compiler where the instruction ends.

La tecnologia RFID (Radio Frequency IDentification) e i suoi protocolli di sicurezza

RFID – Identificazione di oggetti basata su radio frequenze Una overview sulla tecnologia RFID e sui protocolli di sicurezza per la protezione della comunicazione tra tag e reader. Di seguito riportiamo la descrizione sintetica offerta da wikipedia sui sistemi RFID. In telecomunicazioni ed elettronica con l’acronimo RFID (dall’inglese radio-frequency identification, in italiano identificazione a radio frequenza) si intende una tecnologia per l’identificazione e/o memorizzazione dati automatica di […]

REMUS: Un sistema operativo con protezione avanzata

REMUS: Un sistema operativo con protezione avanzata Traduzione in lingua italiana dell’articolo “REMUS: A Security-Enhanced Operating System” autori dell’opera: MASSIMO BERNASCHI – Istituto Applicazioni del Calcolo, CNR EMANUELE GABRIELLI and LUIGI V. MANCINI -Università di Roma abstract Presentiamo una dettagliata analisi delle System Call UNIX e le classifichiamo secondo il loro livello di minaccia rispetto […]

Buffer Overflows: Attacks and Defenses for the Vulnerability of the Decade

Proseguendo la trattazione dell’argomento Buffer Overflow vi consiglio di leggere questo ulteriore articolo che illustra ancor meglio la già affrontata tematica degli attacchi informatici di questo tipo. Nell’articolo di cui ho personalmente curato la versione in italiano vengono trattate le principali tipologie esistenti dell’attacco buffer overflow e quali sono le contromisure efficaci per sgominare questo […]

Reset spie servizi – Tagliando mini R53

Guida  per spegnere le indicazioni Service della mini dopo il tagliando, sia per il cambio olio che per la sostituzione dei freni – Per modelli R53 (per modelli precedenti alla R56)

Dopo aver effettuato il tagliando:

1 – A chiavi inserite ma con il quadro spento premere e tenere premuto il tasto per azzerare i km parziali e si accende il display del contachilometri.

2 – Girare la chiave di mezzo scatto (sempre tenendo il pulsante premuto), si accende anche il display sul contagiri ma le pompe rimangono spente ed appare la scritta (SIA) con a fianco i km che mancano al SERVICE OLIO, appena appare questa scritta mollare il pulsante.

3 – Ripremerlo tenendolo premuto finche’ al posto della scritta “SIA” non appare la scritta “rst”, mollare e ripremere un colpo senza tenerlo premuto et voilà azzerato il SERVICE OLIO.

Per azzerare anche il SERVICE FRENI, dalla schermata del SERVICE OLIO premere il pulsante una volta e appaiono a fianco della scritta “SIA” i giorni che mancano alla scadenza del SERVICE FRENI, seguire la stessa procedura del punto 3. Da li passera’ in automatico alla scritta end e uscira’ dal menù. Per azzerare solo il SERVICE FRENI, seguire la procedura dal punto 1, ma appena arrivati alla schermata del SERVICE OLIO con affianco la scritta “SIA” invece di tenere premuto, dare un solo colpo e passera’ alla schermata del SERVICE FRENI e li stessa procedura del punto 3.

Il metodo è stato testato da me personalmente, in quanto eseguo periodicamente i tagliandi sulla mia Mini da solo (cambio olio,sostituzione filtro olio, sostituzione filtro aria, sostituzione filtro nafta, sostituzione filtroantipolline), non mi assumo alcuna responsabilità sui possibili malfunzionamenti causabili al vostro autoveicolo dopo la lettura di questa guida;

Prima di eseguire qualunque lavoro sulla propria auto è bene leggere e documentarsi online sulle procedure standard di operazione assicurandosi di lavorare in sicurezza e soprattutto evitando versamenti di materiale inquinante nell’ambiente.