Data Structure using OOP Project

Page 1 of 1

1 Replies - 135 Views - Last Post: 05 December 2017 - 11:52 AM Rate Topic: -----

#1 banbaluta24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 23-September 13

Data Structure using OOP Project

Posted 05 December 2017 - 11:50 AM

I have this data structure project and here is the classes I implemented and the .csv file
My problem is when I run the program the Hash Table content supposed to display from zero (0) to 100 but it Starts from 1 - 101
I"m trying to fix it my keep displaying the same content from 1 - 101
[/**
002
*
003
* @author Samici
004
*
005
*/
006
import java.text.*;
007
public class HashTable {
008
private int freeSpaces;
009

010
private final LinkedList[] LINKED_LIST;
011

012
private static final NumberFormat FORMATTER = NumberFormat.getInstance();
013

014
public HashTable() {
015
LINKED_LIST = new LinkedList[freeSpaces = 101];
016
}
017

018
public void insert(String state, int population) {
019
int hash = toUnicodeModulus(state);
020

021
if (LINKED_LIST[hash] == null) {
022
freeSpaces--;
023

024
LINKED_LIST[hash] = new LinkedList(new Node(state, population));
025
} else {
026
LINKED_LIST[hash].insert(new Node(state, population));
027
}
028
}
029

030
public double find(String state) {
031
LinkedList list = LINKED_LIST[toUnicodeModulus(state)];
032

033
if (list == null) {
034
System.out.println(state + " is not found");
035

036
return -1;
037
}
038

039
for (Node node = list.head; node != null; node = node.nextNode) {
040
if (node.stateName.equals(state)) {
041
System.out.println(state + " is found with a population of " + FORMATTER.format(node.statePopulation));
042

043
return node.statePopulation;
044
}
045
}
046

047
System.out.println(state + " is not found");
048

049
return -1;
050
}
051

052
public void delete(String state) {
053
int hash = toUnicodeModulus(state);
054

055
if (LINKED_LIST[hash] == null) {
056
return;
057
}
058

059
if (LINKED_LIST[hash].remove(state)) {
060
freeSpaces++;
061

062
LINKED_LIST[hash] = null;
063
}
064
}
065

066
/**
067
* Prints a formatted representation of
068
* the hash table to the console.
069
*/
070
public void display() {
071
for (int i = 0; i < LINKED_LIST.length; i++) {
072
if (LINKED_LIST[i] == null) {
073
System.out.println((i + 1) + ". Empty");
074
continue;
075
}
076

077
for (Node node = LINKED_LIST[i].head; node != null; node = node.nextNode) {
078
System.out.print((i + 1) + ". ");
079

080
node.printNode();
081
}
082
}
083
}
084

085
/**
086
* Prints the amount of free spaces and collisions
087
* inside the hash table.
088
*/
089
public void printFreeAndCollisions() {
090
int collisions = 0;
091

092
for (LinkedList list : LINKED_LIST) {
093
if (list == null) {
094
continue;
095
}
096

097

098
if (list.head != list.tail) {
099
collisions++;
100
}
101
}
102

103
System.out.println("Hash table has " + freeSpaces + " empty spaces and " + collisions + " collisions.");
104
}
105

106
/**
107
* A helper method (hash function) to convert a String
108
* to its respective hash.
109
*/
110
private int toUnicodeModulus(String state) {
111
int hash = 0;
112

113
for (char c : state.toCharArray()) {
114
hash += c;
115
}
116

117
return hash % LINKED_LIST.length;
118
}
119

120
private class Node {
121

122
String stateName;
123

124
int statePopulation;
125

126
Node nextNode;
127

128
public Node(String state, int population) {
129
stateName = state;
130
statePopulation = population;
131
}
132

133
public void printNode() {
134
System.out.printf("%-25s%,10d\n", stateName, statePopulation);
135
}
136
}
137

138
/**
139
* A LinkedList class that will hold
140
* multiple Node objects with the same
141
* hash.
142
*/
143
private class LinkedList {
144

145

146
Node head;
147

148
Node tail;
149

150
public LinkedList(Node node) {
151
this.head = node;
152
this.tail = node;
153
}
154

155
public void insert(Node node) {
156
tail.nextNode = node;
157
tail = node;
158
}
159

160

161
public boolean remove(String state) {
162

163
Node previousNode = head;
164

165

166
if (head.stateName.equals(state) && tail.stateName.equals(state)) {
167
System.out.println(state + " has been deleted from hash table");
168
return true;
169
}
170

171

172
if (head.stateName.equals(state)) {
173
head = head.nextNode;
174
System.out.println(state + " has been deleted from hash table");
175
return false;
176
}
177

178
/**
179
* Iterate through the list and remove
180
* the correct Node according to its
181
* state name.
182
*/
183
for (Node node = head; node != null; node = node.nextNode) {
184
if (node.stateName.equals(state)) {
185
previousNode.nextNode = node.nextNode;
186
System.out.println(state + " has been deleted from hash table");
187
return false;
188
}
189

190
previousNode = node;
191
}
192

193
System.out.println(state + " cannot be found within the hash table and thus was NOT deleted");
194
return false;
195
}
196
}
197
}]
[/**
02
*
03
* @author Samici
04
*
05
*/
06
import java.io.*;
07
import java.nio.file.*;
08
import java.util.*;
09
public class Project5 {
10
private static final HashTable HASH_TABLE = new HashTable();
11

12
private static final Scanner SCANNER = new Scanner(System.in);
13

14
public static void main(String[] args) throws Exception {
15
System.out.println("Project 5");
16
System.out.println("Hash Tables");
17
System.out.println();
18

19
File file = null;
20

21
/*
22
* Prompt the user for an existing file and
23
* store it in a variable.
24
*/
25
do {
26
System.out.print("Enter the file name: ");
27

28
file = new File(SCANNER.nextLine());
29
} while (!file.exists());
30

31
/*
32
* Read the file into a list of Strings.
33
*/
34
List<String> lines = Files.readAllLines(file.toPath());
35

36
/*
37
* Add the entries to the hash table.
38
*/
39
for (int i = 1; i < lines.size(); i++) {
40
String[] information = lines.get(i).split(",");
41

42
HASH_TABLE.insert(information[0], Integer.parseInt(information[3]));
43
}
44

45
System.out.println("\nThere were " + (lines.size() - 1) + " state records read into the hash table.");
46

47
System.out.println("\nHash table content:\n");
48

49
HASH_TABLE.display();
50

51
System.out.println();
52

53
HASH_TABLE.delete("Vermont");
54
HASH_TABLE.delete("California");
55
HASH_TABLE.delete("South Carolina");
56

57
System.out.println();
58

59
HASH_TABLE.find("Florida");
60
HASH_TABLE.find("Rhode Island");
61
HASH_TABLE.find("California");
62

63
System.out.println();
64

65
HASH_TABLE.delete("Kentucky");
66
HASH_TABLE.delete("Minnesota");
67
HASH_TABLE.delete("West Virginia");
68

69
System.out.println();
70

71
HASH_TABLE.display();
72

73
System.out.println();
74

75
HASH_TABLE.printFreeAndCollisions();
76
}
77

78
}]
And here is the .csv file
[State,Capital,Abbreviation,Population,Region,US House Seats
Alabama,Montgomery,AL,4833722,South,7
Alaska,Juno,AK,735132,West,1
Arizona,Phoenix,AZ,6626624,Southwest,9
Arkansas,Little Rock,AR,2959373,South,4
California,Sacramento,CA,38332521,West,53
Colorado,Denver,CO,5268367,West,7
Connecticut,Hartford,CT,3596080,New England,5
Delaware,Dover,DE,925749,Middle Atlantic,1
Florida,Tallahassee,FL,19552860,South,27
Georgia,Atlanta,GA,9992167,South,14
Hawaii,Honolulu,HI,1404054,West,2
Idaho,Boise,ID,1612136,West,2
Illinois,Springfield,IL,12882135,Midwest,18
Indiana,Indianapolis,IN,6570902,Midwest,9
Iowa,Des Moines,IA,3090416,Midwest,4
Kansas,Topeka,KS,2893957,Midwest,4
Kentucky,Frankfort,KY,4395295,South,6
Louisiana,Baton Rouge,LA,4625470,South,6
Maine,Augusta,ME,1328302,New England,2
Maryland,Annapolis,MD,5928814,Middle Atlantic,8
Massachusetts,Boston,MA,6692824,New England,9
Michigan,Lansing,MI,9895622,Midwest,14
Minnesota,St Paul,MN,5420380,Midwest,8
Mississippi,Jackson,MS,2991207,South,4
Missouri,Jefferson City,MO,6044171,Midwest,8
Montana,Helena,MT,1015165,West,1
Nebraska,Lincoln,NE,1868516,Midwest,3
Nevada,Carson City,NV,2790136,West,4
New Hampshire,Concord,NH,1323459,New England,2
New Jersey,Trenton,NJ,8899339,Middle Atlantic,12
New Mexico,Santa Fe,NM,2085287,Southwest,3
New York,Albany,NY,19651127,Middle Atlantic,27
North Carolina,Raleigh,NC,9848060,South,13
North Dakota,Bismarck,ND,723393,Midwest,1
Ohio,Columbus,OH,11570808,Midwest,16
Oklahoma,Oklahoma City,OK,3850568,Southwest,5
Oregon,Salem,OR,3930065,West,5
Pennsylvania,Harrisburg,PA,12773801,Middle Atlantic,18
Rhode Island,Providence,RI,1051511,New England,2
South Carolina,Columbia,SC,4774839,South,7
South Dakota,Pierre,SD,844877,Midwest,1
Tennessee,Nashville,TN,6495978,South,9
Texas,Austin,TX,26448193,Southwest,36
Utah,Salt Lake City,UT,2900872,West,4
Vermont,Montpelier,VT,626630,New England,1
Virginia,Richmond,VA,8260405,Middle Atlantic,11
Washington,Olympia,WA,6971406,West,10
West Virginia,Charleston,WV,1854304,Middle Atlantic,3
Wisconsin,Madison,WI,5742713,Midwest,8
Wyoming,Cheyenne,WY,582658,West,1]

Is This A Good Question/Topic? 0
  • +

Replies To: Data Structure using OOP Project

#2 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12276
  • View blog
  • Posts: 45,364
  • Joined: 27-December 08

Re: Data Structure using OOP Project

Posted 05 December 2017 - 11:52 AM

You have been around since 2013 and have several posts. You should know the rules. Please do not duplicate post.

Also, when posting code, please use code tags: :code:.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1