java programs are built from classes. there is nothing else, but classes

243

Upload: godwin-ford

Post on 20-Jan-2016

228 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Java programs are built from classes. There is nothing else, but classes
Page 2: Java programs are built from classes. There is nothing else, but classes

Java programs are built from classes.

Page 3: Java programs are built from classes. There is nothing else, but classes

Java programs are built from classes.

There is nothing else, but classes.

Page 4: Java programs are built from classes. There is nothing else, but classes
Page 5: Java programs are built from classes. There is nothing else, but classes

A class contains members:

Page 6: Java programs are built from classes. There is nothing else, but classes

A class contains members:

a) data (variables)

Page 7: Java programs are built from classes. There is nothing else, but classes

A class contains members:

a) data (variables)

b) procedures (methods)

Page 8: Java programs are built from classes. There is nothing else, but classes
Page 9: Java programs are built from classes. There is nothing else, but classes

Members could be static or not static.

Page 10: Java programs are built from classes. There is nothing else, but classes
Page 11: Java programs are built from classes. There is nothing else, but classes

End of story.

Page 12: Java programs are built from classes. There is nothing else, but classes

Let’s see some examples now.

Page 13: Java programs are built from classes. There is nothing else, but classes

class One {

}

Draw this:

Page 14: Java programs are built from classes. There is nothing else, but classes

class One {

}

One

Draw this: Don’t forget the blueprint (even though it’s empty).

Page 15: Java programs are built from classes. There is nothing else, but classes

class Two { int u; double q;}

Draw this:

Page 16: Java programs are built from classes. There is nothing else, but classes

class Two { int u; double q;}

Two

Draw this: Notice that the blueprint is no longer empty:

Page 17: Java programs are built from classes. There is nothing else, but classes

class Two { int u; double q;}

Two

u

q

Draw this: Notice that the blueprint is no longer empty:

Page 18: Java programs are built from classes. There is nothing else, but classes

class Two { int u; double q;}

Two

u

q

Draw this: Notice that the blueprint is no longer empty:

u and q are called instance variables

Page 19: Java programs are built from classes. There is nothing else, but classes

class Student {

}

Draw this:

Page 20: Java programs are built from classes. There is nothing else, but classes

class Student {

}

Student

Draw this:

Easy. Student or One – same difference.

Page 21: Java programs are built from classes. There is nothing else, but classes

class Student { int age; }

Now draw this:

Page 22: Java programs are built from classes. There is nothing else, but classes

class Student { int age; }

Now draw this:

Student

age

Page 23: Java programs are built from classes. There is nothing else, but classes

class Z { int m; double n; }

Draw this:

Page 24: Java programs are built from classes. There is nothing else, but classes

class Z { int m; double n; }

Draw this:

Z

m

n

Page 25: Java programs are built from classes. There is nothing else, but classes

class M { int m; static double n; }

Draw this:

Page 26: Java programs are built from classes. There is nothing else, but classes

class M { int m; static double n; }

Draw this:

M

mn

Page 27: Java programs are built from classes. There is nothing else, but classes

class M { int m; static double n; }

Draw this:

M

m

n

Page 28: Java programs are built from classes. There is nothing else, but classes

class M { int m; double n; }

Draw this:

M

m

n

Page 29: Java programs are built from classes. There is nothing else, but classes

class M { int m; static double n; }

Draw this:

M

mn

Page 30: Java programs are built from classes. There is nothing else, but classes

class Q { int v; double z; public static void main(String[ ] args) {

} }

Draw this:

Page 31: Java programs are built from classes. There is nothing else, but classes

class Q { int v; double z; public static void main(String[ ] args) {

} }

Draw this:

For this the picture is bigger:

Page 32: Java programs are built from classes. There is nothing else, but classes

class Q { int v; double z; public static void main(String[ ] args) {

} }

Draw this:

For this the picture is bigger:

Qv

zmain

args

Page 33: Java programs are built from classes. There is nothing else, but classes

class Q { int v; double z; public static void main(String[ ] args) {

} }

Draw this:

For this the picture is bigger:

Qv

zmain

args

Page 34: Java programs are built from classes. There is nothing else, but classes

class Q { int v; double z; public static void main(String[ ] args) {

} }

Draw this:

For this the picture is bigger:

Qv

zmain

args

Page 35: Java programs are built from classes. There is nothing else, but classes

class E { int v; double z; public static void main(String[ ] args) { int q; q = 3; } }

Draw this:

Page 36: Java programs are built from classes. There is nothing else, but classes

class E { int v; double z; public static void main(String[ ] args) { int q; q = 3; } }

Draw this:

Same as above, only main is no longer empty:

Page 37: Java programs are built from classes. There is nothing else, but classes

class E { int v; double z; public static void main(String[ ] args) { int q; q = 3; } }

Draw this:

Same as above, only main is no longer empty:

Ev

z

Page 38: Java programs are built from classes. There is nothing else, but classes

class E { int v; double z; public static void main(String[ ] args) { int q; q = 3; } }

Draw this:

Same as above, only main is no longer empty:

Ev

zmain

args

Page 39: Java programs are built from classes. There is nothing else, but classes

class E { int v; double z; public static void main(String[ ] args) { int q; q = 3; } }

Draw this:

Same as above, only main is no longer empty:

Ev

zmain

argsint q;

Page 40: Java programs are built from classes. There is nothing else, but classes

class E { int v; double z; public static void main(String[ ] args) { int q; q = 3; } }

Draw this:

Same as above, only main is no longer empty:

Ev

zmain

argsint q;

q is a local variable.

Page 41: Java programs are built from classes. There is nothing else, but classes

class E { int v; double z; public static void main(String[ ] args) { int q; q = 3; } }

Draw this:

Same as above, only main is no longer empty:

Ev

zmain

argsint q;

q is a local variable. It is local to main.

Page 42: Java programs are built from classes. There is nothing else, but classes

class E { int v; double z; public static void main(String[ ] args) { int q; q = 3; } }

Draw this:

Same as above, only main is no longer empty:

Ev

zmain

argsint q;

q = 3;

Page 43: Java programs are built from classes. There is nothing else, but classes

class E { int v; double z; public static void main(String[ ] args) { int q; q = 3; } }

Draw this:

Same as above, only main is no longer empty:

Ev

zmain

argsint q;

q = 3;

Page 44: Java programs are built from classes. There is nothing else, but classes

class E { int v; double z; public static void main(String[ ] args) { int q; q = 3; } }

Draw this:

Same as above, only main is no longer empty:

Ev

zmain

argsint q;

q = 3;

3

Page 45: Java programs are built from classes. There is nothing else, but classes

class Student { int age; double gpa; public static void main(String[ ] args) { int q = 21; String n = “John”; } }

Draw this:

Page 46: Java programs are built from classes. There is nothing else, but classes

class Student { int age; double gpa; public static void main(String[ ] args) { int q = 21; String n = “John”; } }

Draw this:

Student

age

gpamain

args

Page 47: Java programs are built from classes. There is nothing else, but classes

class Student { int age; double gpa; public static void main(String[ ] args) { int q = 21; String n = “John”; } }

Draw this:

Student

age

gpamain

argsq

Page 48: Java programs are built from classes. There is nothing else, but classes

class Student { int age; double gpa; public static void main(String[ ] args) { int q = 21; String n = “John”; } }

Draw this:

Student

age

gpamain

argsq 21

Page 49: Java programs are built from classes. There is nothing else, but classes

class Student { int age; double gpa; public static void main(String[ ] args) { int q = 21; String n = “John”; } }

Draw this:

Student

age

gpamain

argsq 21

n

Page 50: Java programs are built from classes. There is nothing else, but classes

class Student { int age; double gpa; public static void main(String[ ] args) { int q = 21; String n = “John”; } }

Draw this:

Student

age

gpamain

argsq 21

n

Page 51: Java programs are built from classes. There is nothing else, but classes

class Student { int age; double gpa; public static void main(String[ ] args) { int q = 21; String n = “John”; } }

Draw this:

Student

age

gpamain

argsq 21

n

java.lang.String

“John”

Page 52: Java programs are built from classes. There is nothing else, but classes

class Student { int age; double gpa; public static void main(String[ ] args) { int q = 21; String n = “John”; } }

Draw this:

Student

age

gpamain

argsq 21

n

java.lang.String

“John”

String is a reference type.

Page 53: Java programs are built from classes. There is nothing else, but classes

Types in Java:

Page 54: Java programs are built from classes. There is nothing else, but classes

Types in Java:

a) primitive

Page 55: Java programs are built from classes. There is nothing else, but classes

Types in Java:

a) primitive (int, double, boolean, char)

Page 56: Java programs are built from classes. There is nothing else, but classes

Types in Java:

a) primitive (int, double, boolean, char)

b) reference types

Page 57: Java programs are built from classes. There is nothing else, but classes

Types in Java:

a) primitive (int, double, boolean, char)

b) reference types (classes)

Page 58: Java programs are built from classes. There is nothing else, but classes

Types in Java:

a) primitive (int, double, boolean, char)

int x;

Page 59: Java programs are built from classes. There is nothing else, but classes

Types in Java:

a) primitive (int, double, boolean, char)

int x;

int y;

Page 60: Java programs are built from classes. There is nothing else, but classes

Types in Java:

a) primitive (int, double, boolean, char)

int x;

int y;

x = 5;

Page 61: Java programs are built from classes. There is nothing else, but classes

Types in Java:

a) primitive (int, double, boolean, char)

int x;

int y;

x = 5;

Page 62: Java programs are built from classes. There is nothing else, but classes

Types in Java:

a) primitive (int, double, boolean, char)

int x;

int y;

x = 5;

5

Page 63: Java programs are built from classes. There is nothing else, but classes

Types in Java:

a) primitive (int, double, boolean, char)

int x;

int y;

x = 5;

5

y = x;

Page 64: Java programs are built from classes. There is nothing else, but classes

Types in Java:

a) primitive (int, double, boolean, char)

int x;

int y;

x = 5;

5

y = x;

Page 65: Java programs are built from classes. There is nothing else, but classes

Types in Java:

a) primitive (int, double, boolean, char)

int x;

int y;

x = 5;

5

y = x;

5

Page 66: Java programs are built from classes. There is nothing else, but classes

Types in Java:

a) primitive (int, double, boolean, char)

int x;

int y;

x = 5;

5

y = x;

5

x = x + 2;

Page 67: Java programs are built from classes. There is nothing else, but classes

Types in Java:

a) primitive (int, double, boolean, char)

int x;

int y;

x = 5;

5

y = x;

5

x = x + 2;

Page 68: Java programs are built from classes. There is nothing else, but classes

Types in Java:

a) primitive (int, double, boolean, char)

int x;

int y;

x = 5;

5

y = x;

5

x = 5 + 2;

Page 69: Java programs are built from classes. There is nothing else, but classes

Types in Java:

a) primitive (int, double, boolean, char)

int x;

int y;

x = 5;

5

y = x;

5

x = 5 + 2;

Page 70: Java programs are built from classes. There is nothing else, but classes

Types in Java:

a) primitive (int, double, boolean, char)

int x;

int y;

x = 5;

5

y = x;

5

x = 5 + 2;

Page 71: Java programs are built from classes. There is nothing else, but classes

Types in Java:

a) primitive (int, double, boolean, char)

int x;

int y;

x = 5;

7

y = x;

5

x = x + 2;

Page 72: Java programs are built from classes. There is nothing else, but classes

Types in Java:

a) primitive (int, double, boolean, char)

int x;

int y;

x = 5;

7

y = x;

5

x = x + 2;

But this doesn’t change y.

Page 73: Java programs are built from classes. There is nothing else, but classes

Types in Java:

a) primitive (int, double, boolean, char)

int x;

int y;

x = 5;

7

y = x;

5

x = x + 2;

But this doesn’t change y.

And that’s because y is a copy of x.

Page 74: Java programs are built from classes. There is nothing else, but classes

Types in Java:

a) primitive (int, double, boolean, char)

int x;

int y;

x = 5;

7

y = x;

5

x = x + 2;

But this doesn’t change y.

And that’s because y is a copy of x.

Things don’t work the same with reference types.

Page 75: Java programs are built from classes. There is nothing else, but classes

Types in Java:

a) primitive (int, double, boolean, char)

int x;

int y;

x = 5;

7

y = x;

5

x = x + 2;

But this doesn’t change y.

And that’s because y is a copy of x.

But the above is true of primitive types:

int (long, short, byte)double (float)charboolean

Page 76: Java programs are built from classes. There is nothing else, but classes

Types in Java:

a) primitive (int, double, boolean, char)

b) reference types

Page 77: Java programs are built from classes. There is nothing else, but classes

Types in Java:

a) primitive (int, double, boolean, char)

b) reference types

Penguin x;

Page 78: Java programs are built from classes. There is nothing else, but classes

Types in Java:

a) primitive (int, double, boolean, char)

b) reference types

Penguin x;

x = new Penguin();

Page 79: Java programs are built from classes. There is nothing else, but classes

Types in Java:

a) primitive (int, double, boolean, char)

b) reference types

Penguin x;

x = new Penguin();

Page 80: Java programs are built from classes. There is nothing else, but classes

Types in Java:

a) primitive (int, double, boolean, char)

b) reference types

Penguin x;

x = new Penguin();

Page 81: Java programs are built from classes. There is nothing else, but classes

Types in Java:

a) primitive (int, double, boolean, char)

b) reference types

Penguin x;

x = new Penguin();

Penguin y;

Page 82: Java programs are built from classes. There is nothing else, but classes

Types in Java:

a) primitive (int, double, boolean, char)

b) reference types

Penguin x;

x = new Penguin();

Penguin y;

y = x;

Page 83: Java programs are built from classes. There is nothing else, but classes

Types in Java:

a) primitive (int, double, boolean, char)

b) reference types

Penguin x;

x = new Penguin();

Penguin y;

y = x;

Page 84: Java programs are built from classes. There is nothing else, but classes

Types in Java:

a) primitive (int, double, boolean, char)

b) reference types

Penguin x;

x = new Penguin();

Penguin y;

y = x;

Page 85: Java programs are built from classes. There is nothing else, but classes

Types in Java:

a) primitive (int, double, boolean, char)

b) reference types

Penguin x;

x = new Penguin();

Penguin y;

y = x;

x.turnLeft();

Page 86: Java programs are built from classes. There is nothing else, but classes

Types in Java:

a) primitive (int, double, boolean, char)

b) reference types

Penguin x;

x = new Penguin();

Penguin y;

y = x;

x.turnLeft();

Page 87: Java programs are built from classes. There is nothing else, but classes

Types in Java:

a) primitive (int, double, boolean, char)

b) reference types

Penguin x;

x = new Penguin();

Penguin y;

y = x;

x.turnLeft();

Page 88: Java programs are built from classes. There is nothing else, but classes

Types in Java:

a) primitive (int, double, boolean, char)

b) reference types

Penguin x;

x = new Penguin();

Penguin y;

y = x;

x.turnLeft();

But that also changes the value of y.

Page 89: Java programs are built from classes. There is nothing else, but classes

Types in Java:

a) primitive (int, double, boolean, char)

b) reference types

Penguin x;

x = new Penguin();

Penguin y;

y = x;

x.turnLeft();

But that also changes the value of y.

The Penguin object is only one, and shared.

Page 90: Java programs are built from classes. There is nothing else, but classes

Types in Java:

a) primitive (int, double, boolean, char)

b) reference types

Page 91: Java programs are built from classes. There is nothing else, but classes

Types in Java:

a) primitive (int, double, boolean, char)

b) reference types (classes)

Page 92: Java programs are built from classes. There is nothing else, but classes

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.

Page 93: Java programs are built from classes. There is nothing else, but classes

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) doesn’t.

Page 94: Java programs are built from classes. There is nothing else, but classes

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) doesn’t.

For objects, a reference to them is kept in the variable.

Page 95: Java programs are built from classes. There is nothing else, but classes

Let’s continue with the examples.

Page 96: Java programs are built from classes. There is nothing else, but classes

class J { int x; public static void main(String[ ] args) { J z = new J(); int x = 5; z.x = 5; } }

Draw this:

Page 97: Java programs are built from classes. There is nothing else, but classes

class J { int x; public static void main(String[ ] args) { J z = new J(); int x = 5; z.x = 5; } }

Draw this:

main

args

x

J

Page 98: Java programs are built from classes. There is nothing else, but classes

class J { int x; public static void main(String[ ] args) { J z = new J(); int x = 5; z.x = 5; } }

Draw this:

main

args

x

J

z

Page 99: Java programs are built from classes. There is nothing else, but classes

class J { int x; public static void main(String[ ] args) { J z = new J(); int x = 5; z.x = 5; } }

Draw this:

main

args

x

J

z

x0

Page 100: Java programs are built from classes. There is nothing else, but classes

class J { int x; public static void main(String[ ] args) { J z = new J(); int x = 5; z.x = 5; } }

Draw this:

main

args

x

J

z

x0

Page 101: Java programs are built from classes. There is nothing else, but classes

class J { int x; public static void main(String[ ] args) { J z = new J(); int x = 5; z.x = 5; } }

Draw this:

main

args

x

J

z

x

x

0

Page 102: Java programs are built from classes. There is nothing else, but classes

class J { int x; public static void main(String[ ] args) { J z = new J(); int x = 5; z.x = 5; } }

Draw this:

main

args

x

J

z

x

x 5

0

Page 103: Java programs are built from classes. There is nothing else, but classes

class J { int x; public static void main(String[ ] args) { J z = new J(); int x = 5; z.x = 5; } }

Draw this:

main

args

x

J

z

x

x 5

0

Page 104: Java programs are built from classes. There is nothing else, but classes

class J { int x; public static void main(String[ ] args) { J z = new J(); int x = 5; z.x = 5; } }

Draw this:

main

args

x

J

z

x

x 5

0

Page 105: Java programs are built from classes. There is nothing else, but classes

class J { int x; public static void main(String[ ] args) { J z = new J(); int x = 5; z.x = 5; } }

Draw this:

main

args

x

J

z

x

x 5

0

Page 106: Java programs are built from classes. There is nothing else, but classes

class J { int x; public static void main(String[ ] args) { J z = new J(); int x = 5; z.x = 5; } }

Draw this:

main

args

x

J

z

x

x 5

0

Page 107: Java programs are built from classes. There is nothing else, but classes

class J { int x; public static void main(String[ ] args) { J z = new J(); int x = 5; z.x = 5; } }

Draw this:

main

args

x

J

z

x

x 5

5

Page 108: Java programs are built from classes. There is nothing else, but classes

class J { int x; public static void main(String[ ] args) { J z = new J(); int x = 5; z.x = 5; } }

Draw this:

main

args

x

J

z

x

x 5

5

Page 109: Java programs are built from classes. There is nothing else, but classes

Draw this:

Page 110: Java programs are built from classes. There is nothing else, but classes

Draw this:

class Student { int age; double gpa; public static void main(String[ ] args) { int q; String n; Student s; s = new Student(); } }

Page 111: Java programs are built from classes. There is nothing else, but classes

Draw this:

class Student { int age; double gpa; public static void main(String[ ] args) { int q; String n; Student s; s = new Student(); } }

What kind of variables are age and gpa?

Page 112: Java programs are built from classes. There is nothing else, but classes

Draw this:

class Student { int age; double gpa; public static void main(String[ ] args) { int q; String n; Student s; s = new Student(); } }

Yes, they are instance variables.

Page 113: Java programs are built from classes. There is nothing else, but classes

Draw this:

class Student { int age; double gpa; public static void main(String[ ] args) { int q; String n; Student s; s = new Student(); } }

What kind of variables are q, n, and s?

Page 114: Java programs are built from classes. There is nothing else, but classes

Draw this:

class Student { int age; double gpa; public static void main(String[ ] args) { int q; String n; Student s; s = new Student(); } }

Exactly, they are local variables.

Page 115: Java programs are built from classes. There is nothing else, but classes

Draw this:

class Student { int age; double gpa; public static void main(String[ ] args) { int q; String n; Student s; s = new Student(); } }

They’re local to main.

Page 116: Java programs are built from classes. There is nothing else, but classes

Draw this:

class Student { int age; double gpa; public static void main(String[ ] args) { int q; String n; Student s; s = new Student(); } }

Now let’s draw!

Page 117: Java programs are built from classes. There is nothing else, but classes

Draw this:

class Student { int age; double gpa; public static void main(String[ ] args) { int q; String n; Student s; s = new Student(); } }

Student

age

gpa

main

argsq

Page 118: Java programs are built from classes. There is nothing else, but classes

Draw this:

class Student { int age; double gpa; public static void main(String[ ] args) { int q; String n; Student s; s = new Student(); } }

Student

age

gpa

main

argsq

n

Page 119: Java programs are built from classes. There is nothing else, but classes

Draw this:

class Student { int age; double gpa; public static void main(String[ ] args) { int q; String n; Student s; s = new Student(); } }

Student

age

gpa

main

argsq

n

s

Page 120: Java programs are built from classes. There is nothing else, but classes

Draw this:

class Student { int age; double gpa; public static void main(String[ ] args) { int q; String n; Student s; s = new Student(); } }

Student

age

gpa

main

argsq

n

s

age

gpa 0

0

Page 121: Java programs are built from classes. There is nothing else, but classes

Draw this:

class Student { int age; double gpa; public static void main(String[ ] args) { int q; String n; Student s; s = new Student(); } }

Student

age

gpa

main

argsq

n

s

age

gpa 0

0

Page 122: Java programs are built from classes. There is nothing else, but classes

Draw this:

class Student { int age; double gpa; public static void main(String[ ] args) { int q; String n; Student s; s = new Student(); } }

Student

age

gpa

main

argsq

n

s

age

gpa 0

0

Notice that q and n have no values in them.

Page 123: Java programs are built from classes. There is nothing else, but classes

Draw this:

class Student { int age; double gpa; public static void main(String[ ] args) { int q; String n; Student s; s = new Student(); } }

Student

age

gpa

main

argsq

n

s

age

gpa 0

0

Notice that q and n have no values in them.Unlike instance or class variables, local variablesneed to be initialized explicitly by the programmer.

Page 124: Java programs are built from classes. There is nothing else, but classes

Draw this:

class Student { int age; double gpa; public static void main(String[ ] args) { int q; String n; Student s; s = new Student(); } }

Student

age

gpa

main

argsq

n

s

age

gpa 0

0

Instance and class variables have default values.

Page 125: Java programs are built from classes. There is nothing else, but classes

Draw this:

class Student { int age; double gpa; public static void main(String[ ] args) { int q; String n; Student s; s = new Student(); } }

Student

age

gpa

main

argsq

n

s

age

gpa 0

0

Instance and class variables have default values.It’s not hard to guess the default values.

Page 126: Java programs are built from classes. There is nothing else, but classes

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; } }

Page 127: Java programs are built from classes. There is nothing else, but classes

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; } }

Student

age

gpa

main

args

Page 128: Java programs are built from classes. There is nothing else, but classes

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; } }

Student

age

gpa

main

argss

age

gpa 0

20

Page 129: Java programs are built from classes. There is nothing else, but classes

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; } }

Student

age

gpa

main

argss

q

age

gpa 0

20age

gpa 0

20

Page 130: Java programs are built from classes. There is nothing else, but classes

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; } }

Student

age

gpa

main

argss

q

age

gpa 0

20age

gpa 0

20

Page 131: Java programs are built from classes. There is nothing else, but classes

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; } }

Student

age

gpa

main

argss

q

age

gpa 0

20age

gpa 0

20

Page 132: Java programs are built from classes. There is nothing else, but classes

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; } }

Student

age

gpa

main

argss

q

age

gpa 0

20age

gpa 0

20

Page 133: Java programs are built from classes. There is nothing else, but classes

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; } }

Student

age

gpa

main

argss

q

age

gpa 0

19age

gpa 0

20

Page 134: Java programs are built from classes. There is nothing else, but classes

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; } }

Student

age

gpa

main

argss

q

age

gpa 0

19age

gpa 0

20

Page 135: Java programs are built from classes. There is nothing else, but classes

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; } }

Student

age

gpa

main

argss

q

age

gpa 0

19age

gpa 0

21

Page 136: Java programs are built from classes. There is nothing else, but classes

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; } }

Student

age

gpa

main

argss

q

age

gpa 0

19age

gpa 0

21

Page 137: Java programs are built from classes. There is nothing else, but classes

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; } }

Student

age

gpa

main

argss

q

age

gpa 0

19age

gpa 0

21

Page 138: Java programs are built from classes. There is nothing else, but classes

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; } }

Student

age

gpa

main

argss

q

age

gpa 0

19age

gpa 0

21

Page 139: Java programs are built from classes. There is nothing else, but classes

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; } }

Student

age

gpa

main

argss

q

age

gpa 0

19age

gpa 0

21

Page 140: Java programs are built from classes. There is nothing else, but classes

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; } }

Student

age

gpa

main

argss

q

age

gpa 0

19age

gpa 0

21

Page 141: Java programs are built from classes. There is nothing else, but classes

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; } }

Student

age

gpa

main

argss

q

age

gpa 0

22age

gpa 0

21

Page 142: Java programs are built from classes. There is nothing else, but classes

Now write this (in Java): Create two accounts, for Doug and Adrian.

Page 143: Java programs are built from classes. There is nothing else, but classes

Now write this (in Java): Create two accounts, for Doug and Adrian. Initial balance for both accounts is 10.

Page 144: Java programs are built from classes. There is nothing else, but classes

Now write this (in Java): Create two accounts, for Doug and Adrian. Initial balance for both accounts is 10. Then Doug’s account doubles, Adrian’s decreases by 3 dollars, and Doug’s account has one more dollar deposited into it.

Page 145: Java programs are built from classes. There is nothing else, but classes

Let me try it. How about this?

Page 146: Java programs are built from classes. There is nothing else, but classes

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++; } }

Let me try it. How about this?

Page 147: Java programs are built from classes. There is nothing else, but classes

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++; } }

Create two accounts, for Doug and Adrian. Initial balance for both accounts is 10. Then Doug’s account doubles, Adrian’s decreases by 3 dollars, and Doug’s account has one more dollar deposited into it.

Page 148: Java programs are built from classes. There is nothing else, but classes

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++; } }

Create two accounts, for Doug and Adrian. Initial balance for both accounts is 10. Then Doug’s account doubles, Adrian’s decreases by 3 dollars, and Doug’s account has one more dollar deposited into it.

Page 149: Java programs are built from classes. There is nothing else, but classes

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++; } }

Create two accounts, for Doug and Adrian. Initial balance for both accounts is 10. Then Doug’s account doubles, Adrian’s decreases by 3 dollars, and Doug’s account has one more dollar deposited into it.

Page 150: Java programs are built from classes. There is nothing else, but classes

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++; } }

Create two accounts, for Doug and Adrian. Initial balance for both accounts is 10. Then Doug’s account doubles, Adrian’s decreases by 3 dollars, and Doug’s account has one more dollar deposited into it.

Page 151: Java programs are built from classes. There is nothing else, but classes

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++; } }

Create two accounts, for Doug and Adrian. Initial balance for both accounts is 10. Then Doug’s account doubles, Adrian’s decreases by 3 dollars, and Doug’s account has one more dollar deposited into it.

Page 152: Java programs are built from classes. There is nothing else, but classes

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++; } }

Create two accounts, for Doug and Adrian. Initial balance for both accounts is 10. Then Doug’s account doubles, Adrian’s decreases by 3 dollars, and Doug’s account has one more dollar deposited into it.

Page 153: Java programs are built from classes. There is nothing else, but classes

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++; } }

Create two accounts, for Doug and Adrian. Initial balance for both accounts is 10. Then Doug’s account doubles, Adrian’s decreases by 3 dollars, and Doug’s account has one more dollar deposited into it.

Page 154: Java programs are built from classes. There is nothing else, but classes

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

balance

Page 155: Java programs are built from classes. There is nothing else, but classes

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++; } }

Looks good.

Page 156: Java programs are built from classes. There is nothing else, but classes

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++; } }

Looks good. Thank you.

Page 157: Java programs are built from classes. There is nothing else, but classes

Draw this:

Page 158: Java programs are built from classes. There is nothing else, but classes

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++; } }

Page 159: Java programs are built from classes. There is nothing else, but classes

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++; } }

Too easy, almost the same thing...

Page 160: Java programs are built from classes. There is nothing else, but classes

Draw this:

Page 161: Java programs are built from classes. There is nothing else, but classes

class Account { double balance = 20; void deposit(int n) { this.balance += n; } }

Draw this:

Page 162: Java programs are built from classes. There is nothing else, but classes

class Account { double balance = 20; void deposit(int n) { this.balance += n; } }

Draw this:

Account

this.balance

n

deposit

this.balance += n;

Page 163: Java programs are built from classes. There is nothing else, but classes

class Account { double balance = 20; void deposit(int n) { this.balance += n; } }

Draw this:

Account

this.balance

n

deposit

+this.balance += n;

Page 164: Java programs are built from classes. There is nothing else, but classes

class Account { double balance = 20; void deposit(int n) { this.balance += n; } }

Draw this: balance is an instance variable.

Account

this.balance

n

deposit

+this.balance += n;

Page 165: Java programs are built from classes. There is nothing else, but classes

class Account { double balance = 20; void deposit(int n) { this.balance += n; } }

Draw this: balance is an instance variable. It belongs to the object.

Account

this.balance

n

deposit

+this.balance += n;

Page 166: Java programs are built from classes. There is nothing else, but classes

class Account { double balance = 20; void deposit(int n) { this.balance += n; } }

Draw this: deposit is an instance method.It belongs to the object and describes an update on its instance variables (state).

Account

this.balance

n

deposit

+this.balance += n;

Page 167: Java programs are built from classes. There is nothing else, but classes

class Account { double balance = 20; void deposit(int n) { this.balance += n; } }

Draw this: n is not a local variable in deposit.

Account

this.balance

n

deposit

+this.balance += n;

Page 168: Java programs are built from classes. There is nothing else, but classes

class Account { double balance = 20; void deposit(int n) { this.balance += n; } }

Draw this: It is called a parameter of the method deposit.It is a way of naming the method’s inputs.It is a way to refer to the ingredients of the recipe.

Account

this.balance

n

deposit

+this.balance += n;

Page 169: Java programs are built from classes. There is nothing else, but classes

class Account { double balance = 20; void deposit(int n) { this.balance += n; } }

Draw this: The keyword this allows an object to refer to itself.

Account

this.balance

n

deposit

+this.balance += n;

Page 170: Java programs are built from classes. There is nothing else, but classes

Draw this:

Account

this.balance

n

deposit

+this.balance += n;

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); }}

Page 171: Java programs are built from classes. There is nothing else, but classes

Draw this: Account

this.balance

n

deposit

+this.balance += n;

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); }}

mainargs

Page 172: Java programs are built from classes. There is nothing else, but classes

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); }}

+

this.balance

n

deposit

+this.balance += n;

mainargs

m

20

Page 173: Java programs are built from classes. There is nothing else, but classes

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); }}

+

this.balance

n

deposit

+this.balance += n;

mainargs

m

+

q

2020

Page 174: Java programs are built from classes. There is nothing else, but classes

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); }}

+

this.balance

n

deposit

+this.balance += n;

mainargs

m

+

q

2020

Page 175: Java programs are built from classes. There is nothing else, but classes

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); }}

+

this.balance

n

deposit

+this.balance += n;

mainargs

m

+

q

2020

Page 176: Java programs are built from classes. There is nothing else, but classes

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); }}

+

this.balance

n

deposit

+this.balance += n;

mainargs

m

+

q

2020

Page 177: Java programs are built from classes. There is nothing else, but classes

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); }}

+

this.balance

n

deposit

+this.balance += n;

mainargs

m

+

q

2020

Page 178: Java programs are built from classes. There is nothing else, but classes

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); }}

+

this.balance

n

deposit

+this.balance += n;

mainargs

m

+

q

202010

Page 179: Java programs are built from classes. There is nothing else, but classes

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); }}

+

this.balance

n

deposit

+this.balance += n;

mainargs

m

+

q

3020

Page 180: Java programs are built from classes. There is nothing else, but classes

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); }}

+

this.balance

n

deposit

+this.balance += n;

mainargs

m

+

q

3020

Page 181: Java programs are built from classes. There is nothing else, but classes

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); }}

+

this.balance

n

deposit

+this.balance += n;

mainargs

m

+

q

3020

Page 182: Java programs are built from classes. There is nothing else, but classes

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); }}

+

this.balance

n

deposit

+this.balance += n;

mainargs

m

+

q

30203

Page 183: Java programs are built from classes. There is nothing else, but classes

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); }}

+

this.balance

n

deposit

+this.balance += n;

mainargs

m

+

q

3023

Page 184: Java programs are built from classes. There is nothing else, but classes

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; }}

Page 185: Java programs are built from classes. There is nothing else, but classes

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; }}

This is a constructor.

Page 186: Java programs are built from classes. There is nothing else, but classes

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; }}

It’s like an initialization procedure.

Page 187: Java programs are built from classes. There is nothing else, but classes

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; }}

It’s attached to the blueprint

Page 188: Java programs are built from classes. There is nothing else, but classes

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; }}

It’s attached to the blueprint (not instances)

Page 189: Java programs are built from classes. There is nothing else, but classes

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; }}

It’s attached to the blueprint (not instances)This one is empty.

Page 190: Java programs are built from classes. There is nothing else, but classes

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; }}

Account

this.balance

x

Account

mainargs

Page 191: Java programs are built from classes. There is nothing else, but classes

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; }}

Account

this.balance

x

Account

mainargs

m

10

0balance

Page 192: Java programs are built from classes. There is nothing else, but classes

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; }}

Account

this.balance

x

Account

mainargs

m

0balance

Page 193: Java programs are built from classes. There is nothing else, but classes

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; }}

Account

this.balance

x

Account

mainargs

m

0balance

q

0balance

Page 194: Java programs are built from classes. There is nothing else, but classes

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; }}

Account

this.balance

x

Account

mainargs

m

0balance

q

0balance

Page 195: Java programs are built from classes. There is nothing else, but classes

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; }}

Draw this: Account

this.balance

x

Account

mainargs

m

0balance

q

0balance

10 + m.balance

Page 196: Java programs are built from classes. There is nothing else, but classes

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; }}

Draw this: Account

this.balance

x

Account

mainargs

m

0balance

q

0balance

10 + 0

Page 197: Java programs are built from classes. There is nothing else, but classes

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; }}

Draw this: Account

this.balance

x

Account

mainargs

m

0balance

q

0balance

10

Page 198: Java programs are built from classes. There is nothing else, but classes

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; }}

Draw this: Account

this.balance

x

Account

mainargs

m

10balance

q

0balance

Page 199: Java programs are built from classes. There is nothing else, but classes

class X { int x;}

Draw this:

Page 200: Java programs are built from classes. There is nothing else, but classes

X

x

How easy.

class X { int x;}

Draw this:

Page 201: Java programs are built from classes. There is nothing else, but classes

class A { int x; A (int initialValue) { this.x = initialValue; } /* do this when you first create the object but don’t make it part of the generated instance */}

Draw this:

Page 202: Java programs are built from classes. There is nothing else, but classes

class A { int x; A (int initialValue) { this.x = initialValue; } /* do this when you first create the object but don’t make it part of the generated instance */}

Draw this: That’s called a constructor.

A

x

initialValue

A

Page 203: Java programs are built from classes. There is nothing else, but classes

class A { int x; A (int initialValue) { this.x = initialValue; } /* do this when you first create the object but don’t make it part of the generated instance */}

Draw this: That’s called a constructor.

A

x

initialValue

A

Page 204: Java programs are built from classes. There is nothing else, but classes

class A { int x; A (int initialValue) { this.x = initialValue; } /* do this when you first create the object but don’t make it part of the generated instance */}

Draw this: Notice the name of the constructor.

A

x

initialValue

A

Page 205: Java programs are built from classes. There is nothing else, but classes

class A { int x; A (int initialValue) { this.x = initialValue; } /* do this when you first create the object but don’t make it part of the generated instance */}

Draw this: Notice the name of the constructor.It’s the name of the class.

A

x

initialValue

A

Page 206: Java programs are built from classes. There is nothing else, but classes

class A { int x; A (int initialValue) { this.x = initialValue; } /* do this when you first create the object but don’t make it part of the generated instance */}

Draw this: Notice the name of the constructor.It’s the name of the class. A constructor looks like a method (procedure).

A

x

initialValue

A

Page 207: Java programs are built from classes. There is nothing else, but classes

class A { int x; A (int initialValue) { this.x = initialValue; } /* do this when you first create the object but don’t make it part of the generated instance */}

Draw this: Notice the name of the constructor.It’s the name of the class. A constructor looks like a method (procedure).But doesn’t have a return type.

A

x

initialValue

A

Page 208: Java programs are built from classes. There is nothing else, but classes

class A { int x; A (int initialValue) { this.x = initialValue; } /* do this when you first create the object but don’t make it part of the generated instance */}

Draw this: Here’s how the constructor works:

A

x

initialValue

A

Page 209: Java programs are built from classes. There is nothing else, but classes

class A { int x; A (int initialValue) { this.x = initialValue; } /* do this when you first create the object but don’t make it part of the generated instance */}

Draw this: Here’s how the constructor works:

A

x

initialValue

A

this.x = initialValue;

Page 210: Java programs are built from classes. There is nothing else, but classes

class A { int x; A (int initialValue) { this.x = initialValue; } /* do this when you first create the object but don’t make it part of the generated instance */}

Draw this: When the constructor is invoked, a value is passed

(as an argument)to the constructor.

A

x

initialValue

A

this.x = initialValue;

Page 211: Java programs are built from classes. There is nothing else, but classes

class A { int x; A (int initialValue) { this.x = initialValue; } /* do this when you first create the object but don’t make it part of the generated instance */}

Draw this: The constructor then starts executing.

A

x

initialValue

A

this.x = initialValue;

Page 212: Java programs are built from classes. There is nothing else, but classes

class A { int x; A (int initialValue) { this.x = initialValue; } /* do this when you first create the object but don’t make it part of the generated instance */}

Draw this: The constructor then starts executing.The value is taken from the argument,

A

x

initialValue

A

this.x = initialValue;

Page 213: Java programs are built from classes. There is nothing else, but classes

class A { int x; A (int initialValue) { this.x = initialValue; } /* do this when you first create the object but don’t make it part of the generated instance */}

Draw this: The constructor then starts executing.The value is taken from the argument,

into the instance variable.

A

x

initialValue

A

this.x = initialValue;

Page 214: Java programs are built from classes. There is nothing else, but classes

class A { int x; A (int initialValue) { this.x = initialValue; } /* do this when you first create the object but don’t make it part of the generated instance */}

Draw this: Thus the instance variable will have beeninitialized with what is in initialValue.

A

x

initialValue

A

this.x = initialValue;

initialValue;

Page 215: Java programs are built from classes. There is nothing else, but classes

Speaking of this, can I show you an example?

Page 216: Java programs are built from classes. There is nothing else, but classes

Sure, go ahead.

Page 217: Java programs are built from classes. There is nothing else, but classes

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.”); }}

Page 218: Java programs are built from classes. There is nothing else, but classes

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.”); }}

Page 219: Java programs are built from classes. There is nothing else, but classes

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”); } }

Page 220: Java programs are built from classes. There is nothing else, but classes

That’s 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”); } }

Page 221: Java programs are built from classes. There is nothing else, but classes

That’s 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.

Page 222: Java programs are built from classes. There is nothing else, but classes

That’s funny. Yes.

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).

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”); } }

Page 223: Java programs are built from classes. There is nothing else, but classes

Draw this:

class Checking { double balance; Checking(double x) { this.balance = x; } void deposit(double y) { this.balance = this.balance + y; }}

Page 224: Java programs are built from classes. There is nothing else, but classes

Draw this:

class Tea { double bird; void book(double watch) { this.bird = watch – this.bird; } Tea (double snake) { this.bird = snake * 2; }}

Page 225: Java programs are built from classes. There is nothing else, but classes

Draw this:

class Tea { double bird; void book(double watch) { this.bird = watch – this.bird; } Tea (double snake) { this.bird = snake * 2; }}

instance variable

Page 226: Java programs are built from classes. There is nothing else, but classes

class Tea { double bird; void book(double watch) { this.bird = watch – this.bird; } Tea (double snake) { this.bird = snake * 2; }}

Draw this:

instance method

Page 227: Java programs are built from classes. There is nothing else, but classes

class Tea { double bird; void book(double watch) { this.bird = watch – this.bird; } Tea (double snake) { this.bird = snake * 2; }}

Draw this:

formal parameter

Page 228: Java programs are built from classes. There is nothing else, but classes

class Tea { double bird; void book(double watch) { this.bird = watch – this.bird; } Tea (double snake) { this.bird = snake * 2; }}

Draw this:

formal parameter(like a local variable only initialized when invoked)

Page 229: Java programs are built from classes. There is nothing else, but classes

class Tea { double bird; void book(double watch) { this.bird = watch – this.bird; } Tea (double snake) { this.bird = snake * 2; }}

Draw this:

constructor

Page 230: Java programs are built from classes. There is nothing else, but classes

Draw this:

class Tea { double bird; void book(double watch) { this.bird = watch – this.bird; } Tea (double snake) { this.bird = snake * 2; }}

Page 231: Java programs are built from classes. There is nothing else, but classes

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); } }

Page 232: Java programs are built from classes. There is nothing else, but classes

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.

Page 233: Java programs are built from classes. There is nothing else, but classes

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.

(Soliloquy p. 133) We should also use BlueJ to examine this example thoroughly.

Page 234: Java programs are built from classes. There is nothing else, but classes

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; } }

Page 235: Java programs are built from classes. There is nothing else, but classes

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; } }

This should be easy, by now.

Page 236: Java programs are built from classes. There is nothing else, but classes
Page 237: Java programs are built from classes. There is nothing else, but classes

So what types do we have in Lab Eight (for example)?

Page 238: Java programs are built from classes. There is nothing else, but classes

So what types do we have in Lab Eight (for example)?

Point

Page 239: Java programs are built from classes. There is nothing else, but classes

So what types do we have in Lab Eight (for example)?

PointCircle

Page 240: Java programs are built from classes. There is nothing else, but classes

So what types do we have in Lab Eight (for example)?

PointCircleRectangle

Page 241: Java programs are built from classes. There is nothing else, but classes

So what types do we have in Lab Eight (for example)?

PointCircleRectangle

We should also discuss Robot (to understand Penguin).

Page 242: Java programs are built from classes. There is nothing else, but classes

Thank you.

Page 243: Java programs are built from classes. There is nothing else, but classes

Thank you.