"Molto tempo fa, i computer potevano solo visualizzare il testo. I programmi visualizzavano i dati sullo schermo dopo aver ricevuto l'input dalla tastiera. Questa è chiamata 'interfaccia utente della console' o semplicemente 'console'. Un'interfaccia a finestra è un'alternativa alla console. Con questo tipo di interfaccia, l'utente interagisce con il programma attraverso una o più finestre. Dato che stiamo solo imparando a programmare, inizieremo lavorando con la console."

"Va bene."

"Il testo viene visualizzato sulla console (schermo) consecutivamente, riga per riga. Il testo viene inserito utilizzando la tastiera. Per evitare errori, l'input da tastiera viene visualizzato sullo schermo. A volte sembra che l'utente umano e il programma stiano facendo a turno scrivere cose sullo schermo. "

"È possibile utilizzare il metodo System.out.print () per visualizzare il testo sullo schermo. Questo metodo visualizza semplicemente il testo, mentre System.out.println () visualizza il testo e sposta il cursore sulla riga successiva."

Codice Risultato
System.out.print("Rain");
System.out.print("In");
System.out.print("Spain");
PioggiaInSpagna
System.out.print("Rain");
System.out.println("In");
System.out.print("Spain");
PioggiaIn
Spagna
System.out.println("Rain");
System.out.println("In");
System.out.println("Spain");
Pioggia
in
Spagna

"Per mantenere parti di testo separate, dobbiamo aggiungere uno spazio. Ad esempio:"

Codice Risultato
int a = 5, b = 6;
System.out.print(a);
System.out.print(b);
56
int a = 5, b = 6;
System.out.print(" " + a + " " + b);
 5 6
int a = 5, b = 6;
System.out.print("The sum is " + (a + b));
The sum is 11

"Fatto"

"Questo ti permette di visualizzare qualsiasi cosa sullo schermo: tutti gli oggetti Java possono essere trasformati in una stringa. Tutte le classi Java derivano dalla classe Object, che ha il metodo toString(). Questo metodo viene chiamato quando vuoi trasformare un oggetto in un corda."

Codice Descrizione
Cat cat = new Cat("Oscar");
System.out.println("The cat is " + cat);
Questi tre esempi sono equivalenti.
Cat cat = new Cat("Oscar");
System.out.println("The cat is " + cat.toString());
Cat cat = new Cat("Oscar");
String catText = cat.toString();
System.out.println("The cat is " + catText);
3
Compito
Java Syntax,  livello 3lezione 3
Bloccato
Display right away
Programming isn't always difficult. Sometimes you need to do simple tasks. And the more similar tasks you have to do, the more you want to automate them. Let's implement one more method. Ultimately, objects without methods don't do anything. We'll pass the string s to the method, which will then display it on the screen.

"Ma il mio programma mostrava ' Il gatto è com.codegym.lesson3.Cat@1fb8ee3 '. Cosa diavolo dovrebbe significare?"

"Il metodo toString() standard della classe Object restituisce una stringa costituita dal nome della classe e dall'indirizzo di memoria dell'oggetto (in formato esadecimale)."

"Uh-huh. E che vantaggio potrebbe venire da un metodo del genere?"

"Puoi scrivere la tua implementazione di toString() nella tua classe. Quindi questo è il metodo che verrà chiamato."

"Davvero? Va bene."

"Ecco alcuni compiti di Diego."

3
Compito
Java Syntax,  livello 3lezione 3
Bloccato
Currency exchange
Loan sharking, sales, banking: do you have a backup plan if programming doesn't work out? No? In that case, let's help traders and financiers with our top-notch programs. First, we'll organize a currency exchange: write a program that converts euros to dollars at a given exchange rate.
3
Compito
Java Syntax,  livello 3lezione 3
Bloccato
Task with percentages
If you weren't afraid of problems with percentages in school, then this problem won't scare you either. But if you do suddenly find yourself afraid, it's time to face your fear and realize that this is actually a simple and pleasant topic. Let's implement a method that increases the passed integer by 10 percent.
1
Compito
Java Syntax,  livello 3lezione 3
Bloccato
Code entry
Your attention, please! Now recruiting code entry personnel for CodeGym. So turn up your focus, let your fingers relax, read the code, and then... type it into the appropriate box. Code entry is far from a useless exercise, though it might seem so at first glance: it allows a beginner to get used to and remember syntax (modern IDEs seldom make this possible).