OOP2025 JavaExercises Lab8 TheJavaCollectionsFramework
OOP2025 JavaExercises Lab8 TheJavaCollectionsFramework
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 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 Exercise on Lists
3 import j a v a . u t i l . ∗ ;
7 /∗ ∗
∗ Function t o i n s e r t an e l e m e n t i n t o a l i s t a t t h e b e g i n n i n g
9 ∗/
p u b l i c s t a t i c v o i d i n s e r t F i r s t ( L i s t <I n t e g e r > l i s t , i n t v a l u e ) {
11 /∗ TODO ∗/
}
13
/∗ ∗
15 ∗ Function t o i n s e r t an e l e m e n t i n t o a l i s t a t t h e end
∗/
17 p u b l i c s t a t i c v o i d i n s e r t L a s t ( L i s t <I n t e g e r > l i s t , i n t v a l u e ) {
/∗ TODO ∗/
19 }
21 /∗ ∗
∗ Function to replace the 3rd element of a list with a given value
23 ∗/
p u b l i c s t a t i c v o i d r e p l a c e ( L i s t <I n t e g e r > l i s t , i n t v a l u e ) {
25 /∗ TODO ∗/
}
27
/∗ ∗
29 ∗ Function t o remove t h e 3 rd e l e m e n t from a l i s t
∗/
31 p u b l i c s t a t i c v o i d removeThird ( L i s t <I n t e g e r > l i s t ) {
/∗ TODO ∗/
33 }
35 /∗ ∗
∗ Function t o remove t h e e l e m e n t ”666” from a l i s t
37 ∗/
p u b l i c s t a t i c v o i d remo veEv il ( L i s t <I n t e g e r > l i s t ) {
39 /∗ TODO ∗/
}
41
/∗ ∗
43 ∗ Function r e t u r n i n g a L i s t <I n t e g e r > c o n t a i n i n g
∗ t h e f i r s t 10 s q u a r e numbers ( i . e . , 1 , 4 , 9 , 1 6 , . . . )
45 ∗/
p u b l i c s t a t i c L i s t <I n t e g e r > g e n e r a t e S q u a r e ( ) {
47 /∗ TODO ∗/
}
2
HaQT Object-Oriented Programming
49
/∗ ∗
51 ∗ Function t o v e r i f y i f a l i s t c o n t a i n s a c e r t a i n v a l u e
∗/
53 p u b l i c s t a t i c b o o l e a n c o n t a i n s ( L i s t <I n t e g e r > l i s t , i n t v a l u e ) {
/∗ TODO ∗/
55 }
57 /∗ ∗
∗ Function to copy a list into another list (without using library functions)
59 ∗ Note w e l l : t h e t a r g e t l i s t must be emptied b e f o r e t h e copy
∗/
61 p u b l i c s t a t i c v o i d copy ( L i s t <I n t e g e r > s o u r c e , L i s t <I n t e g e r > t a r g e t ) {
/∗ TODO ∗/
63 }
65 /∗ ∗
∗ Function t o r e v e r s e t h e e l e m e n t s o f a l i s t
67 ∗/
p u b l i c s t a t i c v o i d r e v e r s e ( L i s t <I n t e g e r > l i s t ) {
69 /∗ TODO ∗/
}
71
/∗ ∗
73 ∗ Function to reverse the elements of a list (without using library functions)
∗/
75 p u b l i c s t a t i c v o i d r e v e r s e M a n u a l ( L i s t <I n t e g e r > l i s t ) {
/∗ TODO ∗/
77 }
79 /∗ ∗
∗ Function t o i n s e r t t h e same e l e m e n t both a t t h e
81 ∗ b e g i n n i n g and t h e end o f t h e same L i n k e d L i s t
∗ Note w e l l : you can u s e L i n k e d L i s t s p e c i f i c methods
83 ∗/
p u b l i c s t a t i c v o i d i n s e r t B e g i n n i n g E n d ( L i n k e d L i s t <I n t e g e r > l i s t ,
85 int value ) {
/∗ TODO ∗/
87 }
}
3
HaQT Object-Oriented Programming
2 Exercise on Sets
public c l a s s Sets {
6 /∗ ∗
∗ Function r e t u r n i n g t h e i n t e r s e c t i o n o f two g i v e n s e t s
8 ∗ ( without using l i b r a r y f u n c t i o n s )
∗/
10 p u b l i c s t a t i c Set<I n t e g e r > i n t e r s e c t i o n M a n u a l ( Set<I n t e g e r > f i r s t ,
Set<I n t e g e r > s e c o n d ) {
12 /∗ TODO ∗/
}
14
/∗ ∗
16 ∗ Function r e t u r n i n g t h e union o f two g i v e n s e t s
∗ ( without using l i b r a r y f u n c t i o n s )
18 ∗/
p u b l i c s t a t i c Set<I n t e g e r > unionManual ( Set<I n t e g e r > f i r s t ,
20 Set<I n t e g e r > s e c o n d ) {
/∗ TODO ∗/
22 }
24 /∗ ∗
∗ Function returning the intersection of two given sets (see retainAll())
26 ∗/
p u b l i c s t a t i c Set<I n t e g e r > i n t e r s e c t i o n ( Set<I n t e g e r > f i r s t ,
28 Set<I n t e g e r > s e c o n d ) {
/∗ TODO ∗/
30 }
32 /∗ ∗
∗ Function returning the union of two given sets (see addAll())
34 ∗/
p u b l i c s t a t i c Set<I n t e g e r > union ( Set<I n t e g e r > f i r s t , Set<I n t e g e r >
,→ s e c o n d ) {
36 /∗ TODO ∗/
}
38
/∗ ∗
40 ∗ Function t o t r a n s f o r m a s e t i n t o a l i s t w i t h o u t d u p l i c a t e s
∗ Note w e l l : c o l l e c t i o n s can be c r e a t e d from a n o t h e r c o l l e c t i o n !
42 ∗/
p u b l i c s t a t i c L i s t <I n t e g e r > t o L i s t ( Set<I n t e g e r > s o u r c e ) {
44 /∗ TODO ∗/
}
46
/∗ ∗
4
HaQT Object-Oriented Programming
72 /∗ ∗
∗ Function a c c e p t i n g a s t r i n g s ,
74 ∗ and r e t u r n i n g a s e t c o m p r i s i n g a l l r e c u r r i n g c h a r a c t e r s .
∗ For example a l l R e c u r r i n g C h a r s ( ”mamma” ) −> [m, a ] .
76 ∗/
p u b l i c s t a t i c Set<Character > a l l R e c u r r i n g C h a r s ( S t r i n g s ) {
78 /∗ TODO ∗/
}
80
/∗ ∗
82 ∗ Function t o t r a n s f o r m a s e t i n t o an a r r a y
∗/
84 p u b l i c s t a t i c I n t e g e r [ ] toArray ( Set<I n t e g e r > s o u r c e ) {
/∗ TODO ∗/
86 }
88 /∗ ∗
∗ Function t o r e t u r n t h e f i r s t item from a T r e e S e t
90 ∗ Note w e l l : u s e T r e e S e t s p e c i f i c methods
∗/
92 p u b l i c s t a t i c i n t g e t F i r s t ( TreeSet<I n t e g e r > s o u r c e ) {
/∗ TODO ∗/
94 }
96 /∗ ∗
∗ Function t o r e t u r n t h e l a s t item from a T r e e S e t
98 ∗ Note w e l l : u s e T r e e S e t s p e c i f i c methods
5
HaQT Object-Oriented Programming
∗/
100 p u b l i c s t a t i c i n t g e t L a s t ( TreeSet<I n t e g e r > s o u r c e ) {
/∗ TODO ∗/
102 }
104 /∗ ∗
∗ Function t o g e t an e l e m e n t from a T r e e S e t
106 ∗ which i s s t r i c t l y g r e a t e r than a g i v e n e l e m e n t .
∗ Note w e l l : u s e T r e e S e t s p e c i f i c methods
108 ∗/
p u b l i c s t a t i c i n t g e t G r e a t e r ( TreeSet<I n t e g e r > s o u r c e , i n t v a l u e ) {
110 /∗ TODO ∗/
}
112 }
3 Exercise on Maps
8 p u b l i c c l a s s Maps {
/∗ ∗
10 ∗ Function to return the number of key-value mappings of a map
∗/
12 p u b l i c s t a t i c i n t count (Map<I n t e g e r , I n t e g e r > map) {
/∗ TODO ∗/
14 }
16 /∗ ∗
∗ Function t o remove a l l mappings from a map
18 ∗/
p u b l i c s t a t i c v o i d empty (Map<I n t e g e r , I n t e g e r > map) {
20 /∗ TODO ∗/
}
22
/∗ ∗
24 ∗ Function to test if a map contains a mapping for the specified key
∗/
26 p u b l i c s t a t i c b o o l e a n c o n t a i n s (Map<I n t e g e r , I n t e g e r > map , i n t key ) {
/∗ TODO ∗/
28 }
6
HaQT Object-Oriented Programming
30 /∗ ∗
∗ Function t o t e s t i f a map c o n t a i n s a mapping f o r
32 ∗ t h e s p e c i f i e d key and i f i t s v a l u e e q u a l s t h e s p e c i f i e d v a l u e
∗/
34 p u b l i c s t a t i c b o o l e a n c on t a i n s K e y V a l u e (Map<I n t e g e r , I n t e g e r > map ,
i n t key ,
36 int value ) {
/∗ TODO ∗/
38 }
40 /∗ ∗
∗ Function t o r e t u r n t h e key s e t o f map
42 ∗/
p u b l i c s t a t i c Set<I n t e g e r > k e y S e t (Map<I n t e g e r , I n t e g e r > map) {
44 /∗ TODO ∗/
}
46
/∗ ∗
48 ∗ Function t o r e t u r n t h e v a l u e s o f a map
∗/
50 p u b l i c s t a t i c C o l l e c t i o n <I n t e g e r > v a l u e s (Map<I n t e g e r , I n t e g e r > map) {
/∗ TODO ∗/
52 }
54 /∗ ∗
∗ Function , i n t e r n a l l y u s i n g a map , r e t u r n i n g ” b l a c k ” ,
56 ∗ ” w h i t e ” , o r ” r e d ” depending on i n t i n p u t v a l u e .
∗ ” black ” = 0 , ” white ” = 1 , ” red ” = 2
58 ∗/
public s t a t i c String getColor ( int value ) {
60 /∗ TODO ∗/
}
62 }
4.1 Comparable
A comparable object is capable of comparing itself with another object. The class itself must
implements the java.lang.Comparable interface to compare its instances.
Consider a Movie class that has members like, rating, name, year. Suppose we wish to sort
a list of Movies based on year of release. We can implement the Comparable interface with
the Movie class, and we override the method compareTo() of Comparable interface.
7
HaQT Object-Oriented Programming
/∗ ∗
2 ∗ A Java program t o d em o n s t r a t e u s e o f Comparable
∗/
4
package hus . oop . comparable ;
6
import j a v a . i o . ∗ ;
8 import j a v a . u t i l . ∗ ;
10 /∗ ∗
∗ A c l a s s ’ Movie ’ t h a t implements Comparable
12 ∗/
c l a s s Movie implements Comparable<Movie> {
14 p r i v a t e S t r i n g name ;
p r i v a t e double r a t i n g ;
16 pri vate i n t year ;
18 // Used t o s o r t movies by y e a r
p u b l i c i n t compareTo ( Movie movie ) {
20 /∗ TODO ∗/
}
22
// C o n s t r u c t o r
24 p u b l i c Movie ( S t r i n g name , d o u b l e r a t i n g , i n t y e a r ) {
/∗ TODO ∗/
26 }
28 // G e t t e r methods f o r a c c e s s i n g p r i v a t e data
p u b l i c double getRating ( ) {
30 /∗ TODO ∗/
}
32
p u b l i c S t r i n g getName ( ) {
34 /∗ TODO ∗/
}
36
p u b l i c i n t getYear ( ) {
38 /∗ TODO ∗/
}
40 }
c l a s s ComparableTest {
4 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 ) {
L i s t <Movie> l i s t = new A r r a y L i s t <>() ;
6 l i s t . add ( new Movie ( ” Force Awakens” , 8 . 3 , 2 0 1 5 ) ) ;
l i s t . add ( new Movie ( ” S t a r Wars” , 8 . 7 , 1 9 7 7 ) ) ;
8
HaQT Object-Oriented Programming
4.2 Comparator
Unlike Comparable, Comparator is external to the element type we are comparing. It’s a
separate class. We create multiple separate classes (that implement Comparator) to compare
by different members. Collections class has a second sort() method and it takes Comparator.
The sort() method invokes the compare() to sort objects.
1. Create a class that implements Comparator (and thus the compare() method that does
the work previously done by compareTo()).
2. Make an instance of the Comparator class.
3. Call the overloaded sort() method, giving it both the list and the instance of the class
that implements Comparator.
/∗ ∗
2 ∗ A Java program t o de m o n st r a t e Comparator i n t e r f a c e
∗/
4
10 /∗ ∗
∗ A c l a s s ’ Movie ’ t h a t implements Comparable
12 ∗/
c l a s s Movie implements Comparable<Movie> {
9
HaQT Object-Oriented Programming
14 p r i v a t e S t r i n g name ;
p r i v a t e double r a t i n g ;
16 pri vate i n t year ;
18 // Used t o s o r t movies by y e a r
p u b l i c i n t compareTo ( Movie m) {
20 /∗ TODO ∗/
}
22
// C o n s t r u c t o r
24 p u b l i c Movie ( S t r i n g name , d o u b l e r a t i n g , i n t y e a r ) {
/∗ TODO ∗/
26 }
28 // G e t t e r methods f o r a c c e s s i n g p r i v a t e data
p u b l i c double getRating ( ) {
30 /∗ TODO ∗/
}
32
p u b l i c S t r i n g getName ( ) {
34 /∗ TODO ∗/
}
36
p u b l i c i n t getYear ( ) {
38 /∗ TODO ∗/
}
40 }
10
HaQT Object-Oriented Programming
34 // Uses Comparable t o s o r t by y e a r
System . out . p r i n t l n ( ”\ n S o r t e d by y e a r ” ) ;
36 Collections . sort ( l i s t ) ;
f o r ( Movie movie : l i s t ) {
38 System . out . p r i n t l n ( movie . getYear ( ) + ” ” +
movie . g e t R a t i n g ( ) + ” ” +
40 movie . getName ( ) + ” ” ) ;
}
42 }
}
11
HaQT Object-Oriented Programming
Write code for an application designed as shown in the following class diagram.
12
HaQT Object-Oriented Programming
3 p u b l i c a b s t r a c t c l a s s Country {
p r o t e c t e d S t r i n g code ;
5 p r o t e c t e d S t r i n g name ;
p u b l i c S t r i n g getName ( ) {
21 r e t u r n name ;
}
23
p u b l i c v o i d setName ( S t r i n g name ) {
25 t h i s . name = name ;
}
27
public abstract int getPopulation () ;
29
p u b l i c d o u b l e getArea ( ) ;
31
p u b l i c d o u b l e getGdp ( ) ;
33 }
13
HaQT Object-Oriented Programming
3 p u b l i c c l a s s A f r i c a C o u n t r y e x t e n d s Country {
private int population ;
5 p r i v a t e double area ;
p r i v a t e d o u b l e gdp ;
7
p u b l i c A f r i c a C o u n t r y ( S t r i n g code ,
9 S t r i n g name ,
in t population ,
11 d o u b l e area ,
d o u b l e gdp ) {
13 s u p e r ( code , name ) ;
this . population = population ;
15 t h i s . area = area ;
t h i s . gdp = gdp ;
17 }
27 p u b l i c d o u b l e getArea ( ) {
return area ;
29 }
35 p u b l i c d o u b l e getGdp ( ) {
r e t u r n gdp ;
37 }
39 p u b l i c v o i d setGdp ( d o u b l e gdp ) {
t h i s . gdp = gdp ;
41 }
}
p u b l i c c l a s s AsiaCountry e x t e n d s Country {
4 private int population ;
p r i v a t e double area ;
14
HaQT Object-Oriented Programming
6 p r i v a t e d o u b l e gdp ;
8 p u b l i c AsiaCountry ( S t r i n g code ,
S t r i n g name ,
10 in t population ,
d o u b l e area ,
12 d o u b l e gdp ) {
s u p e r ( code , name ) ;
14 this . population = population ;
t h i s . area = area ;
16 t h i s . gdp = gdp ;
}
18
...
20 }
p u b l i c c l a s s EuropeCountry e x t e n d s Country {
4 private int population ;
p r i v a t e double area ;
6 p r i v a t e d o u b l e gdp ;
8 p u b l i c EuropeCountry ( S t r i n g code ,
S t r i n g name ,
10 in t population ,
d o u b l e area ,
12 d o u b l e gdp ) {
s u p e r ( code , name ) ;
14 this . population = population ;
t h i s . area = area ;
16 t h i s . gdp = gdp ;
}
18
...
20 }
import j a v a . u t i l . Arrays ;
4
p u b l i c c l a s s CountryArrayManager {
6 p r i v a t e Country [ ] c o u n t r i e s ;
private int length ;
8
15
HaQT Object-Oriented Programming
p u b l i c CountryArrayManager ( ) {
10 c o u n t r i e s = new Country [ 1 ] ;
this . length = 0;
12 }
14 p u b l i c CountryArrayManager ( i n t maxLength ) {
c o u n t r i e s = new Country [ maxLength ] ;
16 this . length = 0;
}
18
p u b l i c i n t ge t Len gth ( ) {
20 return this . length ;
}
22
p u b l i c Country [ ] g e t C o u n t r i e s ( ) {
24 return this . countries ;
}
26
private void c o r r e c t ( ) {
28 int nullFirstIndex = 0;
f o r ( i n t i = 0 ; i < t h i s . c o u n t r i e s . l e n g t h ; i ++ ) {
30 i f ( this . countries [ i ] == null ) {
nullFirstIndex = i ;
32 break ;
}
34 }
36 i f ( n u l l F i r s t I n d e x > 0) {
this . length = nullFirstIndex ;
38 f o r ( i n t i = n u l l F i r s t I n d e x ; i < t h i s . c o u n t r i e s . l e n g t h ; i ++ ) {
this . countries [ i ] = null ;
40 }
}
42 }
50 p u b l i c v o i d append ( Country c o u n t r y ) {
i f ( t h i s . l e n g t h >= t h i s . c o u n t r i e s . l e n g t h ) {
52 allocateMore () ;
}
54
t h i s . c o u n t r i e s [ t h i s . length ] = country ;
56 t h i s . l e n g t h ++ ;
}
58
p u b l i c b o o l e a n add ( Country country , i n t i n d e x ) {
16
HaQT Object-Oriented Programming
64 i f ( t h i s . l e n g t h >= t h i s . c o u n t r i e s . l e n g t h ) {
allocateMore () ;
66 }
72 t h i s . c o u n t r i e s [ index ] = country ;
t h i s . l e n g t h ++ ;
74 return true ;
}
76
p u b l i c b o o l e a n remove ( i n t i n d e x ) {
78 i f ( ( i n d e x < 0 ) | | ( i n d e x >= c o u n t r i e s . l e n g t h ) ) {
return f a l s e ;
80 }
82 f o r ( i n t i = i n d e x ; i < l e n g t h − 1 ; i ++ ) {
this . countries [ i ] = this . countries [ i + 1];
84 }
96 return t h i s . c o u n t r i e s [ index ] ;
}
98
/∗ ∗
100 ∗ Sort the c o u n t r i e s in order of i n c r e a s i n g population
∗ using s e l e c t i o n sort algorithm .
102 ∗ @return a r r a y o f i n c r e a s i n g p o p u l a t i o n c o u n t r i e s .
∗/
104 p u b l i c Country [ ] s o r t B y I n c r e a s i n g P o p u l a t i o n ( ) {
Country [ ] newArray = new Country [ t h i s . l e n g t h ] ;
106 System . a r r a y c o p y ( t h i s . c o u n t r i e s , 0 , newArray , 0 , t h i s . l e n g t h ) ;
110 r e t u r n newArray ;
}
17
HaQT Object-Oriented Programming
112
/∗ ∗
114 ∗ Sort the c o u n t r i e s in order of d e c r e a s i n g population
∗ using s e l e c t i o n sort algorithm .
116 ∗ @return a r r a y o f d e c r e a s i n g p o p u l a t i o n c o u n t r i e s .
∗/
118 p u b l i c Country [ ] s o r t B y D e c r e a s i n g P o p u l a t i o n ( ) {
Country [ ] newArray = new Country [ t h i s . l e n g t h ] ;
120 System . a r r a y c o p y ( t h i s . c o u n t r i e s , 0 , newArray , 0 , t h i s . l e n g t h ) ;
124 r e t u r n newArray ;
}
126
/∗ ∗
128 ∗ Sort the c o u n t r i e s in order of i n c r e a s i n g area
∗ u s i n g bubble s o r t a l g o r i t h m .
130 ∗ @return a r r a y o f i n c r e a s i n g a r e a c o u n t r i e s .
∗/
132 p u b l i c Country [ ] s o r t B y I n c r e a s i n g A r e a ( ) {
Country [ ] newArray = new Country [ t h i s . l e n g t h ] ;
134 System . a r r a y c o p y ( t h i s . c o u n t r i e s , 0 , newArray , 0 , t h i s . l e n g t h ) ;
138 r e t u r n newArray ;
}
140
/∗ ∗
142 ∗ Sort the c o u n t r i e s in order of d e c r e a s i n g area
∗ u s i n g bubble s o r t a l g o r i t h m .
144 ∗ @return a r r a y o f i n c r e a s i n g a r e a c o u n t r i e s .
∗/
146 p u b l i c Country [ ] s o r t B y D e c r e a s i n g A r e a ( ) {
Country [ ] newArray = new Country [ t h i s . l e n g t h ] ;
148 System . a r r a y c o p y ( t h i s . c o u n t r i e s , 0 , newArray , 0 , t h i s . l e n g t h ) ;
152 r e t u r n newArray ;
}
154
/∗ ∗
156 ∗ S o r t t h e c o u n t r i e s i n o r d e r o f i n c r e a s i n g GDP
∗ using i n s e r t i o n sort algorithm .
158 ∗ @return a r r a y o f i n c r e a s i n g GDP c o u n t r i e s .
∗/
160 p u b l i c Country [ ] s o r t B y I n c r e a s i n g G d p ( ) {
Country [ ] newArray = new Country [ t h i s . l e n g t h ] ;
162 System . a r r a y c o p y ( t h i s . c o u n t r i e s , 0 , newArray , 0 , t h i s . l e n g t h ) ;
18
HaQT Object-Oriented Programming
166 r e t u r n newArray ;
}
168
/∗ ∗
170 ∗ S o r t t h e c o u n t r i e s i n o r d e r o f i n c r e a s i n g GDP
∗ using i n s e r t i o n sort algorithm .
172 ∗ @return a r r a y o f i n c r e a s i n g i n s e r t i o n c o u n t r i e s .
∗/
174 p u b l i c Country [ ] sortByDecreasingGdp ( ) {
Country [ ] newArray = new Country [ t h i s . l e n g t h ] ;
176 System . a r r a y c o p y ( t h i s . c o u n t r i e s , 0 , newArray , 0 , t h i s . l e n g t h ) ;
180 r e t u r n newArray ;
}
182
public AfricaCountry [ ] f i l t e r A f r i c a C o u n t r y ( ) {
184 /∗ TODO ∗/
}
186
p u b l i c AsiaCountry [ ] f i l t e r A s i a C o u n t r y ( ) {
188 /∗ TODO ∗/
}
190
p u b l i c EuropeCountry [ ] f i l t e r E u r o p e C o u n t r y ( ) {
192 /∗ TODO ∗/
}
194
p u b l i c NorthAmericaCountry f i l t e r N o r t h A m e r i c a C o u n t r y ( ) {
196 /∗ TODO ∗/
}
198
p u b l i c OceaniaCountry f i l t e r O c e a n i a C o u n t r y ( ) {
200 /∗ TODO ∗/
}
202
p u b l i c SouthAmericaCountry f i l t e r S o u t h A m e r i c a C o u n t r y ( ) {
204 /∗ TODO ∗/
}
206
p u b l i c Country [ ] f i l t e r M o s t P o p u l o u s C o u n t r i e s ( i n t howMany) {
208 /∗ TODO ∗/
}
210
p u b l i c Country [ ] f i l t e r L e a s t P o p u l o u s C o u n t r i e s ( i n t howMany) {
212 return null ;
}
214
p u b l i c Country [ ] f i l t e r L a r g e s t A r e a C o u n t r i e s ( i n t howMany) {
19
HaQT Object-Oriented Programming
216 /∗ TODO ∗/
}
218
p u b l i c Country [ ] f i l t e r S m a l l e s t A r e a C o u n t r i e s ( i n t howMany) {
220 return null ;
}
222
p u b l i c Country [ ] f i l t e r H i g h e s t G d p C o u n t r i e s ( i n t howMany) {
224 /∗ TODO ∗/
}
226
p u b l i c Country [ ] f i l t e r L o w e s t G d p C o u n t r i e s ( i n t howMany) {
228 /∗ TODO ∗/
}
230
p u b l i c s t a t i c S t r i n g c o d e O f C o u n t r i e s T o S t r i n g ( Country [ ] c o u n t r i e s ) {
232 S t r i n g B u i l d e r c o d e O f C o u n t r i e s = new S t r i n g B u i l d e r ( ) ;
c o d e O f C o u n t r i e s . append ( ” [ ” ) ;
234 f o r ( i n t i = 0 ; i < c o u n t r i e s . l e n g t h ; i ++ ) {
Country c o u n t r y = c o u n t r i e s [ i ] ;
236 i f ( c o u n t r y != n u l l ) {
c o d e O f C o u n t r i e s . append ( c o u n t r y . getCode ( ) )
238 . append ( ” ” ) ;
}
240 }
return codeOfCountries . t o S t r i n g ( ) . trim ( ) + ” ] ” ;
242 }
244 p u b l i c s t a t i c v o i d p r i n t ( Country [ ] c o u n t r i e s ) {
S t r i n g B u i l d e r c o u n t r i e s S t r i n g = new S t r i n g B u i l d e r ( ) ;
246 c o u n t r i e s S t r i n g . append ( ” [ ” ) ;
f o r ( i n t i = 0 ; i < c o u n t r i e s . l e n g t h ; i ++ ) {
248 Country c o u n t r y = c o u n t r i e s [ i ] ;
i f ( c o u n t r y != n u l l ) {
250 c o u n t r i e s S t r i n g . append ( c o u n t r y . t o S t r i n g ( ) ) . append ( ” \n” ) ;
}
252 }
System . out . p r i n t ( c o u n t r i e s S t r i n g . t o S t r i n g ( ) . t r i m ( ) + ” ] ” ) ;
254 }
}
20
HaQT Object-Oriented Programming
9 p u b l i c c l a s s App {
p r i v a t e s t a t i c f i n a l S t r i n g COMMA DELIMITER = ” , ” ;
11 p r i v a t e s t a t i c f i n a l CountryArrayManager countryManager = new
,→ CountryArrayManager ( ) ;
13 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 ) {
init () ;
15
/∗ TODO: w r i t e code t o t e s t program ∗/
17 }
29 i f ( d a t a L i s t . g e t ( 0 ) . e q u a l s ( ” code ” ) ) {
continue ;
31 }
33 i f ( d a t a L i s t . s i z e ( ) != 6 ) {
continue ;
35 }
37 /∗
∗ TODO: c r e a t e Country and append c o u n t r i e s i n t o
39 ∗ CountryArrayManager h e r e .
∗/
41 }
} c a t c h ( IOException e ) {
43 e . printStackTrace () ;
} finally {
45 try {
i f ( dataReader != n u l l ) {
47 dataReader . c l o s e ( ) ;
}
49 } c a t c h ( IOException e ) {
e . printStackTrace () ;
51 }
}
53 }
55 p u b l i c s t a t i c L i s t <S t r i n g > p a r s e D a t a L i n e T o L i s t ( S t r i n g d a t a L i n e ) {
L i s t <S t r i n g > r e s u l t = new A r r a y L i s t <>() ;
57 i f ( d a t a L i n e != n u l l ) {
S t r i n g [ ] s p l i t D a t a = d a t a L i n e . s p l i t (COMMA DELIMITER) ;
21
HaQT Object-Oriented Programming
59 f o r ( i n t i = 0 ; i < s p l i t D a t a . l e n g t h ; i ++ ) {
r e s u l t . add ( s p l i t D a t a [ i ] ) ;
61 }
}
63
return r e s u l t ;
65 }
67 p u b l i c s t a t i c S t r i n g [ ] parseDataLineToArray ( S t r i n g d a t a L i n e ) {
i f ( dataLine = = n u l l ) {
69 return null ;
}
71
r e t u r n d a t a L i n e . s p l i t (COMMA DELIMITER) ;
73 }
75 public s t a t i c void i n i t ( ) {
S t r i n g f i l e P a t h = ” data / c o u n t r i e s . c s v ” ;
77 readListData ( f i l e P a t h ) ;
}
79
public s t a t i c void testOriginalData ( ) {
81 S t r i n g c o d e s S t r i n g = CountryArrayManager . c o d e O f C o u n t r i e s T o S t r i n g (
,→ countryManager . g e t C o u n t r i e s ( ) ) ;
System . out . p r i n t ( c o d e s S t r i n g ) ;
83 }
22
HaQT Object-Oriented Programming
109 }
23