Okay here's what I have to do. I have to create a method inside the HashSet program called toString2.
Here is what I did. I created a HashSet with 9 calendardates, January 1 through 9.
HashSet<CalendarDate> test = new HashSet<CalendarDate>();
for (int i = 1; i < 9; i++){
test.add(new CalendarDate(1,i));
}
The output of "toString" is this:
[1/8, 1/2, 1/5, 1/1, 1/6, 1/3, 1/4, 1/7]
It spits them out in the Hash order they get put in, I guess.
Now I basically have to create a program that puts values in the right array index. If I put integers {1,91,71,5,45,7,9} in a Hashset, the program basically creates this:
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
1 5 7 9
91 45
71
Seems straight forward. They are put into indexes by (integer % arraylength) which is 10, so the last digit of the numbers correspond with indexes.
So I have to create a program that produces a similar output with CalendarDate. My teacher said that this is what happens when I input January 1st through 8th.
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
1/6 1/2 1/8 1/7
1/3 1/5 1/4
1/1
So that must be the Hash value of those, when you do the same thing with integers. So here is code I used.
public void toString(){
int foo;
if (!isEmpty()){
for (int i = 0; i < elementData.length; i++){
HashEntry<E> current = elementData[i];
while (current != null){
foo = current.hashCode();
foo = foo % elementData.length;
System.out.print(foo);
current=current.next;
}
}
}
} So it does the exact same thing. Gets the hashcode and then divides it by 10, or the length of the array.
Here is the output:
19829114
I don't know how these correspond. When we run toString, which runs through the 8 dates the same way as , I already said this, but we get this.
[1/8, 1/2, 1/5, 1/1, 1/6, 1/3, 1/4, 1/7]
I thought it would be return 32331188, as that is how my teacher said it would be. I just want to know what I am doing wrong. Thanks! Edit: It should return 32331188 based on this:
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
1/6 1/2 1/8 1/7
1/3 1/5 1/4
1/1