La presentazione è in caricamento. Aspetta per favore

La presentazione è in caricamento. Aspetta per favore

Programmazione Ingegneria TLC

Presentazioni simili


Presentazione sul tema: "Programmazione Ingegneria TLC"— Transcript della presentazione:

1 Programmazione Ingegneria TLC
Esercitazione 1, 03/03/2009

2 Scrivere, compilare ed eseguire un programma Java
Preparazione del testo del programma Uso di un editor Compilazione del programma javac NomeFile.java Esecuzione del programma compilato java NomeFile

3 Uso di un ambiente di sviluppo
Aprire un ambiente di sviluppo (noi useremo JCreator) e scrivere un programma Java nella finestra di editing. Notate che alcune istruzioni java sono già impostate nella finestra di editing. Note: Usate i quattro tasti freccia per posizionarvi sui caratteri del testo e il tasto Canc per cancellarli (cancella a destra del cursore). I caratteri { } [ ] non si trovano su tutte le tastiere. Tenere premuto il tasto ALT GR e digitare sulla tastiera numerica di destra i caratteri 123 per il carattere '{', 125 per '}', 91 per '[', 93 per ']' e 126 per '~'.

4 Scaricare JCreator LE Il programma è gratuito e gira sui sistemi W2K, Vista e XP Si può scaricare dal sito La versione attuale è la Per usare il JCreator è necessario scaricare e installare anche le J2SDK che si possono reperire sempre dalla stessa pagina dei download Una volta scaricato il software è sufficiente cliccare sull’eseguibile e seguire le istruzioni a video per installarlo

5

6 Operazioni comuni su disco (1)
Apertura di un file da disco Salvataggio del programma su disco È sempre opportuno salvare il programma su disco periodicamente, in quanto le modifiche vengono registrate solo nella memoria RAM, il cui contenuto viene perso ogni volta che il calcolatore viene spento. La frequenza con cui eseguire i salvataggi va valutata considerando il rapporto tra il tempo necessario a compiere tale operazione e quello necessario a riscrivere il programma (o le ultime variazioni) nel caso di una interruzione di corrente o del blocco del sistema operativo.

7 Operazioni comuni su disco (2)
Compilazione Il programma scritto, per poter essere eseguito dal calcolatore, deve essere prima compilato. In una apposita finestra viene visualizzato il risultato della compilazione. Se si sono verificati errori durante la compilazione, questi verranno segnalati e il programma dovrà essere corretto e compilato di nuovo. Esecuzione e visualizzazione dell'output Quando la compilazione ha avuto successo, si può finalmente eseguire il programma. In una apposita finestra comparirà l'output del programma.

8 Uso del JCreator Terminology
Project - A project is a Java application or a package library. Project workspace - A project workspace is a folder that holds all of the information relating to a project. When you create a project in JCreator, all of the project's source code and associated files are maintained in the project workspace. Subproject - After you create a project you can add other projects, called subprojects, to the project workspace. In most cases, subprojects are the packages related to your Java application. You can also use the subprojects for holding some of the JDK source files as a reference.

9 Programma di esempio : ListFiles /1
Console Application Overview The program you are about to create, ListFiles, will obtain and display a list of all the files contained in the directory you define. You will use the java.io.File class to create this console application. You will also become familiar with File View; it displays all of the files that make up your project. If you need to save space on your desktop, you can auto-hide File View by clicking the auto-hide pin. The most important file in this project is ListFiles.java which contains the source code for your application. JCreator begins this file for you based on the information you enter into the Project wizard.

10 Programma di esempio : ListFiles /2
You will enter the java.io.File class into your code. When you type a class name or keyword, you can either type the complete word or you can select the appropriate word from the Code Completion tool which is part of the JCreator editor. It ensures that casing and spelling are correct. This application will use a Local folder system. This means that the folders and files you create in JCreator, as well as any actions you take, are reflected on your local or network drive. For example, if you delete a file or folder from JCreator's File View, that file or folder is also deleted from you hard drive or network drive.

11 Programma di esempio : ListFiles /3
Descrizione The program you are about to create, ListFiles, will obtain and display a list of all the files contained in the directory you define. You will use the java.io.File class to create this console application. You will also become familiar with File View; it displays all of the files that make up your project. If you need to save space on your desktop, you can auto-hide File View by clicking the auto-hide pin. The most important file in this project is ListFiles.java which contains the source code for your application. JCreator begins this file for you based on the information you enter into the Project wizard.

12 Programma di esempio : ListFiles /4
You will enter the java.io.File class into your code. When you type a class name or keyword, you can either type the complete word or you can select the appropriate word from the Code Completion tool which is part of the JCreator editor. It ensures that casing and spelling are correct. This application will use a Local folder system. This means that the folders and files you create in JCreator, as well as any actions you take, are reflected on your local or network drive. For example, if you delete a file or folder from JCreator's File View, that file or folder is also deleted from you hard drive or network drive.

13 Programma di esempio : ListFiles /5
Creazione Click File > New > Project. The Project wizard opens to the Project Template window, which lists the application types available through JCreator. Click Basic Java Application and then click Next. In the Project Paths window, click both Create new workspace and Local folder system. Enter ListFiles in the Name field, and click Next. Notice that the project name is added to Location, Source Path, and Output Path.

14 Programma di esempio : ListFiles /6
In the Project ClassPath window, ensure that JDK version is checked and click Next. In the Project Tools window, ensure that the Default option is checked and click Finish. After JCreator creates the project, click Finish again to close the window. Notice, in the following image, that JCreator created a new folder for the project, and opened the File View pane. In addtion, JCreator opens the new file in the Code Editor where you will enter and edit source code for your application.

15 Programma di esempio : ListFiles /7
Inserimento del codice If necessary, click the File View tab to open the File View window. The project, ListFiles, includes an empty folder for classes and a folder for source files called src. The src folder contains a file called listfiles.java. Use this file you to create your application. Open listfiles.java and enter your name as the author. Notice that the class name, ListFiles, is automatically entered. Replace // TODO, add your application code with java.io.f. Wait for a list of code completion options to open.

16 Programma di esempio : ListFiles /8
In the list of code completion options, see the figure below, highlight and double-click File to complete your code. Alternatively, you can insert File by highlighting File and pressing ENTER or TAB.

17 Programma di esempio : ListFiles /9
Type or copy-and-paste the following code to complete this program:

18 Programma di esempio : ListFiles /10
Compilazione ed esecuzione Build and run the application by pressing F5 or by clicking the Run button in the toolbar. In the following image, the Run button is on the far left. The output window opens and displays a list of the files located in the directory you specified in the Project wizard.

19 Input e output da tastiera
Ci sono molti modi per stampare stringhe e leggerle da tastiera in Java. Per l'output, a lezione avete visto il metodo JOptionPane.showMessageDialog (null, Messaggio); Per inserire un input, si usa il metodo predefnito showInputDialog presente nella stessa classe JOptionPane della libreria swing

20 Input da tastiera String stringaLetta = JOptionPane.showInputDialog("Inserisci stringa") crea una finestra di dialogo avente come messaggio "Inserisci stringa", legge una stringa da tastiera, restituisce (il riferimento a) tale stringa alla variabile stringaLetta

21 Esempio: iniziali di un nome letto da tastiera
import javax.swing.JOptionPane; public class Iniziali { public static void main (String[] args) { String n = JOptionPane.showInputDialog ("Inserisci nome"); String c = JOptionPane.showInputDialog ("Inserisci cognome"); String in = n.substring(0,1).toUpperCase(); String ic = c.substring(0,1).toUpperCase(); System.out.println("Nome: " + n + " " + c); System.out.println("Iniziali: " + in + ic); System.exit(0); }

22 Input/Output su finestra
import javax.swing.JOptionPane; public class FinestraOutput { public static void main(String[] args) { String nome = JOptionPane.showInputDialog ("Come ti chiami?"); nome = nome.toUpperCase(); String stringaVisualizzata = "Ciao " + nome + ", come stai?"; JOptionPane.showMessageDialog (null,stringaVisualizzata); System.exit(0); }}

23 Esercizio 1a Public class Eserc1A { public static void main(String[] args) { Sistem.out.println("This is my first Java program") Sistem.out.println("but it won't be my last."); } Aprire con notepad il file Eserc1A.java e compilarlo. Il programma presenta degli errori. Si correggano gli errori fino ad ottenere un programma Java corretto. Eseguire quindi il programma.

24 Esercizio 1b Scrivere un programma Java che legga in input da tastiera una stringa e restituisca in output su video la stringa ottenuta trasformando la stringa immessa in caratteri maiuscoli. Ad esempio, se viene inserita da tastiera la stringa "HeLLo", il risultato dovrà essere "HELLO". Effettuare l'input da tastiera e l'output usando le finestre di dialogo della classe JOptionPane.

25 Esercizio 1c Scrivere un programma Java che legga in input da tastiera una stringa e restituisca in output su video la stringa ottenuta concatenando la stringa ottenuta dalla stringa inserita trasformando tutti i caratteri della stringa in maiuscolo e la stringa ottenuta dalla stringa inserita trasformando tutti i caratteri in minuscolo. Ad esempio, se viene inserita da tastiera la stringa "Ciao", il risultato dovrà essere "CIAOciao". Effettuare l'input da tastiera usando la finestra di dialogo della classe JOptionPane e e l'output usando il metodo println

26 Esercizio 1d Riscrivere il programma Java dell'Esercizio 1c usando il minor numero di variabili. In particolare si usi una sola variabile per memorizzare la stringa letta in input da tastiera.

27 Esercizio 1e Scrivere un programma Java che stampi su video il codice di un programma Java corretto che non esegue nessuna operazione.

28 Esercizio 1f Scrivere un programma Java che, crei un oggetto di tipo String, che rappresenta il proprio nome e stampi il primo e l'ultimo carattere della stringa. Illustrare ciò che avviene in memoria durante l'esecuzione del programma mediante un diagramma

29 Esercizio 1a: soluzione
public class Eserc1A { public static void main(String[] args) { System.out.println("This is my first Java program"); System.out.println("but it won't be my last."); }

30 Esercizio 1b: soluzione
import javax.swing.JOptionPane; public class Eserc1B { public static void main(String[] args) { String leggi = JOptionPane.showInputDialog ("Inserisci una stringa:"); System.out.println("La stringa " + leggi + " convertita in maiuscolo e' " + leggi.toUpperCase()); System.exit(0); }

31 Esercizio 1c: soluzione
import javax.swing.JOptionPane; public class Eserc1C { public static void main(String[] args) { String leggi = JOptionPane.showInputDialog ("inserisci una stringa:"); String maiuscola = leggi.toUpperCase(); String minuscola = leggi.toLowerCase(); System.out.println("La stringa soluzione e' " + maiuscola + minuscola ); System.exit(0); }

32 Esercizio 1d: soluzione
import javax.swing.JOptionPane; public class Eserc1D { public static void main(String[] args) { String leggi JOptionPane.showInputDialog ("inserisci una stringa:"); System.out.println("La stringa soluzione e' " + leggi.toUpperCase() + leggi.toLowerCase()); System.exit(0); }

33 Esercizio 1e: soluzione
public class Eserc1E { public static void main(String[] args) { System.out.println("public class Eserc1E {\n public static void main(String[] args) {\n\n }\n}" ); }

34 Esercizio 1f: soluzione
import javax.swing.JOptionPane; public class Eserc1F { public static void main(String[] args) { String leggi = JOptionPane.showInputDialog("Inserisci un nome di almeno un carattere:"); JOptionPane.showMessageDialog(null,"Il primo e l'ultimo carattere della stringa sono: " + leggi.substring(0,1) + " e "+ leggi.substring(leggi.length()-1, leggi.length())); System.exit(0); }

35 Informazioni Ricevimento: da concordare con lo studente
a molte domande possiamo rispondere per usate anche la mailing-list del corso Raffaele Nicolussi Stefano Paolozzi Web: Mailing-list: istruzioni sul sito (in allestimento)


Scaricare ppt "Programmazione Ingegneria TLC"

Presentazioni simili


Annunci Google