Hi all,
I am currently revising for an exam which will cover all the basics of object-oriented programming, it was going quite well until I met Collection :) I have got two questions to which I have started writing answers to but got stuck...Could someone please have a look at what I have done so far and maybe provide me with a guideline how I can finish the code?
QUESTION 1 (implementing Map):
The following table shows model numbers and names of fountain pens.
Model number Model name
707 "Antares"
1001 "Bootes"
911 "Rigel"
1032 "Vega"
Such tables are implemented and managed by instances of a class called
PenCatalogue.
(i) The class PenCatalogue needs a single private instance variable called
penMap. Write the declaration for penMap, it should be declared to be of a
suitable interface type to hold an unsorted map with integer keys and string
values. [3]
MY ANSWER:
private Map<Integer, String> penMap;
(ii) Write a zero-argument constructor for PenCatalogue that initialises penMap
to an empty map. [3]
public PenCatalogue()
{
penMap = new HashMap<Integer, String>();
}
iii)
Write an instance method called fillUp() for the PenCatalogue class that
takes no arguments and returns no value. This method should simply enter
the five entries shown above into the map.
public void fillUp()
{
penMap.put<707, "Antares">;
penMap.put<1001, "Bootes">;
penMap.put<911, "Rigel">;
penMap.put<1032, "Vega">;
}
iv)Write a second instance method for the PenCatalogue class called
updateCatalogue() which takes no arguments and returns no value. This
method should work irrespective of how many entries might subsequently be
added to the map referenced by penMap. This method should iterate through
all of the map’s keys. Where the integer key has a value less than 1000, then
" – withdrawn" should be added to the associated map value (as illustrated
below).
Now this is where I struggle..I think I need to use the for loop somewhere here but I haven't found a similar question in our course books so I am not quite sure how to proceed.
Any help will be highly appreciated :) I am going trough all kinds of questions just in case they might come up in the exam so I might be having more questions :D
Many many thanks in advance
Magda