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

Post on 20-Jan-2016

234 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Java programs are built from classes.

Java programs are built from classes.

There is nothing else, but classes.

A class contains members:

A class contains members:

a) data (variables)

A class contains members:

a) data (variables)

b) procedures (methods)

Members could be static or not static.

End of story.

Let’s see some examples now.

class One {

}

Draw this:

class One {

}

One

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

class Two { int u; double q;}

Draw this:

class Two { int u; double q;}

Two

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

class Two { int u; double q;}

Two

u

q

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

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

class Student {

}

Draw this:

class Student {

}

Student

Draw this:

Easy. Student or One – same difference.

class Student { int age; }

Now draw this:

class Student { int age; }

Now draw this:

Student

age

class Z { int m; double n; }

Draw this:

class Z { int m; double n; }

Draw this:

Z

m

n

class M { int m; static double n; }

Draw this:

class M { int m; static double n; }

Draw this:

M

mn

class M { int m; static double n; }

Draw this:

M

m

n

class M { int m; double n; }

Draw this:

M

m

n

class M { int m; static double n; }

Draw this:

M

mn

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

} }

Draw this:

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

} }

Draw this:

For this the picture is bigger:

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

} }

Draw this:

For this the picture is bigger:

Qv

zmain

args

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

} }

Draw this:

For this the picture is bigger:

Qv

zmain

args

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

} }

Draw this:

For this the picture is bigger:

Qv

zmain

args

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

Draw this:

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:

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

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

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;

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.

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.

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;

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;

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

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

Draw this:

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

Draw this:

Student

age

gpamain

args

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

Draw this:

Student

age

gpamain

argsq

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

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

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

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”

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.

Types in Java:

Types in Java:

a) primitive

Types in Java:

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

Types in Java:

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

b) reference types

Types in Java:

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

b) reference types (classes)

Types in Java:

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

int x;

Types in Java:

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

int x;

int y;

Types in Java:

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

int x;

int y;

x = 5;

Types in Java:

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

int x;

int y;

x = 5;

Types in Java:

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

int x;

int y;

x = 5;

5

Types in Java:

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

int x;

int y;

x = 5;

5

y = x;

Types in Java:

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

int x;

int y;

x = 5;

5

y = x;

Types in Java:

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

int x;

int y;

x = 5;

5

y = x;

5

Types in Java:

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

int x;

int y;

x = 5;

5

y = x;

5

x = x + 2;

Types in Java:

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

int x;

int y;

x = 5;

5

y = x;

5

x = x + 2;

Types in Java:

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

int x;

int y;

x = 5;

5

y = x;

5

x = 5 + 2;

Types in Java:

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

int x;

int y;

x = 5;

5

y = x;

5

x = 5 + 2;

Types in Java:

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

int x;

int y;

x = 5;

5

y = x;

5

x = 5 + 2;

Types in Java:

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

int x;

int y;

x = 5;

7

y = x;

5

x = x + 2;

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.

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.

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.

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

Types in Java:

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

b) reference types

Types in Java:

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

b) reference types

Penguin x;

Types in Java:

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

b) reference types

Penguin x;

x = new Penguin();

Types in Java:

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

b) reference types

Penguin x;

x = new Penguin();

Types in Java:

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

b) reference types

Penguin x;

x = new Penguin();

Types in Java:

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

b) reference types

Penguin x;

x = new Penguin();

Penguin y;

Types in Java:

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

b) reference types

Penguin x;

x = new Penguin();

Penguin y;

y = x;

Types in Java:

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

b) reference types

Penguin x;

x = new Penguin();

Penguin y;

y = x;

Types in Java:

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

b) reference types

Penguin x;

x = new Penguin();

Penguin y;

y = x;

Types in Java:

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

b) reference types

Penguin x;

x = new Penguin();

Penguin y;

y = x;

x.turnLeft();

Types in Java:

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

b) reference types

Penguin x;

x = new Penguin();

Penguin y;

y = x;

x.turnLeft();

Types in Java:

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

b) reference types

Penguin x;

x = new Penguin();

Penguin y;

y = x;

x.turnLeft();

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.

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.

Types in Java:

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

b) reference types

Types in Java:

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

b) reference types (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.

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.

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.

Let’s continue with the examples.

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

Draw this:

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

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

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

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

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

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

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

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

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

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

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

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

Draw this:

Draw this:

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

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?

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.

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?

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.

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.

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!

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

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

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

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

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

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.

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.

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.

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.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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.

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

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

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.

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.

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.

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.

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.

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.

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.

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

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.

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.

Draw this:

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

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

Draw this:

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

Draw this:

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

Draw this:

Account

this.balance

n

deposit

this.balance += n;

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

Draw this:

Account

this.balance

n

deposit

+this.balance += n;

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;

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;

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;

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;

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;

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;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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.

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.

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

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)

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.

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

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

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

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

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

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

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

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

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

class X { int x;}

Draw this:

X

x

How easy.

class X { int x;}

Draw this:

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:

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

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

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

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

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

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

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

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;

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;

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;

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;

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;

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;

Speaking of this, can I show you an example?

Sure, go ahead.

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

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

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

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

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.

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

Draw this:

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

Draw this:

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

Draw this:

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

instance variable

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

Draw this:

instance method

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

Draw this:

formal parameter

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)

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

Draw this:

constructor

Draw this:

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

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

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.

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.

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

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.

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

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

Point

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

PointCircle

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

PointCircleRectangle

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

PointCircleRectangle

We should also discuss Robot (to understand Penguin).

Thank you.

Thank you.

top related