Write a program that uses a HashMap to compute a histogram of positive numbers entered by the user. The HashMap’s key should be the number that is entered, and the value should be a counter of the number of times the key has been entered so far. Use -1 as a sentinel value to signal the end of user input. For example, if the user inputs:

5
12
3
5
5
3
21
-1
Then the program should output the following (not necessarily in this order):
The number 3 occurs 2 times.
The number 5 occurs 3 times.
The number 12 occurs 1 times.
The number 21 occurs 1 times.

The solution is a fairly straightforward implementation of a HashMap to map a number to a counter. It may be worth mentioning that equals and hashcode must be overwritten if the student wants to use HashMap with their own set. Note that Java will automatically unbox/box between an int and Integer. This solution takes advantage of this feature.


See the code in HashMapHistogram.java.

Computer Science & Information Technology

You might also like to view...

In File Explorer, the left navigation pane provides a way to move quickly to various locations where files may be stored. Describe the file storage locations available by default in the left navigation pane.

What will be an ideal response?

Computer Science & Information Technology

This is sometimes known as separation of duties, is a practice of separating tasks and assigning them to different responsible groups or employees, therefore limiting full control of services or information and eliminating

What will be an ideal response?

Computer Science & Information Technology

If you want the entire column of a table to display at a specific width, do you need to apply a custom class to the entire column?

What will be an ideal response?

Computer Science & Information Technology

Which of the following correctly uses the setInterval() method to call a function named count-down() every two seconds?

a. ``` var interval = window.setInterval("countdown()", 2); ``` b. ``` window.setInterval(2000) = countdown(); ``` c. ``` var interval = window.setInterval("countdown()", 2000); ``` d. ``` window.setInterval("countdown()", 2000); ```

Computer Science & Information Technology