OOP2025_JavaExercises_Lab9_OOPAndCollectionsFramework
OOP2025_JavaExercises_Lab9_OOPAndCollectionsFramework
1. Coding style:
• Read Java code convention: ”Google Java Style Guide” or ”Java Code
Conventions - Oracle”.
• Follow the Java Naming Conventions for variables, methods, and classes
STRICTLY. Use CamelCase for names. Variable and method names begin
with lowercase, while class names begin with uppercase. Use nouns for
variables (e.g., radius) and class names (e.g., Circle). Use verbs for methods
(e.g., getArea(), isEmpty()).
• Use Meaningful Names: Do not use names like a, b, c, d, x, x1, x2,
and x1688 - they are meaningless. Avoid single-alphabet names like i, j, k.
They are easy to type, but usually meaningless. Use single-alphabet names
only when their meaning is clear, e.g., x, y, z for co-ordinates and i for array
index. Use meaningful names like row and col (instead of x and y, i and j,
x1 and x2), numStudents (not n), maxGrade, size (not n), and upperbound
(not n again). Differentiate between singular and plural nouns (e.g., use
books for an array of books, and book for each item).
• Use consistent indentation and coding style. Many IDEs (such as Eclipse /
NetBeans) can re-format your source codes with a single click.
3. The only way to learn programming is program, program and program on chal-
lenging problems. The problems in this tutorial are certainly NOT challenging.
There are tens of thousands of challenging problems available – used in training
for various programming contests (such as International Collegiate Programming
Contest (ICPC), International Olympiad in Informatics (IOI)).
1
HaQT Object-Oriented Programming
1 Exercises on OOP
1.1 Library
Write code for an application designed as shown in the following class diagram.
3 /∗ ∗
∗ C l a s s g e n e r a l i z i n g Books and DVDs
5 ∗/
p u b l i c a b s t r a c t c l a s s Item {
7 String t i t l e ;
i n t year ;
9
p u b l i c Item ( S t r i n g t i t l e , i n t y e a r ) {
2
HaQT Object-Oriented Programming
11 /∗ TODO ∗/
}
13
/∗ TODO ∗/
15
@Override
17 public String toString () {
r e t u r n ” Item [ ” + ” t i t l e =’” + t i t l e + ’ \ ’ ’
19 + ” , y e a r=” + y e a r + ’ ] ’ ;
}
21 }
3 /∗ ∗
∗ C l a s s r e p r e s e n t i n g a DVD
5 ∗/
p u b l i c c l a s s Dvd e x t e n d s Item {
7 int duration ;
9 p u b l i c Dvd( S t r i n g t i t l e , i n t year , i n t d u r a t i o n ) {
/∗ TODO ∗/
11 }
13 /∗ TODO ∗/
15 @Override
public String toString () {
17 r e t u r n ”Dvd [ ” + ” d u r a t i o n=” + d u r a t i o n
+ ” , t i t l e =’” + t i t l e + ’ \ ’ ’
19 + ” , y e a r=” + y e a r + ’ ] ’ ;
}
21 }
3 /∗ ∗
∗ C l a s s r e p r e s e n t i n g a Book
5 ∗/
p u b l i c c l a s s Book e x t e n d s Item {
7 i n t pages ;
9 p u b l i c Book ( S t r i n g t i t l e , i n t year , i n t p a g e s ) {
3
HaQT Object-Oriented Programming
/∗ TODO ∗/
11 }
13 /∗ TODO ∗/
15 @Override
public String toString () {
17 r e t u r n ”Book [ ” + ” p a g e s=” + p a g e s
+ ” , t i t l e =’” + t i t l e + ’ \ ’ ’
19 + ” , y e a r=” + y e a r + ’ ] ’ ;
}
21 }
3 import j a v a . u t i l . Date ;
5 /∗ ∗
∗ Rent implements a r e n t o f an Item f o r a d e l i m i t e d time frame
7 ∗/
p u b l i c c l a s s Rent {
9 Item item ;
Student s t u d e n t ;
11 Date b e g i n ;
Date end ;
13
p u b l i c Rent ( Item item , Student s t u d e n t , Date begin , Date end ) {
15 /∗ TODO ∗/
}
17
/∗ TODO ∗/
19
@Override
21 public String toString () {
r e t u r n ” Rent [ ” + ” item=” + item
23 + ” , s t u d e n t=” + s t u d e n t
+ ” , b e g i n=” + b e g i n
25 + ” , end=” + end + ’ ] ’ ;
}
27 }
4
HaQT Object-Oriented Programming
3 import j a v a . u t i l . O b j e c t s ;
23 /∗ TODO ∗/
25 @Override
p u b l i c i n t compareTo ( Student s ) {
27 /∗ TODO ∗/
}
29
@Override
31 p u b l i c b o o l e a n e q u a l s ( Object o ) {
i f ( this == o) {
33 return true ;
}
35
i f ( o = = n u l l | | g e t C l a s s ( ) != o . g e t C l a s s ( ) ) {
37 return f a l s e ;
}
39
Student s t u d e n t = ( Student ) o ;
41 r e t u r n Double . compare ( s t u d e n t . aver age , a v e r a g e ) = = 0
&& O b j e c t s . e q u a l s ( name , s t u d e n t . name )
43 && O b j e c t s . e q u a l s ( lastname , s t u d e n t . l a s t n a m e )
&& O b j e c t s . e q u a l s ( phone , s t u d e n t . phone ) ;
45 }
47 @Override
p u b l i c i n t hashCode ( ) {
49 r e t u r n O b j e c t s . hash ( name , lastname , phone , a v e r a g e ) ;
}
51
@Override
53 public String toString () {
5
HaQT Object-Oriented Programming
r e t u r n ” Student [ ” +
55 ”name=’” + name + ’ \ ’ ’ +
” , la s t n a m e =’” + l a s t n a m e + ’ \ ’ ’ +
57 ” , phone =’” + phone + ’ \ ’ ’ +
” , a v e r a g e=” + a v e r a g e +
59 ’] ’;
}
61 }
3 import j a v a . t e x t . P a r s e E x c e p t i o n ;
import j a v a . t e x t . SimpleDateFormat ;
5 import j a v a . u t i l . L o c a l e ;
7 /∗ ∗
∗ Implement t h e c l a s s e s d e s c r i b e d i n UML diagram .
9 ∗ Test them with t h e f o l l o w i n g main .
∗/
11 p u b l i c c l a s s TestApp {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) throws P a r s e E x c e p t i o n {
13 Item i 1 = new Book ( ” S o f f o c a r e ” , 2 0 0 2 , 1 7 0 ) ;
Item i 2 = new Dvd( ”Moon” , 2 0 1 1 , 1 3 0 ) ;
15
Student s 1 = new Student ( ” 0001 ” , ” D a r r e l l ” , ” Abbott ” ) ;
17 Student s 2 = new Student ( ” 0002 ” , ” Nick ” , ” Drake ” ) ;
27 L i b r a r y l = new L i b r a r y ( r e n t s ) ;
System . out . p r i n t l n ( l . g e t L o n g e s t R e n t ( ) ) ;
29 }
}
6
HaQT Object-Oriented Programming
2 Exercises on Collections
2.1 MyList
Write code for an application designed as shown in the following class diagram.
7
HaQT Object-Oriented Programming
v o i d add ( Object o , i n t i n d e x ) ;
6 v o i d remove ( i n t i n d e x ) ;
Object g e t ( i n t i n d e x ) ;
8 int size () ;
}
/∗ ∗
4 ∗ Implementation o f a s i m p l i f i e d A r r a y L i s t
∗/
6 p u b l i c c l a s s MyArrayList e x t e n d s MyAbstractList {
s t a t i c f i n a l i n t INITIAL SIZE = 1 6 ;
8 Object [ ] e l e m e n t s ;
int size ;
10
p u b l i c MyArrayList ( ) {
12 e l e m e n t s = new Object [ INITIAL SIZE ] ;
size = 0;
14 }
16 @Override
p u b l i c v o i d add ( Object o ) {
18 i f ( s i z e >= e l e m e n t s . l e n g t h − 1 ) {
8
HaQT Object-Oriented Programming
enlarge () ;
20 }
e l e m e n t s [ s i z e ++ ] = o ;
22 }
24 @Override
p u b l i c v o i d add ( Object o , i n t i n d e x ) {
26 /∗ TODO ∗/
}
28
@Override
30 p u b l i c Object g e t ( i n t i n d e x ) {
c h e c k B o u n d a r i e s ( index , s i z e − 1 ) ;
32 return elements [ index ] ;
}
34
@Override
36 p u b l i c v o i d remove ( i n t i n d e x ) {
/∗ TODO ∗/
38 }
40 @Override
public int size () {
42 /∗ TODO ∗/
}
44
void enlarge ( ) {
46 Object [ ] tmp = new Object [ e l e m e n t s . l e n g t h ∗ 2 ] ;
System . a r r a y c o p y ( e l e m e n t s , 0 , tmp , 0 , e l e m e n t s . l e n g t h ) ;
48 e l e m e n t s = tmp ;
}
50 }
9
HaQT Object-Oriented Programming
/∗ TODO ∗/
16 }
18 p u b l i c Object g e t P a y l o a d ( ) {
/∗ TODO ∗/
20 }
22 p u b l i c v o i d s e t P a y l o a d ( Object dataValue ) {
/∗ TODO ∗/
24 }
26 p u b l i c MyLinkedListNode getNext ( ) {
/∗ TODO ∗/
28 }
30 p u b l i c v o i d s e t N e x t ( MyLinkedListNode nextValue ) {
/∗ TODO ∗/
32 }
}
3 /∗ ∗
∗ Implementation o f a s i m p l i f i e d L i n k e d L i s t c l a s s
5 ∗/
p u b l i c c l a s s MyLinkedList e x t e n d s MyAbstractList {
7 MyLinkedListNode head ;
int size ;
9
p u b l i c MyLinkedList ( ) {
11 head = n u l l ;
size = 0;
13 }
15 p r i v a t e MyLinkedListNode getNodeByIndex ( i n t i n d e x ) {
MyLinkedListNode c u r r e n t = head ;
17 f o r ( i n t i = 0 ; i < i n d e x ; i ++ ) {
c u r r e n t = c u r r e n t . getNext ( ) ;
19 }
return current ;
21 }
23 @Override
p u b l i c v o i d add ( Object o ) {
25 add ( o , s i z e ) ;
}
27
10
HaQT Object-Oriented Programming
@Override
29 p u b l i c v o i d add ( Object o , i n t i n d e x ) {
c h e c k B o u n d a r i e s ( index , s i z e ) ;
31 i f ( index = = 0) {
head = new MyLinkedListNode ( o , head ) ;
33 } else {
MyLinkedListNode c u r r e n t = getNodeByIndex ( i n d e x − 1 ) ;
35 c u r r e n t . s e t N e x t ( new MyLinkedListNode ( o , c u r r e n t . getNext ( ) ) ) ;
}
37 s i z e ++ ;
}
39
@Override
41 p u b l i c Object g e t ( i n t i n d e x ) {
/∗ TODO ∗/
43 }
45 @Override
p u b l i c v o i d remove ( i n t i n d e x ) {
47 /∗ TODO ∗/
}
49
@Override
51 public int size () {
/∗ TODO ∗/
53 }
}
11
HaQT Object-Oriented Programming
2.2 PhoneBook
Write code for an application designed as shown in the following class diagram.
12
HaQT Object-Oriented Programming
20 /∗ TODO ∗/
22 @Override
p u b l i c i n t compareTo ( Student s ) {
24 /∗ TODO ∗/
}
26
@Override
28 p u b l i c i n t hashCode ( ) {
r e t u r n O b j e c t s . hash ( name , lastname , phone , a v e r a g e ) ;
30 }
32 @Override
p u b l i c b o o l e a n e q u a l s ( Object o ) {
34 /∗ TODO ∗/
}
36
@Override
38 public String toString () {
r e t u r n ” Student [ ” +
40 ”name=’” + name + ’ \ ’ ’ +
” , la s t n a m e =’” + l a s t n a m e + ’ \ ’ ’ +
42 ” , phone =’” + phone + ’ \ ’ ’ +
” , a v e r a g e=” + a v e r a g e +
44 ’] ’;
}
46 }
16 /∗ ∗
∗ S e a r c h a p e r s o n w i t h i n t h e PhoneBook by name
18 ∗
13
HaQT Object-Oriented Programming
24 /∗ ∗
∗ S e a r c h a p e r s o n w i t h i n t h e PhoneBook by l a s t n a m e
26 ∗
∗ @param l a s t na m e The la s t n a m e t o be s e a r c h e d
28 ∗ @return The p e r s o n found , n u l l o t h e r w i s e
∗/
30 Student searchByLastname ( S t r i n g l a s t n a m e ) ;
32 /∗ ∗
∗ S e a r c h a p e r s o n w i t h i n t h e PhoneBook by number
34 ∗
∗ @param phone The phone t o be s e a r c h e d
36 ∗ @return The p e r s o n found , n u l l o t h e r w i s e
∗/
38 Student searchByNumber ( S t r i n g phone ) ;
40 /∗ ∗
∗ D e l e t e a p e r s o n from t h e PhoneBook
42 ∗
∗ @param phone The phone number t o be s e a r c h e d .
44 ∗/
v o i d deleteByNumber ( S t r i n g phone ) ;
46 }
12 p u b l i c PhoneBookList ( ) {
phoneBook = new A r r a y L i s t <>() ;
14 }
16 @Override
p u b l i c v o i d addPerson ( Student p ) {
18 /∗ TODO ∗/
14
HaQT Object-Oriented Programming
}
20
@Override
22 p u b l i c Student searchByName ( S t r i n g name ) {
/∗ TODO ∗/
24 }
26 @Override
p u b l i c Student searchByLastname ( S t r i n g l a s t n a m e ) {
28 /∗ TODO ∗/
}
30
@Override
32 p u b l i c Student searchByNumber ( S t r i n g phone ) {
/∗ TODO ∗/
34 }
36 @Override
p u b l i c v o i d deleteByNumber ( S t r i n g phone ) {
38 /∗ TODO ∗/
}
40 }
24 p u b l i c Student searchByLastname ( S t r i n g l a s t n a m e ) {
15
HaQT Object-Oriented Programming
/∗ TODO ∗/
26 }
32 p u b l i c v o i d deleteByNumber ( S t r i n g phone ) {
/∗ TODO ∗/
34 }
}
3 /∗ ∗
∗ The PhoneBook I n t e r f a c e d e f i n e s t h e f u n c t i o n a l i t i e s o f a
5 ∗ b a s i c phone book .
∗
7 ∗ P r o v i d e two d i f f e r e n t i m p l e m e n t a t i o n s o f t h e PhoneBook i n t e r f a c e
∗ working with t h e u s e c a s e below . The f i r s t , ( a ) i n t e r n a l l y u s e s
9 ∗ an A r r a y l i s t , t h e s e c o nd ( b ) i n t e r n a l l y u s e s an HashMap .
∗/
11 p u b l i c c l a s s TestApp {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
13 PhoneBook pb = new PhoneBookMap ( ) ;
15 // Comment t h i s l i n e f o r s w i t c h i n g i m p l e m e n t a t i o n
// PhoneBook pb = new PhoneBookArray ( ) ;
17 pb . addPerson ( new Student ( ” N i c o l a ” , ” B i c o c c h i ” , ” 34567 ” ) ) ;
pb . addPerson ( new Student ( ”Marco” , ” R iz zo ” , ” 45243 ” ) ) ;
19 pb . addPerson ( new Student ( ” L u i s a ” , ” Poppi ” , ” 24564 ” ) ) ;
27 // D e l e t e an e l e m e n t !
pb . deleteByNumber ( ” 24564 ” ) ;
29 System . out . p r i n t l n ( pb . searchByLastname ( ” Poppi ” ) ) ;
}
31 }
16
HaQT Object-Oriented Programming
2.3 Polynomials
Write code for an application designed as shown in the following class diagram.
3 /∗ ∗
∗ I n t e r f a c e r e p r e s e n t i n g a p o l y n o m i a l with a r b i t r a r y g r a d e
5 ∗/
p u b l i c i n t e r f a c e Poly {
7
/∗ ∗
9 ∗ Returns t h e d e g r e e o f t h e p o l y n o m i a l
17
HaQT Object-Oriented Programming
∗
11 ∗ @return t h e d e g r e e o f t h e p o l y n o m i a l
∗/
13 int degree () ;
15 /∗ ∗
∗ Returns an new p o l y n o m i a l which i s t h e d e r i v a t i v e o f t h e c u r r e n t
17 ∗ o b j e c t . The c a l l i n v o k e d on o b j e c t ( 1 ) , r e t u r n s o b j e c t ( 2 )
∗ ( 1 ) c0 + c 1 ∗ x + . . . + c n ∗ xˆn
19 ∗ ( 2 ) c1 + 2 c 2 ∗ x + . . . + nc n ∗ x ˆ ( n−1)
∗
21 ∗ @return Returns an new p o l y n o m i a l which i s t h e d e r i v a t i v e o f t h e
∗ current object
23 ∗/
Poly d e r i v a t i v e ( ) ;
25
/∗ ∗
27 ∗ Returns t h e c o e f f i c i e n t o f t h e monomial with t h e s p e c i f i e d d e g r e e
∗
29 ∗ @param d e g r e e The d e g r e e t o be q u e r i e d ( g e t t h e c o e f f i c i e n t )
∗ @return The c o e f f i c i e n t o f t h e monomial with t h e s p e c i f i e d d e g r e e
31 ∗/
double c o e f f i c i e n t ( i n t degree ) ;
33
/∗ ∗
35 ∗ Returns a d o u b l e [ ] c o n t a i n i n g a l l t h e c o e f f i c i e n t s
∗
37 ∗ @return A d o u b l e [ ] c o n t a i n i n g a l l t h e c o e f f i c i e n t s
∗/
39 double [ ] c o e f f i c i e n t s ( ) ;
}
18
HaQT Object-Oriented Programming
@Override
16 p u b l i c b o o l e a n e q u a l s ( Object o ) {
/∗ TODO ∗/
18 }
20 @Override
p u b l i c i n t hashCode ( ) {
22 r e t u r n O b j e c t s . hashCode ( c o e f f i c i e n t s ( ) ) ;
}
24
@Override
26 public String toString () {
/∗ TODO ∗/
28 }
}
3 /∗ ∗
∗ C l a s s r e p r e s e n t i n g a p o l y n o m i a l with c o e f f i c i e n t s s t o r e d a s on
5 ∗ array of doubles
∗/
7 p u b l i c c l a s s ArrayPoly e x t e n d s A b s t r a c t P o l y {
p r i v a t e f i n a l double [ ] c o e f f i c i e n t s ;
9
p u b l i c ArrayPoly ( d o u b l e [ ] coefficients ) {
11 /∗ TODO ∗/
}
13
@Override
15 public int degree () {
/∗ TODO ∗/
17 }
19 @Override
p u b l i c Poly d e r i v a t i v e ( ) {
21 /∗ TODO ∗/
}
23
@Override
25 p u b l i c double c o e f f i c i e n t ( i n t degree ) {
/∗ TODO ∗/
27 }
29 @Override
p u b l i c double [ ] c o e f f i c i e n t s () {
31 /∗ TODO ∗/
19
HaQT Object-Oriented Programming
}
33 }
3 import j a v a . u t i l . A r r a y L i s t ;
import j a v a . u t i l . L i s t ;
5
/∗ ∗
7 ∗ C l a s s r e p r e s e n t i n g a p o l y n o m i a l with c o e f f i c i e n t s s t o r e d a s a l i s t
∗/
9 public c l a s s ListPoly extends AbstractPoly {
L i s t <Double> c o e f f i c i e n t s ;
11
p u b l i c ListPoly ( double [ ] c o e f f s ) {
13 /∗ TODO ∗/
}
15
@Override
17 public int degree () {
/∗ TODO ∗/
19 }
21 @Override
p u b l i c Poly d e r i v a t i v e ( ) {
23 /∗ TODO ∗/
}
25
@Override
27 p u b l i c double c o e f f i c i e n t ( i n t degree ) {
/∗ TODO ∗/
29 }
31 @Override
p u b l i c double [ ] c o e f f i c i e n t s () {
33 /∗ TODO ∗/
}
35 }
1 package oop . c o l l e c t i o n s . e x e r c i s e s . p o l y n o m i a l s ;
3 /∗ ∗
∗ Develop two c l a s s e s , namely ArrayPoly and L i s t P o l y ,
20
HaQT Object-Oriented Programming
5 ∗ f o r managing p o l y n o m i a l s .
∗ More s p e c i f i c a l l y , t h e two c l a s s e s must e x h i b i t t h e same
7 ∗ f u n c t i o n a l i t i e s ( they both implement t h e Poly I n t e r f a c e ) but
∗ u s i n g d i f f e r e n t mechanisms i n t e r n a l l y .
9 ∗ <p>
∗ Given a g e n e r i c po ly nomi al , c0 + c 1 ∗ x + . . . + c n ∗ xˆn
11 ∗ <p>
∗ ArrayPoly s t o r e s a p o l y n o m i a l u s i n g d o u b l e [ ] ( c0 . . c n ) .
13 ∗ <p>
∗ L i s t P o l y , s t o r e s a p o l y n o m i a l u s i n g L i s t <Double> ( c0 . . c n ) .
15 ∗ <p>
∗ Both c l a s s e s must work with t h e main method p r o v i d e d below .
17 ∗/
p u b l i c c l a s s TestApp {
19 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
27 i f ( ap . e q u a l s ( l p ) && l p . e q u a l s ( ap ) )
System . out . p r i n t l n ( ”ap = = l p ” ) ;
29 else
System . out . p r i n t l n ( ”ap != l p ” ) ;
31
ap = ap . d e r i v a t i v e ( ) ;
33 System . out . p r i n t l n ( ”ap ’ = ” + ap . t o S t r i n g ( ) ) ;
35 ap = ap . d e r i v a t i v e ( ) ;
System . out . p r i n t l n ( ”ap ’ ’ = ” + ap . t o S t r i n g ( ) ) ;
37
lp = lp . derivative () ;
39 System . out . p r i n t l n ( ” l p ’ = ” + l p . t o S t r i n g ( ) ) ;
41 lp = lp . derivative () ;
System . out . p r i n t l n ( ” l p ’ ’ = ” + l p . t o S t r i n g ( ) ) ;
43 }
}
21