KaiquanMah commited on
Commit
224e6b2
·
verified ·
1 Parent(s): 4a2a54d

Create 02A Objects' methods

Browse files
Week 4: Writing classes/02A Objects' methods ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Once an object has been created by calling a constructor, the information content of the object
2
+ can be observed and modified through the public OPERATIONS it provides.
3
+ For example, as we know, a list has a public method add, which can be used to add an element to the list:
4
+
5
+ ArrayList<Double> measurements = new ArrayList<>();
6
+ measurements.add(2.5);
7
+ measurements.add(14.0);
8
+ measurements.add(-2.75);
9
+
10
+ System.out.println(measurements);
11
+
12
+ Program outputs:
13
+ [2.5, 14.0, -2.75]
14
+
15
+
16
+
17
+
18
+
19
+
20
+ Changing the data content of one object does not change the data content of other objects of the same class.
21
+ Objects are therefore INDEPENDENT ENTITIES.
22
+ When we add one element to a list, the other lists of the same class do not change.
23
+
24
+ ArrayList<Double> measurements1 = new ArrayList<>();
25
+ ArrayList<Double> measurements2 = new ArrayList<>();
26
+
27
+ measurements1.add(2.5);
28
+ measurements1.add(14.0);
29
+ measurements1.add(-2.75);
30
+
31
+ measurements2.add(0.25);
32
+ measurements2.add(0.75);
33
+
34
+ System.out.println(measurements1);
35
+ System.out.println(measurements2);
36
+
37
+
38
+ Program outputs:
39
+ [2.5, 14.0, -2.75]
40
+ [0.25, 0.75]
41
+
42
+