1 Programmazione Programmazione ad ad Oggetti Oggetti JAVA

annuncio pubblicitario
JAVA Classes: String
• La classe String definisce il tipo e le operazioni su
sequenze di caratteri
• Un oggetto String è trattato come una costante:
non è possibile modificare i caratteri
• Metodi per estrarre caratteri, confrontare due
oggetti String, concatenare due oggetti String,
rimpiazzare caratteri, convertire da maiuscole a
minuscole e viceversa
• NB. Restituiscono una copia modificata
dell’oggetto sul quale si applica la modifica
• Per confrontare due stringhe di caratteri usare il
metodo equals
Programmazione ad Oggetti
Utilities & Networking Classes
Marco Botta
Dipartimento di Informatica - Universita` degli Studi d i Torino
C.so Svizzera, 185 - I-10149 Torino (Italy)
e-mail: botta@d i.unito .it - URL: http://www.di.unito.it/~botta
Slide design: Matteo Baldoni
e-mail: baldoni @di.unito.it - URL: http://www.di.unito.it/~baldoni
Ingegneria del Software: Programmazione ad Oggetti
JAVA Classes: String
JAVA Classes: String
class StringDemo {
public static void main(String args[]) {
String s1 = "Hello";
String s2 = ”World";
System.out. println (s1+" "+s2);
s2 = s1.replace("e","a"));
System.out. println (s1+" -> "+s2);
s2 = s1.UpperCase();
System.out. println (s1+" -> "+s2);
s2 = s1.substring(1,3);
System.out. println (s1+" -> "+s2);
}
}
Hello
Hello
Hello
Hello
class equalsDemo {
public static void main(String args[]) {
String s1 = "Hello";
String s2 = "Hello";
String s3 = "HELLO";
System.out. println (s1+" equals "+s2+" -> "+s1.equals(s2));
System.out. println (s1+" equals "+s3+" -> "+s1.equals(s3));
System.out. println (s1+" equalsIgnoreCase "+s3+" -> "+
s1.equalsIgnoreCase (s3));
System.out. println (s1 + " == " + s2 + " -> " + (s1 == s2));
}
}
Hello
Hello
Hello
Hello
World
-> Hallo
-> HELLO
-> Hel
Ingegneria del Software: Programmazione ad Oggetti
3
JAVA Classes: Utilities
equals Hello -> true
equals HELLO -> false
equalsIgnoreCase HELLO -> true
== Hello -> false
Ingegneria del Software: Programmazione ad Oggetti
4
JAVA Classes: Utilities
• I packages java.util e java.lang definiscono un
insieme di classi di utilità che implementano
strutture dati e funzioni di uso generale
•Enumeration: una interface che enumera un
insieme di valori
•Vector: un array a dimensione variabile
•BitSet: un Vector di bit
•Stack: una estensione di Vector che realizza
uno stack
Ingegneria del Software: Programmazione ad Oggetti
2
•Dictionary: una classe astratta per
algoritmi che associano chiavi a valori
•Hashtable: una implementazione di
Dictionary che usa codici hash
•Properties: una estensione di Hashtable
che associa chiavi alfanumeriche a valori
•Math: funzioni matematiche
•Random: generatore di numeri casuali
•Date: per rappresentare data e ora
•System: funzioni e variabili di sistema
(exit, in, out, etc.)
5
Ingegneria del Software: Programmazione ad Oggetti
6
1
JAVA Classes: AWT/Swing
JAVA Classes: Networking
• Java fornisce un package java.awt che definisce
classi di oggetti grafici quali canvases, panels,
buttons, menus, checkboxes, choices, scrollbars,
textfields, etc.
• Questi oggetti sono molto rudimentali, ma
permettono di sviluppare interfacce grafiche
portabili su più piattaforme
• La libreria Swing estende questi oggetti di base con
funzionalità avanzate
– stessi nomi di classe ma con J iniziale (JButton,
JFrame, JMenu, etc.)
• Il package java.net definisce un insieme di classi
per il networking
– Protocollo UDP
Ingegneria del Software: Programmazione ad Oggetti
• DatagramPacket: oggetto che contiene i dati da
inviare
• DatagramSocket: canale di comunicazione
– Protocollo TCP
• Socket: canale di comunicazione bidirezionale
affidabile e persistente
• ServerSocket : simile a Socket, ma accetta
connessioni da un client ( fornisce un servizio)
Ingegneria del Software: Programmazione ad Oggetti
7
JAVA Classes: Networking
import java.net.*;
class WriteServer {
public static int serverPort = 666;
public static int clientPort = 999;
public static int buffer_size = 1024;
public static DatagramSocket ds;
public static byte buffer[] = new byte[buffer_size];
public static void TheServer() throws Exception {
int pos=0;
while (true) {
int c = System.in.read();
switch (c) {
case -1: System.out.println("Server Quits..");
return;
case '\r': break;
case '\n': ds.send(new DatagramPacket(buffer, pos,
InetAddress.getLocalHost(),clientPort));
pos =0;
break;
default: buffer[pos++] = (byte) c;
}
}
}
Ingegneria del Software: Programmazione ad Oggetti
9
JAVA Classes: Networking
JAVA Classes: Networking
public static void TheClient () throws Exception {
while (true) {
DatagramPacket p = new DatagramPacket(buffer, buffer.length);
ds.receive(p);
System.out. println(new String(p.getData (),0,0,p.getLength()));
}
}
public static void main(String args[]) throws Exception {
if (args .length == 1) {
ds = new DatagramSocket(serverPort);
TheServer ();
} else {
ds = new DatagramSocket(clientPort);
TheClient ();
}
}
WriteServer
WriteServer 1
// esegue il server
// esegue il client
Ingegneria del Software: Programmazione ad Oggetti
10
JAVA Classes: Networking
• Il package java.net fornisce inoltre due classi per
la gestione e l’interazione con server WWW
– URL (Uniform Resource Locator)
import java.net.URL;
class myURL {
public static void main(String args []) throws Exception {
URL hp= new URL("http://www.starwave.com/people/naughton");
System.out.println("Protocol: " + hp.getProtocol());
System.out.println("Port: " + hp.getPort());
System.out.println("Host: " + hp.getHost());
System.out.println("File: " + hp.getFile());
System.out.println(" Ext :" + hp. toExternalForm());
}
}
http://www.di .unito.it:80/~ botta/didattica/progobj.html
• rappresenta un indirizzo di una risorsa remota: e`
costituito da 4 parti
– protocollo (http)
– host (www.di.unito.it)
– porta (80)
– File (/~botta/didattica/progobj.html)
Protocol: http
Port: -1
Host: www.starwave.com
File: /people/naughton
Ext: http://www.starwave.com/people/naughton
– URLConnection
• rappresenta una connessione
• permette di accedere alle risorse remote
Ingegneria del Software: Programmazione ad Oggetti
8
11
Ingegneria del Software: Programmazione ad Oggetti
12
2
JAVA Classes: Networking
import java.net.*;
import java.io.*;
class myURL {
public static void main(String args[]) throws Exception {
int c;
URL hp = new URL("http","127.0.0.1",80,"/");
URLConnection hpCon = hp.openConnection();
System.out.println("Date: " + hpCon.getDate());
System.out.println("Type: " + hpCon.getContentType());
System.out.println("Exp : " + hpCon .getExpiration());
System.out.println("Last M: " + hpCon.getLastModified());
System.out.println("Length: " + hpCon.getContentLength());
if (hpCon.getContentLength() > 0) {
System.out.println ("=== Content ===");
InputStream input = hpCon.getInputStream();
int i=hpCon.getContentLength();
while (((c = input.read()) != -1) && (--i > 0)) {
System.out.print((char) c);
}
input.close();
} else { System.out.println("No Content Available"); }
}
Ingegneria del Software: Programmazione ad Oggetti
}
13
3
Scarica