Question

Implement message passing using hash table or dictionary that maps constant strings to function pointers in...

Implement message passing using hash table or dictionary that maps constant strings to function pointers in java or c++ or python programming language.

Provide well documented working code.

Hint:Assign message names to constant strings and implement constant as keys that maps to function pointers.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

#include <iostream>
#include <functional>
#include <map>

using namespace std;

//MESSAGE DECLARATION
const string BUTTON_CLICk = "Button_Clicked";
const string MOUSE_CLICk = "MButton_Clicked";
const string KEY_CLICk = "KButton_Clicked";
const string MOUSE_RMB_CLICk = "MRMB_Clicked";

//Message Handlers
void onButtonCLicked()
{
   cout << "\nMouse Button Clicked";
}

void onMButtonCLicked()
{
   cout << "\nMouse MButton Clicked";

}
void onKButtonCLicked()
{
   cout << "\nKey Button Clicked";

}

void onMouseRMBCLicked()
{
   cout << "\nMouse RMB Button Clicked";

}


//Process Message function
void processMessage(std::map<string, std::function<void()>>& aMap,const string command)
{
   //Invocation of the respective Handler function
   aMap[command]();
}


int main()
{
   //Assigning the map with the message and corresponding handlers
   std::map<string, std::function<void()>> mapofMessageHandlers;

   mapofMessageHandlers[BUTTON_CLICk] = onButtonCLicked;
   mapofMessageHandlers[MOUSE_CLICk] = onMButtonCLicked;
   mapofMessageHandlers[KEY_CLICk] = onKButtonCLicked;
   mapofMessageHandlers[MOUSE_RMB_CLICk] = onMouseRMBCLicked;

   processMessage(mapofMessageHandlers,MOUSE_CLICk);
}

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
Implement message passing using hash table or dictionary that maps constant strings to function pointers in...
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
  • 0. Introduction. This involves designing a perfect hash function for a small set of strings. It...

    0. Introduction. This involves designing a perfect hash function for a small set of strings. It demonstrates that if the set of possible keys is small, then a perfect hash function need not be hard to design, or hard to understand. 1. Theory. A hash table is an array that associates keys with values. A hash function takes a key as its argument, and returns an index in the array. The object that appears at the index is the key’s...

  • (in Java) We wish to insert the following strings into a hash table: BEN AL SUE...

    (in Java) We wish to insert the following strings into a hash table: BEN AL SUE CHUCK KAREN MIKE SAM MIKE ALICE TARA TIM BOB Let's make the hash table size p=19 ( 19 is a prime ). Use letter encodings where A maps to 1, B maps to 2, etc and strings map to powers of 26. For example, "ABC" would map to 1*(26^2) + 2*(26^1)+ 3*(26^0) mod 19 (you might find the web site Wolfram Alpha helpful. Just...

  • Language: Python Topic: Dictionaries Function name: catch_flight Parameters: dictionary, tuple containing two strings Returns: dictionary Description:...

    Language: Python Topic: Dictionaries Function name: catch_flight Parameters: dictionary, tuple containing two strings Returns: dictionary Description: You’ve been stuck in NYC for around 8 months all by yourself because you haven’t been able to find a good time to fly home. You’re willing to go to any city but want to see how many flights to each location fit your budget. You’re given a dictionary that has city names (strings) as the keys and a list of prices (list) and...

  • In C++ and not Java: Implement a spelling checker by using a hash table. Assume that...

    In C++ and not Java: Implement a spelling checker by using a hash table. Assume that the dictionary comes from two sources: an existing large dictionary and a second file containing a personal dictionary. Output all misspelled words and the line numbers on which they occur. Also, for each misspelled word, list any words in the dictionary that are obtainable by applying any of the following rules: 1. Add one character. 2. Remove one character. 3. Exchange adjacent characters The...

  • Implement an eager delete() method for linear probing hash table. The eager delete will delete the...

    Implement an eager delete() method for linear probing hash table. The eager delete will delete the pair from the hash table, not just set the value to null. *Algorithhms* In Java Language Please I want to add the delete method to this hash table code. public class Linear ProbingHashST<Key, Value> private int M = 30001; private Value [] vals = (Value[]) new Object[M]: private Key [] keys = (Key []) new Object[M]: array doubling and halving code omitted private int...

  • The task of this project is to implement in Java Hash Table structure using Linear Probing...

    The task of this project is to implement in Java Hash Table structure using Linear Probing Collision Strategy.   You can assume that no duplicates or allowed and perform lazy deletion (similar to BST). Specification Create a generic class called HashTableLinearProbe <K,V>, where K is the key and V is the value. It should contain a private static class, HashEntry<K,V>. Use this class to create array to represent Hashtable:             HashEntry<K,V> hashtable[]; Implement all methods listed below and test each method...

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

  • Assume that you have to store strings in the hash table by using the hashing technique...

    Assume that you have to store strings in the hash table by using the hashing technique To compute the index for storing the strings, use a hash function that states the following: The index for a specific string will be equal to the sum of the ASCII values of the characters modulo 599. Write a C++ program to get us input five strings and use the above hash function in order to generate index. Collision may happen, you don’t need...

  • design hash table Suppose you need to design a hash table. The keys themselves are (pointers...

    design hash table Suppose you need to design a hash table. The keys themselves are (pointers to) C-style zero-terminating strings, so the string "foobar" occupies seven bytes. You are interested in minimizing the space used while keeping search time within reasonable bounds. You expect to store 1000 names with (on average) 7 letters each. If, for example, you choose separate chaining with 2000 buckets, the space required will be 24000 bytes: 8000 = (2000 buckets times 4 bytes per bucket...

  • python Function Name: find_me Parameters: a dictionary of strings mapping to strings ( dict ), person1...

    python Function Name: find_me Parameters: a dictionary of strings mapping to strings ( dict ), person1 ( str), person2 (str) Returns: number of people in between the first and last person ( int ) Description: Given a dictionary and two names, find the number of people in between them. The dictionary maps a person ( key ) to the person ( value ) in front of them in a line. If person2 is the value of the personi key, then...

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