-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPractice2.txt
More file actions
236 lines (161 loc) · 7.33 KB
/
Practice2.txt
File metadata and controls
236 lines (161 loc) · 7.33 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
Practical task #2
_______________________
Note: You are not allowed to use any types from java.util package except for these two:
1) java.util.Iterator
2) java.util.NoSuchElementException
_______________________
Create Container interface and place it in the root package (com.epam.rd.java.basic.practice2)
—————————————————-
package com.epam.rd.java.basic.practice2;
public interface Container extends Iterable<Object> {
// Removes all of the elements.
void clear();
// Returns the number of elements.
int size();
// Returns a string representation of this container.
String toString();
// Returns an iterator over elements.
// Iterator must implements the remove method.
Iterator<Object> iterator();
}
—————————————————-
_______________________
Task 1
——————————————————————————————
Type names: Array, ArrayImpl
——————————————————————————————
1.1 Create Array interface containing the following:
—————————————————-
package com.epam.rd.java.basic.practice2;
public interface Array extends Container {
// Add the specified element to the end.
void add(Object element);
// Sets the element at the specified position.
void set(int index, Object element);
// Returns the element at the specified position.
Object get(int index);
// Returns the index of the first occurrence of the specified element,
// or -1 if this array does not contain the element.
// (use 'equals' method to check an occurrence)
int indexOf(Object element);
// Removes the element at the specified position.
void remove(int index);
}
—————————————————-
1.2. Create ArrayImpl class that implements the Array interface.
Implement the storage of the objects inside the container using an array of objects.
The method ‘iterator’ should return an instance of the IteratorImpl class that implements java.util.Iterator<Object> interface.
IteratorImpl class should be defined inside the ArrayImpl class (it is to be an inner class).
In case the three elements A, B, C were added to the container using ‘add’ method, then:
1) ‘toString’ method should return the following string: "[A, B, C]”
2) the elements iteration order should be: A B C
1.3. In the ArrayImpl class, create ‘main’ method where you should demonstrate the functionality of:
1) all the methods of the Array interface (including those inherited from Container and Iterable);
2) all the methods of the Iterator interface (hasNext/next/remove).
_______________________
Task 2
——————————————————————————————
Package name: com.epam.rd.java.basic.practice2
Type names: List, ListImpl
——————————————————————————————
2.1. Create a List interface containing the following:
—————————————————-
package com.epam.rd.java.basic.practice2
public interface List extends Container {
// Inserts the specified element at the beginning.
void addFirst(Object element);
// Appends the specified element to the end.
void addLast(Object element);
// Removes the first element.
void removeFirst();
// Removes the last element.
void removeLast();
// Returns the first element.
Object getFirst();
// Returns the last element.
Object getLast();
// Returns the first occurrence of the specified element.
// Returns null if no such element.
// (use 'equals' method to check an occurrence)
Object search(Object element);
// Removes the first occurrence of the specified element.
// Returns true if this list contained the specified element.
// (use 'equals' method to check an occurrence)
boolean remove(Object element);
}
—————————————————-
Storage of the objects inside the container should be implemented by using set of nodes - instances of the Node class.
Each node stores an object and a reference to the next node.
Node class should be defined inside the ListImpl class (it should be a static nested class).
The ‘iterator’ method should return an instance of the IteratorImpl class that implements java.util.Iterator<Object> interface.
The IteratorImpl class should be defined inside the ListImpl class (it should be an inner class).
In case the three elements A, B, C were added to the container using the ‘addLast’ method, then:
1) toString method should return the following string "[A, B, C]”
2) the elements iteration order should be: A B C
2.3 In the ListImpl class, create ‘main’ method where you should demonstrate the functionality of:
1) all the methods from the List interface (including those inherited from Container and Iterable);
2) all the methods of the Iterator interface (hasNext/next/remove).
_______________________
Task 3
——————————————————————————————
Type names: Queue, QueueImpl
——————————————————————————————
3.1. Create Queue interface containing the following:
—————————————————-
public interface Queue extends Container {
// Appends the specified element to the end.
void enqueue(Object element);
// Removes the head.
Object dequeue();
// Returns the head.
Object top();
}
—————————————————-
3.2. Create QueueImpl class that implements the Queue interface.
In case the three elements A, B, C were added to the container using the ‘enqueue’ method, then:
1) toString method should return the following string "[A, B, C]”
2) the elements iteration order should be: A B C
3.3 In the QueueImpl class, create ‘main’ method where you should demonstrate the functionality of:
1) all the methods from the Queue interface (including those inherited from Container and Interable);
2) all the methods of the Iterator interface (hasNext/next/remove).
_______________________
Task 4
——————————————————————————————
Type names: Stack, StackImpl
——————————————————————————————
4.1. Create Stack interface containing the following:
—————————————————-
package com.epam.rd.java.basic.practice2;
public interface Stack extends Container {
// Pushes the specified element onto the top.
void push(Object element);
// Removes and returns the top element.
Object pop();
// Returns the top element.
Object top();
}
—————————————————-
4.3. Create StackImpl class that implements the Stack interface.
In case the three elements A, B, C were added to the container using ‘push’ method , then:
1) toString method should return the following string "[A, B, C]”
3) the elements iteration order should be: C B A
4.3. In the StackImpl class, create ‘main’ method where you should demonstrate the functionality of:
1) all the methods from the Stack interface (including those inherited from Container and Interable);
2) all the methods of the Iterator interface (hasNext/next/remove).
_______________________
Notes.
1. The result should be presented as a project named Practice2.
2. The root package for all the classes: com.epam.rd.java.basic.practice2
3. Additionally, add Demo class to your root package that demonstrates the actions of all the subtasks.
4. Upload the project to the repository, run the build successfully in Jenkins, optimize the metrics in Sonar.
_______________________
Questions.
1. What are overriding, overloading, method hiding?
2. What is inheritance? keywords ‘implements’, ‘extends’.
3. What types cannot be inherited?
4. Limitations when overriding a method.
5. What is encapsulation? What is it for? how to implement it?
6. Contexts of the use of the keyword ‘final’.
7. The order of calling constructors, initialization blocks during object creation.
8. Anonymous classes.
9. What is the difference between nested classes and inner classes?