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 following financial information is for Frizell Company. Additional information: 1. Inventory at the beginning of 2013 was $115,000. 2. Accounts receivable (net) at the beginning of 2013 were...
-
Inland Corporation issued 30,000 shares of \(\$ 5\) par value common stock at \(\$ 15\) per share and 8,000 shares of \(\$ 50\) par value, eight percent preferred stock at \(\$ 85\) per share. Later,...
-
How does a decrease in U.S. interest rates affect the EU/U.S. exchange rate?
-
(Related to Checkpoint 12.1) (Calculating project cash flows and NPV) You are considering expanding your product line that currently consists of skateboards to include gas-powered skateboards, and...
-
The primary purpose of the reformer is to convert methane and water to carbon monoxide and hydrogen (Equation 13.1). The extent of this reaction is limited by chemical equilibrium. where Subscript...
-
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...
-
The expected average rate of return for a proposed investment of $600,000 in a fixed asset, with a useful life of four years, straight-line depreciation, no residual value, and an expected total net...
-
The soft goods department of a large department store sells 175 units per month of a certain large bath towel. The unit cost of a towel to the store is $2.50 and the cost of placing an order has been...
-
John and Mike work in the same department. Mike is a new employee. John has worked at the company for a long time. John and Mike do the same job. They have the same job title. They have to work...
-
Jeffery Wei received a 7-year non-subsidized student loan of $33,000 at an annual interest rate of 5.2%. What are Jeffery's monthly loan payments for this loan after he graduates in 4 years? (Round...
-
Caliente Corp, who reports monthly, received $ 1 2 0 , 0 0 0 in advance for six months of services to be provided on 7 / 1 / 2 3 . What general journal entry should be recorded at 7 / 1 / 2 3 and...
-
Jones has a loss on discontinued operations of $ 2 5 0 K ( after tax ) . If the tax rate is 3 5 % , what amount should Jones report this discontinued item at on its income statement?
-
Identifying product versus general, selling, and administrative costs Required Indicate whether each of the following costs should be classified as a product cost or as a selling, general, and...
-
What steps must a business take to implement a program of social responsibility?
-
When dealing with investment property, the ____________ is of extreme importance to the investor because that is the amount of capital gain or loss that will be reported to the IRS for tax purposes....
-
Hello, I am looking for some guidance on 2 related questions ( b & c ) for a Corporate Finance course. Please see the questions in attachment with my attempt to answer them. However I don't feel...
-
You expect to receive $6,000 at graduation in two years. You plan on investing it at 8 percent until you have $91,000. How long will you wait from now? (Do not round your intermediate calculations.)...
Study smarter with the SolutionInn App