Spaces:
Running
Running
Create 11A Other object methods (beyond get/set)
Browse files
Week 4: Writing classes/11A Other object methods (beyond get/set)
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Of course, object methods do not have to be just straightforward methods
|
| 2 |
+
for setting and observing attributes.
|
| 3 |
+
However, in general, methods do make some use of attributes;
|
| 4 |
+
the purpose of public operations on objects is to view and modify the
|
| 5 |
+
data content of the object.
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
Consider the class 'Notebook'.
|
| 9 |
+
Since notes are a list, it would be more convenient than a
|
| 10 |
+
direct setting method to provide a method for the client to
|
| 11 |
+
'add a single note to the notebook':
|
| 12 |
+
|
| 13 |
+
class Notebook {
|
| 14 |
+
private String owner;
|
| 15 |
+
private ArrayList<String> notes;
|
| 16 |
+
|
| 17 |
+
// CONSTRUCTOR
|
| 18 |
+
public Notebook(String owner) {
|
| 19 |
+
this.owner= owner;
|
| 20 |
+
// not from parameter, but initialize an empty list
|
| 21 |
+
this.notes= new ArrayList<>();
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
public String getOwner() {
|
| 25 |
+
return this.owner;
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
public void addNote(String note) {
|
| 29 |
+
this.notes.add(note);
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
// Returns all notes in one string
|
| 33 |
+
public String allNotes() {
|
| 34 |
+
// join method connects all elements of the list with
|
| 35 |
+
// the given separator
|
| 36 |
+
// kind of like the opposite of 'split' method
|
| 37 |
+
String nb = String.join("\n", notes);
|
| 38 |
+
return nb;
|
| 39 |
+
}
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
Now it's easier for the customer to use the notebook.
|
| 44 |
+
The actual storage format is encapsulated (i.e. hidden from the client).
|
| 45 |
+
This means that it doesn't matter if the internal implementation of
|
| 46 |
+
the class changes (for example, from a list to a table or a hash table)
|
| 47 |
+
as long as the public operations are preserved.
|
| 48 |
+
|
| 49 |
+
public class Testclass {
|
| 50 |
+
public static void main(String[] args) {
|
| 51 |
+
Notebook book = new Notebook("Mike Memorizer");
|
| 52 |
+
book.addNote("Go to the store!");
|
| 53 |
+
book.addNote("Cram for test!");
|
| 54 |
+
book.addNote("Watch the news!");
|
| 55 |
+
|
| 56 |
+
System.out.println(book.allNotes());
|
| 57 |
+
}
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
Program outputs:
|
| 61 |
+
Go to the store!
|
| 62 |
+
Cram for test!
|
| 63 |
+
Watch the news!
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
As another example, consider the class 'Cube'.
|
| 74 |
+
In addition to methods for setting and getting the length of a page,
|
| 75 |
+
the class could have methods for calculating the area and volume, for example:
|
| 76 |
+
|
| 77 |
+
class Cube {
|
| 78 |
+
private int sidelength;
|
| 79 |
+
|
| 80 |
+
// constructor
|
| 81 |
+
public Cube(int sidelength) {
|
| 82 |
+
this.sidelength = sidelength;
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
public int getSidelength() {
|
| 86 |
+
return sidelength;
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
public void setSidelength(int sidelength) {
|
| 90 |
+
this.sidelength= sidelength;
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
public int Area() {
|
| 94 |
+
return sidelength* sidelength* 6;
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
public int Volume() {
|
| 98 |
+
return sidelength * sidelength* sidelength;
|
| 99 |
+
}
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
Example on using the class:
|
| 103 |
+
public class TestClass {
|
| 104 |
+
public static void main(String[] args) {
|
| 105 |
+
Cube smallCube = new Cube(3);
|
| 106 |
+
System.out.println(smallCube.Area());
|
| 107 |
+
System.out.println(smallCube.Volume());
|
| 108 |
+
|
| 109 |
+
Cube largeCube = new Cube(15);
|
| 110 |
+
System.out.println(largeCube.Area());
|
| 111 |
+
System.out.println(largeCube.Volume());
|
| 112 |
+
}
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
Program outputs:
|
| 116 |
+
54
|
| 117 |
+
27
|
| 118 |
+
1350
|
| 119 |
+
3375
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
|