Due by the end of this meeting
hashCode
for that String.
Look at the code and familiarize yourself
with the add method.
We're going to add two more methods
that are very similar.
add method,
named contains and delete.
Revise each to perform its task:
contains to boolean.
contains it should just return true.
For delete it should set the entry in the table to null.
contains return false
if it gets thru the loop to the end.
Delete the line labeled "DELETE THIS LINE" and fix any errors that happen to crop up. Then run the program and see the following extra output:
Note that the program says that "Mark"
is not in the hash table,
even tho' it is clearly visible at location 10.
The problem is
that it was supposed to go into location 7,
but collided with "me".
Now that "me" has been deleted,
your contains method
notices the empty space at 7
and concludes that "Mark" cannot be there.
private static class called Node.
Give it a single String instance variable.
Give it a constructor that sets the value of that IV.
Give it a toString method that returns the value of the IV.
Give it an equals(String) method.
(NOTE: this does not @Override the version from Object!)
Have it return true if the given String
is equals to the Node's IV.
Back in OpenLinear
(not in Node)
create two methods
isOpen
and isUsed
which each take a Node argument.
isOpen is true
if the given Node is null
or if its IV is null.
isUsed is true
if the given Node is not null.
Change the hash table array's base type
from String to Node.
(That needs to happen in main
and in any method
that takes the hash table as an argument.)
Revise the add,
contains
and delete
methods to make them work properly.
add should place the value it's adding
in the first open spot.
Doing so may lead to an error. Try to think what that error would be and how you might avoid it.Hint: What if we tried to add "Mark" again?You do not need to fix the error for this lab, but please add a comment saying what the error is and how it might be avoided.
contains and delete should continue
until they reach an unused spot.
delete should not set the array element to null,
but the IV of that Node.
That will mark the entry as used but open.
Run your program again and confirm that it finds "Mark" in the hash table.
Note also that the search for "me" is longer. Because 7 is used (even tho' it's open) the program has to keep looking at the following locations, just in case "me" was the "bumped" element.
Submit this/these files: