Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Java programs are built from classes. There is nothing else, but classes."— Presentation transcript:

1

2 Java programs are built from classes.

3 There is nothing else, but classes.

4

5 A class contains members:

6 a) data (variables)

7 A class contains members: a) data (variables) b) procedures (methods)

8

9 Members could be static or not static.

10

11 End of story.

12 Let’s see some examples now.

13 class One { } Draw this:

14 class One { } One Draw this:Don’t forget the blueprint (even though it’s empty).

15 class Two { int u; double q; } Draw this:

16 class Two { int u; double q; } Two Draw this:Notice that the blueprint is no longer empty:

17 class Two { int u; double q; } Two u q Draw this:Notice that the blueprint is no longer empty:

18 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

19 class Student { } Draw this:

20 class Student { } Student Draw this: Easy. Student or One – same difference.

21 class Student { int age; } Now draw this:

22 class Student { int age; } Now draw this: Student age

23 class Z { int m; double n; } Draw this:

24 class Z { int m; double n; } Draw this: Z m n

25 class M { int m; static double n; } Draw this:

26 class M { int m; static double n; } Draw this: M m n

27 class M { int m; static double n; } Draw this: M m n

28 class M { int m; double n; } Draw this: M m n

29 class M { int m; static double n; } Draw this: M m n

30 class Q { int v; double z; public static void main(String[ ] args) { } Draw this:

31 class Q { int v; double z; public static void main(String[ ] args) { } Draw this: For this the picture is bigger:

32 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

33 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

34 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

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

36 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:

37 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

38 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

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

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

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

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

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

44 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

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

46 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

47 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

48 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

49 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

50 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

51 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”

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

53 Types in Java:

54 a) primitive

55 Types in Java: a) primitive (int, double, boolean, char)

56 Types in Java: a) primitive (int, double, boolean, char) b) reference types

57 Types in Java: a) primitive (int, double, boolean, char) b) reference types (classes)

58 Types in Java: a) primitive (int, double, boolean, char) int x;

59 Types in Java: a) primitive (int, double, boolean, char) int x; int y;

60 Types in Java: a) primitive (int, double, boolean, char) int x; int y; x = 5;

61 Types in Java: a) primitive (int, double, boolean, char) int x; int y; x = 5;

62 Types in Java: a) primitive (int, double, boolean, char) int x; int y; x = 5; 5

63 Types in Java: a) primitive (int, double, boolean, char) int x; int y; x = 5; 5 y = x;

64 Types in Java: a) primitive (int, double, boolean, char) int x; int y; x = 5; 5 y = x;

65 Types in Java: a) primitive (int, double, boolean, char) int x; int y; x = 5; 5 y = x; 5

66 Types in Java: a) primitive (int, double, boolean, char) int x; int y; x = 5; 5 y = x; 5 x = x + 2;

67 Types in Java: a) primitive (int, double, boolean, char) int x; int y; x = 5; 5 y = x; 5 x = x + 2;

68 Types in Java: a) primitive (int, double, boolean, char) int x; int y; x = 5; 5 y = x; 5 x = 5 + 2;

69 Types in Java: a) primitive (int, double, boolean, char) int x; int y; x = 5; 5 y = x; 5 x = 5 + 2;

70 Types in Java: a) primitive (int, double, boolean, char) int x; int y; x = 5; 5 y = x; 5 x = 5 + 2;

71 Types in Java: a) primitive (int, double, boolean, char) int x; int y; x = 5; 7 y = x; 5 x = x + 2;

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

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

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

75 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

76 Types in Java: a) primitive (int, double, boolean, char) b) reference types

77 Types in Java: a) primitive (int, double, boolean, char) b) reference types Penguin x;

78 Types in Java: a) primitive (int, double, boolean, char) b) reference types Penguin x; x = new Penguin();

79 Types in Java: a) primitive (int, double, boolean, char) b) reference types Penguin x; x = new Penguin();

80 Types in Java: a) primitive (int, double, boolean, char) b) reference types Penguin x; x = new Penguin();

81 Types in Java: a) primitive (int, double, boolean, char) b) reference types Penguin x; x = new Penguin(); Penguin y;

82 Types in Java: a) primitive (int, double, boolean, char) b) reference types Penguin x; x = new Penguin(); Penguin y; y = x;

83 Types in Java: a) primitive (int, double, boolean, char) b) reference types Penguin x; x = new Penguin(); Penguin y; y = x;

84 Types in Java: a) primitive (int, double, boolean, char) b) reference types Penguin x; x = new Penguin(); Penguin y; y = x;

85 Types in Java: a) primitive (int, double, boolean, char) b) reference types Penguin x; x = new Penguin(); Penguin y; y = x; x.turnLeft();

86 Types in Java: a) primitive (int, double, boolean, char) b) reference types Penguin x; x = new Penguin(); Penguin y; y = x; x.turnLeft();

87 Types in Java: a) primitive (int, double, boolean, char) b) reference types Penguin x; x = new Penguin(); Penguin y; y = x; x.turnLeft();

88 Types in Java: a) primitive (int, double, boolean, char) b) reference types Penguin x; x = new Penguin(); Penguin y; y = x; x.turnLeft(); But that also changes the value of y.

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

90 Types in Java: a) primitive (int, double, boolean, char) b) reference types

91 Types in Java: a) primitive (int, double, boolean, char) b) reference types (classes)

92 Types in Java: a) primitive (int, double, boolean, char) b) reference types (classes) A value of primitive type fits in a location of that type.

93 Types in Java: a) primitive (int, double, boolean, char) b) reference types (classes) A value of primitive type fits in a location of that type. A value of reference type (an object) doesn’t.

94 Types in Java: a) primitive (int, double, boolean, char) b) reference types (classes) A value of primitive type fits in a location of that type. A value of reference type (an object) doesn’t. For objects, a reference to them is kept in the variable.

95 Let’s continue with the examples.

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

97 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

98 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

99 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

100 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

101 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

102 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

103 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

104 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

105 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

106 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

107 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

108 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

109 Draw this:

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

111 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?

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

113 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?

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

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

116 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!

117 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

118 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

119 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

120 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

121 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

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

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

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

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

126 Draw this: class Student { int age = 20; double gpa; public static void main(String[ ] args) { Student s = new Student(); Student q = new Student(); q.age = 19; s.age = 21; q.age = 3 + q.age; }

127 Draw this: class Student { int age = 20; double gpa; public static void main(String[ ] args) { Student s = new Student(); Student q = new Student(); q.age = 19; s.age = 21; q.age = 3 + q.age; } Student age gpa main args

128 Draw this: class Student { int age = 20; double gpa; public static void main(String[ ] args) { Student s = new Student(); Student q = new Student(); q.age = 19; s.age = 21; q.age = 3 + q.age; } Student age gpa main args s age gpa 0 20

129 Draw this: class Student { int age = 20; double gpa; public static void main(String[ ] args) { Student s = new Student(); Student q = new Student(); q.age = 19; s.age = 21; q.age = 3 + q.age; } Student age gpa main args s q age gpa 0 20 age gpa 0 20

130 Draw this: class Student { int age = 20; double gpa; public static void main(String[ ] args) { Student s = new Student(); Student q = new Student(); q.age = 19; s.age = 21; q.age = 3 + q.age; } Student age gpa main args s q age gpa 0 20 age gpa 0 20

131 Draw this: class Student { int age = 20; double gpa; public static void main(String[ ] args) { Student s = new Student(); Student q = new Student(); q.age = 19; s.age = 21; q.age = 3 + q.age; } Student age gpa main args s q age gpa 0 20 age gpa 0 20

132 Draw this: class Student { int age = 20; double gpa; public static void main(String[ ] args) { Student s = new Student(); Student q = new Student(); q.age = 19; s.age = 21; q.age = 3 + q.age; } Student age gpa main args s q age gpa 0 20 age gpa 0 20

133 Draw this: class Student { int age = 20; double gpa; public static void main(String[ ] args) { Student s = new Student(); Student q = new Student(); q.age = 19; s.age = 21; q.age = 3 + q.age; } Student age gpa main args s q age gpa 0 19 age gpa 0 20

134 Draw this: class Student { int age = 20; double gpa; public static void main(String[ ] args) { Student s = new Student(); Student q = new Student(); q.age = 19; s.age = 21; q.age = 3 + q.age; } Student age gpa main args s q age gpa 0 19 age gpa 0 20

135 Draw this: class Student { int age = 20; double gpa; public static void main(String[ ] args) { Student s = new Student(); Student q = new Student(); q.age = 19; s.age = 21; q.age = 3 + q.age; } Student age gpa main args s q age gpa 0 19 age gpa 0 21

136 Draw this: class Student { int age = 20; double gpa; public static void main(String[ ] args) { Student s = new Student(); Student q = new Student(); q.age = 19; s.age = 21; q.age = 3 + q.age; } Student age gpa main args s q age gpa 0 19 age gpa 0 21

137 Draw this: class Student { int age = 20; double gpa; public static void main(String[ ] args) { Student s = new Student(); Student q = new Student(); q.age = 19; s.age = 21; q.age = 3 + q.age; } Student age gpa main args s q age gpa 0 19 age gpa 0 21

138 Draw this: class Student { int age = 20; double gpa; public static void main(String[ ] args) { Student s = new Student(); Student q = new Student(); q.age = 19; s.age = 21; q.age = 3 + 19; } Student age gpa main args s q age gpa 0 19 age gpa 0 21

139 Draw this: class Student { int age = 20; double gpa; public static void main(String[ ] args) { Student s = new Student(); Student q = new Student(); q.age = 19; s.age = 21; q.age = 22; } Student age gpa main args s q age gpa 0 19 age gpa 0 21

140 Draw this: class Student { int age = 20; double gpa; public static void main(String[ ] args) { Student s = new Student(); Student q = new Student(); q.age = 19; s.age = 21; q.age = 22; } Student age gpa main args s q age gpa 0 19 age gpa 0 21

141 Draw this: class Student { int age = 20; double gpa; public static void main(String[ ] args) { Student s = new Student(); Student q = new Student(); q.age = 19; s.age = 21; q.age = 3 + q.age; } Student age gpa main args s q age gpa 0 22 age gpa 0 21

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

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

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

145 Let me try it. How about this?

146 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?

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

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

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

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

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

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

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

154 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

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

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

157 Draw this:

158 class BankAccount { double balance = 25; public static void main(String[ ] args) { BankAccount doug = new BankAccount(); BankAccount adrian = new BankAccount(); doug.balance = doug.balance * 2; adrian.balance -= 3; doug.balance++; }

159 Draw this: 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...

160 Draw this:

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

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

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

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

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

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

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

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

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

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

171 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

172 Draw this: Account class Account { double balance = 20; void deposit(int n) { this.balance += n; } public static void main(String[ ] args) { Account m = new Account(); Account q = new Account(); m.deposit(10); q.deposit(3); } + this.balance n deposit + this.balance += n; main args m 20

173 Draw this: Account class Account { double balance = 20; void deposit(int n) { this.balance += n; } public static void main(String[ ] args) { Account m = new Account(); Account q = new Account(); m.deposit(10); q.deposit(3); } + this.balance n deposit + this.balance += n; main args m + q 20

174 Draw this: Account class Account { double balance = 20; void deposit(int n) { this.balance += n; } public static void main(String[ ] args) { Account m = new Account(); Account q = new Account(); m.deposit(10); q.deposit(3); } + this.balance n deposit + this.balance += n; main args m + q 20

175 Draw this: Account class Account { double balance = 20; void deposit(int n) { this.balance += n; } public static void main(String[ ] args) { Account m = new Account(); Account q = new Account(); m.deposit(10); q.deposit(3); } + this.balance n deposit + this.balance += n; main args m + q 20

176 Draw this: Account class Account { double balance = 20; void deposit(int n) { this.balance += n; } public static void main(String[ ] args) { Account m = new Account(); Account q = new Account(); m.deposit(10); q.deposit(3); } + this.balance n deposit + this.balance += n; main args m + q 20

177 Draw this: Account class Account { double balance = 20; void deposit(int n) { this.balance += n; } public static void main(String[ ] args) { Account m = new Account(); Account q = new Account(); m.deposit(10); q.deposit(3); } + this.balance n deposit + this.balance += n; main args m + q 20

178 Draw this: Account class Account { double balance = 20; void deposit(int n) { this.balance += n; } public static void main(String[ ] args) { Account m = new Account(); Account q = new Account(); m.deposit(10); q.deposit(3); } + this.balance n deposit + this.balance += n; main args m + q 20 10

179 Draw this: Account class Account { double balance = 20; void deposit(int n) { this.balance += n; } public static void main(String[ ] args) { Account m = new Account(); Account q = new Account(); m.deposit(10); q.deposit(3); } + this.balance n deposit + this.balance += n; main args m + q 30 20

180 Draw this: Account class Account { double balance = 20; void deposit(int n) { this.balance += n; } public static void main(String[ ] args) { Account m = new Account(); Account q = new Account(); m.deposit(10); q.deposit(3); } + this.balance n deposit + this.balance += n; main args m + q 30 20

181 Draw this: Account class Account { double balance = 20; void deposit(int n) { this.balance += n; } public static void main(String[ ] args) { Account m = new Account(); Account q = new Account(); m.deposit(10); q.deposit(3); } + this.balance n deposit + this.balance += n; main args m + q 30 20

182 Draw this: Account class Account { double balance = 20; void deposit(int n) { this.balance += n; } public static void main(String[ ] args) { Account m = new Account(); Account q = new Account(); m.deposit(10); q.deposit(3); } + this.balance n deposit + this.balance += n; main args m + q 30 20 3

183 Draw this: Account class Account { double balance = 20; void deposit(int n) { this.balance += n; } public static void main(String[ ] args) { Account m = new Account(); Account q = new Account(); m.deposit(10); q.deposit(3); } + this.balance n deposit + this.balance += n; main args m + q 30 23

184 Draw this: class Account { double balance; Account(double x) { } public static void main(String[ ] args) { Account m = new Account(10); Account q = new Account(3); m.balance = 10 + m.balance; }

185 Draw this: 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.

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

187 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

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

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

190 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

191 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

192 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

193 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

194 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

195 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 + m.balance

196 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 + 0

197 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

198 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

199 class X { int x; } Draw this:

200 X x How easy. class X { int x; } Draw this:

201 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:

202 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

203 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

204 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

205 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

206 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

207 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

208 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

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

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

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

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

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

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

215 Speaking of this, can I show you an example?

216 Sure, go ahead.

217 Here it is: class Speakers { public static void main(String[ ] args) { Speaker a = new Speaker(“Larry”); Speaker b = new Speaker(“Michael”); Speaker c = new Speaker(“Toni”); a.talk(); b.talk(); c.talk(); } class Speaker { String speaker; Speaker(String name) { speaker = name; } void talk() { System.out.println(this.speaker + “ is very happy.”); }

218 This writer is very happy.Here it is: class Speakers { public static void main(String[ ] args) { Speaker a = new Speaker(“Larry”); Speaker b = new Speaker(“Michael”); Speaker c = new Speaker(“Toni”); a.talk(); b.talk(); c.talk(); } class Speaker { String speaker; Speaker(String name) { speaker = name; } void talk() { System.out.println(this.speaker + “ is very happy.”); }

219 This writer is very happy.Ha, ha. Draw this: class Student { String name; Student(String w) { this.name = w; } class Tuesday { public static void main(String[ ] args) { Student q = new Student(“Larry”); Student u = new Student(“John”); Student s = new Student(“Adrian”); }

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

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

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

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

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

225 Draw this: class Tea { double bird; void book(double watch) { this.bird = watch – this.bird; } Tea (double snake) { this.bird = snake * 2; } instance variable

226 class Tea { double bird; void book(double watch) { this.bird = watch – this.bird; } Tea (double snake) { this.bird = snake * 2; } Draw this: instance method

227 class Tea { double bird; void book(double watch) { this.bird = watch – this.bird; } Tea (double snake) { this.bird = snake * 2; } Draw this: formal parameter

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

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

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

231 Draw this: class Hat { public static void main(String[ ] args) { Tea m = new Tea(-2); Tea n = new Tea(42); m.book(92); n.bird = m.bird + n.bird; n.book(25); }

232 Draw this: class Hat { public static void main(String[ ] args) { Tea m = new Tea(-2); Tea n = new Tea(42); m.book(92); n.bird = m.bird + n.bird; n.book(25); } class Tea { double bird; void book(double watch) { this.bird = watch – this.bird; } Tea (double snake) { this.bird = snake * 2; } Where Tea is the class seen before.

233 Draw this: class Hat { public static void main(String[ ] args) { Tea m = new Tea(-2); Tea n = new Tea(42); m.book(92); n.bird = m.bird + n.bird; n.book(25); } 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.

234 Last example: class X { int q; } class Play { public static void main(String[ ] args) { X u = new X(); X p = new X(); u.q = 3; p.q = u.q + p.q; u.q = u.q + p.q; }

235 Last example: 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.

236

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

238 Point

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

240 So what types do we have in Lab Eight (for example)? Point Circle Rectangle

241 So what types do we have in Lab Eight (for example)? Point Circle Rectangle We should also discuss Robot (to understand Penguin).

242 Thank you.

243


Download ppt "Java programs are built from classes. There is nothing else, but classes."

Similar presentations


Ads by Google