0% found this document useful (0 votes)
2 views23 pages

OOP2025 JavaExercises Lab8 TheJavaCollectionsFramework

The document provides guidance on writing good Java programs, emphasizing the importance of coding style, documentation, and the need for maintainable code. It includes exercises on lists and sets, with various functions to implement, such as inserting elements, removing duplicates, and finding intersections. The document encourages hands-on programming practice and adherence to Java conventions for better code quality.

Uploaded by

phungnhathung9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views23 pages

OOP2025 JavaExercises Lab8 TheJavaCollectionsFramework

The document provides guidance on writing good Java programs, emphasizing the importance of coding style, documentation, and the need for maintainable code. It includes exercises on lists and sets, with various functions to implement, such as inserting elements, removing duplicates, and finding intersections. The document encourages hands-on programming practice and adherence to Java conventions for better code quality.

Uploaded by

phungnhathung9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

HaQT Object-Oriented Programming

Lab 8. The Java Collections Framework

Writing Good Programs


The only way to learn programming is program, program and program. Learning
programming is like learning cycling, swimming or any other sports. You can’t learn
by watching or reading books. Start to program immediately. On the other hands, to
improve your programming, you need to read many books and study how the masters
program.
It is easy to write programs that work. It is much harder to write programs that not
only work but also easy to maintain and understood by others – I call these good
programs. In the real world, writing program is not meaningful. You have to write
good programs, so that others can understand and maintain your programs.
Pay particular attention to:

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.

2. Program Documentation: Comment! Comment! and more Comment to


explain your code to other people and to yourself three days later.

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

1 package hus . oop . c o l l e c t i o n s . l i s t ;

3 import j a v a . u t i l . ∗ ;

5 public class Lists {

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

package hus . oop . c o l l e c t i o n s . s e t ;


2
import j a v a . u t i l . ∗ ;
4

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

48 ∗ Function t o remove d u p l i c a t e s from a l i s t


∗ 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 !
50 ∗/
p u b l i c s t a t i c L i s t <I n t e g e r > r e m o v e D u p l i c a t e s ( L i s t <I n t e g e r > s o u r c e ) {
52 /∗ TODO ∗/
}
54
/∗ ∗
56 ∗ Function t o remove d u p l i c a t e s from a l i s t
∗ w i t h o u t u s i n g t h e c o n s t r u c t o r s t r i c k s e e n above
58 ∗/
p u b l i c s t a t i c L i s t <I n t e g e r > removeDuplicatesManual ( L i s t <I n t e g e r > s o u r c e
,→ ) {
60 /∗ TODO ∗/
}
62
/∗ ∗
64 ∗ Function a c c e p t i n g a s t r i n g s
∗ r e t u r n i n g the f i r s t r e c u r r i n g c h a r a c t e r
66 ∗ For example f i r s t R e c u r r i n g C h a r a c t e r ( ” abaco ” ) −> a .
∗/
68 public s t a t i c String firstRecurringCharacter ( String s ) {
/∗ TODO ∗/
70 }

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

package hus . oop . c o l l e c t i o n s . map ;


2

import java . util . Collection ;


4 import java . util . HashMap ;
import java . util . Map ;
6 import java . util . Set ;

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 Exercise on Comparable vs Comparator

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 }

package hus . oop . comparable ;


2

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

8 l i s t . add ( new Movie ( ” Empire S t r i k e s Back” , 8 . 8 , 1 9 8 0 ) ) ;


l i s t . add ( new Movie ( ” Return o f t h e J e d i ” , 8 . 4 , 1 9 8 3 ) ) ;
10
Collections . sort ( l i s t ) ;
12
System . out . p r i n t l n ( ” Movies a f t e r s o r t i n g : ” ) ;
14 f o r ( Movie movie : l i s t ) {
System . out . p r i n t l n ( movie . getName ( ) + ” ” +
16 movie . g e t R a t i n g ( ) + ” ” +
movie . getYear ( ) ) ;
18 }
}
20 }

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.

To compare movies by Rating, we need to do 3 things:

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

package hus . oop . comparator ;


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> {

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 }

package hus . oop . comparator ;


2
/∗ ∗
4 ∗ C l a s s t o compare Movies by name
∗/
6 c l a s s NameCompare implements Comparator<Movie> {
p u b l i c i n t compare ( Movie l e f t , Movie r i g h t ) {
8 /∗ TODO ∗/
}
10 }

package hus . oop . comparator ;


2
/∗ ∗
4 ∗ C l a s s t o compare Movies by r a t i n g s
∗/
6 c l a s s RatingCompare implements Comparator<Movie> {

10
HaQT Object-Oriented Programming

p u b l i c i n t compare ( Movie l e f t , Movie r i g h t ) {


8 /∗ TODO ∗/
}
10 }

package hus . oop . comparator ;


2
c l a s s ComparatorTest {
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 l i s t . add ( new Movie ( ” Empire S t r i k e s Back” , 8 . 8 , 1 9 8 0 ) ) ;
l i s t . add ( new Movie ( ” Return o f t h e J e d i ” , 8 . 4 , 1 9 8 3 ) ) ;
10
// S o r t by r a t i n g : ( 1 ) C r e a t e an o b j e c t o f ratingCompare
12 // (2) Call Collections . sort
// (3) Print Sorted l i s t
14 System . out . p r i n t l n ( ” S o r t e d by r a t i n g ” ) ;
RatingCompare ratingCompare = new RatingCompare ( ) ;
16 C o l l e c t i o n s . s o r t ( l i s t , ratingCompare ) ;
f o r ( Movie movie : l i s t ) {
18 System . out . p r i n t l n ( movie . g e t R a t i n g ( ) + ” ” +
movie . getName ( ) + ” ” +
20 movie . getYear ( ) ) ;
}
22
// C a l l o v e r l o a d e d s o r t method with RatingCompare
24 // ( Same t h r e e s t e p s a s above )
System . out . p r i n t l n ( ”\ n S o r t e d by name” ) ;
26 NameCompare nameCompare = new NameCompare ( ) ;
C o l l e c t i o n s . s o r t ( l i s t , nameCompare ) ;
28 f o r ( Movie movie : l i s t ) {
System . out . p r i n t l n ( movie . getName ( ) + ” ” +
30 movie . g e t R a t i n g ( ) + ” ” +
movie . getYear ( ) ) ;
32 }

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

4.3 Country Manager

Write code for an application designed as shown in the following class diagram.

12
HaQT Object-Oriented Programming

1 package hus . oop . co un t r y ar r a y m a n a g e r ;

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 ;

7 p u b l i c Country ( S t r i n g code , S t r i n g name ) {


t h i s . code = code ;
9 t h i s . name = name ;
}
11
p u b l i c S t r i n g getCode ( ) {
13 r e t u r n code ;
}
15
p u b l i c v o i d setCode ( S t r i n g code ) {
17 t h i s . code = code ;
}
19

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

1 package hus . oop . co un t r y ar r a y m a n a g e r ;

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 }

19 public int getPopulation () {


return population ;
21 }

23 public void setPopulation ( i n t population ) {


this . population = population ;
25 }

27 p u b l i c d o u b l e getArea ( ) {
return area ;
29 }

31 p u b l i c void setArea ( double area ) {


t h i s . area = area ;
33 }

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 }
}

package hus . oop . co un t r y ar r a y m a n a g e r ;


2

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 }

package hus . oop . co un t r y ar r a y m a n a g e r ;


2

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 }

package hus . oop . co un t r y ar r a y m a n a g e r ;


2

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 }

44 private void allocateMore ( ) {


Country [ ] newArray = new Country [ 2 ∗ t h i s . c o u n t r i e s . l e n g t h ] ;
46 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 . c o u n t r i e s .
,→ l e n g t h ) ;
t h i s . c o u n t r i e s = newArray ;
48 }

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

60 i f ( ( index < 0) | | ( index > t h i s . c o u n t r i e s . length ) ) {


return f a l s e ;
62 }

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 }

68 f o r ( i n t i = t h i s . length ; i > index ; i - - ) {


t h i s . countries [ i ] = t h i s . countries [ i −1];
70 }

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 }

86 this . countries [ this . length − 1] = null ;


this . length - - ;
88 return true ;
}
90
p u b l i c Country countryAt ( i n t i n d e x ) {
92 i f ( ( i n d e x < 0 ) | | ( i n d e x >= t h i s . l e n g t h ) ) {
return null ;
94 }

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 ) ;

108 /∗ TODO: s o r t newArray ∗/

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 ) ;

122 /∗ TODO: s o r t newArray ∗/

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 ) ;

136 /∗ TODO: s o r t newArray ∗/

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 ) ;

150 /∗ TODO: s o r t newArray ∗/

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

164 /∗ TODO: s o r t newArray ∗/

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 ) ;

178 /∗ TODO: s o r t newArray ∗/

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 }
}

1 package hus . oop . co un t r y ar r a y m a n a g e r ;

3 import java . i o . BufferedReader ;


import java . io . FileReader ;
5 import java . i o . IOException ;
import java . util . List ;
7 import java . u t i l . ArrayList ;

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 }

19 public s t a t i c void readListData ( String f i l e P a t h ) {


B u f f e r e d R e a d e r dataReader = n u l l ;
21 try {
dataReader = new B u f f e r e d R e a d e r ( new F i l e R e a d e r ( f i l e P a t h ) ) ;
23
// Read f i l e i n j a v a l i n e by l i n e .
25 String line ;
w h i l e ( ( l i n e = dataReader . r e a d L i n e ( ) ) != n u l l ) {
27 L i s t <S t r i n g > d a t a L i s t = p a r s e D a t a L i n e T o L i s t ( l i n e ) ;

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 }

85 public s t a t i c void testSortIncreasingByPopulation () {


Country [ ] c o u n t r i e s = countryManager . 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 ( ) ;
87 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 (
,→ 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 ) ;
89 }

91 public s t a t i c void testSortDecreasingByPopulation ( ) {


/∗ TODO ∗/
93 }

95 public s t a t i c void testSortIncreasingByArea ( ) {


/∗ TODO ∗/
97 }

99 public s t a t i c void testSortDecreasingByArea () {


/∗ TODO ∗/
101 }

103 public s t a t i c void testSortIncreasingByGdp () {


/∗ TODO ∗/
105 }

107 public s t a t i c void testSortDecreasingByGdp ( ) {


/∗ TODO ∗/

22
HaQT Object-Oriented Programming

109 }

111 public s t a t i c void t e s t F i l t e r A f r i c a C o u n t r y ( ) {


/∗ TODO ∗/
113 }

115 public s t a t i c void testFilterAsiaCountry ( ) {


/∗ TODO ∗/
117 }

119 public s t a t i c void testFilterEuropeCountry () {


/∗ TODO ∗/
121 }

123 public s t a t i c void testFilterNorthAmericaCountry ( ) {


/∗ TODO ∗/
125 }

127 public s t a t i c void testFilterOceaniaCountry () {


/∗ TODO ∗/
129 }

131 public s t a t i c void testFilterSouthAmericaCountry ( ) {


/∗ TODO ∗/
133 }

135 public s t a t i c void testFilterMostPopulousCountries ( ) {


/∗ TODO ∗/
137 }

139 public s t a t i c void testFilterLeastPopulousCountries () {


/∗ TODO ∗/
141 }

143 public s t a t i c void t e s t 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 ( ) {


/∗ TODO ∗/
145 }

147 public s t a t i c void t e s t 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 ( ) {


/∗ TODO ∗/
149 }

151 public s t a t i c void testFilterHighestGdpCountries () {


/∗ TODO ∗/
153 }

155 public s t a t i c void testFilterLowestGdpCountries ( ) {


/∗ TODO ∗/
157 }
}

23

You might also like