Assignment - Week 5 (Collections) : 0. Warm-Up
Assignment - Week 5 (Collections) : 0. Warm-Up
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()
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.
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:
- 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.
Once you have all code completed, run the provided JUnit tests (they should compile and the all
tests should pass)