SlideShare a Scribd company logo
1 of 7
Download to read offline
Complete code in Java
The hashtable you'll be making will use Strings as the keys and Object as the values. Similar to
linked lists, by storing Object as values, you can store any kind of object in the hashtable.
package Dictionary;
import java.util.ArrayList;
import java.util.Hashtable;
import List.ListInterface;
import List.MyLinkedList;
public class MyHashtable implements DictionaryInterface {
protected int tableSize;
protected int size;
// The LinkedList is of type Entry
protected MyLinkedList[] table;
public MyHashtable(int tableSize) {
table = new MyLinkedList[tableSize];
this.tableSize = tableSize;
}
public int biggestBucket()
{
int biggestBucket = 0;
for(int i = 0; i < table.length; i++) {
// Loop through the table looking for non-null locations.
if (table[i] != null) {
// If you find a non-null location, compare the bucket size against the largest
// bucket size found so far. If the current bucket size is bigger, set biggestBucket
// to this new size.
MyLinkedList bucket = table[i];
if (biggestBucket < bucket.size())
biggestBucket = bucket.size();
}
}
return biggestBucket; // Return the size of the biggest bucket found.
}
// Returns the average bucket length. Gives a sense of how many collisions are happening
overall.
public float averageBucket() {
float bucketCount = 0; // Number of buckets (non-null table locations)
float bucketSizeSum = 0; // Sum of the size of all buckets
for(int i = 0; i < table.length; i++) {
// Loop through the table
if (table[i] != null) {
// For a non-null location, increment the bucketCount and add to the bucketSizeSum
MyLinkedList bucket = table[i];
bucketSizeSum += bucket.size();
bucketCount++;
}
}
// Divide bucketSizeSum by the number of buckets to get an average bucket length.
return bucketSizeSum/bucketCount;
}
public String toString()
{
String s = "";
for(int tableIndex = 0; tableIndex < tableSize; tableIndex++) {
if (table[tableIndex] != null) {
MyLinkedList bucket = table[tableIndex];
for(int listIndex = 0; listIndex < bucket.size(); listIndex++) {
Entry e = (Entry)bucket.get(listIndex);
s = s + "key: " + e.key + ", value: " + e.value + "n";
}
}
}
return s;
}
protected class Entry
{
String key;
Object value;
Entry(String key, Object value) {
this.key = key;
this.value = value;
}
}
// Implement these methods
public boolean isEmpty() {return true;} // returns true if the Dictionary is empty, false otherwise.
public int size(){return -1;} //Returns the number of key/value pairs stored in the dictionary.
// Adds a value stored under the given key. If the key has already been stored in the Dictionary,
// replaces the value associated with the key and returns the old value. If the key isn't in the
dictionary
// returns null.
public Object put(String key, Object value){
// 1. Compute an array index given the key
// 2. If that location in the table is null,
// that means nothing has been previously stored using a key with this hash code.
// a. Create a new MyLinkedList to be the bucket.
// b. Add the new Entry for the key/value pair to the list.
// c. Set this location in the array equal to the new bucket (list).
// d. Increment the size (the number of unique keys you have stored).
// 3. If the location in the table isn't null,
// that means keys with this colliding hash code have been previously stored.
// 3a, a value exists for the key
// a. Linearly search through the bucket (the list) stored at this array
// location comparing the key for each entry with the key passed into put().
// If you get a match, this means this key as been previously stored.
// Save the old value in the Entry (so you can return it) and replace it with the new value.
// (use the code below)
// Entry oldValue = new Entry(key, e.value);
// e.value = value;
// NOTE: this is technically not correct as you would need to create a deep copy of the entry,
// however, that is outside the realm of this assignment. The code above will be
// enough to complete the assignment
// return old value here
// return oldValue.value;
// 3b, a value does not exist for the key
// b. If you don't find the key in the bucket,
// then just add a new Entry (with the key and value) to the beginning of the list.
// Increment the size.
return null;
}
public Object get(String key){
// 1. Compute an array index given the key
// 2. If that location in the table is null,
// that means nothing has been stored using a key with this hash code.
// So we can return null.
// 4. Linearly search through the bucket (the list),
// comparing the key for each entry with the key passed into get().
// Extracting each element in
// the Linked List
// If you find a match, return the value.
return null;
} // Retrieves the value stored with the key.
public void remove(String key){
// 1. Compute an array index given the key
// 2. If that location in the table is null, then this key has definitely not been used to store a value.
// 3. If the location in the table has a bucket,
// we need to linearly search it to see if it contains an Entry with the key.
// If you find an Entry in the bucket (linked list) with the key:
// a. Remove this Entry from the bucket.
// b. Decrement size (the number of unique keys stored in the hashtable).
} // Deletes the key/value pair stored with the given key.
public void clear(){} // Empties the dictionary.
public String[] getKeys(){
// 1. Create a String[] with a size equal to the number of unique keys in the hashtable
// 2. Iterate through the hashtable array.
// For each table location that isn't null
// a. Iterate though the bucket (linked list)
// getting the key out of each Entry and storing it in
// the array of strings you created in step 1.
// 3. Return the String[]
return null;
}
}

More Related Content

Similar to Complete code in Java The hashtable you'll be making will use String.pdf

package singlylinkedlist; public class Node { public String valu.pdf
package singlylinkedlist; public class Node { public String valu.pdfpackage singlylinkedlist; public class Node { public String valu.pdf
package singlylinkedlist; public class Node { public String valu.pdf
amazing2001
 
Please help me to make a programming project I have to sue them today- (1).pdf
Please help me to make a programming project I have to sue them today- (1).pdfPlease help me to make a programming project I have to sue them today- (1).pdf
Please help me to make a programming project I have to sue them today- (1).pdf
seoagam1
 
Please complete all the code as per instructions in Java programming.docx
Please complete all the code as per instructions in Java programming.docxPlease complete all the code as per instructions in Java programming.docx
Please complete all the code as per instructions in Java programming.docx
cgraciela1
 
you will implement some sorting algorithms for arrays and linked lis.pdf
you will implement some sorting algorithms for arrays and linked lis.pdfyou will implement some sorting algorithms for arrays and linked lis.pdf
you will implement some sorting algorithms for arrays and linked lis.pdf
clearvisioneyecareno
 
Problem- Describe an algorithm for concatenating two singly linked lis.pdf
Problem- Describe an algorithm for concatenating two singly linked lis.pdfProblem- Describe an algorithm for concatenating two singly linked lis.pdf
Problem- Describe an algorithm for concatenating two singly linked lis.pdf
JamesPXNNewmanp
 
Given below is the completed implementation of MyLinkedList class. O.pdf
Given below is the completed implementation of MyLinkedList class. O.pdfGiven below is the completed implementation of MyLinkedList class. O.pdf
Given below is the completed implementation of MyLinkedList class. O.pdf
info430661
 
Problem- Describe an algorithm for concatenating two singly linked lis.pdf
Problem- Describe an algorithm for concatenating two singly linked lis.pdfProblem- Describe an algorithm for concatenating two singly linked lis.pdf
Problem- Describe an algorithm for concatenating two singly linked lis.pdf
kingsandqueens3
 
Data Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdfData Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdf
rohit219406
 
week4_srcArrayMethods.javaweek4_srcArrayMethods.javapackage ed.docx
week4_srcArrayMethods.javaweek4_srcArrayMethods.javapackage ed.docxweek4_srcArrayMethods.javaweek4_srcArrayMethods.javapackage ed.docx
week4_srcArrayMethods.javaweek4_srcArrayMethods.javapackage ed.docx
alanfhall8953
 
I have created a class hasdhedDictionary that implements the Diction.pdf
I have created a class hasdhedDictionary that implements the Diction.pdfI have created a class hasdhedDictionary that implements the Diction.pdf
I have created a class hasdhedDictionary that implements the Diction.pdf
allystraders
 
Lecture 18Dynamic Data Structures and Generics (II).docx
Lecture 18Dynamic Data Structures and Generics (II).docxLecture 18Dynamic Data Structures and Generics (II).docx
Lecture 18Dynamic Data Structures and Generics (II).docx
SHIVA101531
 
03.DS_Store__MACOSX03._.DS_Store03A2.DS_Store__.docx
03.DS_Store__MACOSX03._.DS_Store03A2.DS_Store__.docx03.DS_Store__MACOSX03._.DS_Store03A2.DS_Store__.docx
03.DS_Store__MACOSX03._.DS_Store03A2.DS_Store__.docx
honey725342
 
Note- Can someone help me with the public boolean isEmpty()- public bo.pdf
Note- Can someone help me with the public boolean isEmpty()- public bo.pdfNote- Can someone help me with the public boolean isEmpty()- public bo.pdf
Note- Can someone help me with the public boolean isEmpty()- public bo.pdf
Augstore
 
Once you have all the structures working as intended- it is time to co.docx
Once you have all the structures working as intended- it is time to co.docxOnce you have all the structures working as intended- it is time to co.docx
Once you have all the structures working as intended- it is time to co.docx
farrahkur54
 
Javai have to make a method that takes a linked list and then retu.pdf
Javai have to make a method that takes a linked list and then retu.pdfJavai have to make a method that takes a linked list and then retu.pdf
Javai have to make a method that takes a linked list and then retu.pdf
stopgolook
 
Note- Can someone help me with the Public boolean add(E value) method.pdf
Note- Can someone help me with the Public boolean add(E value) method.pdfNote- Can someone help me with the Public boolean add(E value) method.pdf
Note- Can someone help me with the Public boolean add(E value) method.pdf
Stewart29UReesa
 
The LinkedList1 class implements a Linked list. class.pdf
The LinkedList1 class implements a Linked list. class.pdfThe LinkedList1 class implements a Linked list. class.pdf
The LinkedList1 class implements a Linked list. class.pdf
malavshah9013
 
File LinkedList.java Defines a doubly-l.pdf
File LinkedList.java Defines a doubly-l.pdfFile LinkedList.java Defines a doubly-l.pdf
File LinkedList.java Defines a doubly-l.pdf
Conint29
 

Similar to Complete code in Java The hashtable you'll be making will use String.pdf (20)

package singlylinkedlist; public class Node { public String valu.pdf
package singlylinkedlist; public class Node { public String valu.pdfpackage singlylinkedlist; public class Node { public String valu.pdf
package singlylinkedlist; public class Node { public String valu.pdf
 
Please help me to make a programming project I have to sue them today- (1).pdf
Please help me to make a programming project I have to sue them today- (1).pdfPlease help me to make a programming project I have to sue them today- (1).pdf
Please help me to make a programming project I have to sue them today- (1).pdf
 
Please complete all the code as per instructions in Java programming.docx
Please complete all the code as per instructions in Java programming.docxPlease complete all the code as per instructions in Java programming.docx
Please complete all the code as per instructions in Java programming.docx
 
you will implement some sorting algorithms for arrays and linked lis.pdf
you will implement some sorting algorithms for arrays and linked lis.pdfyou will implement some sorting algorithms for arrays and linked lis.pdf
you will implement some sorting algorithms for arrays and linked lis.pdf
 
Problem- Describe an algorithm for concatenating two singly linked lis.pdf
Problem- Describe an algorithm for concatenating two singly linked lis.pdfProblem- Describe an algorithm for concatenating two singly linked lis.pdf
Problem- Describe an algorithm for concatenating two singly linked lis.pdf
 
Given below is the completed implementation of MyLinkedList class. O.pdf
Given below is the completed implementation of MyLinkedList class. O.pdfGiven below is the completed implementation of MyLinkedList class. O.pdf
Given below is the completed implementation of MyLinkedList class. O.pdf
 
Problem- Describe an algorithm for concatenating two singly linked lis.pdf
Problem- Describe an algorithm for concatenating two singly linked lis.pdfProblem- Describe an algorithm for concatenating two singly linked lis.pdf
Problem- Describe an algorithm for concatenating two singly linked lis.pdf
 
Describe an algorithm for concatenating two singly linked lists L and.pdf
Describe an algorithm for concatenating two singly linked lists L and.pdfDescribe an algorithm for concatenating two singly linked lists L and.pdf
Describe an algorithm for concatenating two singly linked lists L and.pdf
 
Data Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdfData Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdf
 
Implement the additional 5 methods as indicated in the LinkedList fi.pdf
Implement the additional 5 methods as indicated in the LinkedList fi.pdfImplement the additional 5 methods as indicated in the LinkedList fi.pdf
Implement the additional 5 methods as indicated in the LinkedList fi.pdf
 
week4_srcArrayMethods.javaweek4_srcArrayMethods.javapackage ed.docx
week4_srcArrayMethods.javaweek4_srcArrayMethods.javapackage ed.docxweek4_srcArrayMethods.javaweek4_srcArrayMethods.javapackage ed.docx
week4_srcArrayMethods.javaweek4_srcArrayMethods.javapackage ed.docx
 
I have created a class hasdhedDictionary that implements the Diction.pdf
I have created a class hasdhedDictionary that implements the Diction.pdfI have created a class hasdhedDictionary that implements the Diction.pdf
I have created a class hasdhedDictionary that implements the Diction.pdf
 
Lecture 18Dynamic Data Structures and Generics (II).docx
Lecture 18Dynamic Data Structures and Generics (II).docxLecture 18Dynamic Data Structures and Generics (II).docx
Lecture 18Dynamic Data Structures and Generics (II).docx
 
03.DS_Store__MACOSX03._.DS_Store03A2.DS_Store__.docx
03.DS_Store__MACOSX03._.DS_Store03A2.DS_Store__.docx03.DS_Store__MACOSX03._.DS_Store03A2.DS_Store__.docx
03.DS_Store__MACOSX03._.DS_Store03A2.DS_Store__.docx
 
Note- Can someone help me with the public boolean isEmpty()- public bo.pdf
Note- Can someone help me with the public boolean isEmpty()- public bo.pdfNote- Can someone help me with the public boolean isEmpty()- public bo.pdf
Note- Can someone help me with the public boolean isEmpty()- public bo.pdf
 
Once you have all the structures working as intended- it is time to co.docx
Once you have all the structures working as intended- it is time to co.docxOnce you have all the structures working as intended- it is time to co.docx
Once you have all the structures working as intended- it is time to co.docx
 
Javai have to make a method that takes a linked list and then retu.pdf
Javai have to make a method that takes a linked list and then retu.pdfJavai have to make a method that takes a linked list and then retu.pdf
Javai have to make a method that takes a linked list and then retu.pdf
 
Note- Can someone help me with the Public boolean add(E value) method.pdf
Note- Can someone help me with the Public boolean add(E value) method.pdfNote- Can someone help me with the Public boolean add(E value) method.pdf
Note- Can someone help me with the Public boolean add(E value) method.pdf
 
The LinkedList1 class implements a Linked list. class.pdf
The LinkedList1 class implements a Linked list. class.pdfThe LinkedList1 class implements a Linked list. class.pdf
The LinkedList1 class implements a Linked list. class.pdf
 
File LinkedList.java Defines a doubly-l.pdf
File LinkedList.java Defines a doubly-l.pdfFile LinkedList.java Defines a doubly-l.pdf
File LinkedList.java Defines a doubly-l.pdf
 

More from aarifi9988

The disease cystic fibrosis is a recessive trait- Two healthy parents.pdf
The disease cystic fibrosis is a recessive trait- Two healthy parents.pdfThe disease cystic fibrosis is a recessive trait- Two healthy parents.pdf
The disease cystic fibrosis is a recessive trait- Two healthy parents.pdf
aarifi9988
 
Problem 3-70 A Part II Comprehensive Problem- Reviewing the Accounting.pdf
Problem 3-70 A Part II Comprehensive Problem- Reviewing the Accounting.pdfProblem 3-70 A Part II Comprehensive Problem- Reviewing the Accounting.pdf
Problem 3-70 A Part II Comprehensive Problem- Reviewing the Accounting.pdf
aarifi9988
 
Match each item with a statement below- Using this network mode within.pdf
Match each item with a statement below- Using this network mode within.pdfMatch each item with a statement below- Using this network mode within.pdf
Match each item with a statement below- Using this network mode within.pdf
aarifi9988
 
Jump to level 1 What properties apply to each attribute-A programmer m.pdf
Jump to level 1 What properties apply to each attribute-A programmer m.pdfJump to level 1 What properties apply to each attribute-A programmer m.pdf
Jump to level 1 What properties apply to each attribute-A programmer m.pdf
aarifi9988
 

More from aarifi9988 (20)

Do you think that management of an Alaska Native Regional Corporation.pdf
Do you think that management of an Alaska Native Regional Corporation.pdfDo you think that management of an Alaska Native Regional Corporation.pdf
Do you think that management of an Alaska Native Regional Corporation.pdf
 
Draw the punnott square of RrYyx rryy and grve the genatype ratio and.pdf
Draw the punnott square of RrYyx rryy and grve the genatype ratio and.pdfDraw the punnott square of RrYyx rryy and grve the genatype ratio and.pdf
Draw the punnott square of RrYyx rryy and grve the genatype ratio and.pdf
 
Consider an environment in which there is a one-to-one mapping between.pdf
Consider an environment in which there is a one-to-one mapping between.pdfConsider an environment in which there is a one-to-one mapping between.pdf
Consider an environment in which there is a one-to-one mapping between.pdf
 
A population has a mean -161 and a standard deviation -29- Find the me.pdf
A population has a mean -161 and a standard deviation -29- Find the me.pdfA population has a mean -161 and a standard deviation -29- Find the me.pdf
A population has a mean -161 and a standard deviation -29- Find the me.pdf
 
4- X and Y are joint continuous random variables and their joint p-d-f.pdf
4- X and Y are joint continuous random variables and their joint p-d-f.pdf4- X and Y are joint continuous random variables and their joint p-d-f.pdf
4- X and Y are joint continuous random variables and their joint p-d-f.pdf
 
The human body usually responds to mutated cells byA replicating the m.pdf
The human body usually responds to mutated cells byA replicating the m.pdfThe human body usually responds to mutated cells byA replicating the m.pdf
The human body usually responds to mutated cells byA replicating the m.pdf
 
The disease cystic fibrosis is a recessive trait- Two healthy parents.pdf
The disease cystic fibrosis is a recessive trait- Two healthy parents.pdfThe disease cystic fibrosis is a recessive trait- Two healthy parents.pdf
The disease cystic fibrosis is a recessive trait- Two healthy parents.pdf
 
See picture above What is the cardinality of the relationship between.pdf
See picture above  What is the cardinality of the relationship between.pdfSee picture above  What is the cardinality of the relationship between.pdf
See picture above What is the cardinality of the relationship between.pdf
 
How many different 6 -letter passwords can be formed from the letters.pdf
How many different 6 -letter passwords can be formed from the letters.pdfHow many different 6 -letter passwords can be formed from the letters.pdf
How many different 6 -letter passwords can be formed from the letters.pdf
 
Problem 3-70 A Part II Comprehensive Problem- Reviewing the Accounting.pdf
Problem 3-70 A Part II Comprehensive Problem- Reviewing the Accounting.pdfProblem 3-70 A Part II Comprehensive Problem- Reviewing the Accounting.pdf
Problem 3-70 A Part II Comprehensive Problem- Reviewing the Accounting.pdf
 
ll of the following are true of gonorrhea in women EXCEPT- Primary sit.pdf
ll of the following are true of gonorrhea in women EXCEPT- Primary sit.pdfll of the following are true of gonorrhea in women EXCEPT- Primary sit.pdf
ll of the following are true of gonorrhea in women EXCEPT- Primary sit.pdf
 
Lahars are hot mudflows caused by the mixing of volcanic ash and rain.pdf
Lahars are hot mudflows caused by the mixing of volcanic ash and rain.pdfLahars are hot mudflows caused by the mixing of volcanic ash and rain.pdf
Lahars are hot mudflows caused by the mixing of volcanic ash and rain.pdf
 
Match each item with a statement below- Using this network mode within.pdf
Match each item with a statement below- Using this network mode within.pdfMatch each item with a statement below- Using this network mode within.pdf
Match each item with a statement below- Using this network mode within.pdf
 
Jump to level 1 What properties apply to each attribute-A programmer m.pdf
Jump to level 1 What properties apply to each attribute-A programmer m.pdfJump to level 1 What properties apply to each attribute-A programmer m.pdf
Jump to level 1 What properties apply to each attribute-A programmer m.pdf
 
Which of the following methods is the most appropriate when data have.pdf
Which of the following methods is the most appropriate when data have.pdfWhich of the following methods is the most appropriate when data have.pdf
Which of the following methods is the most appropriate when data have.pdf
 
Which of these bacteria most likely does not possess the enzymes super.pdf
Which of these bacteria most likely does not possess the enzymes super.pdfWhich of these bacteria most likely does not possess the enzymes super.pdf
Which of these bacteria most likely does not possess the enzymes super.pdf
 
There are n-10 battery packs in stock- Each battery has the ability to.pdf
There are n-10 battery packs in stock- Each battery has the ability to.pdfThere are n-10 battery packs in stock- Each battery has the ability to.pdf
There are n-10 battery packs in stock- Each battery has the ability to.pdf
 
The pandemic has required organizations to redefine- redesign- and shi.pdf
The pandemic has required organizations to redefine- redesign- and shi.pdfThe pandemic has required organizations to redefine- redesign- and shi.pdf
The pandemic has required organizations to redefine- redesign- and shi.pdf
 
The general journals are- Select one- a- Used to record similar repeti.pdf
The general journals are- Select one- a- Used to record similar repeti.pdfThe general journals are- Select one- a- Used to record similar repeti.pdf
The general journals are- Select one- a- Used to record similar repeti.pdf
 
The blood vessels in the legs are twice as long as the blood vessels i.pdf
The blood vessels in the legs are twice as long as the blood vessels i.pdfThe blood vessels in the legs are twice as long as the blood vessels i.pdf
The blood vessels in the legs are twice as long as the blood vessels i.pdf
 

Recently uploaded

Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 

Recently uploaded (20)

Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 

Complete code in Java The hashtable you'll be making will use String.pdf

  • 1. Complete code in Java The hashtable you'll be making will use Strings as the keys and Object as the values. Similar to linked lists, by storing Object as values, you can store any kind of object in the hashtable. package Dictionary; import java.util.ArrayList; import java.util.Hashtable; import List.ListInterface; import List.MyLinkedList; public class MyHashtable implements DictionaryInterface { protected int tableSize; protected int size; // The LinkedList is of type Entry protected MyLinkedList[] table; public MyHashtable(int tableSize) { table = new MyLinkedList[tableSize]; this.tableSize = tableSize; } public int biggestBucket() { int biggestBucket = 0; for(int i = 0; i < table.length; i++) { // Loop through the table looking for non-null locations. if (table[i] != null) { // If you find a non-null location, compare the bucket size against the largest
  • 2. // bucket size found so far. If the current bucket size is bigger, set biggestBucket // to this new size. MyLinkedList bucket = table[i]; if (biggestBucket < bucket.size()) biggestBucket = bucket.size(); } } return biggestBucket; // Return the size of the biggest bucket found. } // Returns the average bucket length. Gives a sense of how many collisions are happening overall. public float averageBucket() { float bucketCount = 0; // Number of buckets (non-null table locations) float bucketSizeSum = 0; // Sum of the size of all buckets for(int i = 0; i < table.length; i++) { // Loop through the table if (table[i] != null) { // For a non-null location, increment the bucketCount and add to the bucketSizeSum MyLinkedList bucket = table[i]; bucketSizeSum += bucket.size(); bucketCount++; } } // Divide bucketSizeSum by the number of buckets to get an average bucket length.
  • 3. return bucketSizeSum/bucketCount; } public String toString() { String s = ""; for(int tableIndex = 0; tableIndex < tableSize; tableIndex++) { if (table[tableIndex] != null) { MyLinkedList bucket = table[tableIndex]; for(int listIndex = 0; listIndex < bucket.size(); listIndex++) { Entry e = (Entry)bucket.get(listIndex); s = s + "key: " + e.key + ", value: " + e.value + "n"; } } } return s; } protected class Entry { String key; Object value; Entry(String key, Object value) { this.key = key; this.value = value;
  • 4. } } // Implement these methods public boolean isEmpty() {return true;} // returns true if the Dictionary is empty, false otherwise. public int size(){return -1;} //Returns the number of key/value pairs stored in the dictionary. // Adds a value stored under the given key. If the key has already been stored in the Dictionary, // replaces the value associated with the key and returns the old value. If the key isn't in the dictionary // returns null. public Object put(String key, Object value){ // 1. Compute an array index given the key // 2. If that location in the table is null, // that means nothing has been previously stored using a key with this hash code. // a. Create a new MyLinkedList to be the bucket. // b. Add the new Entry for the key/value pair to the list. // c. Set this location in the array equal to the new bucket (list). // d. Increment the size (the number of unique keys you have stored). // 3. If the location in the table isn't null, // that means keys with this colliding hash code have been previously stored. // 3a, a value exists for the key // a. Linearly search through the bucket (the list) stored at this array // location comparing the key for each entry with the key passed into put(). // If you get a match, this means this key as been previously stored. // Save the old value in the Entry (so you can return it) and replace it with the new value.
  • 5. // (use the code below) // Entry oldValue = new Entry(key, e.value); // e.value = value; // NOTE: this is technically not correct as you would need to create a deep copy of the entry, // however, that is outside the realm of this assignment. The code above will be // enough to complete the assignment // return old value here // return oldValue.value; // 3b, a value does not exist for the key // b. If you don't find the key in the bucket, // then just add a new Entry (with the key and value) to the beginning of the list. // Increment the size. return null; } public Object get(String key){ // 1. Compute an array index given the key // 2. If that location in the table is null, // that means nothing has been stored using a key with this hash code. // So we can return null. // 4. Linearly search through the bucket (the list), // comparing the key for each entry with the key passed into get(). // Extracting each element in // the Linked List
  • 6. // If you find a match, return the value. return null; } // Retrieves the value stored with the key. public void remove(String key){ // 1. Compute an array index given the key // 2. If that location in the table is null, then this key has definitely not been used to store a value. // 3. If the location in the table has a bucket, // we need to linearly search it to see if it contains an Entry with the key. // If you find an Entry in the bucket (linked list) with the key: // a. Remove this Entry from the bucket. // b. Decrement size (the number of unique keys stored in the hashtable). } // Deletes the key/value pair stored with the given key. public void clear(){} // Empties the dictionary. public String[] getKeys(){ // 1. Create a String[] with a size equal to the number of unique keys in the hashtable // 2. Iterate through the hashtable array. // For each table location that isn't null // a. Iterate though the bucket (linked list) // getting the key out of each Entry and storing it in // the array of strings you created in step 1. // 3. Return the String[] return null; }
  • 7. }