Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

***YOU MUST USE JAVA***** Chapter 21 Assignment There are several tasks in the a

ID: 3695604 • Letter: #

Question

***YOU MUST USE JAVA*****

Chapter 21 Assignment

There are several tasks in the assignment. Complete them all in the main method of one class. The assignment is about USA presidents. The last 10 presidents are numbered 35 to 44; 35 being Kennedy and 44 being Obama.

Create a map

Create a map object (you choose the map class type) that will have Integer keys and String values. This map will be used to store data for the last 10 presidents.

Write 10 statements (loop not required) to add president data to this map. For example key 35 for value Kennedy, key 36 for value Johnson, etc. NOTE: The data in the map must be correct because this map will be used later for a quiz.

Output the size of the map.

To verify that the map was created properly, process it and display the keys and values (see sample run below). NOTE: For full points, you must use the Map.Entry<K,V)interface to process the map.

Create a quiz

Create, run, and score a 5-question “presidents quiz” as follows:

Make a set that will store Integers. You pick the class type for the set.

Use a while loop to add 5 random numbers to the set. The random numbers must be in the range from 35-44 so we can use them for our map.

Create an iterator that can traverse the set, using the Integers in the set to pose and score the questions (see sample run below).

Use the map to determine if the user’s responses are correct and score the quiz. After all 5 quiz questions, display the user’s score on the quiz.

Display alpha president list

Finally, create another data structure from the map that stores just the president’s names in alphabetical order.

Process this new data structure with a foreach loop to display all 10 presidents in alphabetical order as shown in the sample run.

Sample Run

Size of map: 10
President #35: Kennedy
President #36: Johnson
President #37: Nixon
President #38: Ford
President #39: Carter
President #40: Reagan
President #41: George Bush
President #42: Clinton
President #43: George W Bush
President #44: Obama

Presidents Quiz

Who was president #36?
Johnson
Who was president #37?
Nixon
Who was president #40?
Carter
Who was president #41?
George W Bush
Who was president #44?
Obama

Your score was 3

Here are the last 10 presidents in alphabetical order:
Carter
Clinton
Ford
George Bush
George W Bush
Johnson
Kennedy
Nixon
Obama
Reagan

Explanation / Answer

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; class Test { public static void main(String[] args) throws IOException { Map president = new HashMap(); president.put(35, "Kennedy"); president.put(36, "Johnson"); president.put(37, "Nixon"); president.put(38, "Ford"); president.put(39, "Carter"); president.put(40, "Reagan"); president.put(41, "George Bush"); president.put(42, "Clinton"); president.put(43, "George W Bush"); president.put(44, "Obama"); System.out.println("Size of map: " + president.size()); for (Map.Entry entry : president.entrySet()) { System.out.println(entry.getKey() + " : " + entry.getValue()); } Set set = new HashSet(); int i = 0; while (i < 5) { int r = 35 + ((int) (Math.random() * 100) % 9); set.add(r); i++; } BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int score = 0; Iterator it = set.iterator(); while (it.hasNext()) { int num = (int) it.next(); System.out.println("Who was president #" + num + " ?"); String ans = br.readLine(); if (ans.equalsIgnoreCase(president.get(num))) score++; } System.out.println("Your score was " + score); System.out.println("Here are the last 10 presidents in alphabetical order:"); TreeSet names = new TreeSet(); for (Map.Entry entry : president.entrySet()) { names.add(entry.getValue()); } for(String n : names){ System.out.println(n); } } }