Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Here is the code I have so far!!! import java.util.*; public class hashfunction { static class Character{ public static int TABLESIZE = 9; private String

image text in transcribedimage text in transcribed

Here is the code I have so far!!!

import java.util.*;

public class hashfunction {

static class Character{

public static int TABLESIZE = 9;

private String firstName;

private String lastName;

private int age;

// Constructor: Do not touch

public Character(String fn, String ln){

firstName = fn;

lastName = ln;

}

// Function key()

// Should return the proper index to place the specific Character

// into the hash table

// Use the first letter of the last name and modulus to find the proper index

public int key(){

// for now, returns the ascii value of first character of last name

int k = lastName.charAt(0);

return k;

}

// the details of the specific Character when you print it. Do not touch

public String toString(){

String str = "Last name: " + lastName;

str += " First name: " + firstName;

str += " Hash Table Key: " + key();

return str;

}

public String getLastName(){

return lastName;

}

public static void main(String[] args){

Character[] hashtable = new Character[TABLESIZE]; // empty hashtable

//use this to test part 2!!! If it works, comment or delete

// Character s1 = new Character("Anakin", "Skywalker");

// System.out.println(s1);

//insert other code here!!!

index = hash % array_size

while(true) {

Scanner in = new Scanner(System.in);

System.out.print("Last name: ");

String ln = in.next();

System.out.print("First name: ");

String fn = in.next();

Lab Introduction: You may choose Java or Python to complete the lab. Be sure to check BeachBoard for the resources that you need such as the skeleton code and a video. Instructions: In the skeleton note the following: The TABLESIZE is initialized for you already (9). Do not change it or else your output may not match the expected output There is an object called Character that has the following O Variables: . (String) firstName . (String) lastName Methods: . (int) key () . For now, it returns the ascii value of the first letter of lastName . A "toString" method so that it prints out the details of the character . String getLastName() returns the last name (getter) A hash table of size 9 is already initialized (but empty) Your task: 1. Change the body of key () so that it does the following: Generates the proper key between 0-8 (because there are 9 spots in the hash table) through a hashing function in terms of the ascii value of the first letter of lastName 2. Before moving on to the main function implementation, look at the following characters. Next to them, write the hash values for the current implementation (bold faced names are the last names). You may want to refer to the Ascii table: A-65 B-66 C-67 D-68 E-69 F-70 G-71 H-721-73 J-74 K-75 L-76 M-77 N-78 0-79 P-80 Q-81 R-82S-83 T-84 U-85 V-86 W-87 X-88 Y-89 Z-90a-97 -98c-99d-100 e-101 F-102 g-103 h-104 1-105 j-106 k-107 1-108 m-109 1-110 0-111 P-112 q-113 -114 S-115t-116 u-117 V-118 W-119 X-120 y-121 Z-122 Anakin Skywalker: Obi Wan Kenobi: General Grievous: Sheev Palpatine: r2 d2: Leia Organa: Ahsoka Tano: Sheev palpatine: Boba Fett: Mace Windu: Asajj Ventress: General Hux: ****Test your code with some of the names above to see if you get the correct key before moving on to step 3 3. Using the Characters above (and in that order), simulate what would happen if you tried to insert them into hash table using their respective keys. In the case there is a collision (when you try to insert a Character into a space that already has a character), then prioritize the first character and ignore the second character. Index 0 1 2 3 4 5 6 7 8 Character 4. On a loop, ask the user for a first name and a last name. Create a Character object with each set of inputs. Find the key of the Character input to see if it can fit in our hash table. If it does, insert it into the hash table. If it does not, output an error message saying that there's no room for that character. Continue to ask them until they fill out all spots in the hash table. Look at the example below: Last name: Skywalker First name: Anakin Inserted into the hash table, it is Last name: Palpatine First name: Sheev Inserted into the hash table, it is Last name: Grievous First name: General Error: Filled that spot you already have Last name: 5. Once the whole hash table has been filled, prompt the user to ask for a last name. Search through the last name using two methods: brute force and hashing. Time the two methods and print out the index they were found in and the amount of time it took to find them Finished Filling Hash Table! Last name: Organa Using brute force Index: 7 Time: 222ms Using hashing Index: 7 Time: 4ms 6. Demo and submit. NOTE: The sequence that I gave to you in the lab was so that you can test. This may or may not be the actual sequence that I test during the demo

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Neo4j Data Modeling

Authors: Steve Hoberman ,David Fauth

1st Edition

1634621913, 978-1634621915

More Books

Students also viewed these Databases questions

Question

1. What do I want to achieve?

Answered: 1 week ago