Using Java to create the following classes below. Create a class called Country
ID: 3850738 • Letter: U
Question
Using Java to create the following classes below.
Create a class called Country that has a two-dimensional array with the following data:
private String[][] provinces = new String[10][2]();
provinces[0][0]="ab";
provinces[1][0]="bc";
provinces[2][0]="mb";
provinces[3][0]="nb";
provinces[4][0]="nl";
provinces[5][0]="ns";
provinces[6][0]="on";
provinces[7][0]="pe";
provinces[8][0]="qc";
provinces[9][0]="sk";
provinces[0][1]="alberta";
provinces[1][1]="british columbia";
provinces[2][1]="manitoba";
provinces[3][1]="new brunswick";
provinces[4][1]="newfoundland";
provinces[5][1]="nova scotia";
provinces[6][1]="ontario";
provinces[7][1]="prince edward island";
provinces[8][1]="quebec";
provinces[9][1]="saskatchewan";
Create a method called createHashMap() that loops through the provinces array and creates a HashMap whose keys are the province abbreviations and whose values are the province names.
Create another method, called showAllMappings(), that uses an EntrySet Map to display all of the keys and values in the following format:
“The abbreviation ab is for the province of Alberta” (note: your method must uppercase the first letter and lowercase the rest of the province name).
Create a third method, called showAllMappings2(), that uses a Set and an Iterator to display all of the keys and values in the following format:
“The province Alberta has an abbreviation of ab” (note: your method must uppercase the first letter and lowercase the rest of the province name).
Create a fourth method, with the signature of public boolean doesAnyProvinceContain(String substring) that returns true if and only if any province contains the substring.
Important: all of your methods must check for null values wherever a null could be a problem.
And Obviously a Main class(tester) if it need one.
Explanation / Answer
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class Country {
private String[][] provinces = new String[10][2];
Map<String, String> map = new HashMap<String, String>();
public Country() {
// TODO Auto-generated constructor stub
provinces[0][0] = "ab";
provinces[1][0] = "bc";
provinces[2][0] = "mb";
provinces[3][0] = "nb";
provinces[4][0] = "nl";
provinces[5][0] = "ns";
provinces[6][0] = "on";
provinces[7][0] = "pe";
provinces[8][0] = "qc";
provinces[9][0] = "sk";
provinces[0][1] = "alberta";
provinces[1][1] = "british columbia";
provinces[2][1] = "manitoba";
provinces[3][1] = "new brunswick";
provinces[4][1] = "newfoundland";
provinces[5][1] = "nova scotia";
provinces[6][1] = "ontario";
provinces[7][1] = "prince edward island";
provinces[8][1] = "quebec";
provinces[9][1] = "saskatchewan";
}
/**
* @return
*/
public void createHashMap() {
for (int i = 0; i < provinces.length; i++) {
map.put(provinces[i][0], provinces[i][1]);
}
}
/**
* @param map
*/
public void showAllMappings() {
Iterator<?> entries = map.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry entry = (Map.Entry) entries.next();
String key = (String) entry.getKey();
String value = (String) entry.getValue();
System.out.println("The abbreviation " + key
+ " is for the province of "
+ value.substring(0, 1).toUpperCase() + value.substring(1));
}
}
/**
*
* @param map
*/
public void showAllMappings2() {
Set<String> keySet = map.keySet();
Iterator<String> iterator = keySet.iterator();
while (iterator.hasNext()) {
String key = (String) iterator.next();
String value = (String) map.get(key);
System.out.println("The province "
+ value.substring(0, 1).toUpperCase() + value.substring(1)
+ " has an abbreviation of " + key);
}
}
/**
* returns true if and only if any province contains the substring.
*
* @param substring
* @return
*/
public boolean doesAnyProvinceContain(String substring) {
if (map.get(substring) != null)
return true;
else
return false;
}
}
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
Country country = new Country();
country.createHashMap();
System.out.println("calling showAllMappings:");
country.showAllMappings();
System.out.println(" calling showAllMappings2:");
country.showAllMappings2();
System.out.println("contains provinence ab:"
+ country.doesAnyProvinceContain("ab"));
System.out.println("contains provinence ab2:"
+ country.doesAnyProvinceContain("ab2"));
}
}
OUTPUT:
calling showAllMappings:
The abbreviation sk is for the province of Saskatchewan
The abbreviation pe is for the province of Prince edward island
The abbreviation ns is for the province of Nova scotia
The abbreviation nb is for the province of New brunswick
The abbreviation mb is for the province of Manitoba
The abbreviation on is for the province of Ontario
The abbreviation ab is for the province of Alberta
The abbreviation bc is for the province of British columbia
The abbreviation nl is for the province of Newfoundland
The abbreviation qc is for the province of Quebec
calling showAllMappings2:
The province Saskatchewan has an abbreviation of sk
The province Prince edward island has an abbreviation of pe
The province Nova scotia has an abbreviation of ns
The province New brunswick has an abbreviation of nb
The province Manitoba has an abbreviation of mb
The province Ontario has an abbreviation of on
The province Alberta has an abbreviation of ab
The province British columbia has an abbreviation of bc
The province Newfoundland has an abbreviation of nl
The province Quebec has an abbreviation of qc
contains provinence ab:true
contains provinence ab2:false
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.