Question: Use a HashMap to create a reusable class for choosing one of the 13 predefined colors in class Color. The names of the colors should
Use a HashMap to create a reusable class for choosing one of the 13 predefined colors in class Color. The names of the colors should be used as keys, and the predefined Color objects should be used as values. Use your new class in an application that allows the user to select a color and draw a shape in that color.

1 // Fig. 16.17: WordTypeCount.java 2 // Program counts the number of occurrences of each word in a String. 3 import java.util.Map; 4 import java.util.HashMap; 5 import java.util.Set; 6 import java.util.TreeSet; 7 import java.util.Scanner; public static void main(String[] args) { 8 9 public class WordTypeCount { 10 II 12 13 14 15 16 } 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 // create HashMap to store String keys and Integer values Map myMap = new HashMap (); createMap (myMap); // create map based on user input displayMap (myMap); // display map content. // create map from user input private static void createMap (Map map) { Scanner scanner = new Scanner(System.in); // create scanner System.out.println("Enter a string: "); // prompt for user input String input = scanner.nextLine(); // tokenize the input String[] tokens input.split(" "); // processing input text. for (String token tokens) { String word token.toLowerCase(); // get lowercase word. // if the map contains the word if (map.containsKey(word)) { // is word in map? int count = map.get(word); // get current count map.put(word, count + 1); // increment count } else { map.put(word, 1); // add new word with a count of 1 to map // display map content private static void displayMap (Map map) { Set keys = map.keySet(); // get keys // sort keys TreeSet sortedKeys = new TreeSet (keys); 35 36 37 38 } 39 } 40 } 41 42 43 44 45 46 47 48 49 50 51 52 53 54 } 55 56 57 58 } 59} System.out.printf("%nMap contains: %nKey\t\tValue%n"); // generate output for each key in map for (String key sortedKeys) { System.out.printf("%-10s % 10s %n", key, map.get(key)); System.out.printf( "%nsize: %d%nisEmpty: %b%n", map.size(), map.isEmpty(); Enter a string: this is a sample sentence with several words this is another sample sentence with several different words Map contains: Key Value a 1 another 1 different 1 is 2 sample 2 sentence 2 several 2 this 2 with 2 words 2 size: 10 isEmpty: false
Step by Step Solution
3.43 Rating (143 Votes )
There are 3 Steps involved in it
Heres a reusable class ColorChooser that uses a HashMap to store the predefined colors from the Color class import javaawtColor import javautilHashMap ... View full answer
Get step-by-step solutions from verified subject matter experts
