Assignment FAQ Development Guide Code Checklist |
You should use this guide to help you to complete your assignment. You should follow it step by step, resolving errors before going on to the next step.
Once you can follow this guide without errors, you should come up with more test cases and compare the output of your program with that of the solution.
Before we get started, make sure you are comfortable with using the jdb commands from previous homeworks as we will use them in this development guide as well.
$ make Driver
javac -g Driver.java
./HashTable.java:83: error: missing return statement
}
^
./HashTable.java:99: error: missing return statement
}
^
./HashTable.java:104: error: missing return statement
}
^
./HashTable.java:109: error: missing return statement
}
^
4 errors
make: *** [Driver.class] Error 1
Get rid of these errors by adding return statements to the incomplete non-void methods in HashTable.java. Make sure that your program compiles without any errors.
$ ./Driver
Initial Symbol Table:
Hash Table 1:
size is 5 elements, occupancy is 0 elements.
Please enter a command:
(c)heck memory, (i)nsert, (l)ookup, (o)ccupancy, (w)rite:
Note: All the hashing, (assigning to initial location, increment, and the loop of going to the next location) goes here. The method stops when it finds an empty index, finds the same item at an index, finds an occupied index that is less than the current element, or if you exhaust the table.
Note: This method should leave index, which is a instance field of Hash Table that all methods have access to, where it left off so that Insert and Lookup know that location.
Note: This method has no loops. Bumping is handled via recursion. Duplicate insertion of an item at its last probe location in a full list is a valid test case (but don't test that case now).
When you are done, you should expect insert to be printed correctly.
$ \jdb -attach 80[xx]
Replace xx by the value that you copied from your first screen.
...
First, we will set a breakpoint in the locate method.
main[1] stop in HashTable.locate
Deferring breakpoint HashTable.locate.
It will be set after the class is loaded.
main[1] run
Now, in your first screen you should insert Aa with student number 1 to the Hash Table. Switch back to your second screen and continue running jdb.
main[1] next
Execute next as many times as needed until you execute the line that saves the hash code of the parameter element
main[1] print hashValue <replace hashValue by the variable you used to save the hash code of element>
hashValue = 162
162 is the ASCII sum for Aa. If this is not correct, make sure you implemented your UCSDStudent hashCode method correctly!
main[1] next
Execute next as many times as needed until you set index using the formula discussed in class.
main[1] print index
index = 2
If index is not equal to 2, you made a mistake in your algorithm. In that case, go back and debug your locate before you continue.
main[1] next
Execute next as many times as needed until you return from locate.
At this point you enter the insert method. Continue entering next until
you finally insert element to the table.
main[1] print table[index]
table[index] = "name: Aa Studentnum: 1 "
If you get different results, go back and debug your insert before moving on.
Once done, continue using jdb to insert the following: name: Bb Studentnum: 2 and name: Cc Studentnum: 3. Once you finish, your output should match the following when you write the Hash Table:
The Symbol Table contains:
Hash Table 1:
size is 5 elements, occupancy is 3 elements.
at index 1: name: Cc with studentNum: 3
at index 2: name: Aa with studentNum: 1
at index 4: name: Bb with studentNum: 2
$ ./Driver
Initial Symbol Table:
Hash Table 1:
size is 5 elements, occupancy is 0 elements.
Please enter a command:
(c)heck memory, (i)nsert, (l)ookup, (o)ccupancy, (w)rite: i
Please enter UCSD Student name to insert: Aa
Please enter UCSD Student number: 1
Please enter a command:
(c)heck memory, (i)nsert, (l)ookup, (o)ccupancy, (w)rite: i
Please enter UCSD Student name to insert: Bb
Please enter UCSD Student number: 2
Please enter a command:
(c)heck memory, (i)nsert, (l)ookup, (o)ccupancy, (w)rite: i
Please enter UCSD Student name to insert: Cc
Please enter UCSD Student number: 3
Please enter a command:
(c)heck memory, (i)nsert, (l)ookup, (o)ccupancy, (w)rite: w
The Symbol Table contains:
Hash Table 1:
size is 5 elements, occupancy is 3 elements.
at index 1: name: Cc with studentNum: 3
at index 2: name: Aa with studentNum: 1
at index 4: name: Bb with studentNum: 2
$ jdb Driver
Now in a second terminal, run the following
$ \jdb -attach 80[xx]
Replace xx by the value that you copied from your first screen.
Set a break point in the lookup method by typing the following
main[1] stop in HashTable.lookup
Deferring breakpoint HashTable.lookup.
It will be set after the class is loaded.
Execute the program by
main[1] run
Now in the first window enter the choice like below
Listening for transport dt_socket at address: 80xx
Initial Symbol Table:
Hash Table 1:
size is 5 elements, occupancy is 0 elements.
Please enter a command:
(c)heck memory, (i)nsert, (l)ookup, (o)ccupancy, (w)rite:i
Please enter UCSD Student name to insert: Sravya
Please enter UCSD Student number: 125
Please enter a command:
(c)heck memory, (i)nsert, (l)ookup, (o)ccupancy, (w)rite:i
Please enter UCSD Student name to insert: Tiffany
Please enter UCSD Student number: 224
Please enter a command:
(c)heck memory, (i)nsert, (l)ookup, (o)ccupancy, (w)rite:i
Please enter UCSD Student name to insert: Yixuan
Please enter UCSD Student number: 323
Please enter a command:
(c)heck memory, (i)nsert, (l)ookup, (o)ccupancy, (w)rite:l
Please enter UCSD Student name to insert: Sravya
In the second terminal you should see something similar as below
Breakpoint hit: "thread=main", HashTable.lookup(), line=228 bci=0
228 if (debug)
Keep entering next in the terminal until you get to call locate check and see whether it
works as intended or not. Once you get to the locate call you can enter step which will allow
you to enter locate and see the details of the method through your terminal. Make sure to check
each index your program check and do hand calculation to see if the program is looking at the
correct index given the probe sequence and size of the table or not. This way you could point out
the issue. Also check whether your program is going beyond the elements that it should check and
not returning the correct index. You could also use list command that will show you which
line of code will be executing next.
233 Your code to call locate and find the index for the student
main[1] step
>
Step completed: "thread=main", HashTable.locate(), line=157 bci=0
157 if (debug)
Keep going next untill you initialize the HashValue of the student.
Once you are in locate you could enter locals at any point which will list the local variables
and arguments of your method
main[1] locals
Method arguments:
element = instance of UCSDStudent(id=569)
Local variables:
hashValue = 630
This way you could keep track of the hash value or any other local variable you have. You could also use print or dump commands to check the values of instances or behavior of classes. Let's check...
main[1] print element
element = name: Sravya Studentnum: 0
Notice how the student number is 0 that's because you have created a new base instance when user
asked the program to look up student Tiffany
main[1] dump element
element = {
name: Sravya
Studentnum: 0
}
Enter next and execute the program until you have assigned index as well, now check the locals again,
are they correct, do you see any discrepancy from what you expected those values to be?
main[1] locals
Method arguments:
element = instance of UCSDStudent(id=569)
Local variables:
hashValue = 630
original = 0
increment = 3
now dump the table[index] to see what is the element in that position
main[1] dump table[index]
table[index] = {
name: Sravya
Studentnum: 125
}
Awesome the table contains the student and your program should correctly return the proper result.
Keep executing until locate and lookup both return and check whether your program prints the
correct output or not in your first terminal
Student found!!!
name: Sravya Studentnum: 125
Please enter a command:
(c)heck memory, (i)nsert, (l)ookup, (o)ccupancy, (w)rite:
Now test it with a student name that doesn't exist and see the behavior. You should check the table and each index that go through by printing them or dumping the table and checking locals.
If you see the program is stuck and not asking for you prompt ensure to enter cont in the second terminal until you see the prompt in the first terminal.
Please enter a command: ((i)nsert, (l)ookup, (w)rite): l
Please enter UCSD Student name to lookup: Nick
Keep execuating until you are in your locate and all the local variables are initialized,
you should see something similar to below
main[1] locals
Method arguments:
element = instance of UCSDStudent(id=581)
Local variables:
hashValue = 389
original = 4
increment = 2
After you initialized your index,
main[1] print index
index = 4
main[1] print table[index]
table[index] = null
So you can see that table value at index is null which means the student doesn't exist and locate
should return false and lookup should handle the returning result to tell the Driver that student
doesn't exist
Student Nick not there!
Please enter a command:
(c)heck memory, (i)nsert, (l)ookup, (o)ccupancy, (w)rite:
$ ./Driver -x
[Hash Table 1 - Allocated]
------------ TRACKED MEMORY ------------
80 bytes of heap memory, created in Driver calling HashTable Ctor for the HashTable object.
Initial Symbol Table:
Hash Table 1:
size is 5 elements, occupancy is 0 elements.
Please enter a command:
(c)heck memory, (i)nsert, (l)ookup, (o)ccupancy, (w)rite: i
Please enter UCSD Student name to insert: name
Please enter UCSD Student number: 123
[Hash Table 1 - Inserting name]
[Hash Table 1 - Locate]
[Processing name]
[Hash Value Is 417]
[Trying Index 2]
[Hash Table 1 - Found Empty Spot]
Please enter a command:
(c)heck memory, (i)nsert, (l)ookup, (o)ccupancy, (w)rite: o
The occupancy of the hash table is 1
Please enter a command:
(c)heck memory, (i)nsert, (l)ookup, (o)ccupancy, (w)rite: i
Please enter UCSD Student name to insert: amen
Please enter UCSD Student number: 234
[Hash Table 1 - Inserting amen]
[Hash Table 1 - Locate]
[Processing amen]
[Hash Value Is 417]
[Trying Index 2]
[Hash Table 1 - Comparing amen and name]
[Trying Index 4]
[Hash Table 1 - Found Empty Spot]
Please enter a command:
(c)heck memory, (i)nsert, (l)ookup, (o)ccupancy, (w)rite: i
Please enter UCSD Student name to insert: mean
Please enter UCSD Student number: 345
[Hash Table 1 - Inserting mean]
[Hash Table 1 - Locate]
[Processing mean]
[Hash Value Is 417]
[Trying Index 2]
[Hash Table 1 - Comparing mean and name]
[Trying Index 4]
[Hash Table 1 - Comparing mean and amen]
[Bumping To Next Location...]
[Hash Table 1 - Inserting amen]
[Hash Table 1 - Locate]
[Processing amen]
[Hash Value Is 417]
[Trying Index 1]
[Hash Table 1 - Found Empty Spot]
Please enter a command:
(c)heck memory, (i)nsert, (l)ookup, (o)ccupancy, (w)rite: o
The occupancy of the hash table is 3
Please enter a command:
(c)heck memory, (i)nsert, (l)ookup, (o)ccupancy, (w)rite: w
------------ TRACKED MEMORY ------------
80 bytes of heap memory, created in Driver calling HashTable Ctor for the HashTable object.
24 bytes of heap memory, created in Driver calling UCSDStudent Ctor for the UCSDStudent object.
24 bytes of heap memory, created in Driver calling UCSDStudent Ctor for the UCSDStudent object.
24 bytes of heap memory, created in Driver calling UCSDStudent Ctor for the UCSDStudent object.
The Symbol Table contains:
Hash Table 1:
size is 5 elements, occupancy is 3 elements.
at index 1: name: amen with studentNum: 234
at index 2: name: name with studentNum: 123
at index 4: name: mean with studentNum: 345
Please enter a command:
(c)heck memory, (i)nsert, (l)ookup, (o)ccupancy, (w)rite: l
Please enter UCSD Student name to lookup: Tracker
[Hash Table 1 - Lookup]
[Hash Table 1 - Locate]
[Processing Tracker]
[Hash Value Is 716]
[Trying Index 1]
[Hash Table 1 - Comparing Tracker and amen]
[Trying Index 2]
[Hash Table 1 - Comparing Tracker and name]
[Trying Index 3]
Student Tracker not there!
Please enter a command:
(c)heck memory, (i)nsert, (l)ookup, (o)ccupancy, (w)rite: i
Please enter UCSD Student name to insert: Tracker
Please enter UCSD Student number: 1
[Hash Table 1 - Inserting Tracker]
[Hash Table 1 - Locate]
[Processing Tracker]
[Hash Value Is 716]
[Trying Index 1]
[Hash Table 1 - Comparing Tracker and amen]
[Trying Index 2]
[Hash Table 1 - Comparing Tracker and name]
[Trying Index 3]
[Hash Table 1 - Found Empty Spot]
Please enter a command:
(c)heck memory, (i)nsert, (l)ookup, (o)ccupancy, (w)rite:
------------ TRACKED MEMORY ------------
80 bytes of heap memory, created in Driver calling HashTable Ctor for the HashTable object.
24 bytes of heap memory, created in Driver calling UCSDStudent Ctor for the UCSDStudent object.
24 bytes of heap memory, created in Driver calling UCSDStudent Ctor for the UCSDStudent object.
24 bytes of heap memory, created in Driver calling UCSDStudent Ctor for the UCSDStudent object.
24 bytes of heap memory, created in Driver calling UCSDStudent Ctor for the UCSDStudent object.
24 bytes of heap memory, created in Driver calling UCSDStudent Ctor for the UCSDStudent object.
Final Symbol Table:
Hash Table 1:
size is 5 elements, occupancy is 4 elements.
at index 1: name: amen with studentNum: 234
at index 2: name: name with studentNum: 123
at index 3: name: Tracker with studentNum: 1
at index 4: name: mean with studentNum: 345
No memory leaks! All memory has been correctly jettisoned.
% ~/../public/hw6/java/Driver [-x]
% ~/../public/hw6/java/Calc [-x]
Note: In order for your homework to be collected correctly, you must name your files Driver.java and HashTable.java, and the files must be located in hw6/java in your home directory.
Assignment FAQ Development Guide Code Checklist |