Problem: Some time back, we had implemented code in an exercise \'RetreiveWorldC
ID: 3568959 • Letter: P
Question
Problem:
Some time back, we had implemented code in an exercise 'RetreiveWorldCapitals', to create a Map to hold countries and capitals. In this exercise, you have to add type (generic) information to the Map while creating it. In the previous exercise, you added generic information to a List, in this exercise you will add it to a Map. All the logic has already been implemented, you simply have to add type information to the Map when it is being created. If you do this properly, the code will compile and run as expected.
What we expect:
We have populated the the variable 'worldCapitals', with the names of a few countries and capitals. We have data for the following countries: Barbados - Bridgetown Costa Rica - San Jose Iran - Tehran Djibouti - Djibouti Fiji - Suva Estonia - Tallinn Georgia - Tbilisi When the program is run, it will ask the user to enter the name of a country. If the country entered is 'Australia', we expect 'Canberra' to be printed to the system console. However, if the input is 'Mexico', we expect 'not found' to be printed, since Mexico is not in the data.
-------------------------------------------------------------------------------------------------------------------------------------------------------
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class GenericRetrieveWorldCapitals {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter the name of a country (from one of the following): ");
System.out.println("Barbados, Costa Rica, Iran, Djibouti, Fiji, Estonia, Georgia : ");
String country = scanner.nextLine();
///{ start code here
///}
worldCapitals.put("Barbados", "Bridgetown");
worldCapitals.put("Costa Rica", "San Jose");
worldCapitals.put("Iran", "Tehran");
worldCapitals.put("Djibouti", "Djibouti");
worldCapitals.put("Fiji", "Suva");
worldCapitals.put("Estonia", "Tallinn");
worldCapitals.put("Georgia", "Tbilisi");
String capital = (String)worldCapitals.get(country);
if(capital != null) {
System.out.println(capital);
} else {
System.out.println("not found");
}
}
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Complete the missing piece of code in the exercise to print the class of the type of object using the showType() method as show below.
--------------------------------------------------------------------------------------------------------
class Gen<T> {
T ob;
Gen (T o){
ob = o;
}
T getob(){
return ob;
}
void showType() {
//{Write your code here
//}
}
}
class BasicGeneric {
public static void main(String args[]){
Gen<Integer> iob;
iob = new Gen<Integer>(88);
iob.showType();
int v = iob.getob();
System.out.println("value - " + v);
System.out.println();
Gen<String> strob = new Gen<String> ("Generics Test");
strob.showType();
String str = strob.getob();
System.out.println("value - " + str);
}
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Problem:
Some time back in the 'Slapstick' exercise, you wrote code to append the contents of one list to another. In this exercise, we will examine a better way to wotk with collections in Java. In Java, a List can hold any type of object. This was initially considered a (good) feature of Java collections. However, very soon, many programmers wanted to be able to impose constraints on a List, so it holds only one type of Object. Java released a feature called generics in JDK 1.5, which allowed programmers to provide extra type information to classes, while declaring them. This type information could be used by those classes to impose constraints (among other things). In this exercise, you have to add type information to List and ArrayList, so they are constrained to only hold String objects. The logic of the exercise has already been implemented. You have to complete the code so it compiles. If the code compiled properly, it will pretty do what it is supposed to do.
What we expect:
If the line entered when the program is run is: "The brown fox is really lazy" We expect the following line to be printed: "The brown fox is really lazy and so be it"
---------------------------------------------------------------------------------------------------------------
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
public class GenericSlapstick {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter a sentence: ");
String sentence = scanner.nextLine();
String wordsArr[] = sentence.split(" ");
String slapStickArr[] = {"and", "so", "be", "it"};
///{
//write your code here
//start
//end
///}
for(String word : wordsArr) {
words.add(word);
}
words.addAll(slapstick);
// print the list
StringBuffer buff = new StringBuffer();
Iterator iter = words.iterator();
while(iter.hasNext()) {
buff.append((String)iter.next());
buff.append(" ");
}
System.out.println(buff.toString());
}
}
Explanation / Answer
Here you go :)
Comment if you have any doubts.
//GenericRetrieveWorldCapitals class
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class GenericRetrieveWorldCapitals {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out
.println("Please enter the name of a country (from one of the following): ");
System.out
.println("Barbados, Costa Rica, Iran, Djibouti, Fiji, Estonia, Georgia : ");
String country = scanner.nextLine();
Map<String, String> worldCapitals = new HashMap<String, String>();
worldCapitals.put("Barbados", "Bridgetown");
worldCapitals.put("Costa Rica", "San Jose");
worldCapitals.put("Iran", "Tehran");
worldCapitals.put("Djibouti", "Djibouti");
worldCapitals.put("Fiji", "Suva");
worldCapitals.put("Estonia", "Tallinn");
worldCapitals.put("Georgia", "Tbilisi");
String capital = (String) worldCapitals.get(country);
if (capital != null) {
System.out.println(capital);
} else {
System.out.println("not found");
}
}
}
//#################################################################################
//BasicGeneric class
class Gen<T> {
T ob;
Gen(T o) {
ob = o;
}
T getob() {
return ob;
}
void showType()
{
this.getClass();
}
}
public class BasicGeneric {
public static void main(String args[]) {
Gen<Integer> iob;
iob = new Gen<Integer>(88);
iob.showType();
int v = iob.getob();
System.out.println("value - " + v);
System.out.println();
Gen<String> strob = new Gen<String>("Generics Test");
strob.showType();
String str = strob.getob();
System.out.println("value - " + str);
}
}
//###########################################################################
//GenericSlapstick class
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
public class GenericSlapstick {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter a sentence: ");
String sentence = scanner.nextLine();
String wordsArr[] = sentence.split(" ");
String slapStickArr[] = { "and", "so", "be", "it" };
List<String> words=new ArrayList<String>();
List<String> slapstick=new ArrayList<String>();
for (String word : slapStickArr) {
slapstick.add(word);
}
for (String word : wordsArr) {
words.add(word);
}
words.addAll(slapstick);
// print the list
StringBuffer buff = new StringBuffer();
Iterator iter = words.iterator();
while (iter.hasNext()) {
buff.append((String) iter.next());
buff.append(" ");
}
System.out.println(buff.toString());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.