Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can someone convert the following C++ code into Java please Thanks! // hash-tabl

ID: 3845006 • Letter: C

Question

 Can someone convert the following C++ code into Java please Thanks! // hash-table.cpp  #include <cstring> #include <iostream> 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; }   

Explanation / Answer

// a person with a name & number
public class Person
{
public Person(String a)
{
      this(a, "");
}
public Person()
{
      this("", "");
}

public Person(String a, String b)
{
   name = a;
   number = b;
}
public String name = new String(new char[100]);
public String number = new String(new char[15]);
}

// the hash table class
public class HashTable
{
   // insert a person
   public final void insert(Person p)
   {
      // hash the person
      int index = hash(p.name);
      System.out.print("Trying to insert ");
      System.out.print(p.name);
      System.out.print(" at ");
      System.out.print(index);
      System.out.print(" ");

      // while that spot is filled, keep on
      while (strcmp(phonebook[index].name, "") != 0)
      {
       index++;
       if (index >= GlobalMembers.MAX_SIZE)
       {
          index = 0;
       }
      }

      // insert them
      System.out.print("Actually inserting ");
      System.out.print(p.name);
      System.out.print(" at ");
      System.out.print(index);
      System.out.print(" ");
      phonebook[index] = p;
   }


   // lookup a person's phone number
   public final void lookup(String 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 >= GlobalMembers.MAX_SIZE)
       {
          index = 0;
       }
      }

      // print the number
      System.out.print(name);
      System.out.print("'s phone number is ");
      System.out.print(phonebook[index].number);
      System.out.print(" (found at ");
      System.out.print(index);
      System.out.print(")");
      System.out.print(" ");
   }


   // hash a string into a number
   public final int hash(String name)
   {
      int value = 0;
      // add up all the ASCII values

      for (int i = 0; i < name.length(); i++)
      {
       value = value + name.charAt(i);
      }
      // mod by size to prevent overflow
      return value % GlobalMembers.MAX_SIZE;
   }

   // the people are all stored in an array
   private Person[] phonebook = tangible.Arrays.initializeWithDefaultPersonInstances(MAX_SIZE);
}

package <missing>;

public class GlobalMembers
{
   // the most things we can store in the table
   public static final int MAX_SIZE = 100;


   // main program
   public static int Main()
   {
      HashTable table = new HashTable();

      table.insert(new Person("Bob", "345-8214"));
      table.insert(new Person("Joe", "555-7374"));
      table.insert(new Person("John", "633-1214"));
      table.insert(new Person("Alice", "234-7234"));
      table.insert(new Person("Gary", "943-7236"));
      table.insert(new Person("Claire", "333-8231"));
      table.insert(new Person("Wendy", "361-3617"));
      table.insert(new Person("Jim", "824-6217"));
      table.insert(new Person("Elena", "347-3829"));
      table.insert(new Person("Elane", "924-0884"));


      table.lookup("John");
      table.lookup("Bob");
      table.lookup("Elena");
      table.lookup("Elane");

      return 0;
   }
}

package tangible;


public final class Arrays
{
   public static Person[] initializeWithDefaultPersonInstances(int length)
   {
       Person[] array = new Person[length];
       for (int i = 0; i < length; i++)
       {
           array[i] = new Person();
       }
       return array;
   }

   public static <T extends java.io.Closeable> void deleteArray(T[] array)
   {
       for (T element : array)
       {
           if (element != null)
               element.close();
       }
   }
}