Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Can someone convert the following C++ code into Java please Thanks! // hash-table.cpp #include #include using namespace std; // the most things we can store
Can someone convert the following C++ code into Java please Thanks! // hash-table.cpp #include #include using namespace std; // the most things we can store in the table const int MAX_SIZE = 100; // a person with a name & number struct Person { Person(const char* a = "", const char* b = "") { strcpy(name, a); strcpy(number, b); } char name[100]; char number[15]; }; // the hash table class class HashTable { public: // insert a person void insert(Person p) { // hash the person int index = hash(p.name); cout << "Trying to insert " << p.name << " at " << index << endl; // while that spot is filled, keep on while(strcmp(phonebook[index].name, "") != 0) { index++; if(index >= MAX_SIZE) { index = 0; } } // insert them cout << "Actually inserting " << p.name << " at " << index << endl; phonebook[index] = p; } // lookup a person's phone number void lookup(const char* name) { // hash the person int index = hash(name); // while that spot is not the person, keep on while(strcmp(phonebook[index].name, name) != 0) { index++; if(index >= MAX_SIZE) { index = 0; } } // print the number cout << name << "'s phone number is " << phonebook[index].number << " (found at " << index << ")" << endl; } // hash a string into a number int hash(const char* name) { int value = 0; // add up all the ASCII values for(unsigned int i = 0; i < strlen(name); i++) { value = value + name[i]; } // mod by size to prevent overflow return value % MAX_SIZE; } private: // the people are all stored in an array Person phonebook[MAX_SIZE]; }; // main program int main( ) { HashTable table; table.insert(Person("Bob", "345-8214")); table.insert(Person("Joe", "555-7374")); table.insert(Person("John", "633-1214")); table.insert(Person("Alice", "234-7234")); table.insert(Person("Gary", "943-7236")); table.insert(Person("Claire", "333-8231")); table.insert(Person("Wendy", "361-3617")); table.insert(Person("Jim", "824-6217")); table.insert(Person("Elena", "347-3829")); table.insert(Person("Elane", "924-0884")); table.lookup("John"); table.lookup("Bob"); table.lookup("Elena"); table.lookup("Elane"); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started