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
Fantastic news! We've Found the answer you've been seeking!
Step by Step Answer:
Answer rating: 100% (1 review)
Heres a reusable class ColorChooser that uses a HashMap to store the predefined colors from the Color class import javaawtColor import javautilHashMap ...View the full answer
Answered By
Aysha Ali
my name is ayesha ali. i have done my matriculation in science topics with a+ . then i got admission in the field of computer science and technology in punjab college, lahore. i have passed my final examination of college with a+ also. after that, i got admission in the biggest university of pakistan which is university of the punjab. i am studying business and information technology in my university. i always stand first in my class. i am very brilliant client. my experts always appreciate my work. my projects are very popular in my university because i always complete my work with extreme devotion. i have a great knowledge about all major science topics. science topics always remain my favorite topics. i am also a home expert. i teach many clients at my home ranging from pre-school level to university level. my clients always show excellent result. i am expert in writing essays, reports, speeches, researches and all type of projects. i also have a vast knowledge about business, marketing, cost accounting and finance. i am also expert in making presentations on powerpoint and microsoft word. if you need any sort of help in any topic, please dont hesitate to consult with me. i will provide you the best work at a very reasonable price. i am quality oriented and i have 5 year experience in the following field.
matriculation in science topics; inter in computer science; bachelors in business and information technology
_embed src=http://www.clocklink.com/clocks/0018-orange.swf?timezone=usa_albany& width=200 height=200 wmode=transparent type=application/x-shockwave-flash_
4.40+
11+ Reviews
14+ Question Solved
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 National Collegiate Athletic Association (NCAA) continues to penalize college sports teams because their athletes receive cars, cash, clothing, and other gifts from sports agents. NCAA rules...
-
Demand is relatively constant and is known. LO.1
-
A thin metallic wire of thermal conductivity k, diameter D, and length 2L is annealed by passing an electrical current through the wire to induce a uniform volumetric heat generation q. The ambient...
-
In reference to Coca Cola Company: Prepare a reasonable objective for each of the four strategic areas of a balanced scorecard, including proposed key performance indicators for determining target...
-
In a single process production system, Honest Company produces Product Z.For October, 2021, the company accounting records reflect the following: Beginning Work In Process (50% complete as to...
-
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...
-
What practices in the Eco-Seagate help team members to become more cohesive?
-
Consider a two-period model in which an individual needs to decide how much to consume in the present, c, and how much to consume in the future, c. The individual receives income, y, in the present,...
-
Koopman Company began operations on January 1, 2015, and uses the FIFO inventory method for financial reporting and the average cost inventory method for income taxes. At the beginning of 2017,...
-
Assume you have been given $400,000 CAD with access to all listed stocks, bonds, futures, and options worldwide. You can trade in options and futures, in combination with the underlying asset....
-
1.Basis of Islamic Accounting Theory The history of the birth of Islamic accounting is inseparable from the development of Islam, the obligation to record non-cash transactions (see QS. Al-Baqarah:...
-
What kind of information can you provide from the label on the door frame of a car? A scene of a car from drivers side a. factory recommended mileage a year b. factory recommended tire pressure c....
-
Discuss the idea of drawing separate demand curves for different market segments. It seems logical because each target market should have its own marketing mix. But wont this lead to many demand...
-
Find the cross product a x b and verify that it is orthogonal to both a and b. a = (t, 1, 1/t), b = (t 2 , t 2 , 1)
-
A stock is expected to pay a dividend of $1.50 at the end of the year (i.e., D 1 = $1.50), and it should continue to grow at a constant rate of 10% a year. If its required return is 14%, what is the...
-
The Hobby Shop has a checking account with a ledger balance of $1,700. The firm has $2,400 in uncollected deposits and $4,200 in outstanding checks. What is the amount of the disbursement float on...
-
An investment will pay you $34,000 in 11 years. If the appropriate discount rate is 6.1 percent compounded daily, what is the present value? (Use 365 days a year. Do not round intermediate...
Study smarter with the SolutionInn App