Java programs are built from classes.
There is nothing else, but classes.
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 m n
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 m n
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: Q v z main args
class Q { int v; double z; public static void main(String[ ] args) { } Draw this: For this the picture is bigger: Q v z main args
class Q { int v; double z; public static void main(String[ ] args) { } Draw this: For this the picture is bigger: Q v z main 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: E v 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: E v z main 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: E v z main args int 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: E v z main args int 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: E v z main args int 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: E v z main args int 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: E v z main args int 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: E v z main args int 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 gpa main args
class Student { int age; double gpa; public static void main(String[ ] args) { int q = 21; String n = “John”; } Draw this: Student age gpa main args q
class Student { int age; double gpa; public static void main(String[ ] args) { int q = 21; String n = “John”; } Draw this: Student age gpa main args q 21
class Student { int age; double gpa; public static void main(String[ ] args) { int q = 21; String n = “John”; } Draw this: Student age gpa main args q 21 n
class Student { int age; double gpa; public static void main(String[ ] args) { int q = 21; String n = “John”; } Draw this: Student age gpa main args q 21 n
class Student { int age; double gpa; public static void main(String[ ] args) { int q = 21; String n = “John”; } Draw this: Student age gpa main args q 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 gpa main args q 21 n java.lang.String “John” String is a reference type.
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) char boolean
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 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 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 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:
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 args q
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 args q 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 args q 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 args q n s age gpa0 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 args q n s age gpa0 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 args q n s age gpa0 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 args q n s age gpa0 0 Notice that q and n have no values in them. Unlike instance or class variables, local variables need to be initialized explicitly by the programmer.
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 args q n s age gpa0 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 args q n s age gpa0 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 args s 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 args s q age gpa 0 20 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 args s q age gpa 0 20 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 args s q age gpa 0 20 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 args s q age gpa 0 20 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 args s q age gpa 0 19 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 args s q age gpa 0 19 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 args s q age gpa 0 19 age 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 args s q age gpa 0 19 age 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 args s q age gpa 0 19 age 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 = ; } Student age gpa main args s q age gpa 0 19 age 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 args s q age gpa 0 19 age 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 args s q age gpa 0 19 age 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 args s q age gpa 0 22 age 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:
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); } main args
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; main args 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; main args m + q 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; main args m + q 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; main args m + q 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; main args m + q 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; main args m + q 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; main args m + q 20 10
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; main args m + q 30 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; main args m + q 30 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; main args m + q 30 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; main args m + q
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; main args m + q 30 23
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 main args
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 main args m 10 0 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; } Account this.balance x Account main args m 0 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; } Account this.balance x Account main args m 0 balance q 0
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 main args m 0 balance q 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 main args m 0 balance q 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 main args m 0 balance q
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 main args m 0 balance q 0 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 main args m 10 balance q 0
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 been initialized 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.
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); }
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); } 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); } 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)?
Point
So what types do we have in Lab Eight (for example)? Point Circle
So what types do we have in Lab Eight (for example)? Point Circle Rectangle
So what types do we have in Lab Eight (for example)? Point Circle Rectangle We should also discuss Robot (to understand Penguin).
Thank you.