Joined: Tue Nov 30, 2010 9:46 am Posts: 1 Has thanked: 0 time Have thanks: 0 time
I had some problem in Map in Java I don't understand it very well
I try to solve some activity but I don't sure is correct if any body can help me to check it to me
Thanks
The following table shows model area code and names of five star Houses in that area.
Area Code House names 141 “adison Sas” 142 “FourSeasons” 145 “life Regency” 147 “Sartoon”
This table is implemented by instances of a class called HouseCatalogue.
(a) Declare a private instance variable (Attribute) called HouseMap which should hold an unsorted map with integer keys and string values. Private Map< Integer, String> HouseMap = new HashMap<Integer, String>(); (b) Write a zero-argument constructor of HouseCatalogue that initializes HouseMap to an empty map. HouseMap.isEmpty(); (c) Write an instance method called addHouse() for the HouseCatalogue class that takes no arguments, and returns no value. This method should simply enter the four entries shown above into the HouseMap. Public void addHouse() { HouseMap.put(141," Radison Sas") ; HouseMap.put(142," ForSeason ") ; HouseMap.put(145," Hayat Regency "); HouseMap.put(147," Concord al-Salam ") ; }
** an instance method called printHouse() for the HouseCatalogue class that takes an integer as argument and return a string value. This method should print the value (House name) of the area code that is equal to integer argument and return it. Otherwise it will return null. Public string printHouse( int area) { for(Integer eachcode : HouseMap.keySet()) { if ( HouseMap.keySet()== area)
{ System.out.println("House name is"+ HouseMap.get(eachcode)); } } }