I have just copied my java code below I am nearly finished I
I have just copied my java code below. I am nearly finished I just cannot get it to work. This is the assignment.
1. Write a Java NewHashTable<K, E> class as specified on the following pages. Use chaining to resolve collisions. You cannot use any of the hash classes in the Java library (or any other source). You can use the Java LinkedList, ArrayList, Vector or other related classes for your chains. NOTE: There may be some difficulties with creating the chains for the table.
2. You will receive a file by email of the most frequent last names in the U.S. Each line includes name, rank, frequency, proportion per 100,000, and cumulative proportion per 100,000 all separated by commas. Create a Java class to represent the information for each name with five data members. Include all the usual methods including equals and hashCode, both of which use the name.
3. Write a Java program to read in the information in the file, create instances of the class from part 2, and store them in your NewHashTable using the name as the key and the class from part 2 as the value. After reading and storing the first 10 names in the file, display the NewHashTable using the toString method, continue to read in the rest of the data. Also, make sure the rehash method is called at least once.
The program then get names from a user and if a name is found in the NewHashTable, displays the information about that name and removes the key and value from the table (This is just to see if your remove method works). Use both the containsKey and get methods when doing the searching (redundant, but good for testing). Include found names, names not found, and names previously found but now removed in the test cases.
import java.util.LinkedList;
@SuppressWarnings({ \"unchecked\", \"unused\", \"rawtypes\", \"hiding\" })
public class NewHashTable<K,V> {
//private class to hold a key and its value
private class HashEntry<K,V>{
private K key;
private V value;
public HashEntry(K key, V value){
this.key = key;
this.value = value;
}
public String toString(){
return key+\"=\"+value;
}
@Override
public boolean equals(Object obj) {
if(!(obj instanceof HashEntry))
return false;
HashEntry other = (HashEntry) obj;
return (key.equals(other.key));
}
}
private int capacity;
private float loadFactor;
private LinkedList<HashEntry>[] table;
private int size;
//Default Constructor
public NewHashTable(){
capacity = 11;
loadFactor = .75f;
table =new LinkedList[capacity];
for(LinkedList<HashEntry> bucket : table)
bucket = null;
size = 0;
}
//One argument Constructor
//@param initialCapacity initial capacity of table
public NewHashTable(int initialCapacity){
if(initialCapacity<0)
throw new IllegalArgumentException(\"\"+initialCapacity);
capacity = initialCapacity;
loadFactor = .75f;
table = new LinkedList[capacity];
for(LinkedList<HashEntry> bucket : table)
bucket = null;
size = 0;
}
//Two argument Constructor
//@param initialCapacity initial capacity of this NewHashTable
//@param loadFactor customized loadFactor
public NewHashTable(int initialCapacity, float loadFactor){
if(initialCapacity<0)
throw new IllegalArgumentException(\"Capacity: \"+initialCapacity);
if(loadFactor<0)
throw new IllegalArgumentException(\"Load Factor: \"+loadFactor);
capacity = initialCapacity;
this.loadFactor = loadFactor;
}
//@return capacity the capacity of this NewHashTable
public int getCapacity(){
return capacity;
}
//@return true if this NewHashTable is empty, false otherwise
public boolean isEmpty(){
return size==0;
}
//@return size the number of keys in this NewHashTable
public int size(){
return size;
}
//@param key key to be searched for in this NewHashTable
//@return true if key is found within this NewHashTable
public boolean containsKey(K key){
int hash = Math.abs(key.hashCode()%capacity);
return table[hash]!=null
&&table[hash].contains(new HashEntry<K,V>(key,null));
}
//Increases the capacity and reorganizes this NewHashTable
//Called automatically when size exceeds the capacity and
//the loadFactor in put() method
private void rehash(){
int newCapacity = capacity*2,hash=0,length=0; //doubles capacity
LinkedList<HashEntry>[] newTable = new LinkedList[newCapacity];
for(int i = 0; i < capacity;i++){
if(table[i]!=null){
length = table[i].size();
for(int j=0;j<length;j++){
HashEntry<K,V> entry = table[i].get(j);
hash = Math.abs(entry.key.hashCode()%newCapacity);
if(newTable[hash]==null)
newTable[hash]=new LinkedList<HashEntry>();
newTable[hash].add(entry);
}
}
}
table = newTable;
capacity = newCapacity;
System.out.println(\"Rehash Complete\");
}
//Returns the value to which key is mapped, null if
//this NewHashTable does not contain this key
//@param key the key associated with the value to be returned
//@return the value to which key is mapped
public V get(K key){
if(key == null)
throw new NullPointerException(\"Key: \"+key);
if(!containsKey(key))
return null;
int hash = Math.abs(key.hashCode()%capacity);
int i = table[hash].indexOf(new HashEntry<K,V>(key,null));
size++;
return (V) table[hash].get(i).value;
}
//Maps a key to a value in this table
//@param key NewHashTable key
//@param value the value to which key will be mapped
//@return the previous value mapped to key, null if there was
//no previous mapping
public V put(K key, V value){
if(key == null || value == null)
throw new NullPointerException(\"Key: \"+key+\" Value: \"+value);
int hash = Math.abs(key.hashCode()%capacity);
V previousValue = null;
if(table[hash]==null){
table[hash] = new LinkedList<HashEntry>();
table[hash].add(new HashEntry<K,V>(key,value));
}
else{
int i = table[hash].indexOf(new HashEntry<K,V>(key,null));
if(i!=-1){
previousValue = (V) table[hash].get(i).value;
table[hash].set(i,new HashEntry<K,V>(key,value));
}
else
table[hash].add(new HashEntry<K,V>(key,value));
}
size++;
if(size>=loadFactor*capacity)
rehash();
return previousValue;
}
//Removes a key and its value from this NewHashTable
//@param key the key to be removed
//@return the value key was mapped to, null if key
//was not mapped to a value
public V remove(K key){
if(key == null)
throw new NullPointerException(\"Key is null\");
int hash = Math.abs(key.hashCode()%capacity);
if(!containsKey(key))
return null;
int i = table[hash].indexOf(new HashEntry(key,null));
size--;
return (V) table[hash].remove(i).value;
}
//clears this NewHashTable
public void clear(){
for(LinkedList<HashEntry> bucket : table)
bucket = null;
size = 0;
}
//@return a String representation of this NewHashTable
public String toString(){
if(!isEmpty()){
String s = \"{\";
for(int i = 0; i < capacity;i++){
if(table[i]!=null){
for(int j = 0; j < table[i].size();j++)
s+=table[i].get(j)+\",\ \";
}
}
return s.substring(0,s.length()-3)+\"}\";
}
return \"{}\";
}
}
---------------------------------------------------------------------------------
public class Name {
private String lastName;
private int rank;
private int proportion;
private int cumProportion;
/**
* Default Constructor
*/
public Name() {
lastName = null;
rank = 0;
proportion = 0;
cumProportion = 0;
}
/**
*
* @param newLastName
* @param newRank
* @param newStat1
* @param newStat2
*/
public Name(String newLastName, int newRank, int newProportion, int newCumProportion) {
lastName = newLastName;
rank = newRank;
proportion = newProportion;
cumProportion = newCumProportion;
}
/**
* @return the lastName
*/
public String getLastName() {
return lastName;
}
/**
* @return the rank
*/
public int getRank() {
return rank;
}
/**
* @return the stat1
*/
public int getProportion() {
return proportion;
}
/**
* @return the stat2
*/
public int getCumProportion() {
return cumProportion;
}
/**
* @param lastName the lastName to set
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
/**
* @param rank the rank to set
*/
public void setRank(int rank) {
this.rank = rank;
}
/**
* @param stat1 the stat1 to set
*/
public void setStat1(int stat1) {
this.proportion = stat1;
}
/**
* @param stat2 the stat2 to set
*/
public void setStat2(int stat2) {
this.cumProportion = stat2;
}
public String toString()
{
return \"Name:\"+lastName+
\"\ Statistics :\"+rank+\"-\"+proportion+\"-\"+cumProportion;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + cumProportion;
result = prime * result + ((lastName == null) ? 0 : lastName.hashCode());
result = prime * result + proportion;
result = prime * result + rank;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Name other = (Name) obj;
if (cumProportion != other.cumProportion)
return false;
if (lastName == null) {
if (other.lastName != null)
return false;
} else if (!lastName.equals(other.lastName))
return false;
if (proportion != other.proportion)
return false;
if (rank != other.rank)
return false;
return true;
}
}
-----------------------------------------------------------------------------
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Main {
/**
* @param args
*/
@SuppressWarnings(\"resource\")
public static void main(String[] args) throws IOException{
//String searchName;
String data;
@SuppressWarnings(\"unused\")
String input;
int count=0;
@SuppressWarnings(\"unused\")
Scanner kb = new Scanner(System.in);
NewHashTable<Name,String> ht = new NewHashTable<Name, String>();
Name[] lastNames=new Name[151671];
Scanner list = new Scanner(new File(\"list.txt\"));
while (list.hasNext()) {
data = list.nextLine();
StringTokenizer tokens = new StringTokenizer(data, \",\");
String lastName = tokens.nextToken().trim();
int rank = Integer.parseInt(tokens.nextToken().trim());
int proportion = Integer.parseInt(tokens.nextToken().trim());
int cumProportion = Integer.parseInt(tokens.nextToken().trim());
lastNames[count]=new Name(lastName, rank,proportion,cumProportion);
ht.put(lastNames[0], lastName);
count++;
if(count==9) /* Prints the first 10 names after reading and storing them in the NewHashTable */
System.out.println(\"String form of hash table is: \"+ht.toString());
}
for (int i = 0; i < args.length; i++) {
if(ht.containsKey(lastNames[i])) {
System.out.println(\"/t\" ht.get(lastNames[i]));
ht.remove(lastNames[i]);
}
}
}
}
Solution
I did not make any change in Name class and NewHashTable class.
changes are only in Main class. Also I switched the key and value according to the requirement.
Main.java
package Sample;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Main {
/**
* @param args
*/
@SuppressWarnings(\"resource\")
public static void main(String[] args) throws IOException{
//String searchName;
String data;
@SuppressWarnings(\"unused\")
String input;
int count=0;
@SuppressWarnings(\"unused\")
Scanner kb = new Scanner(System.in);
// I interchanged the Key and value. It is mentioned in the requirement that key would be the name and value would be the name class
NewHashTable<String,Name> ht = new NewHashTable<String, Name>();
Name[] lastNames=new Name[151671];
String allNames[] = new String[151671];
Scanner list = new Scanner(new File(\"list.txt\"));
while (list.hasNext()) {
data = list.nextLine();
StringTokenizer tokens = new StringTokenizer(data, \",\");
String lastName = tokens.nextToken().trim();
int rank = Integer.parseInt(tokens.nextToken().trim());
int proportion = Integer.parseInt(tokens.nextToken().trim());
int cumProportion = Integer.parseInt(tokens.nextToken().trim());
lastNames[count]=new Name(lastName, rank,proportion,cumProportion);
allNames[count] = new String(lastName);
ht.put(lastName, lastNames[count]);
count++;
if(count==9) /* Prints the first 10 names after reading and storing them in the NewHashTable */
System.out.println(\"String form of hash table is: \"+ht.toString());
}
//After reading from file
for (int i = 0; i < allNames.length; i++) {
if(allNames[i] != null && ht.containsKey(allNames[i])) {
System.out.println(\"Removed: \\t\" + ht.remove(allNames[i]));
}
}
System.out.println(\"String form of hash table is: \"+ht.toString());
}
}
OUTPUT:
Rehash Complete
String form of hash table is: {
Wess=Name:Wess
Statistics :11-123-123,
Victor=Name:Victor
Statistics :45-400-400,
Marsh=Name:Marsh
Statistics :98-999-222,
Williams=Name:Williams
Statistics :23-1000-1000,
Phillips=Name:Phillips
Statistics :10-2000-2000,
Peter=Name:Peter
Statistics :43-568-876,
Hector=Name:Hector
Statistics :32-2000-2000,
Stewart=Name:Stewart
Statistics :34-3000-3000,
Samuels=Name:Samuels
Statistics :23-343-345,
}
Removed: Name:Williams
Statistics :23-1000-1000
Removed: Name:Hector
Statistics :32-2000-2000
Removed: Name:Stewart
Statistics :34-3000-3000
Removed: Name:Phillips
Statistics :10-2000-2000
Removed: Name:Victor
Statistics :45-400-400
Removed: Name:Samuels
Statistics :23-343-345
Removed: Name:Marsh
Statistics :98-999-222
Removed: Name:Peter
Statistics :43-568-876
Removed: Name:Wess
Statistics :11-123-123
Removed: Name:Hues
Statistics :33-333-333
String form of hash table is: {}
I dont have the actual data with me, hence i just added the following data to the file.
list.txt
Williams,23,1000,1000
Hector,32,2000,2000
Stewart,34,3000,3000
Phillips,10,2000,2000
Victor,45,400,400
Samuels,23,343,345
Marsh,98,999,222
Peter,43,568,876
Wess,11,123,123
Hues,33,333,333













