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

Chapter 21 Assignment There are several tasks in the assignment. Complete them a

ID: 3823744 • Letter: C

Question

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 36 to 45; 36 being Johnson and 45 being Trump.

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 36 for value Johnson, key 37 for value Nixon, 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 36-45 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 #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
President #45: Trump

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
Nixon
Obama
Reagan
Trump

Explanation / Answer

//Quiz.java

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;
import java.util.Map.Entry;
import java.util.Set;


public class Quiz {

   public static void main(String[] args) {

       //Map of type K,V as Integer and String
       Map<Integer, String> prd = new LinkedHashMap<Integer, String>();
       //Putting K,V into Map
       prd.put(36, "Johnson");
       prd.put(37, "Nixon");
       prd.put(38, "Ford");
       prd.put(39, "Carter");
       prd.put(40, "Reagan");
       prd.put(41, "George Bush");
       prd.put(42, "Clinton");
       prd.put(43, "George W Bush");
       prd.put(44, "Obama");
       prd.put(45, "Trump");
      
       //Getting Entries from Map using entrySet
       Set<Entry<Integer, String>> set = prd.entrySet();
      
       //Iterating Entries
       Iterator<Entry<Integer, String>> itr = set.iterator();
       System.out.println("Size of Map:"+prd.size());
       System.out.println("****President Map****");
       while(itr.hasNext()){
           //Getting Each Entry
           Entry<Integer,String> entry = itr.next();
           System.out.println("President #"+entry.getKey()+":"+entry.getValue());
       }

       //Creating Random Number
       Random rn = new Random();
       //Creating Set
       HashSet<Integer> hst = new HashSet<Integer>();
       int i=0;
       System.out.println("***President Quiz****");
       //Adding 5 Random numbers into Set
       while(i<5){
           int r = rn.nextInt(45-36)+36;
           hst.add(r);
           i++;
       }
       Scanner ss = new Scanner(System.in);
       String ans;
       int count=0;
       Iterator<Integer> itrr = hst.iterator();
       //Iterating Set for asking questions
       while(itrr.hasNext()){
           int num = itrr.next();
           System.out.println("Who was president #"+num);
           ans = ss.next();
           //Checking answer from the map using map.get(object)
           if(prd.get(num).equalsIgnoreCase(ans)){
               count++;//Score increment if correct
           }
       }
       //Printing Score
       System.out.println("Your Score was:"+count);
      
       //Getting Names of presidents into Collection from Map
       Collection<String> col = prd.values();
       //Adding Names to ArrayList
       ArrayList<String> al= new ArrayList<String>(col);
       //Sorting List of Names
       Collections.sort(al);
      
       Iterator<String> its = al.iterator();
      
       System.out.println("Here are the 10 Presidents in Alphabetical Order:");
       //Iterating and Printing Names from List
       while(its.hasNext()){
           System.out.println(its.next());
       }
   }

}

Output-1:

Size of Map:10
****President Map****
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
President #45:Trump
***President Quiz****
Who was president #36
Johnson
Who was president #37
Nixon
Who was president #38
Carter
Who was president #39
Carter
Your Score was:3
Here are the 10 Presidents in Alphabetical Order:
Carter
Clinton
Ford
George Bush
George W Bush
Johnson
Nixon
Obama
Reagan
Trump

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote