Question

in java, Hash 8 randomly generated int values (in the range [ 0 - 99 ]...

in java, Hash 8 randomly generated int values (in the range [ 0 - 99 ] inclusive).
The random number generator is initially seeded to value 97.

Each generated value is stored in a hash table size 11.

The first hash function, h1(key) is the division modulo sizeof(table).
I.e., h1(key) = key % sizeof(table)
h1(key) = key % 11.

Any collisions will NOT be stored in the hash table. You will ignore collisions.

The hash table is a random access file used to store 1D table of 11 ints.

The output:

After all values are hashed, print the table's contents. Each entry will
be outputted in the form:
byte offset int index int value

Constraints:
(0) You must use a random access file. An in-memory hash table or in memory
1D array implementation will receive zero credit.

(1) Use exactly one random number generator.

(2) Duplicate data is permitted.

(3) Only one Java file.

(4) Include the standard header.

(5) Minimal (or no) comments.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Random;

public class Hashing {

int hashTable[] = new int[11];
  
public Hashing() {
for(int inx=0;inx < hashTable.length;inx++){
hashTable[inx] = -1;
}
}
  
private int firstHashFunction(int key)
{
int hashValue = -1;
hashValue = key%hashTable.length;
return hashValue;
}
  
private int secondHashFunction(int key)
{
int hashValue = -1;
hashValue = 7 - (key % 7);
return hashValue;
}
  
public void addKeyToHash(int key)
{
int location = firstHashFunction(key);
if(hashTable[location] != -1 )
{
location = secondHashFunction(key);
}
hashTable[location] = key;
System.out.println(key+" , "+ location);
}
  
public void displayHashTable()
{
System.out.println("Hash Table");
for(int inx=0;inx<hashTable.length;inx++)
{
System.out.println(hashTable[inx]);
}
}
  
public static void main(String[] args) {
Hashing h = new Hashing();
Random r = new Random();
for(int inx=0; inx < 8 ; inx++ )
{
int randomNumber;
randomNumber = (inx==0)? r.nextInt(97) : r.nextInt(100);
h.addKeyToHash(randomNumber);
}
h.displayHashTable();
}
}

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
in java, Hash 8 randomly generated int values (in the range [ 0 - 99 ]...
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
  • 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...

  • IN JAVA USING ECLIPSE The objective of this assignment is to create your own hash table...

    IN JAVA USING ECLIPSE The objective of this assignment is to create your own hash table class to hold employees and their ID numbers. You'll create an employee class to hold each person's key (id number) and value (name). Flow of the main program: Create an instance of your hash table class in your main method. Read in the Employees.txt file and store the names and ID numbers into Employee objects and store those in your hash table using the...

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

  • Project Description Allow the user to specify M (the size of the hash table) then, (1)...

    Project Description Allow the user to specify M (the size of the hash table) then, (1) add items found in text file "Project1.txt" to h, an initially-empty instance of a HASH (reject all items that have a key that duplicates the key of a previously added item—item keys must be unique); (2) display h (see format shown below); (3) delete 4 randomly-chosen items from the h; and finally, (4) display h. Note M must be a prime number, so your...

  • 1 Overview For this assignment you are required to write a Java program that plays (n,...

    1 Overview For this assignment you are required to write a Java program that plays (n, k)-tic-tac-toe; (n, k)-tic- tac-toe is played on a board of size n x n and to win the game a player needs to put k symbols on adjacent positions of the same row, column, or diagonal. The program will play against a human opponent. You will be given code for displaying the gameboard on the screen. 2 The Algorithm for Playing (n, k)-Tic-Tac-Toe The...

  • 1). The result of Java expression 5*7>3*(5+2) && 3*5-4<6 is ____ a. 35>35&&9<6 b. true &&...

    1). The result of Java expression 5*7>3*(5+2) && 3*5-4<6 is ____ a. 35>35&&9<6 b. true && false c. false d. False (2). You save text files containing Java language source code using the file extension___ a. .java b. .class c. .txt d. .src    (3). Which of the following is not a primitive data type in the Java programming language? a. boolean b. byte c. int d. Point (4). Given the Java statement int[][] vari = new int[5][7]; the value...

  • Our 1st new array operation/method is remove. Implement as follows: public static boolean remove( int[] arr,...

    Our 1st new array operation/method is remove. Implement as follows: public static boolean remove( int[] arr, int count, int key ) { 1: find the index of the first occurance of key. By first occurance we mean lowest index that contains this value. hint: copy the indexOf() method from Lab#3 into the bottom of this project file and call it from inside this remove method. The you will have the index of the value to remove from the array 2:...

  • Using C++ in Visual Studios Rewrite the code to use a Stack instead of a Queue....

    Using C++ in Visual Studios Rewrite the code to use a Stack instead of a Queue. You will need to think about the differences in the Stack and Queue. Think about how they operate and how the nodes may differ.   Modify the code as necessary. You will need to push to the Stack, pop from the Stack, peek at the Stack. You will need a member functions like in the example code. Your program will need to show that everything...

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
ADVERTISEMENT