a.Nothing is wrong with the program
b.Adding similar key values to a Hashmap results in an exception
c.The constructor method PMs() is wrong
d.The size() method is wrong
import java.util.*;
public class PMs {
private Map m = new HashMap();
public void PMs() {
m.put("Nehru", "Jawaharlal");
m.put("Nanda", "Gulzarilal");
m.put("Shastri", "Lal Bahadur");
m.put("Gandhi", "Indira");
m.put("Desai", "Morarji");
m.put("Singh", "Charan");
m.put("Gandhi", "Rajiv");
m.put("Singh", "Vishwanath Pratap");
m.put("Shekhar", "Chandra");
m.put("Rao", "Narasimha");
m.put("Vajpayee", "Atal Behari");
m.put("Gowda", "Deve");
m.put("Gujaral", "Inder Kumar");
m.put("Singh", "Manmohan");
}
public int size() { return m.size(); }
public static void main(String args[]) {
PMs primeministers = new PMs();
System.out.println("Prime ministers with different last name = " + primeministers.size());
}
}
What's wrong with the following program (if any)?
Hi program will compile
o/p
Prime ministers with different last name = 0
i find your logic wrong at
public int size() { return m.size(); }
public static void main(String args[]) {
PMs primeministers = new PMs();
System.out.println("Prime ministers with different last name = " + primeministers.size());
}
instead of doing this you can use something like
import java.util.*;
import java.util.HashMap;
public class PMs {
public static void main(String args[]) {
Map m = new HashMap();
m.put("Nehru", "Jawaharlal");
m.put("Nanda", "Gulzarilal");
m.put("Shastri", "Lal Bahadur");
m.put("Gandhi", "Indira");
m.put("Desai", "Morarji");
m.put("Singh", "Charan");
m.put("Gandhi", "Rajiv");
m.put("Singh", "Vishwanath Pratap");
m.put("Shekhar", "Chandra");
m.put("Rao", "Narasimha");
m.put("Vajpayee", "Atal Behari");
m.put("Gowda", "Deve");
m.put("Gujaral", "Inder Kumar");
m.put("Singh", "Manmohan");
Iterator it = m.keySet().iterator();
while (it.hasNext()) {
System.out.println("Prime ministers with different last name =" + it.next());
}
}
}
hope this helps
Cheers:)
Reply:why dont you learn java and answer it yourself
Reply:all this greek to me
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment