Java programs are built from classes. - PowerPoint PPT Presentation

1 / 243
About This Presentation
Title:

Java programs are built from classes.

Description:

Don't forget the blueprint (even though it's empty). class Two { int u; ... Notice that the blueprint is no longer empty: u and q are called instance variables ... – PowerPoint PPT presentation

Number of Views:36
Avg rating:3.0/5.0
Slides: 244
Provided by: scie299
Category:

less

Transcript and Presenter's Notes

Title: Java programs are built from classes.


1
(No Transcript)
2
Java programs are built from classes.
3
Java programs are built from classes. There is
nothing else, but classes.
4
(No Transcript)
5
A class contains members
6
A class contains members a) data (variables)
7
A class contains members a) data (variables)
b) procedures (methods)
8
(No Transcript)
9
Members could be static or not static.
10
(No Transcript)
11
End of story.
12
Lets see some examples now.
13
Draw this
class One
14
Draw this
Dont forget the blueprint (even though its
empty).
class One
One
15
Draw this
class Two int u double q
16
Draw this
Notice that the blueprint is no longer empty
Two
class Two int u double q
17
Draw this
Notice that the blueprint is no longer empty
Two
class Two int u double q
u
q
18
Draw this
Notice that the blueprint is no longer empty
Two
class Two int u double q
u
q
u and q are called instance variables
19
Draw this
class Student
20
Draw this
Student
class Student
Easy. Student or One same difference.
21
Now draw this
class Student int age
22
Now draw this
Student
class Student int age
age
23
Draw this
class Z int m double n
24
Draw this
Z
class Z int m double n
m
n
25
Draw this
class M int m static double n
26
Draw this
M
class M int m static double n
n
m
27
Draw this
M
class M int m static double n
m
n
28
Draw this
M
class M int m double n
m
n
29
Draw this
M
class M int m static double n
n
m
30
Draw this
class Q int v double z public
static void main(String args)
31
Draw this
class Q int v double z public
static void main(String args)
For this the picture is bigger
32
Draw this
class Q int v double z public
static void main(String args)
Q
v
z
main
args
For this the picture is bigger
33
Draw this
class Q int v double z public
static void main(String args)
Q
v
z
main
args
For this the picture is bigger
34
Draw this
class Q int v double z public
static void main(String args)
Q
v
z
main
args
For this the picture is bigger
35
Draw this
class E int v double z public
static void main(String args) int q
q 3
36
Draw this
class E int v double z public
static void main(String args) int q
q 3
Same as above, only main is no longer empty
37
Draw this
class E int v double z public
static void main(String args) int q
q 3
E
v
z
Same as above, only main is no longer empty
38
Draw this
class E int v double z public
static void main(String args) int q
q 3
E
v
z
main
args
Same as above, only main is no longer empty
39
Draw this
class E int v double z public
static void main(String args) int q
q 3
E
v
z
main
args
int q
Same as above, only main is no longer empty
40
Draw this
q is a local variable.
class E int v double z public
static void main(String args) int q
q 3
E
v
z
main
args
int q
Same as above, only main is no longer empty
41
Draw this
q is a local variable. It is local to main.
class E int v double z public
static void main(String args) int q
q 3
E
v
z
main
args
int q
Same as above, only main is no longer empty
42
Draw this
class E int v double z public
static void main(String args) int q
q 3
E
v
z
main
args
int q
q 3
Same as above, only main is no longer empty
43
Draw this
class E int v double z public
static void main(String args) int q
q 3
E
v
z
main
args
int q
q 3
Same as above, only main is no longer empty
44
Draw this
class E int v double z public
static void main(String args) int q
q 3
E
v
z
main
args
int q
3
q 3
Same as above, only main is no longer empty
45
Draw this
class Student int age double gpa
public static void main(String args)
int q 21 String n John
46
Draw this
class Student int age double gpa
public static void main(String args)
int q 21 String n John
age
gpa
main
args
Student
47
Draw this
class Student int age double gpa
public static void main(String args)
int q 21 String n John
age
gpa
main
args
q
Student
48
Draw this
class Student int age double gpa
public static void main(String args)
int q 21 String n John
age
gpa
main
args
q
21
Student
49
Draw this
class Student int age double gpa
public static void main(String args)
int q 21 String n John
age
gpa
main
args
q
21
n
Student
50
Draw this
class Student int age double gpa
public static void main(String args)
int q 21 String n John
age
gpa
main
args
q
21
n
Student
51
java.lang.String
Draw this
class Student int age double gpa
public static void main(String args)
int q 21 String n John
John
age
gpa
main
args
q
21
n
Student
52
java.lang.String
Draw this
String is a reference type.
class Student int age double gpa
public static void main(String args)
int q 21 String n John
John
age
gpa
main
args
q
21
n
Student
53
Types in Java
54
Types in Java a) primitive
55
Types in Java a) primitive (int, double,
boolean, char)
56
Types in Java a) primitive (int, double,
boolean, char) b) reference types
57
Types in Java a) primitive (int, double,
boolean, char) b) reference types (classes)
58
Types in Java a) primitive (int, double,
boolean, char)
int x
59
Types in Java a) primitive (int, double,
boolean, char)
int x
int y
60
Types in Java a) primitive (int, double,
boolean, char)
int x
int y
x 5
61
Types in Java a) primitive (int, double,
boolean, char)
int x
int y
x 5
62
Types in Java a) primitive (int, double,
boolean, char)
int x
5
int y
x 5
63
Types in Java a) primitive (int, double,
boolean, char)
int x
5
int y
x 5
y x
64
Types in Java a) primitive (int, double,
boolean, char)
int x
5
int y
x 5
y x
65
Types in Java a) primitive (int, double,
boolean, char)
int x
5
int y
5
x 5
y x
66
Types in Java a) primitive (int, double,
boolean, char)
int x
5
int y
5
x 5
y x
x x 2
67
Types in Java a) primitive (int, double,
boolean, char)
int x
5
int y
5
x 5
y x
x x 2
68
Types in Java a) primitive (int, double,
boolean, char)
int x
5
int y
5
x 5
y x
x 5 2
69
Types in Java a) primitive (int, double,
boolean, char)
int x
5
int y
5
x 5
y x
x 5 2
70
Types in Java a) primitive (int, double,
boolean, char)
int x
5
int y
5
x 5
y x
x 5 2
71
Types in Java a) primitive (int, double,
boolean, char)
int x
7
int y
5
x 5
y x
x x 2
72
Types in Java a) primitive (int, double,
boolean, char)
int x
7
But this doesnt change y.
int y
5
x 5
y x
x x 2
73
Types in Java a) primitive (int, double,
boolean, char)
int x
7
But this doesnt change y. And thats because y
is a copy of x.
int y
5
x 5
y x
x x 2
74
Types in Java a) primitive (int, double,
boolean, char)
int x
7
But this doesnt change y. And thats because y
is a copy of x. Things dont work the same with
reference types.
int y
5
x 5
y x
x x 2
75
Types in Java a) primitive (int, double,
boolean, char)
int x
7
But this doesnt change y. And thats because y
is a copy of x. But the above is true of
primitive types int (long, short,
byte) double (float) char boolean
int y
5
x 5
y x
x x 2
76
Types in Java a) primitive (int, double,
boolean, char) b) reference types
77
Types in Java a) primitive (int, double,
boolean, char) b) reference types
Penguin x
78
Types in Java a) primitive (int, double,
boolean, char) b) reference types
Penguin x
x new Penguin()
79
Types in Java a) primitive (int, double,
boolean, char) b) reference types
Penguin x
x new Penguin()
80
Types in Java a) primitive (int, double,
boolean, char) b) reference types
Penguin x
x new Penguin()
81
Types in Java a) primitive (int, double,
boolean, char) b) reference types
Penguin x
x new Penguin()
Penguin y
82
Types in Java a) primitive (int, double,
boolean, char) b) reference types
Penguin x
x new Penguin()
Penguin y
y x
83
Types in Java a) primitive (int, double,
boolean, char) b) reference types
Penguin x
x new Penguin()
Penguin y
y x
84
Types in Java a) primitive (int, double,
boolean, char) b) reference types
Penguin x
x new Penguin()
Penguin y
y x
85
Types in Java a) primitive (int, double,
boolean, char) b) reference types
Penguin x
x new Penguin()
Penguin y
y x
x.turnLeft()
86
Types in Java a) primitive (int, double,
boolean, char) b) reference types
Penguin x
x new Penguin()
Penguin y
y x
x.turnLeft()
87
Types in Java a) primitive (int, double,
boolean, char) b) reference types
Penguin x
x new Penguin()
Penguin y
y x
x.turnLeft()
88
Types in Java a) primitive (int, double,
boolean, char) b) reference types
Penguin x
x new Penguin()
Penguin y
But that also changes the value of y.
y x
x.turnLeft()
89
Types in Java a) primitive (int, double,
boolean, char) b) reference types
Penguin x
x new Penguin()
Penguin y
But that also changes the value of y.
y x
The Penguin object is only one, and shared.
x.turnLeft()
90
Types in Java a) primitive (int, double,
boolean, char) b) reference types
91
Types in Java a) primitive (int, double,
boolean, char) b) reference types (classes)
92
Types in Java a) primitive (int, double,
boolean, char) b) reference types (classes)
A value of primitive type fits in a location of
that type.
93
Types in Java a) primitive (int, double,
boolean, char) b) reference types (classes)
A value of primitive type fits in a location of
that type.
A value of reference type (an object) doesnt.
94
Types in Java a) primitive (int, double,
boolean, char) b) reference types (classes)
A value of primitive type fits in a location of
that type.
A value of reference type (an object)
doesnt. For objects, a reference to them is
kept in the variable.
95
Lets continue with the examples.
96
Draw this
class J int x public static void
main(String args) J z new J()
int x 5 z.x 5
97
Draw this
class J int x public static void
main(String args) J z new J()
int x 5 z.x 5
x
main
args
J
98
Draw this
class J int x public static void
main(String args) J z new J()
int x 5 z.x 5
x
main
args
z
J
99
Draw this
class J int x public static void
main(String args) J z new J()
int x 5 z.x 5
x
x
0
main
args
z
J
100
Draw this
class J int x public static void
main(String args) J z new J()
int x 5 z.x 5
x
x
0
main
args
z
J
101
Draw this
class J int x public static void
main(String args) J z new J()
int x 5 z.x 5
x
x
0
main
args
z
x
J
102
Draw this
class J int x public static void
main(String args) J z new J()
int x 5 z.x 5
x
x
0
main
args
z
x
5
J
103
Draw this
class J int x public static void
main(String args) J z new J()
int x 5 z.x 5
x
x
0
main
args
z
x
5
J
104
Draw this
class J int x public static void
main(String args) J z new J()
int x 5 z.x 5
x
x
0
main
args
z
x
5
J
105
Draw this
class J int x public static void
main(String args) J z new J()
int x 5 z.x 5
x
x
0
main
args
z
x
5
J
106
Draw this
class J int x public static void
main(String args) J z new J()
int x 5 z.x 5
x
x
0
main
args
z
x
5
J
107
Draw this
class J int x public static void
main(String args) J z new J()
int x 5 z.x 5
x
x
5
main
args
z
x
5
J
108
Draw this
class J int x public static void
main(String args) J z new J()
int x 5 z.x 5
x
x
5
main
args
z
x
5
J
109
Draw this
110
Draw this
class Student int age double gpa
public static void main(String args)
int q String n Student s
s new Student()
111
Draw this
What kind of variables are age and gpa?
class Student int age double gpa
public static void main(String args)
int q String n Student s
s new Student()
112
Draw this
Yes, they are instance variables.
class Student int age double gpa
public static void main(String args)
int q String n Student s
s new Student()
113
Draw this
What kind of variables are q, n, and s?
class Student int age double gpa
public static void main(String args)
int q String n Student s
s new Student()
114
Draw this
Exactly, they are local variables.
class Student int age double gpa
public static void main(String args)
int q String n Student s
s new Student()
115
Draw this
Theyre local to main.
class Student int age double gpa
public static void main(String args)
int q String n Student s
s new Student()
116
Draw this
Now lets draw!
class Student int age double gpa
public static void main(String args)
int q String n Student s
s new Student()
117
Draw this
class Student int age double gpa
public static void main(String args)
int q String n Student s
s new Student()
age
gpa
main
q
args
Student
118
Draw this
class Student int age double gpa
public static void main(String args)
int q String n Student s
s new Student()
age
gpa
main
q
args
n
Student
119
Draw this
class Student int age double gpa
public static void main(String args)
int q String n Student s
s new Student()
age
gpa
Student
120
Draw this
class Student int age double gpa
public static void main(String args)
int q String n Student s
s new Student()
0
age
age
gpa
0
gpa
Student
121
Draw this
class Student int age double gpa
public static void main(String args)
int q String n Student s
s new Student()
0
age
age
gpa
0
gpa
Student
122
Draw this
Notice that q and n have no values in them.
class Student int age double gpa
public static void main(String args)
int q String n Student s
s new Student()
0
age
age
gpa
0
gpa
Student
123
Draw this
Notice that q and n have no values in
them. Unlike instance or class variables, local
variables need to be initialized explicitly by
the programmer.
class Student int age double gpa
public static void main(String args)
int q String n Student s
s new Student()
0
age
age
gpa
0
gpa
Student
124
Draw this
Instance and class variables have default values.
class Student int age double gpa
public static void main(String args)
int q String n Student s
s new Student()
0
age
age
gpa
0
gpa
Student
125
Draw this
Instance and class variables have default
values. Its not hard to guess the default
values.
class Student int age double gpa
public static void main(String args)
int q String n Student s
s new Student()
0
age
age
gpa
0
gpa
Student
126
Draw this
class Student int age 20 double
gpa public static void main(String args)
Student s new Student()
Student q new Student() q.age 19
s.age 21 q.age 3 q.age

127
Draw this
class Student int age 20 double
gpa public static void main(String args)
Student s new Student()
Student q new Student() q.age 19
s.age 21 q.age 3 q.age

age
gpa
main
args
Student
128
Draw this
class Student int age 20 double
gpa public static void main(String args)
Student s new Student()
Student q new Student() q.age 19
s.age 21 q.age 3 q.age

age
20
age
gpa
0
gpa
main
args
s
Student
129
Draw this
class Student int age 20 double
gpa public static void main(String args)
Student s new Student()
Student q new Student() q.age 19
s.age 21 q.age 3 q.age

age
age
20
20
age
gpa
gpa
0
0
gpa
main
args
s
q
Student
130
Draw this
class Student int age 20 double
gpa public static void main(String args)
Student s new Student()
Student q new Student() q.age 19
s.age 21 q.age 3 q.age

age
age
20
20
age
gpa
gpa
0
0
gpa
main
args
s
q
Student
131
Draw this
class Student int age 20 double
gpa public static void main(String args)
Student s new Student()
Student q new Student() q.age 19
s.age 21 q.age 3 q.age

age
age
20
20
age
gpa
gpa
0
0
gpa
main
args
s
q
Student
132
Draw this
class Student int age 20 double
gpa public static void main(String args)
Student s new Student()
Student q new Student() q.age 19
s.age 21 q.age 3 q.age

age
age
20
20
age
gpa
gpa
0
0
gpa
main
args
s
q
Student
133
Draw this
class Student int age 20 double
gpa public static void main(String args)
Student s new Student()
Student q new Student() q.age 19
s.age 21 q.age 3 q.age

age
age
20
19
age
gpa
gpa
0
0
gpa
main
args
s
q
Student
134
Draw this
class Student int age 20 double
gpa public static void main(String args)
Student s new Student()
Student q new Student() q.age 19
s.age 21 q.age 3 q.age

age
age
20
19
age
gpa
gpa
0
0
gpa
main
args
s
q
Student
135
Draw this
class Student int age 20 double
gpa public static void main(String args)
Student s new Student()
Student q new Student() q.age 19
s.age 21 q.age 3 q.age

age
age
21
19
age
gpa
gpa
0
0
gpa
main
args
s
q
Student
136
Draw this
class Student int age 20 double
gpa public static void main(String args)
Student s new Student()
Student q new Student() q.age 19
s.age 21 q.age 3 q.age

age
age
21
19
age
gpa
gpa
0
0
gpa
main
args
s
q
Student
137
Draw this
class Student int age 20 double
gpa public static void main(String args)
Student s new Student()
Student q new Student() q.age 19
s.age 21 q.age 3 q.age

age
age
21
19
age
gpa
gpa
0
0
gpa
main
args
s
q
Student
138
Draw this
class Student int age 20 double
gpa public static void main(String args)
Student s new Student()
Student q new Student() q.age 19
s.age 21 q.age 3 19

age
age
21
19
age
gpa
gpa
0
0
gpa
main
args
s
q
Student
139
Draw this
class Student int age 20 double
gpa public static void main(String args)
Student s new Student()
Student q new Student() q.age 19
s.age 21 q.age 22
age
age
21
19
age
gpa
gpa
0
0
gpa
main
args
s
q
Student
140
Draw this
class Student int age 20 double
gpa public static void main(String args)
Student s new Student()
Student q new Student() q.age 19
s.age 21 q.age 22
age
age
21
19
age
gpa
gpa
0
0
gpa
main
args
s
q
Student
141
Draw this
class Student int age 20 double
gpa public static void main(String args)
Student s new Student()
Student q new Student() q.age 19
s.age 21 q.age 3 q.age

age
age
21
22
age
gpa
gpa
0
0
gpa
main
args
s
q
Student
142
Now write this (in Java) Create two accounts,
for Doug and Adrian.
143
Now write this (in Java) Create two accounts,
for Doug and Adrian. Initial balance for both
accounts is 10.
144
Now write this (in Java) Create two accounts,
for Doug and Adrian. Initial balance for both
accounts is 10. Then Dougs account doubles,
Adrians decreases by 3 dollars, and Dougs
account has one more dollar deposited into it.
145
Let me try it. How about this?
146
Let me try it. How about this?
class Account double balance 10
public static void main(String args)
Account doug, adrian doug new
Account() adrian new Account()
doug.balance doug.balance 2
adrian.balance - 3 doug.balance

147
Create two accounts, for Doug and Adrian. Initial
balance for both accounts is 10. Then Dougs
account doubles, Adrians decreases by 3 dollars,
and Dougs account has one more dollar deposited
into it.
class Account double balance 10
public static void main(String args)
Account doug, adrian doug new
Account() adrian new Account()
doug.balance doug.balance 2
adrian.balance - 3 doug.balance

148
Create two accounts, for Doug and Adrian. Initial
balance for both accounts is 10. Then Dougs
account doubles, Adrians decreases by 3 dollars,
and Dougs account has one more dollar deposited
into it.
class Account double balance 10
public static void main(String args)
Account doug, adrian doug new
Account() adrian new Account()
doug.balance doug.balance 2
adrian.balance - 3 doug.balance

149
Create two accounts, for Doug and Adrian. Initial
balance for both accounts is 10. Then Dougs
account doubles, Adrians decreases by 3 dollars,
and Dougs account has one more dollar deposited
into it.
class Account double balance 10
public static void main(String args)
Account doug, adrian doug new
Account() adrian new Account()
doug.balance doug.balance 2
adrian.balance - 3 doug.balance

150
Create two accounts, for Doug and Adrian. Initial
balance for both accounts is 10. Then Dougs
account doubles, Adrians decreases by 3 dollars,
and Dougs account has one more dollar deposited
into it.
class Account double balance 10
public static void main(String args)
Account doug, adrian doug new
Account() adrian new Account()
doug.balance doug.balance 2
adrian.balance - 3 doug.balance

151
Create two accounts, for Doug and Adrian. Initial
balance for both accounts is 10. Then Dougs
account doubles, Adrians decreases by 3 dollars,
and Dougs account has one more dollar deposited
into it.
class Account double balance 10
public static void main(String args)
Account doug, adrian doug new
Account() adrian new Account()
doug.balance doug.balance 2
adrian.balance - 3 doug.balance

152
Create two accounts, for Doug and Adrian. Initial
balance for both accounts is 10. Then Dougs
account doubles, Adrians decreases by 3 dollars,
and Dougs account has one more dollar deposited
into it.
class Account double balance 10
public static void main(String args)
Account doug, adrian doug new
Account() adrian new Account()
doug.balance doug.balance 2
adrian.balance - 3 doug.balance

153
Create two accounts, for Doug and Adrian. Initial
balance for both accounts is 10. Then Dougs
account doubles, Adrians decreases by 3 dollars,
and Dougs account has one more dollar deposited
into it.
class Account double balance 10
public static void main(String args)
Account doug, adrian doug new
Account() adrian new Account()
doug.balance doug.balance 2
adrian.balance - 3 doug.balance

154
balance
class Account double balance 10
public static void main(String args)
Account doug, adrian doug new
Account() adrian new Account()
doug.balance doug.balance 2
adrian.balance - 3 doug.balance

Account
155
Looks good.
class Account double balance 10
public static void main(String args)
Account doug, adrian doug new
Account() adrian new Account()
doug.balance doug.balance 2
adrian.balance - 3 doug.balance

156
Looks good.
Thank you.
class Account double balance 10
public static void main(String args)
Account doug, adrian doug new
Account() adrian new Account()
doug.balance doug.balance 2
adrian.balance - 3 doug.balance

157
Draw this
158
Draw this
class BankAccount double balance 25
public static void main(String args)
BankAccount doug new BankAccount()
BankAccount adrian new BankAccount()
doug.balance doug.balance 2
adrian.balance - 3 doug.balance

159
Draw this
Too easy, almost the same thing...
class BankAccount double balance 25
public static void main(String args)
BankAccount doug new BankAccount()
BankAccount adrian new BankAccount()
doug.balance doug.balance 2
adrian.balance - 3 doug.balance

160
Draw this
161
Draw this
class Account double balance 20
void deposit(int n) this.balance n

162
Draw this
class Account double balance 20
void deposit(int n) this.balance n

Account
this.balance
deposit
n
this.balance n
163
Draw this
class Account double balance 20
void deposit(int n) this.balance n

Account
this.balance
deposit
n

this.balance n
164
Draw this
balance is an instance variable.
class Account double balance 20
void deposit(int n) this.balance n

Account
165
Draw this
balance is an instance variable. It belongs to
the object.
class Account double balance 20
void deposit(int n) this.balance n

Account
166
Draw this
deposit is an instance method. It belongs to the
object and describes an update
on its instance variables (state).
class Account double balance 20
void deposit(int n) this.balance n

Account
167
Draw this
n is not a local variable in deposit.
class Account double balance 20
void deposit(int n) this.balance n

Account
168
Draw this
It is called a parameter of the method
deposit. It is a way of naming the methods
inputs. It is a way to refer to the ingredients
of the recipe.
class Account double balance 20
void deposit(int n) this.balance n

Account
169
Draw this
The keyword this allows an object to refer to
itself.
class Account double balance 20
void deposit(int n) this.balance n

Account
170
Draw this
class Account double balance 20
void deposit(int n) this.balance n
public static void main(String
args) Account m new Account()
Account q new Account()
m.deposit(10) q.deposit(3)
Account
171
Draw this
Account
class Account double balance 20
void deposit(int n) this.balance n
public static void main(String
args) Account m new Account()
Account q new Account()
m.deposit(10) q.deposit(3)
main
args
172
Draw this
Account
class Account double balance 20
void deposit(int n) this.balance n
public static void main(String
args) Account m new Account()
Account q new Account()
m.deposit(10) q.deposit(3)
20

main
args
m
173
Draw this
Account
class Account double balance 20
void deposit(int n) this.balance n
public static void main(String
args) Account m new Account()
Account q new Account()
m.deposit(10) q.deposit(3)
20
20


main
args
m
q
174
Draw this
Account
class Account double balance 20
void deposit(int n) this.balance n
public static void main(String
args) Account m new Account()
Account q new Account()
m.deposit(10) q.deposit(3)
20
20


main
args
m
q
175
Draw this
Account
class Account double balance 20
void deposit(int n) this.balance n
public static void main(String
args) Account m new Account()
Account q new Account()
m.deposit(10) q.deposit(3)
20
20


main
args
m
q
176
Draw this
Account
class Account double balance 20
void deposit(int n) this.balance n
public static void main(String
args) Account m new Account()
Account q new Account()
m.deposit(10) q.deposit(3)
20
20


main
args
m
q
177
Draw this
Account
class Account double balance 20
void deposit(int n) this.balance n
public static void main(String
args) Account m new Account()
Account q new Account()
m.deposit(10) q.deposit(3)
20
20


main
args
m
q
178
Draw this
Account
class Account double balance 20
void deposit(int n) this.balance n
public static void main(String
args) Account m new Account()
Account q new Account()
m.deposit(10) q.deposit(3)
20
20
10


main
args
m
q
179
Draw this
Account
class Account double balance 20
void deposit(int n) this.balance n
public static void main(String
args) Account m new Account()
Account q new Account()
m.deposit(10) q.deposit(3)
30
20


main
args
m
q
180
Draw this
Account
class Account double balance 20
void deposit(int n) this.balance n
public static void main(String
args) Account m new Account()
Account q new Account()
m.deposit(10) q.deposit(3)
30
20


main
args
m
q
181
Draw this
Account
class Account double balance 20
void deposit(int n) this.balance n
public static void main(String
args) Account m new Account()
Account q new Account()
m.deposit(10) q.deposit(3)
30
20


main
args
m
q
182
Draw this
Account
class Account double balance 20
void deposit(int n) this.balance n
public static void main(String
args) Account m new Account()
Account q new Account()
m.deposit(10) q.deposit(3)
30
20
3


main
args
m
q
183
Draw this
Account
class Account double balance 20
void deposit(int n) this.balance n
public static void main(String
args) Account m new Account()
Account q new Account()
m.deposit(10) q.deposit(3)
30
23


main
args
m
q
184
Draw this
class Account double balance
Account(double x) public static
void main(String args) Account m
new Account(10) Account q new
Account(3) m.balance 10 m.balance

185
Draw this
This is a constructor.
class Account double balance
Account(double x) public static
void main(String args) Account m
new Account(10) Account q new
Account(3) m.balance 10 m.balance

186
Draw this
Its like an initialization procedure.
class Account double balance
Account(double x) public static
void main(String args) Account m
new Account(10) Account q new
Account(3) m.balance 10 m.balance

187
Draw this
Its attached to the blueprint
class Account double balance
Account(double x) public static
void main(String args) Account m
new Account(10) Account q new
Account(3) m.balance 10 m.balance

188
Draw this
Its attached to the blueprint (not instances)
class Account double balance
Account(double x) public static
void main(String args) Account m
new Account(10) Account q new
Account(3) m.balance 10 m.balance

189
Draw this
Its attached to the blueprint (not
instances) This one is empty.
class Account double balance
Account(double x) public static
void main(String args) Account m
new Account(10) Account q new
Account(3) m.balance 10 m.balance

190
Draw this
Account
class Account double balance
Account(double x) public static
void main(String args) Account m
new Account(10) Account q new
Account(3) m.balance 10 m.balance

this.balance
Account
x
main
args
191
Draw this
Account
class Account double balance
Account(double x) public static
void main(String args) Account m
new Account(10) Account q new
Account(3) m.balance 10 m.balance

this.balance
Account
10
x
0
balance
main
args
m
192
Draw this
Account
class Account double balance
Account(double x) public static
void main(String args) Account m
new Account(10) Account q new
Account(3) m.balance 10 m.balance

this.balance
Account
x
0
balance
main
args
m
193
Draw this
Account
class Account double balance
Account(double x) public static
void main(String args) Account m
new Account(10) Account q new
Account(3) m.balance 10 m.balance

this.balance
Account
x
0
0
balance
balance
main
args
m
q
194
Draw this
Account
class Account double balance
Account(double x) public static
void main(String args) Account m
new Account(10) Account q new
Account(3) m.balance 10 m.balance

this.balance
Account
x
0
0
balance
balance
main
args
m
q
195
Draw this
Account
class Account double balance
Account(double x) public static
void main(String args) Account m
new Account(10) Account q new
Account(3) m.balance 10 m.balance

this.balance
Account
x
0
0
balance
balance
main
args
m
q
10 m.balance
196
Draw this
Account
class Account double balance
Account(double x) public static
void main(String args) Account m
new Account(10) Account q new
Account(3) m.balance 10 m.balance

this.balance
Account
x
0
0
balance
balance
main
args
m
q
10 0
197
Draw this
Account
class Account double balance
Account(double x) public static
void main(String args) Account m
new Account(10) Account q new
Account(3) m.balance 10 m.balance

this.balance
Account
x
0
0
balance
balance
main
args
m
q
10
198
Draw this
Account
class Account double balance
Account(double x) public static
void main(String args) Account m
new Account(10) Account q new
Account(3) m.balance 10 m.balance

this.balance
Account
x
0
10
balance
balance
main
args
m
q
199
Draw this
class X int x
200
How easy.
Draw this
X
class X int x
x
201
Draw this
class A int x A (int initialValue)
this.x initialValue / do this
when you first create the object but
dont make it part of the generated instance /
202
Draw this
Thats called a constructor.
class A int x A (int initialValue)
this.x initialValue / do this
when you first create the object but
dont make it part of the generated instance /
A
x
A
initialValue
203
Draw this
Thats called a constructor.
class A int x A (int initialValue)
this.x initialValue / do this
when you first create the object but
dont make it part of the generated instance /
A
x
A
initialValue
204
Draw this
Notice the name of the constructor.
class A int x A (int initialValue)
this.x initialValue / do this
when you first create the object but
dont make it part of the generated instance /
A
x
A
initialValue
205
Draw this
Notice the name of the constructor. Its the name
of the class.
class A int x A (int initialValue)
this.x initialValue / do this
when you first create the object but
dont make it part of the generated instance /
A
x
A
initialValue
206
Draw this
Notice the name of the constructor. Its the name
of the class. A constructor looks like a method
(procedure).
class A int x A (int initialValue)
this.x initialValue / do this
when you first create the object but
dont make it part of the generated instance /
A
x
A
initialValue
207
Draw this
Notice the name of the constructor. Its the name
of the class. A constructor looks like a method
(procedure). But doesnt have a return type.
class A int x A (int initialValue)
this.x initialValue / do this
when you first create the object but
dont make it part of the generated instance /
A
x
A
initialValue
208
Draw this
Heres how the constructor works
class A int x A (int initialValue)
this.x initialValue / do this
when you first create the object but
dont make it part of the generated instance /
A
x
A
initialValue
209
Draw this
Heres how the constructor works
class A int x A (int initialValue)
this.x initialValue / do this
when you first create the object but
dont make it part of the generated instance /
A
x
A
initialValue
this.x initialValue
210
Draw this
When the constructor is invoked, a value is
passed (as an argument) to the constructor.
class A int x A (int initialValue)
this.x initialValue / do this
when you first create the object but
dont make it part of the generated instance /
A
x
A
initialValue
this.x initialValue
211
Draw this
The constructor then starts executing.
class A int x A (int initialValue)
this.x initialValue / do this
when you first create the object but
dont make it part of the generated instance /
A
x
A
initialValue
this.x initialValue
212
Draw this
The constructor then starts executing. The value
is taken from the argument,
class A int x A (int initialValue)
this.x initialValue / do this
when you first create the object but
dont make it part of the generated instance /
A
x
A
initialValue
this.x initialValue
213
Draw this
The constructor then starts executing. The value
is taken from the argument, into the instance
variable.
class A int x A (int initialValue)
this.x initialValue / do this
when you first create the object but
dont make it part of the generated instance /
A
x
A
initialValue
this.x initialValue
214
Draw this
Thus the instance variable will have
been initialized with what is in initialValue.
class A int x A (int initialValue)
this.x initialValue / do this
when you first create the object but
dont make it part of the generated instance /
A
x
initialValue
A
initialValue
this.x initialValue
215
Speaking of this, can I show you an example?
216
Sure, go ahead.
217
Sure, go ahead.
Here it is
class Speakers public static void
main(String args) Speaker a new
Speaker(Larry) Speaker b new
Speaker(Michael) Speaker c new
Speaker(Toni) a.talk()
b.talk() c.talk() class
Speaker String speaker Speaker(String
name) speaker name void
talk() System.out.println(this.speaker
is very happy.)
218
This writer is very happy.
Here it is
class Speakers public static void
main(String args) Speaker a new
Speaker(Larry) Speaker b new
Speaker(Michael) Speaker c new
Speaker(Toni) a.talk()
b.talk() c.talk() class
Speaker String speaker Speaker(String
name) speaker name void
talk() System.out.println(this.speaker
is very happy.)
219
This writer is very happy.
Ha, ha. Draw this
class Student String name
Student(String w) this.name w
class Tuesday public static void
main(String args) Student q new
Student(Larry) Student u new
Student(John) Student s new
Student(Adrian)
220
Thats funny.
Ha, ha. Draw this
class Student String name
Student(String w) this.name w
class Tuesday public static void
main(String args) Student q new
Student(Larry) Student u new
Student(John) Student s new
Student(Adrian)
221
Thats funny.
Yes.
class Student String name
Student(String w) this.name w
class Tuesday public static void
main(String args) Student q new
Student(Larry) Student u new
Student(John) Student s new
Student(Adrian)
We should always try to refer to instance
variables through the instance they belong to.
222
Thats funny.
Yes.
class Student String name
Student(String w) this.name w
class Tuesday public static void
main(String args) Student q new
Student(Larry) Student u new
Student(John) Student s new
Student(Adrian)
We should always try to refer to instance
variables through the instance they belong
to. This way we would also be able to keep them
separate from local variables and
parameters (or arguments).
223
Draw this
class Checking double balance
Checking(double x) this.balance x
void deposit(double y)
this.balance this.balance y
224
Draw this
class Tea double bird void
book(double watch) this.bird watch
this.bird Tea (double snake)
this.bird snake 2
225
Draw this
instance variable
class Tea double bird void
book(double watch) this.bird watch
this.bird Tea (double snake)
this.bird snake 2
226
Draw this
instance method
class Tea double bird void
book(double watch) this.bird watch
this.bird Tea (double snake)
this.bird snake 2
227
Draw this
formal parameter
class Tea double bird void
book(double watch) this.bird watch
this.bird Tea (double snake)
this.bird snake 2
228
Draw this
formal parameter (like a local variable only
initialized when invoked)
class Tea double bird void
book(double watch) this.bird watch
this.bird Tea (double snake)
this.bird snake 2
229
Draw this
class Tea double bird void
book(double watch) this.bird watch
this.bird Tea (double snake)
this.bird snake 2
constructor
230
Draw this
class Tea double bird void
book(double watch) this.bird watch
this.bird Tea (double snake)
this.bird snake 2
231
Draw this
class Hat public static void main(String
args) Tea m new Tea(-2)
Tea n new Tea(42) m.book(92)
n.bird m.bird n.bird n.book(25)
n.book(25)
232
Draw this
class Hat public static void main(String
args) Tea m new Tea(-2)
Tea n new Tea(42) m.book(92)
n.bird m.bird n.bird n.book(25)
n.book(25)
class Tea double bird void
book(double watch) this.bird watch
this.bird Tea (double snake)
this.bird snake 2
Where Tea is the class seen before.
233
Draw this
class Hat public static void main(String
args) Tea m new Tea(-2)
Tea n new Tea(42) m.book(92)
n.bird m.bird n.bird n.book(25)
n.book(25)
Where Tea is the class seen before.
class Tea double bird void
book(double watch) this.bird watch
this.bird Tea (double snake)
this.bird snake 2
(Soliloquy p. 133) We should also use BlueJ to
examine this example thoroughly.
234
Last example
class X int q class Play public
static void main(String args) X u
new X() X p new X() u.q
3 p.q u.q p.q u.q u.q
p.q
235
Last example
This should be easy, by now.
class X int q class Play public
static void main(String args) X u
new X() X p new X() u.q
3 p.q u.q p.q u.q u.q
p.q
236
(No Transcript)
237
So what types do we have in Lab Eight (for
example)?
238
So what types do we have in Lab Eight (for
example)?
Point
239
So what types do we have in Lab Eight (for
example)?
Point Circle
240
So what types do we have in Lab Eight (for
example)?
Point Circle Rectangle
241
So what types do we have in Lab Eight (for
example)?
Point Circle Rectangle
We should also discuss Robot (to understand
Penguin).
242
Thank you.
243
Thank you.
Write a Comment
User Comments (0)
About PowerShow.com