gudii9
asked on
topping2 java challenge issue
Hi,
my above first code fails below test
http://codingbat.com/prob/p196458
public Map<String, String> topping2(Map<String, String> map) {
if(map.get("ice cream")!=null){
map.put("yogurt", map.get("ice cream"));
}
if(map.get("spinach")!=null){
map.put("nuts", map.get("spinach"));
}
else{
map=map;
}
return map;
}
how above code different from below codepublic Map<String, String> topping2(Map<String, String> map) {
//public Map < String, String > topping1(Map < String, String > map) {
// map.put("spinach", "nuts");
if (map.containsKey("spinach")) {
map.put("spinach", "nuts");
}
if (map.containsKey("ice cream")) {
map.put("yogurt", map.get("ice cream"));
// map.put("ice cream", map.get("ice cream"));
}
return map;
}
my above first code fails below test
http://codingbat.com/prob/p196458
Expected Run
topping2({"ice cream": "cherry"}) → {"yogurt": "cherry", "ice cream": "cherry"} {"yogurt": "cherry", "ice cream": "cherry"} OK
topping2({"spinach": "dirt", "ice cream": "cherry"}) → {"yogurt": "cherry", "spinach": "nuts", "ice cream": "cherry"} {"yogurt": "cherry", "spinach": "dirt", "ice cream": "cherry", "nuts": "dirt"} X
topping2({"yogurt": "salt"}) → {"yogurt": "salt"} {"yogurt": "salt"} OK
topping2({"yogurt": "salt", "bread": "butter"}) → {"yogurt": "salt", "bread": "butter"} {"yogurt": "salt", "bread": "butter"} OK
topping2({}) → {} {} OK
topping2({"ice cream": "air", "salad": "oil"}) → {"yogurt": "air", "ice cream": "air", "salad": "oil"} {"yogurt": "air", "ice cream": "air", "salad": "oil"} OK
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
also when else loop is mandatory and when it is optional?
ASKER
If the key "spinach" has a value, change that value to "nuts".what is meaning of above?
if(map.get("spinach")!=null){
map.put("nuts", map.get("spinach"));
}
how above different from belowif(map.get("spinach")!=null){
map.put("spinach", "nuts");
there are multiple ways of doing same thign right how to decide best way?Personally, I would choose the way that is more readable. If I do, then, when I come back to it in the future, I will quickly know what the code is doing.
In this case,
if (map.containsKey("spinach"))
is more readable than
if(map.get("ice cream")!=null)
because I have to remember that Map's get method returns null when the key is not there. also when else loop is mandatory and when it is optional?In this case, it doesn't make sense to use it. The challenge gave us a map and requested that if this then do that and if another thing then do something. That is exactly what our code should do.
if(map.get("spinach")!=null){
map.put("nuts", map.get("spinach"));
}
That would result in a Map entry of {"nuts": "whatever-the-original-valThe challenge told us to change the value not the key.
ASKER
If the key "spinach" has a value, change that value to "nuts".
what is meaning of above?
if(map.get("spinach")!=nul l){
map.put("nuts", map.get("spinach"));
}
how above different from below
if(map.get("spinach")!=nul l){
map.put("spinach", "nuts");
what is meaning of above?
if(map.get("spinach")!=nul
map.put("nuts", map.get("spinach"));
}
how above different from below
if(map.get("spinach")!=nul
map.put("spinach", "nuts");
ASKER
if(map.get("spinach")!=nul l){
map.put("nuts", map.get("spinach"));
}
Select all
Open in new window
That would result in a Map entry of {"nuts": "whatever-the-original-val ue-was"}
The challenge told us to change the value not the key.
as per map api
Java Map API
// Make a new empty map
Map<String, String> map = new HashMap<String, String>();
map.get(key) -- retrieves the stored value for a key, or null if that key is not present in the map.
map.put(key, value) -- stores a new key/value pair in the map. Overwrites any existing value for that key.
map.containsKey(key) -- returns true if the key is in the map, false otherwise.
map.remove(key) -- removes the key/value pair for this key if present. Does nothing if the key is not present.
i thought i was changing value only not there key?
That is the reasone i wrote like
if(map.get("spinach")!=nul l){
map.put("nuts", map.get("spinach"));
}
Select all
Open in new window
That would result in a Map entry of {"nuts": "whatever-the-original-val ue-was"}
not able to visualize what below line doing?
map.put("spinach", "nuts");
map.put("nuts", map.get("spinach"));
}
Select all
Open in new window
That would result in a Map entry of {"nuts": "whatever-the-original-val
The challenge told us to change the value not the key.
as per map api
Java Map API
// Make a new empty map
Map<String, String> map = new HashMap<String, String>();
map.get(key) -- retrieves the stored value for a key, or null if that key is not present in the map.
map.put(key, value) -- stores a new key/value pair in the map. Overwrites any existing value for that key.
map.containsKey(key) -- returns true if the key is in the map, false otherwise.
map.remove(key) -- removes the key/value pair for this key if present. Does nothing if the key is not present.
i thought i was changing value only not there key?
That is the reasone i wrote like
if(map.get("spinach")!=nul
map.put("nuts", map.get("spinach"));
}
Select all
Open in new window
That would result in a Map entry of {"nuts": "whatever-the-original-val
not able to visualize what below line doing?
map.put("spinach", "nuts");
ASKER
If the key "spinach" has a value, change that value to "nuts".when challenge says "that value" does the author mean that as key or value ?
ASKER
i think i got it now.
if key spinach has any value like xyz
change that xyz to nuts
so that spinach key is with value nuts
what happens if spinach key do not have any value?
if key spinach has any value like xyz
change that xyz to nuts
so that spinach key is with value nuts
what happens if spinach key do not have any value?
In the API, we have map.put(key, value)
The first parameter is the key.
If you use
then you will have a key that is a String with the value of "nuts". That is not what they asked us to do. They wanted us to keep the "spinach" key and change it's value.
The first parameter is the key.
If you use
map.put("nuts", map.get("spinach"));
then you will have a key that is a String with the value of "nuts". That is not what they asked us to do. They wanted us to keep the "spinach" key and change it's value.
what happens if spinach key do not have any value?If the map does not have a spinach key then do nothing.
ASKER
topping2({"ice cream": " "})
what happens as above if "ice cream" key has value which is " " or "" or null
please advise
especially in the map.containsKy() approach not map.get approach like
public Map<String, String> topping2(Map<String, String> map) {
//if(map.get("ice cream")!=null){
if (map.containsKey("ice cream")) {
map.put("yogurt", map.get("ice cream"));
}
//if(map.get("spinach")!=n ull){
if (map.containsKey("spinach" )) {
map.put("spinach", "nuts");
}
return map;
}
what happens as above if "ice cream" key has value which is " " or "" or null
please advise
especially in the map.containsKy() approach not map.get approach like
public Map<String, String> topping2(Map<String, String> map) {
//if(map.get("ice cream")!=null){
if (map.containsKey("ice cream")) {
map.put("yogurt", map.get("ice cream"));
}
//if(map.get("spinach")!=n
if (map.containsKey("spinach"
map.put("spinach", "nuts");
}
return map;
}
I believe that I answered your last questions over at
https://www.experts-exchange.com/questions/29046831/topping3-challenge.html?anchorAnswerId=42232744#a42232744
https://www.experts-exchange.com/questions/29046831/topping3-challenge.html?anchorAnswerId=42232744#a42232744
And, don't forget, you can always go back and re-read the answer to this question the last time you asked it :
https://www.experts-exchange.com/questions/28973402/topping2-challenge.html
https://www.experts-exchange.com/questions/28973402/topping2-challenge.html
ASKER
is above approach better or below approach
if (map.containsKey("spinach"
there are multiple ways of doing same thign right how to decide best way?
please advise