Question

April 22, 2019 at 00:23 Submit a java program that utilizes Hash Maps. Write three functions for your program. Pass a String
0 0
Add a comment Improve this question Transcribed image text
Answer #1

//######################### PGM START ###################################

import java.util.HashMap;

public class HashMapImplementation {
   //hash map with integer key and string value
   private static HashMap<Integer,String> colorMap = new HashMap<>();
   private static int key=1;
  
   //function to add color to hash map
   public static void addColor(String c) {
       colorMap.put(key,c);
       System.out.println("Added ("+key+","+c+") to hash map");
       key++;
   }
  
   //function to check if a key is present in hash map
   public static void checkKey(int n) {
       if(colorMap.containsKey(n)) {
           System.out.println("Key "+n+" is present");
       }else {
           System.out.println("Key "+n+" is not present");
       }
   }
   //function to check if a color is present in hash map
   public static void checkColor(String c) {
       if(colorMap.containsValue(c)) {
           System.out.println("Color "+c+" is present");
       }else {
           System.out.println("Color "+c+" is not present");
       }
   }
  
   //main function
   public static void main(String[] args) {
      
       //adding colors to hash map
       addColor("orange");
       addColor("black");
       addColor("white");
       addColor("blue");
      
       //checking a key is in hash map
       checkKey(2);
       checkKey(5);
      
       //checking a color is in hash map
       checkColor("blue");
       checkColor("purple");
   }

}


//##################################### PGM END ################################

OUTPUT
#########

Added (1,orange) to hash map Added (2,black) to hash map Added (3,white) to hash map Added (4,blue) to hash map Key 2 is pres

Add a comment
Know the answer?
Add Answer to:
April 22, 2019 at 00:23 Submit a java program that utilizes Hash Maps. Write three functions...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This should be in Java Create a simple hash table You should use an array for...

    This should be in Java Create a simple hash table You should use an array for the hash table, start with a size of 5 and when you get to 80% capacity double the size of your array each time. You should create a class to hold the data, which will be a key, value pair You should use an integer for you key, and a String for your value. For this lab assignment, we will keep it simple Use...

  • Java using data structures The objective is to create your own Hash Table class to hold...

    Java using data structures The objective is to create your own Hash Table class to hold a list of employees and their ID numbers. I've provided the TableEntry class which will be each data object in the hash table. The list of employees will be provided as a .txt file and must be read with the code. please create a .txt file called Employees.txt with the info provided so that the java code can read it in. Employees.txt: (No WhiteSpace...

  • How to write the insert, search, and remove functions for this hash table program? I'm stuck......

    How to write the insert, search, and remove functions for this hash table program? I'm stuck... This program is written in C++ Hash Tables Hash Table Header File Copy and paste the following code into a header file named HashTable.h Please do not alter this file in any way or you may not receive credit for this lab For this lab, you will implement each of the hash table functions whose prototypes are in HashTable.h. Write these functions in a...

  • please use Java!! We are storing the inventory for our fruit stand in a hash table....

    please use Java!! We are storing the inventory for our fruit stand in a hash table. The attached file shows all the possible key/fruit combinations that can be inserted into your hash table. These are the real price lookup values for the corresponding fruit. Problem Description In this programming assignment, you will implement a hash table class fruitHash. Your hash table must include the following methods: 1) hashFunction(key) This method takes a key as an argument and returns the address...

  • Objectives: Use strings and string library functions. Write a program that asks the user to enter...

    Objectives: Use strings and string library functions. Write a program that asks the user to enter a string and output the string in all uppercase letters. The program should then display the number of white space characters in the string. You program should run continuously until the user enters an empty string. The program must use the following two functions: A function called count_spaces that counts the number of white spaces inside a string. int count_space(char str[]); which tell you...

  • Write a program in C++ that places a dog into a pen, which can hold up...

    Write a program in C++ that places a dog into a pen, which can hold up to four dogs, each time a key (i.e. Enter ) is pressed. Each Dog is an object instantiated from class Dog defined in file Dog.h. The Pen is an object instantiated from class Pen defined in file Pen.h. Each Dog object, when instantiated, randomly chooses a fur color (i.e.” black”, “brown” or “white”), an eye color (“brown”, “black”, or “blue”), breed (Labrador, German Shepherd,...

  • USING C++ PROGRAM Instructions - Part C Write a program that has three functions: All characters...

    USING C++ PROGRAM Instructions - Part C Write a program that has three functions: All characters printed should be in White ► Call the first function from main().Print "I'm in main" (as shown in example) > Print "I'm in function 1" in the first function. Call the second function. ► Print "I'm in function 2” in the second function and then ask the user to enter a float value. Enter 25 Call a third function and pass the value to...

  • A java program for this question please! Recursion: A word is considered elfish if it contains...

    A java program for this question please! Recursion: A word is considered elfish if it contains the letters: e, l, and f in it, in any order. For example, we would say that the following words are elfish: whiteleaf, tasteful, unfriendly, and waffles, because they each contain those letters. Write a recursive method called elfish(), that, given a word, tells us whether or not that word is elfish. The signature of the method should be: public static boolean elfish(String word)...

  • PLease IN C not in C++ or JAVA, Lab3. Write a computer program in C which...

    PLease IN C not in C++ or JAVA, Lab3. Write a computer program in C which will simulate a calculator. Your calculator needs to support the five basic operations (addition, subtraction, multiplication, division and modulus) plus primality testing (natural number is prime if it has no non-trivial divisors). : Rewrite your lab 3 calculator program using functions. Each mathematical operation in the menu should be represented by a separate function. In addition to all operations your calculator had to support...

  • For this assignment, you will use your knowledge of arrays and ArrayLists to write a Java...

    For this assignment, you will use your knowledge of arrays and ArrayLists to write a Java program that will input a file of sentences and output a report showing the tokens and shingles (defined below) for each sentence. Templates are provided below for implementing the program as two separate files: a test driver class containing the main() method, and a sentence utilities class that computes the tokens and shingles, and reports their values. The test driver template already implements accepting...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT