0% found this document useful (0 votes)
111 views5 pages

Assignment - Week 5 (Collections) : 0. Warm-Up

The document provides instructions for several coding exercises involving collections and data structures in Java. It includes definitions for classes like Building, Card, and Person along with examples of how to use collections like HashMap and methods like Collections.sort() to complete tasks like counting words in a string, shuffling a deck of cards, and maintaining a registry of persons. Tests are provided to validate the code works as intended by the specifications.

Uploaded by

Cosmin Grosu
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)
111 views5 pages

Assignment - Week 5 (Collections) : 0. Warm-Up

The document provides instructions for several coding exercises involving collections and data structures in Java. It includes definitions for classes like Building, Card, and Person along with examples of how to use collections like HashMap and methods like Collections.sort() to complete tasks like counting words in a string, shuffling a deck of cards, and maintaining a registry of persons. Tests are provided to validate the code works as intended by the specifications.

Uploaded by

Cosmin Grosu
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/ 5

Assignment - Week 5 (Collections)

0. Warm-up
You have a String representing a text composed of multiple words, separated by space(s).
Fulfill these requirements, also trying to avoid using arrays in your code as much as possible.
(instead of arrays, think about which of the recently learned collections types are the best fit for
each point...)

a) Print the number of ​all​ words (including any duplicates). Hint: may use
String.split(), regexp..
b) Print all words in the initial order they appear in text.
c) Print all words, but sorted alphabetically. Hint: see Collections.sort()

d) Print the number of ​unique​ words.


e) Print all unique words, in the initial order they appear in text.
f) Print all unique words, sorted alphabetically.

g) For each word, count how many times it appears, and then print all unique words
(in their initial order in text) together with the number of times each appears.
h) The same, but now sort the word-count pairs alphabetically by word.
i) Optional:​ print the word-count pairs but now sorted like this: first by count
(descending), then for pairs with same count: alphabetically by word (hint: you
will need to learn more about comparators, and also probably to transform your
Map to some other kind of collection, but still keep the words and their counts as
pairs..)

Example:
- text​: “Once upon a time in a land far far away there lived a great king whose name was
a great mystery”

- word count​: 21
- all (initial order)​: [once, upon, a, time, in, a, land, far, far, away, there, lived, a, great,
king, whose, name, was, a, great, mystery]
- all (sorted)​: [a, a, a, a, away, far, far, great, great, in, king, land, lived, mystery, name,
once, there, time, upon, was, whose]

- unique count​: 16
- unique (initial order)​: [once, upon, a, time, in, land, far, away, there, lived, great, king,
whose, name, was, mystery]
- unique (sorted)​: [a, away, far, great, in, king, land, lived, mystery, name, once, there,
time, upon, was, whose]

- word counts (initial order)​: {once=1, upon=1, a=4, time=1, in=1, land=1, far=2,
away=1, there=1, lived=1, great=2, king=1, whose=1, name=1, was=1, mystery=1}

- word counts (sorted by word)​: {a=4, away=1, far=2, great=2, in=1, king=1, land=1,
lived=1, mystery=1, name=1, once=1, there=1, time=1, upon=1, was=1, whose=1}

- word counts (sorted by count, then word)​: [a=4, far=2, great=2, away=1, in=1,
king=1, land=1, lived=1, mystery=1, name=1, once=1, there=1, time=1, upon=1, was=1,
whose=1]

1. Buildings Registry
a. Create a ​Building​ class representing a building, with the following properties:
● name
● category​ (possible values: residential, office, hospital, religious)
● price
● neighborhood

Tip: you should use an ​enum​ for the values of category field.

b. Create a ​BuildingRegistry​ class, containing these static methods (each receiving a


List<Building> as input parameter, and you need to decide on the return type):

● categoriesCount()​ - ​returns the ​number of categories​ (of the actual buildings,


not all possible categories)
● neighborhoodsList()​ - ​return the list of names of ​unique neighborhoods​, sorted
alphabetically (hint: may use a Set; what implementation can you use to also help
you with sorting?..)

● averagePriceForOneCategory()​ - ​return the average price for building from only


one category (given as a 2nd input parameter to the method)
○ question: what should be the return type for this method?..

● averagePricePerCategory()​ - ​return the ​average price for each building


category​ (for ALL defined categories, even the ones without building)
○ question: what kind of return type should this method have? (as it should
return an average price for each used category, so kind of returning a list
of pairs of 2 values - category and price)
● averagePricePerNeighborhood() - ​return the ​average price for each
neighborhood​.

2. Card Deck
a. Create a ​Card​ class to remember the individual cards. It should have 2 properties:
- number​ (2 - 14)
- suit​ - one of the 4 types: ​diamonds (​♦​), clubs (♣), hearts (​♥​) and spades (♠) (hint:
use an enum for these values)
b. Create a ​CardDeck​ class used for dealing hands.
- Should contain a list of all 52 cards and should remember which ones are dealt
and which ones are available.
- Should contain the following methods:
- List<Card> dealHand(int cards)​: this method returns the specified
number of cards by randomly picking from the list of available cards.
Once picked, a card is marked as “used” (removed from “available” cards)
If there are not enough available cards then return as many as possible,
or an empty list when there are no longer any available cards.
- void shuffle()​: this should mark all cards in the deck as available
(essentially emptying the “used” collection) and re-shuffle all the cards.
- int getAvailableCardCount()
- int getUsedCardCount()

Hints:
- for storing the full list of cards, as well as the lists of available/used cards, you could use
some instances of List
- for shuffling a collection, the ​Collections.shuffle()​ method can be used.

Testing
- Once completed, the CardDeck class should behave like:

CardDeck deck = new CardDeck(); 


System.out.println(deck.dealHand(5)); // <- print 5 cards 3 times 
System.out.println(deck.dealHand(5)); 
System.out.println(deck.dealHand(5)); 
System.out.println(deck.dealHand(50)); // <- only 39 cards should be printed 
System.out.println(deck.dealHand(50)); // <- and empty list should be printed 
deck.shuffle(); 
System.out.println(deck.dealHand(5)); // <- another 5 cards  

- Run all the provided JUnit tests (may also add new tests of your own there)
3. Persons Registry
We want to create a class which can hold the details of some registered persons, and then
allow us to search for them, and get some statistical info about them.

a. Create a class ​Person​ having these properties:


- cnp​ - an integer number (optional: validate that it’s >0)
- name​ - a string (optioan: validate it’s not empty)
- age​ - an integer number (optional: validate that it’s >0)
- hasVoted​ - a boolean, set to true if person has voted in last elections

b. You are given this ​PersonsRegistry​ interface.


Create a class ​PersonsRegistryImpl​ which implements this interface:
- The methods implementations should respect not only the contract imposed by
the interface (method signatures), but also consider the details from method
comments (what to do in special cases, etc)
- The class will need some way to store a list of persons - like a (private) field of
some type of collection (hint: it could be preferable to index the persons by their
CNP, for easy find by cnp and easy check/avoid duplicate cnps, so you could try
using a Map for that..)

public interface P​ersonsRegistry { 


 

//Adds a new person to the registry. If a person with same CNP already exists, 
//it will NOT register this new person (just ignore it, and show an error) 
v
​oid ​register(Person p); 
 

//Finds a person by cnp and removes it from registry. 
//If person is not found, will still work (no errors, and does nothing) 
v
​oid ​unregister(​
int c​np); 
 

//Get the number of currently registered persons. 
i
​nt c
​ount(); 
 

//Get the list of cnp values of all persons. 
S
​et<Integer> cnps(); 
 

//Get the list of unique names of all persons. 
S
​et<String> uniqueNames(); 
 

//Find a person by cnp; returns null if no person found. 
P
​erson findByCnp(​ int ​
cnp); 
 

//Find the persons with a specified name (may be zero, one or more) 
//Comparing person name with specified name should be case insensitive. 
S
​et<Person> findByName(String name); 
 

//Get the average age for all persons (or 0 if it cannot be computed) 
d
​ouble ​
averageAge(); 
 

//Get the percent (value between 0-100) of adults (persons with age>=18) 
//from the number of all persons (or 0 as default if it cannot be computed) 
d
​ouble ​
adultsPercentage(); 
 

//Get the percent (value between 0-100) of adults who voted 
//from the number of all adult persons (age>=18) 
d
​ouble ​
adultsWhoVotedPercentage(); 

Once you have all code completed, run the provided JUnit tests (they should compile and the all
tests should pass)

You might also like