2 Replies - 1229 Views - Last Post: 09 June 2011 - 07:50 PM Rate Topic: -----

#1 hilts001   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 08-June 11

Implement the calcIndex method in the Hash class. calcIndex must retur

Posted 08 June 2011 - 10:17 PM

Implement the calcIndex method in the Hash class. calcIndex must return an integer value between 0 and k-1, where k is the number of elements in the hash array. calcIndex should throw an IllegalArgumentException if null is passed as an argument.


public int calcIndex(String name) throws IllegalArgumentException
    {
    	
    	
    	if(name == null){
    		throw new IllegalArgumentException();
    	}
    	
    	
   
    	for(int index = 0; index < size; index++){
    		index +=size;
    		
    		return index % size;
    	}
		return 0;
    	
    }


but the test method where it had to display the position number of an actor name, it display nathing intead, here are the test cases
marking calcIndex method of Hash class
Running calcIndex TC 1: checking that calcIndex throws IllegalArgumentException when null arg supplied
calcIndex TC 1 passed
Running calcIndex TC 2: checking that calcIndex returns 0 when supplied actor name is all whitespace
calcIndex TC 2 passed
Running calcIndex TC 3: checking that calcIndex returns 7 when supplied actor name is Sylvester Stallone
**** calcIndex TC 3 failed ****
expected retval: 7 but got 0 instead
calcIndex marking completed

This post has been edited by macosxnerd101: 09 June 2011 - 06:19 AM
Reason for edit:: Please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Implement the calcIndex method in the Hash class. calcIndex must retur

#2 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12315
  • View blog
  • Posts: 45,414
  • Joined: 27-December 08

Re: Implement the calcIndex method in the Hash class. calcIndex must retur

Posted 09 June 2011 - 06:21 AM

Look at this section within your for loop. Initially, index = 0. Then, index = size. So size%size == 0, always. Thus, you are always returning 0. A better question is, what hash algorithm are you supposed to be using for this assignment? The way hashing works is that you should take a value, run it through some function, and get an output. Then, that output will tell you where the input is stored in the collection.
index +=size;
return index % size;


Was This Post Helpful? 0
  • +
  • -

#3 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Implement the calcIndex method in the Hash class. calcIndex must retur

Posted 09 June 2011 - 07:50 PM

    	for(int index = 0; index < size; index++){
    		index +=size;
    		
    		return index % size;
    	}
		return 0;
    	


A for() loop is completly useless if you systematically from it (at the first iteration)
A for() loop is completly useless iuf you set it's index value within it
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1