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

There are several tasks in the assignment. Complete them all in the main method

ID: 3718921 • Letter: T

Question

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 andString 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 the foreach() method and a lambda to process the map. See page 832.

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

Here is the code you asked, with detailed comments explaining every steps of this program. Drop a comment if you have any doubts. Thanks.

// PresidentsQuiz.java

package presidents;

import java.util.ArrayList;

import java.util.Collections;

import java.util.HashMap;

import java.util.HashSet;

import java.util.Iterator;

import java.util.Map.Entry;

import java.util.Random;

import java.util.Scanner;

import java.util.Set;

import java.util.TreeSet;

public class PresidentsQuiz {

    public static void main(String[] args) {

        /**

         * creating a map with integer keys and string values

         */

        HashMap<Integer, String> map = new HashMap<Integer, String>();

        /**

         * Adding last 10 presidents with their numbers

         */

        map.put(36, "Johnson");

        map.put(37, "Nixon");

        map.put(38, "Ford");

        map.put(39, "Carter");

        map.put(40, "Reagan");

        map.put(41, "George Bush");

        map.put(42, "Clinton");

        map.put(43, "George W Bush");

        map.put(44, "Obama");

        map.put(45, "Trump");

        /**

         * Displaying the map size

         */

        System.out.println("Size of map: " + map.size());

        /**

         * Creating an Entry<Integer, String> from the map

         */

        Set<Entry<Integer, String>> entries = map.entrySet();

        /**

         * Looping through each entry using lambda expression, and displaying it

         */

        entries.forEach(entry -> System.out.printf("President #%d: %s ",

                entry.getKey(), entry.getValue()));

        /**

         * Creating a set of integers

         */

        Set<Integer> integerSet = new HashSet<>();

        /**

         * Creating a random object and adding 5 random numbers to the set,

         * within the range 36-45

         */

        Random random = new Random();

        while (integerSet.size()!=5) {

            //generating a value between 36 and 45

            int num = random.nextInt(10) + 36;

            integerSet.add(num);

        }

        //Scanner to read user input

        Scanner scanner = new Scanner(System.in);

        //getting an iterator from the integer set

        Iterator<Integer> iterator = integerSet.iterator();

        int score = 0;

        //looping through each elements

        while (iterator.hasNext()) {

            int num = iterator.next();

            //asking question

            System.out.println("Who was president #" + num + "?");

            //getting answer

            String answer = scanner.nextLine();

            //getting correct answer

            String correctAnswer = map.get(num);

            //checking if user input is correct

            if (answer.equalsIgnoreCase(correctAnswer)) {

                score++;

            }

        }

        //displaying the final score

        System.out.println("Your score was " + score);

        /**

         * Creating an array list of strings, adding all values (president

         * names) of map

         */

        ArrayList<String> listInAlphabeticalOrder = new ArrayList<>();

        listInAlphabeticalOrder.addAll(map.values());

        //sorting in alphabetical order

        Collections.sort(listInAlphabeticalOrder);

        //displaying sorted elements using a foreach loop

        System.out.println("Here are the last 10 presidents in alphabetical order");

        for (String s : listInAlphabeticalOrder) {

            System.out.println(s);

        }

    }

}

/*OUTPUT*/

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

Who was president #37?

Nixon

Who was president #38?

Ford

Who was president #41?

Reagan

Who was president #42?

Clinton

Who was president #45?

Johnson

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