Quiz About [Your topic]

Download Report

Transcript Quiz About [Your topic]

Capitolo 3
Oggetti
Lucidi relativi al volume:
Java – Guida alla programmazione
James Cohoon, Jack Davidson
Copyright © 2004 - The McGraw-Hill Companies srl
Java – Guida alla programmazione - James Cohoon, Jack Davidson
Copyright © 2004 - The McGraw-Hill Companies srl
Imparare a utilizzare le classi
 Compito corrente
 Acquisire esperienza nella creazione e nella manipolazione
di oggetti dai tipi Java standard
 Perché
 Prepara alla definizione di classi personali e alla creazione
e manipolazione degli oggetti di queste classi
Java – Guida alla programmazione - James Cohoon, Jack Davidson
Copyright © 2004 - The McGraw-Hill Companies srl
Valori e oggetti
 Numeri
 Dispongono di valori ma non di comportamenti
 Oggetti
 Dispongono di attributi e comportamenti
 System.in
 Fa riferimento a un InputStream
 Attributo: tastiera
 Comportamenti: lettura
 System.out
 Fa riferimento a un OutputStream
 Attributo: monitor
 Comportamenti: stampa
Java – Guida alla programmazione - James Cohoon, Jack Davidson
Copyright © 2004 - The McGraw-Hill Companies srl
Altri tipi di oggetti Java
 Stringa
 Rettangolo
 Colore
 JFrame
Java – Guida alla programmazione - James Cohoon, Jack Davidson
Copyright © 2004 - The McGraw-Hill Companies srl
Considerare
 Istruzioni
int peasPerPod = 8;
String message = "Don't look behind the door!“
 Come si possono rappresentare queste definizioni secondo le
nozioni di Java?
Java – Guida alla programmazione - James Cohoon, Jack Davidson
Copyright © 2004 - The McGraw-Hill Companies srl
Rappresentazione
Il valore della variabile primitiva int
peasPerPod è 8
Il valore del
messaggio contenuto
nella variabile String
è un riferimento a un
oggetto String che
rappresenta la
stringa di caratteri
"Non guardare dietro
la porta!"
Java – Guida alla programmazione - James Cohoon, Jack Davidson
Copyright © 2004 - The McGraw-Hill Companies srl
Rappresentazione abbreviata
peasPerPod
message
8
"Don't look behind the door!"
Java – Guida alla programmazione - James Cohoon, Jack Davidson
Copyright © 2004 - The McGraw-Hill Companies srl
Esempi
 Considerare
String a = "excellence“;
String b = a;
 Qual è la rappresentazione?
Java – Guida alla programmazione - James Cohoon, Jack Davidson
Copyright © 2004 - The McGraw-Hill Companies srl
Esempi
 Considerare
String a = "excellence“;
String b = a;
 Qual è la rappresentazione?
a
b
"excellence"
Java – Guida alla programmazione - James Cohoon, Jack Davidson
Copyright © 2004 - The McGraw-Hill Companies srl
Esempi
I primi
The
first due
t woparametri
paramet ersdel
of t he
costruttore di Rectangle
Rect angle const ruct or specify t he
specificano la posizione
posit
ion of t he
upper-leftsinistro
-hand
dell'angolo
superiore
corner
of t he
new Rect angle
del nuovo
rettangolo
int x = 3;
int y = 4;
int width = 5;
int height = 2;
Rectangle r = new Rectangle(x, y, width, height);
The
t hirdeand
fourt h
Il terzo
il quarto
paramet
ers of
parametro
delt he
costruttore
di Rectangle
Rect
angle const
ruct or
specificano
le
specify
t he dimensions
of
del nuovo
tdimensioni
he new Rect angle
rettangolo
Java – Guida alla programmazione - James Cohoon, Jack Davidson
Copyright © 2004 - The McGraw-Hill Companies srl
Esempi
I primi
The
first due
t woparametri
paramet ersdel
of t he
costruttore di Rectangle
Rect angle const ruct or specify t he
specificano la posizione
posit
ion of t he
upper-leftsinistro
-hand
dell'angolo
superiore
corner
of t he
new Rect angle
del nuovo
rettangolo
int x = 3;
int y = 4;
int width = 5;
int height = 2;
Rectangle r = new Rectangle(x, y, width, height);
x
3
y
4
width
5
height
2
The
t hirdeand
fourt h
Il terzo
il quarto
paramet
ers of
parametro
delt he
costruttore
di Rectangle
Rect
angle const
ruct or
specificano
le
specify
t he dimensions
of
del nuovo
tdimensioni
he new Rect angle
rettangolo
(3, 4)
r
2
Rectangle:
5
Java – Guida alla programmazione - James Cohoon, Jack Davidson
Copyright © 2004 - The McGraw-Hill Companies srl
Non inizializzato e null
 Considerare
String dayOfWeek;
BufferedReader inStream;
Rectangle frame;
 Qual è la rappresentazione?
Java – Guida alla programmazione - James Cohoon, Jack Davidson
Copyright © 2004 - The McGraw-Hill Companies srl
Non inizializzato e null
 Considerare
String dayOfWeek;
BufferedReader inStream;
Rectangle frame;
 Qual è la rappresentazione?
dayOfWeek
-
inStream
-
frame
-
Java – Guida alla programmazione - James Cohoon, Jack Davidson
Copyright © 2004 - The McGraw-Hill Companies srl
Non inizializzato e null
 Considerare
String fontName = null;
BufferedReader fileStream = null;
Rectangle boundingBox = null;
 Qual è la rappresentazione?
Java – Guida alla programmazione - James Cohoon, Jack Davidson
Copyright © 2004 - The McGraw-Hill Companies srl
Non inizializzato e null
 Considerare
String fontName = null;
BufferedReader fileStream = null;
Rectangle boundingBox = null;
 Qual è la rappresentazione?
fontName
null
fileStream
null
boundingBox
null
Java – Guida alla programmazione - James Cohoon, Jack Davidson
Copyright © 2004 - The McGraw-Hill Companies srl
Assegnazione
 Considerare
String word1 = "luminous";
String word2 = "graceful";
Word1 = word2;
 Rappresentazione iniziale
word1
"luminous"
word2
"graceful"
Java – Guida alla programmazione - James Cohoon, Jack Davidson
Copyright © 2004 - The McGraw-Hill Companies srl
Assegnazione
 Considerare
String word1 = "luminous";
String word2 = "graceful";
Word1 = word2;
 Dopo l'assegnazione
word1
word2
"graceful"
Java – Guida alla programmazione - James Cohoon, Jack Davidson
Copyright © 2004 - The McGraw-Hill Companies srl
Variabili finali
 Considerare
final String POEM_TITLE = “Appearance of Brown";
final String WARNING = “Weather ball is black";
 Qual è la rappresentazione?
POEM_TITLE
WARNING
"Appearance of Brown"
"Weather ball is black"
The
locks indicat
e t he memory
ions holds
ant s
I lucchetti
indicano
che lelocat
posizioni
in const
memoria
contengono delle costanti
Java – Guida alla programmazione - James Cohoon, Jack Davidson
Copyright © 2004 - The McGraw-Hill Companies srl
Variabili finali
Una volta The
definito,
il riferimento
reference
cannot be
non modified
può essere
once modificato
it is est ablished
object
Costante
ype
di ttipo
const ant
oggetto
In generale, questi attributi possono
essere tmodificati
attraverso
In general,
hese at t ribut
es can be metodi
membro
modified t hrough member
met hods
Value
Java – Guida alla programmazione - James Cohoon, Jack Davidson
Copyright © 2004 - The McGraw-Hill Companies srl
Variabili finali
 Considerare
final Rectangle BLOCK = new Rectangle(6, 9, 4, 2);
BLOCK.setLocation(1, 4);
BLOCK.resize(8, 3);
 Rappresentazione iniziale
(6, 9)
BLOCK
2
Rectangle:
4
Java – Guida alla programmazione - James Cohoon, Jack Davidson
Copyright © 2004 - The McGraw-Hill Companies srl
Variabili finali
 Considerare
final Rectangle BLOCK = new Rectangle(6, 9, 4, 2);
BLOCK.setLocation(1, 4);
BLOCK.resize(8, 3);
 Rappresentazione iniziale
(1, 4)
BLOCK
3
Rectangle:
8
Java – Guida alla programmazione - James Cohoon, Jack Davidson
Copyright © 2004 - The McGraw-Hill Companies srl
Variabili finali
 Considerare
String LANGUAGE = "Java";
The
reference
cannotilbe
Una
volta definito,
riferimento
nonitpuò
modified once
is
essere
modificato
est ablished
LANGUAGE
Il contenuto è immutabile perché non
The cont
ent smetodi
are immut
ableche
because
esistono
String
ne
la met
modifica
t here consentono
are no St ring
hods t hat
allow t he cont ent s t o be changed
"Java"
Java – Guida alla programmazione - James Cohoon, Jack Davidson
Copyright © 2004 - The McGraw-Hill Companies srl
Utilizzo degli oggetti
 Considerare
BufferedReader stdin = new BufferedReader(
new InputStreamReader(System.in));
System.out.print("Enter your account name: ");
String response = stdin.readLine();
Java – Guida alla programmazione - James Cohoon, Jack Davidson
Copyright © 2004 - The McGraw-Hill Companies srl
Utilizzo degli oggetti
 Considerare
BufferedReader stdin = new BufferedReader(
new InputStreamReader(System.in));
System.out.print("Enter your account name: ");
String response = stdin.readLine();
stdin
BufferedReader:
Java – Guida alla programmazione - James Cohoon, Jack Davidson
Copyright © 2004 - The McGraw-Hill Companies srl
Utilizzo degli oggetti
 Considerare
BufferedReader stdin = new BufferedReader(
new InputStreamReader(System.in));
System.out.print("Enter your account name: ");
String response = stdin.readLine();
 Si supponga che l'interazione con l'utente sia
Enter your account name: artiste
reponse
"artiste"
Java – Guida alla programmazione - James Cohoon, Jack Davidson
Copyright © 2004 - The McGraw-Hill Companies srl
Rappresentazione stringa
 Considerare
 String alphabet = "abcdefghijklmnopqrstuvwxyz";
 Rappresentazione abbreviata standard
alphabet
"abcdefghijklmnopqrstuvwxyz"
 Rappresentazione più veritiera
alphabet
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
y
z
Java – Guida alla programmazione - James Cohoon, Jack Davidson
Copyright © 2004 - The McGraw-Hill Companies srl
Rappresentazione stringa
 Considerare
 String alphabet = "abcdefghijklmnopqrstuvwxyz";
 char c1 = alphabet.charAt(9);
 char c2 = alphabet.charAt(15);
 char c3 = alphabet.charAt(2);
 Quali sono i valori di c1, c2 e c3? Perché?
c1
'j'
c2
'p'
c3
'c'
Java – Guida alla programmazione - James Cohoon, Jack Davidson
Copyright © 2004 - The McGraw-Hill Companies srl
Programma WordLength.java
public class WordLength
public static void main(String[] args)
throws IOException
BufferedReader stdin = new BufferedReader(
new InputStreamReader(System.in));
System.out.print("Enter a word: ");
String word = stdin.readLine();
int wordLength = word.length();
System.out.println("Word " + word + " has length "
+ wordLength + ".");
}
}
Java – Guida alla programmazione - James Cohoon, Jack Davidson
Copyright © 2004 - The McGraw-Hill Companies srl
Altri metodi String
 Considerare
String weddingDate = "August 21, 1976";
String month = weddingDate.substring(0, 6);
System.out.println("Month is " + month + ".");
 Qual è l'output?
Java – Guida alla programmazione - James Cohoon, Jack Davidson
Copyright © 2004 - The McGraw-Hill Companies srl
Altri metodi String
 Considerare
String weddingDate = "August 21, 1976";
String month = weddingDate.substring(0, 6);
System.out.println("Month is " + month + ".");
 Qual è l'output?
Month is August.
Java – Guida alla programmazione - James Cohoon, Jack Davidson
Copyright © 2004 - The McGraw-Hill Companies srl
Altri metodi String
 Considerare
String fruit = "banana";
String searchString = "an";
int n1 = fruit.indexOf(searchString, 0);
int n2 = fruit.indexOf(searchString, n1 + 1);
int n3 = fruit.indexOf(searchString, n2 + 1);
System.out.println("First search: " + n1);
System.out.println("Second search: " + n2);
System.out.println("Third search: " + n3);
 Qual è l'output?
Java – Guida alla programmazione - James Cohoon, Jack Davidson
Copyright © 2004 - The McGraw-Hill Companies srl
Altri metodi String
 Considerare
String fruit = "banana";
String searchString = "an";
int n1 = fruit.indexOf(searchString, 0);
int n2 = fruit.indexOf(searchString, n1 + 1);
int n3 = fruit.indexOf(searchString, n2 + 1);
System.out.println("First search: " + n1);
System.out.println("Second search: " + n2);
System.out.println("Third search: " + n3);
 Qual è l'output?
First search: 1
Second search: 3
Third search: -1
Java – Guida alla programmazione - James Cohoon, Jack Davidson
Copyright © 2004 - The McGraw-Hill Companies srl
Altri metodi String
 Considerare
int v1 = -12;
double v2 = 3.14;
char v3 = 'a';
String s1 = String.valueof(v1);
String s2 = String.valueof(v2);
String s3 = String.valueof(v3);
s1
"-12"
s2
"3.14"
s3
"a"