Use a HashMap to create a reusable class for choosing one of the 13 predefined colors in
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 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.
Transcribed Image Text:
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 Answer:
This question has not been answered yet.
You can Ask your question!
Related Book For
Java How To Program Late Objects Version
ISBN: 9780136123712
8th Edition
Authors: Paul Deitel, Deitel & Associates
Question Posted:
Students also viewed these Computer science questions
-
Hello I am working on the following assignment which is as follow **You have been tasked with developing a complete, modular, object-oriented program that will allow for the management of a...
-
Write a project management plan. we have a template and project description. we need to edit the template(table of contents) with our own ideas. CPSC 8820-01 Project Management Plan Your Unique...
-
for the equality test for M and N. The question concerns how to represent multisets of strings within ML. For each of the given data representations (a), (b) and (c) describe how you would implement...
-
Marty owns an auto body repair shop. The business has no real value without him and the building is pretty much a mess after being full of paint fumes for all those years but the land is worth...
-
Par Corporation acquired an 80 percent interest in Sip Corporation for $180,000 cash on January 1, 2016, when Sip had capital stock of $50,000 and retained earnings of $150,000. The excess of fair...
-
The Dorwyn Company has two new products (special kinds of doors and windows) that will compete with the two new products for the Wyndor Glass Co. (described in Section 2.1). Using units of hundreds...
-
Green, or sustainable finance, is an important part of ethical investment. In many jurisdictions, including Australia and New Zealand, green bonds are becoming an important source of funding....
-
Melvin Patel bid for and won a concession to rent bicycles in the local park during the summer. During the month of June, Patel completed the following transactions for his bicycle rental business:...
-
Exercise 1 Motion on a Ramp In this exercise you will roll a marble down a ramp and calculate its linear acceleration and velocity. You will then create graphs to illustrate the motion of the marble.
-
Use SecureRandom method ints to generate a stream of 50 random numbers in the range 1 to 999, then filter the resulting stream elements to select only the odd numbers and display the results in...
-
The Tip Calculator app does not need a Button to perform its calculations. Reimplement this app to use property listeners to perform the calculations whenever the user modifies the bill amount or...
-
Following the 1990 Iraqi invasion of Kuwait, the price of crude oil soared, as did retail gasoline prices. This led the major U. S. oil companies to try to hold down their reported earnings. The oil...
-
This problem is based on a problem described in Concurrent Programming Many amusement parks in the United States have bumper car rides. Each car holds one person. People who want to ride a bumper car...
-
You can read the article "Modeling the Evolution of Operating Systems: An Empirical Study" which highlights the key aspects needed to consider while developing and deploying OS and other relevant...
-
explain the impact that offering credit can have on company profits and liquidity, and illustrate this using simple worked examples
-
Rope Length is 25m Stable is rectangular What is the maximum area can the horse covered outside?
-
1. Wind Turbines, Inc., has the power to control the market for its product. Antitrust law regulates a. how Alpha acquired its power and what it does with it. b. neither how Alpha acquired its power...
-
A 20.0-kg block is connected to a 30.0-kg block by a string that passes over a light frictionless pulley. The 30.0-kg block is connected to a spring that has negligible mass and a force constant of...
-
What is the mode?
-
How can leaders sustain momentum and relevance around visionary visions amidst evolving external environments, internal dynamics, and competing priorities, fostering adaptability, resilience, and...
-
What cognitive processes underlie the formulation of visionary narratives, and how can leaders leverage storytelling techniques to evoke emotional resonance, foster buy-in, and drive transformative...
-
Let U = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, A = {1, 3, 5, 7, 9}, B = {2, 4, 6, 8, 10}, and C = {1, 2, 4, 5, 8, 9}. List the elements of each set. (a) CC c (b) ( A C ) c (c) A ( B C )
Study smarter with the SolutionInn App