New in Java (Map and Set) -


can me , check answer

(a) declare private instance variable (attribute) called housemap should hold unsorted map integer keys , string values.

private map< integer, string> housemap = new hashmap<integer, string>(); 

(b) write zero-argument constructor of housecatalogue initializes housemap empty map.

housemap = new hashmap<integer, string>(); 

(c) write instance method called addhouse() housecatalogue class takes no arguments, , returns no value. method should enter 4 entries shown above housemap.

public void addhouse() {     housemap.put(101," adison sas") ;     housemap.put(103," fourseasons") ;     housemap.put(105," hayat regency ");      housemap.put(107," concord al-salam ") ; }        

(d) write instance method called printhouse() housecatalogue class takes integer argument, , return string value. method should print value (house name) of area code equal integer argument , return it. otherwise 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));         }     } } 

or

public static void printhouse( int area) {      for(map.entry<integer,string> entry : housemap.entryset())      {         if (entry.getkey().equals(area))          {             system.out.println("house name is"+ entry.getvalue());                //return entry.getvalue();  //  return               }        } } 

(a) lower case letter private , no new hashmap() needed when declaring. when useing java convensions use camelcase when declaring variasbles (housemap) it's fine.

private map<integer, string> housemap; 

(b) have declared variable housemap not housemap (see (a) camelcase) initializing needs same variable:

housemap = new hashmap<integer, string>(); 

(c) seems fine

(d) hum, don't see point in method, should both print value and return it.. well.. first off public lower case letters again, string big letter (name of class` , implementation:

public string printhouse(int area) {      if (housemap.containskey(area)) {         string name = housemap.get(area);         system.out.println("the house area code " + area +                             " " + name));         return name;     }      return null; } 

Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -