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
  • Write a Java program that is able to apply a hash function to a first and...

    Write a Java program that is able to apply a hash function to a first and last name (key) of a person, and lookup the appropriate index in a hash table modeling a telephone directory. The telephone number (value) should be returned on doing a lookup (you can pre-populate the table with 20 entries for this purpose). You can use existing Java code (or libraries) to model the hash table, however, your program should be customized to use the hash...

  • 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...

  • PROGRAM DESCRIPTION Implement a hash table class. Your hash table should resolve collisions by chaining and...

    PROGRAM DESCRIPTION Implement a hash table class. Your hash table should resolve collisions by chaining and use the multiplication method to generate hash keys. You may use your own linked list code from a previous assignment or you can use a standard sequence container. Partial definitions for the hash table class and a generic data class are provided (C++ and Java versions). You may use them as a starting point if you wish. If you choose to design your own...

  • 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...

  • Programming Assignment Objective Write a Java program that utilizes multiple classes. Write a Java program that...

    Programming Assignment Objective Write a Java program that utilizes multiple classes. Write a Java program that utilizes inheritance in a practical manner. Problem: Quiz Bowl Your high school quiz bowl team has been losing its edge and needs to find a method to improve. Knowing that you are a savvy programmer, your coach asks you to write a program that the team members can use to hone their skills. Quiz Bowl questions come in three varieties: True/False Multiple Choice (variable...

  • 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...

  • 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,...

  • 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...

  • 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...

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