You are required to implement a Guess the capitals using Java. You must design y
ID: 3679733 • Letter: Y
Question
You are required to implement a Guess the capitals using Java. You must design your game using OOP concept. Guess the capitals is a quiz game where the player will be repeatedly prompted with randomly selected states and the player is required to guess the capital of each state. Upon receiving the answers from the player, the program reports the result of the quiz. Each quiz consist of 10 randomly questions selected from the questions bank. The player will be prompted with 1 question at a time.
General Requirements: a. You are required to build a Map class GuessCapitalMap using an ArrayList that only accept String data type as values. This class should at least support the following methods:
• // constructor that will populate the map with 30 state/capital pairs // that are stored in a text file
• // adds or replaces (if key exists) an association void put(T1 key, T2 val)
• // returns value given the key T2 get(T1 k)
• // prints the details of all associations in the map void print()
• // check for empty map boolean isEmpty()
• // remove the value given the key, return true if the key is found, return false otherwise boolean remove(T1 k)
The Map class should allow any two classes to be associated. You may assume that the class used as Key implements Comparable, allowing comparison of two keys.
b. You are required to use the class from part (a) to develop your game. You may add more behavior to the class in part (a).
c. Catch all possible Exception.
Explanation / Answer
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class GuessCapitalMap {
private ArrayList<String> states;
private ArrayList<String> capital;
public GuessCapitalMap() {
try {
BufferedReader fileReader = new BufferedReader(new FileReader(
new File("capitals.txt")));
states = new ArrayList<>();
capital = new ArrayList<>();
String line = null;
while ((line = fileReader.readLine()) != null) {
String[] data = line.split("\ ");
states.add(data[0]);
capital.add(data[1]);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public boolean isEmpty() {
return states.isEmpty();
}
public void put(String key, String value) {
boolean found = false;
for (int i = 0; i < states.size(); i++) {
if (key.equals(states.get(i))) {
capital.set(i, value);
found = true;
break;
}
}
if (!found) {
states.add(key);
capital.add(value);
}
}
public String get(String key) {
String value = null;
for (int i = 0; i < states.size(); i++) {
if (key.equals(states.get(i))) {
value = capital.get(i);
break;
}
}
return value;
}
public boolean remove(String key) {
boolean found = false;
for (int i = 0; i < states.size(); i++) {
if (key.equals(states.get(i))) {
capital.remove(i);
states.remove(i);
found = true;
break;
}
}
return found;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.