A geography class is currently studying Europe, and the next test covers the cap
ID: 3700971 • Letter: A
Question
A geography class is currently studying Europe, and the next test covers the capital cities. The test will be fill-in-the-blank questions. Each question will be in one of the following two formats: Format 1: The capital city of is_____. Format 2: The capital city of _____ is . Where placeholders and are provided, and you must fill in the blanks. Here is a copy of your study sheet: Country Capital Country Capital Albania Tirana Liechtenstein Vaduz Andorra Andorra la Vella Lithuania Vilnius Armenia Yerevan Luxembourg Luxembourg Austria Vienna Macedonia Skopje Azerbaijan Baku Malta Valletta Belarus Minsk Moldova Chisinau Belgium Brussels Monaco Monaco Bosnia Herzegovina Sarajevo Montenegro Podgorica Bulgaria Sofia Netherlands Amsterdam Croatia Zagreb Norway Oslo Cyprus Nicosia Poland Warsaw Czech Republic Prague Portugal Lisbon Denmark Copenhagen Romania Bucharest Estonia Tallinn Russia Moscow Finland Helsinki San Marino San Marino France Paris Serbia Belgrade Georgia Tbilisi Slovakia Bratislava Germany Berlin Slovenia Ljubljana Greece Athens Spain Madrid Hungary Budapest Sweden Stockholm Iceland Reykjavik Switzerland Bern Ireland Dublin Turkey Ankara Italy Rome Ukraine Kiev Kazakhstan Astana United Kingdom London Kosovo Pristina Vatican City Vatican City Latvia Riga To skirt the opportunity of genuine learning, you decide to take advantage of your existing abilities and write a Java program to fill in the blanks. Input will be from a data file each in the format given in the description above. Blanks will consist of 5 consecutive underscore characters ('_'). Output will exactly mirror the input except that the blanks will be replaced with the correct country or capital names from the study sheet. All the countries/capitals may or may not be used. Use the Java Map class and other Collection classes as appropriate. Let the user input the file name from the keyboard. with comments.
Explanation / Answer
Here is the required code for you. Everything is explained in detail with comments. Go through it and drop a comment if you have doubts. Defined a couple of methods to perform different tasks, including reading from file, generating random capital name, random country name etc. Please note that the input file format of your data should be same to what I provide. It should be comma separated. It cannot be space delimeted as so many country names have spaces in it, so we cannot split capital and country properly if we use space delimeted file. Thanks
// CountryCapitalGame.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Random;
import java.util.Scanner;
public class CountryCapitalGame {
static HashMap<String, String> countries_and_capitals;
static Scanner scanner;
static Random random;
/**
* method to load countries and capitals from an input file to the hashmap
*
* @param fileName
* - name of the file
* @throws FileNotFoundException
* - if not found
*/
static void loadData(String fileName) throws FileNotFoundException {
Scanner fileScanner = new Scanner(new File(fileName));
while (fileScanner.hasNext()) {
// getting line
String line = fileScanner.nextLine();
/**
* the split method will split a string by given delimeter and
* generate an array, so here 0 and 1 are the array indices, i.e
* every line in input file is consist of country name and capital
* name seperated by a comma. and the trim() method is used to
* remove any trailing/leading white spaces
*/
String country = line.split(",")[0].trim();
String capital = line.split(",")[1].trim();
/**
* Adding to the map
*/
countries_and_capitals.put(country, capital);
}
}
/**
* method to pick a random country name from the map and return it
*/
static String getRandomCountry() {
// getting country names as an array from the hashmap
Object[] countryList = countries_and_capitals.keySet().toArray();
// generating a random index and returning the element at that index
int randomIndex = random.nextInt(countryList.length);
return (String) countryList[randomIndex];
}
/**
* method to pick a random capital name from the map and return it
*/
static String getRandomCapital() {
// getting capital names as an array from the hashmap
Object[] capitalsList = countries_and_capitals.values().toArray();
int randomIndex = random.nextInt(capitalsList.length);
return (String) capitalsList[randomIndex];
}
/**
* method to return a country name, by providing its capital name
*
* @param capital
* - capital of the country to be found
*/
public static String getCountryByCapital(String capital) {
/**
* looping through the keys in hashmap
*/
for (Object country : countries_and_capitals.keySet()) {
// checking if the key returns the target capital
if (countries_and_capitals.get(country).equals(capital)) {
return (String) country;
}
}
return "";
}
public static void main(String[] args) throws FileNotFoundException {
countries_and_capitals = new HashMap<String, String>();
scanner = new Scanner(System.in);
random = new Random();
System.out.println("Enter input file name: ");
String fileName = scanner.nextLine();
loadData(fileName);// loading data from file
int num_questions = 10, score = 0, question_num = 1;
String userInput;
System.out.println("Quiz started...");
/**
* Loop until 10 random questions are asked
*/
while (question_num <= num_questions) {
/**
* determining if the question should be finding country or finding
* capital (randomly)
*/
boolean gameMode = random.nextBoolean();
if (gameMode) {
/**
* prompting the user to find the capital of a given country
*/
String country = getRandomCountry(); /* getting random country*/
System.out.println(question_num + ") The capital city of "
+ country + " is_____");
userInput = scanner.nextLine();
if (countries_and_capitals.get(country).equalsIgnoreCase(
userInput)) {
// right answer
System.out.println("Right answer");
score++;// incrementing score
} else {
System.out.println("Wrong answer");
}
// displaying the answer after question
System.out.println("The capital city of " + country + " is "
+ countries_and_capitals.get(country));
} else {
/**
* prompting the user to find the country name with a given
* capital
*/
String capital = getRandomCapital();// getting random capital
System.out.println(question_num
+ ")The capital city of _____ is " + capital);
userInput = scanner.nextLine();
if (getCountryByCapital(capital).equalsIgnoreCase(userInput)) {
// right answer
System.out.println("Right answer");
score++;// incrementing score
} else {
System.out.println("Wrong answer");
}
// displaying the answer after question
System.out.println("The capital city of "
+ getCountryByCapital(capital) + " is " + capital);
}
question_num++; // incrementing question number
}
/**
* Displaying the final stats
*/
System.out.println("Quiz completed, score: " + score + " out of "
+ num_questions);
}
}
//capitals.txt
Albania,Tirana
Liechtenstein,Vaduz
Andorra,Andorra la Vella
Lithuania,Vilnius
Armenia,Yerevan
Luxembourg,Luxembourg
Austria,Vienna
Macedonia,Skopje
Azerbaijan,Baku
Malta,Valletta
Belarus,Minsk
Moldova,Chisinau
Belgium,Brussels
Monaco,Monaco
Bosnia & Herzegovina,Sarajevo
Montenegro,Podgorica
Bulgaria,Sofia
Netherlands,Amsterdam
Croatia,Zagreb
Norway,Oslo
Cyprus,Nicosia
Poland,Warsaw
Czech Republic,Prague
Portugal,Lisbon
Denmark,Copenhagen
Romania,Bucharest
Estonia,Tallinn
Russia,Moscow
Finland,Helsinki
San Marino,San Marino
France,Paris
Serbia,Belgrade
Georgia,Tbilisi
Slovakia,Bratislava
Germany,Berlin
Slovenia,Ljubljana
Greece,Athens
Spain,Madrid
Hungary,Budapest
Sweden,Stockholm
Iceland,Reykjavik
Switzerland,Bern
Ireland,Dublin
Turkey,Ankara
Italy,Rome
Ukraine,Kiev
Kazakhstan,Astana
United Kingdom,London
Kosovo,Pristina
Vatican City,Vatican City
Latvia,Riga
/*OUTPUT*/
Enter input file name:
capitals.txt
Quiz started...
1)The capital city of _____ is Pristina
kosovo
Right answer
The capital city of Kosovo is Pristina
2)The capital city of _____ is Budapest
hungary
Right answer
The capital city of Hungary is Budapest
3)The capital city of _____ is Bratislava
bratsa
Wrong answer
The capital city of Slovakia is Bratislava
4)The capital city of _____ is Sofia
Sofia
Wrong answer
The capital city of Bulgaria is Sofia
5) The capital city of Montenegro is_____
podgorica
Right answer
The capital city of Montenegro is Podgorica
6) The capital city of Cyprus is_____
Cyind
Wrong answer
The capital city of Cyprus is Nicosia
7)The capital city of _____ is Athens
Greece
Right answer
The capital city of Greece is Athens
8) The capital city of Moldova is_____
Chisinau
Right answer
The capital city of Moldova is Chisinau
9) The capital city of Belgium is_____
Belgium
Wrong answer
The capital city of Belgium is Brussels
10)The capital city of _____ is Monaco
Monaco
Right answer
The capital city of Monaco is Monaco
Quiz completed, score: 6 out of 10
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.