الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

ضرووووري ...qustion For Useing A Hashmap To Do Acountry Map

بدأه A33 في 31 مايو 2009 · 2 رد · 610 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكــم ورحمـة الله وبركاتــه ،،

أشكر كل من ساعدني في إكتشاف خطأي في موظوعي السابق

Telephone Directory

.... وأنا إن شاء الله سأدرج الحل الصحيح في وقت قريب ...ليستفيد منه الجميع وليتعلموا أيضا من أخطائنا ...

.... ولحسن الحظ .... قرر دكتور المادة اليوم أن يعطينا فرصة أخرى بأن نحل مسائل جديدة و تتضمن نفس الفكرة ..

لهذا أنا سأدرج هنا الأسئلة .... وأريد من يساعدني فيها ... أنا سأحاول حلها ... وأرجوا أنتم أيضا أن تحاولوا معي ...

للتبيه تسليمه غدا .....

إليكم الأسئلة ....

Q1.

Write a Java class called CountryMap that will be used to keep information about the countries and their capitals in a HashMap. So, the country name is the key, and the capital is the value associated to the key.

An example of an entry of the HashMap is ("UAE", "Abu Dhabi").

This class should implement the following methods:

Public void addCountry (String country, String capital)

Public String lookupCapital(String country)

Public void removeCountry(String country)

Public void removeAllCountries()

//the following method checks if the map contains a specific key

Public boolean containsCountry(String country)

//the following method checks if the map contains a specific value

Public boolean containsCountry(String capital)

Note:

§ Remember to import java.util.HashMap

§ Some of the above methods require using some methods of the class HashMap. Find appropriate methods from the HashMap documentation.

Q2.

Write a Java class called CountryTester to test the CountryMap class defined in question1.

In the main method of this class, you have to implement the following tasks:

§ Instantiate an object of the CoutryMap class

§ Add the following entries to the map:

- ("UAE", "Abu Dhabi")

- ("Qatar", "Doha")

- ("Bahrain", "Manama")

- ("Oman", "Maskat")

- ("Saudi Arabia", "Riyad")

§ Find out in the map the capital of "Bahrain"

§ Find out in the map the capital of "Egypt"

§ Remove the entry of "Qatar" from the map.

§ Add the following entries to the map:

- ("Jordan", "Aman")

- ("Morocco", "Rabat")

§ Find out in the map if there is any entry for which the capital is "Manama".

§ The method keySet() of the HashMap returns, when it is invoked, a set containing all keys of the map. Use this method together with the iterator of the set to print out all the countries of the map.

§ Remove all entries from the CountryMap object.

وجزاكم جميعا كل الخير ....

تم تعديل هذه المشاركة بواسطة A33 في 31 مايو 2009 في 21:52

#2

السلام عليكــم ورحمـة الله وبركاتــه ،،

أخي الكريم هناك فرق بين طلب الحل و طلب المساعدة، وانت تريد حلا جاهزا، وهذا لن يفيدك بشيء



public class CountryMap {
//create the HashMap
HashMap<String, String> map = new HashMap<String, String>();

public void addCountry (String country, String capital) {
//add the country and the capital to the HashMap
map.put(country, capital);
}
public String lookupCapital(String country) {
//return the capital of the specified country
return map.get(country);
}
public void removeCountry(String country) {
//remove the specified country an its capital from the HashMap
map.remove(country);
}
public void removeAllCountries() {
//clear the HashMap
map.clear();
}
public boolean containsCountry(String country) {
//the countries are the keys of this HashMap, so we check the keys
return map.containsKey(country);
}
public boolean containsCapital(String capital) {
return map.containsValue(capital);
}
public void printCountries() {
Iterator it = map.keySet().iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
}
   import java.util.*;


public static void main(String[] args) {
CountryMap cm = new CountryMap();
cm.addCountry("UAE", "Abu Dhabi");
cm.addCountry("Qatar", "Doha");
cm.addCountry("Bahrain", "Manama");
cm.addCountry("Oman", "Maskat");
cm.addCountry("Saudi Arabia", "Riyad");
System.out.println("The capital of Bahrain is: " + cm.lookupCapital("Bahrain"));
System.out.println("The capital of Egypt is: " + cm.lookupCapital("Egypt"));
System.out.println("The capital of Qatar is: " + cm.lookupCapital("Qatar"));
cm.removeCountry("Qatar");
cm.addCountry("Jordan", "Aman");
cm.addCountry("Morocco", "Rabat");
System.out.println(cm.containsCapital("Manama"));
cm.printCountries();
cm.removeAllCountries();
}
}
   public class CountryTester {

#3

جزاك الله خيــــــــــــــــــــــــرا أخي .... herch>>>>>>

.... أنا أيضا أتممت محاولتي لحله .... ولكن كالعادة .... يوجد خطأ ما ....

جزيت خيرا لحلك للمسأله ... استفدت كثيرا منه ....

((((((((((( أنا طلبت المساعدة .... ولكن في نفس الوقت كنت أحاول في حله ...أعرف كلامك لمصلحتنا ... لهذا جزيت أخي الكريم الجنة ان شاء الله ..... ))))))))

شكرا جزيلا ....

......

مواضيع مشابهة