KaiquanMah commited on
Commit
0adf491
·
verified ·
1 Parent(s): 9bf9ebe

Create 15A Objects inside objects

Browse files
Week 4: Writing classes/15A Objects inside objects ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ It is often useful to store an object inside other objects -
2
+ in effect, using your own class as the type of an attribute defined in another class:
3
+
4
+
5
+
6
+ First, let's define the class Driver:
7
+ class Driver {
8
+ private String name;
9
+ private int birthYear;
10
+
11
+ public Driver(String name, int birthYear) {
12
+ this.name = name;
13
+ this.birthYear = birthYear;
14
+ }
15
+
16
+ public String getName() {
17
+ return name;
18
+ }
19
+
20
+ public int getBirthYear() {
21
+ return birthYear;
22
+ }
23
+ }
24
+
25
+
26
+
27
+
28
+ Next, the class Car is defined, with one attribute of type Driver:
29
+
30
+ class Car {
31
+ private String brand;
32
+ private Driver driver;
33
+
34
+ public Car(String brand, Driver driver) {
35
+ this.brand = brand;
36
+ this.driver = driver;
37
+ }
38
+
39
+ public String getBrand() {
40
+ return brand;
41
+ }
42
+
43
+ public Driver getDriver() {
44
+ return driver;
45
+ }
46
+ }
47
+
48
+
49
+
50
+ If we have a Car object, and we want to know the name of the driver,
51
+ we can do that by first calling the method getDriver() and then calling the method getName() on the driver object:
52
+
53
+ public class TestClass {
54
+ public static void main(String[] args) {
55
+ Driver d = new Driver("Vebastian Settel", 1987);
56
+ Car car = new Car("Maston Artin", d);
57
+
58
+ System.out.println(car.getDriver().getName());
59
+ }
60
+ }
61
+
62
+
63
+
64
+
65
+
66
+
67
+
68
+
69
+ A class can also define a data structure to store objects from another class:
70
+
71
+ Let us first define the class Book:
72
+
73
+ class Book {
74
+ private String author;
75
+ private String name;
76
+ int pages;
77
+
78
+ public Book(String author, String name, int pages) {
79
+ this.author = author;
80
+ this.name = name;
81
+ this.pages = pages;
82
+ }
83
+
84
+ public String getAuthor() {
85
+ return author;
86
+ }
87
+
88
+ public String getName() {
89
+ return name;
90
+ }
91
+
92
+ public int getPages() {
93
+ return pages;
94
+ }
95
+ }
96
+
97
+
98
+ Then you define the Bookcase class, where you can store books.
99
+ In practice, the bookcase stores the books in a list.
100
+
101
+
102
+
103
+ In the Bookcase class, you can conveniently add a method that 'returns all books by a single author in a new list':
104
+
105
+ class Bookcase {
106
+ private ArrayList<Book> books;
107
+
108
+ // CONSTRUCTOR
109
+ public Bookcase() {
110
+ books = new ArrayList<>();
111
+ }
112
+
113
+ public void addBook(Book book) {
114
+ books.add(book);
115
+ }
116
+
117
+ public ArrayList<Book> authorsBooks(String authorName) {
118
+ ArrayList<Book> list = new ArrayList<Book>();
119
+ for (Book b: books) {
120
+ if (b.getAuthor().equals(authorName)) {
121
+ list.add(b);
122
+ }
123
+ }
124
+ return list;
125
+ }
126
+ }
127
+
128
+
129
+
130
+
131
+
132
+