-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoopIntroLec.java
More file actions
122 lines (81 loc) · 3.69 KB
/
Copy pathoopIntroLec.java
File metadata and controls
122 lines (81 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
public class oopIntroLec {
public static void main(String[] args) {
// ======================== slide 3 (classes and objects)
//
// Person rick = new Person();
// rick.firstName = "Rick";
// rick.lastName = "Sanchez";
// System.out.println(rick.firstName);
// doing p1 == p2 will check ids, not contents
// accessing a variable that hasn't been defined will return a null
// default value for number types is 0
// booleans default to false
// empty char gives empty array
//
// Person fred = new Person();
// fred.firstName = "Fred";
// fred.lastName = "Smith";
// System.out.println(fred.firstName);
// System.out.println(fred.lastName);
// -- Dog Example --
//
// d1.age = 3;
// d1.breed = "corgi";
// d1.canBreed = false;
// d1.name = "Tater";
// ======================== slide 4 (object fields)
// Person rick = new Person();
// rick.firstName = "Rick";
// rick.lastName = "Sanchez";
// System.out.println(rick.sayHello());
// -- Dog Method Example --
// Dog d1 = new Dog();
// d1.age = 3;
// d1.breed = "corgi";
// d1.canBreed = false;
// d1.name = "Tater";
//
// d1.callForDog("Tater");
// ======================== slide 5 (class fields)
// System.out.println(Arithmetic.PI);
// System.out.println(Arithmetic.add(5, 5));
// System.out.println(Arithmetic.multiply(5, 5));
// -- DogHelper Example --
Dog dog = new Dog();
dog.name = "Sparky";
dog.age = 3;
dog.callForDog("Sparky");
System.out.println(DogHelper.NUMBER_OF_DOG_BREEDS);
System.out.println(DogHelper.humanToDogYears(dog.age));
// ======================== slide 7 (class vs. object fields)
// Person theBestDrummerAlive = new Person();
// theBestDrummerAlive.name = "Neil Peart";
// Person.worldPopulation += 1; // accessing a static property
//
// System.out.println(Person.worldPopulation);
//
// // this also works, but is usually not a good idea
// System.out.println(theBestDrummerAlive.worldPopulation); // 7500000001
// System.out.println(theBestDrummerAlive.name);
// ======================== slide 8 (constructors)
// Student jane = new Student("Jane Doe", "Zion");
// System.out.print(jane.name + " - ");
// System.out.println(jane.cohort); // prints "Jane Doe - Zion"
// Student john = new Student("John Doe");
// System.out.println(john.name + " - ");
// System.out.println(john.cohort); // prints "John Doe - Unassigned"
// ======================== slide 9 (this keyword)
// Student jane = new Student("Jane Doe", "Zion");
// System.out.print(jane.name + " - ");
// System.out.println(jane.cohort); // prints "Jane Doe - Zion"
// Student john = new Student("John Doe");
// System.out.print(john.name + " - ");
// System.out.println(john.cohort); // prints "John Doe - Unassigned"
// ======================== slide 10 (visibility)
// Student student = new Student("John Doe", 82.4);
// System.out.println(student.name) // "John Doe"
// // DON'T DO THIS - can't access private properties outside the class
// System.out.println(student.grade);
// System.out.println(student.shareGrade()); // Do this instead
}
}