Help with sequential searching

I have the following code and I'm trying to add a counter in order to count and return the number of occurrences of parameterkey, but I'm not sure how to properly implement it, I know I have to instantiate an array counter, which I did, but I'm not sure where to go from there. Any help greatly appreciated.

/** Searches for key in integer array named arr

//arr is an instance variable of the class and has been

//instantiated and filled with random values.

// @param key value to search for

// @return if key is found, the index of the first element

//in array whose value is key; if key is not found,

//the method returns -1

*/

publicint sequentialSearch(int key )

{

int [] keyCount =newint [1];//Instantiate array counter for key

for(int i = 0; i < arr.length; i++ )

{

animate( i, 0 );

if( arr[i] == key )

return i;

keyCount ++;

return keyCount;

}

return -1;

}

[1590 byte] By [kingrichard2005a] at [2007-11-27 0:09:31]
# 1
You're saying two different things -- do you want to return the index of the keyin the array or the count of occurrences of the key in the array?
DrLaszloJamfa at 2007-7-11 16:09:46 > top of Java-index,Java Essentials,Java Programming...
# 2
I need to return both. Do I need a seperate for loop in order to do that?
kingrichard2005a at 2007-7-11 16:09:46 > top of Java-index,Java Essentials,Java Programming...
# 3
How can a function with return type int return 2 numbers?
DrLaszloJamfa at 2007-7-11 16:09:46 > top of Java-index,Java Essentials,Java Programming...
# 4
I would implement two methods:* one returns the numbers of occurrences* the other, the index of the first occurrence, if any
DrLaszloJamfa at 2007-7-11 16:09:46 > top of Java-index,Java Essentials,Java Programming...
# 5
I'm being asked to modify only this method to count the occurrences of key. I'm not supposed to add a second method. How can I apporach this problem without having to add a second method
kingrichard2005a at 2007-7-11 16:09:46 > top of Java-index,Java Essentials,Java Programming...
# 6
Can you change the return type? An int is one number, not two, unless you are allowed to pack two shorts into an int?!
DrLaszloJamfa at 2007-7-11 16:09:46 > top of Java-index,Java Essentials,Java Programming...
# 7
No, the specific question for this problem in my instruction book is as follows:The sequential search finds only the first occurrence of the parameter key. How would you modify the sequentialSearch method to count the occurrences of key?
kingrichard2005a at 2007-7-11 16:09:46 > top of Java-index,Java Essentials,Java Programming...
# 8

If you return values in an array, you can do this:

public int[] sequentialSearch(int key) {

return new int[]{_search(key), _count(key)};

}

private int _search(int key) {

for(int i = 0; i < arr.length; i++) {

if (arr[i]==key)

return i;

}

return -1;

}

private int _count(int key) {

int keyCount = 0;

for(int x : arr) {

if (x==key)

keyCount++;

}

return keyCount;

}

Even better would be to return some type of pair object, rather than an array.

DrLaszloJamfa at 2007-7-11 16:09:46 > top of Java-index,Java Essentials,Java Programming...
# 9

> The sequential search finds only the first

> occurrence of the parameter key. How would you

> modify the sequentialSearch method to count

> the occurrences of key?

I would read as "return the count of occurrences of key instead of the index of the first occurrence",

but then my first language is Hungarian.

DrLaszloJamfa at 2007-7-11 16:09:46 > top of Java-index,Java Essentials,Java Programming...
# 10

I'm thinking maybe it just wants me to change the method completely to only return the number of occurrences, but my instructor had mentioned that this particular method should perform the search, return the index where it finds a specific value and also the number of times that value occurs.

kingrichard2005a at 2007-7-11 16:09:46 > top of Java-index,Java Essentials,Java Programming...
# 11

private boolean returnSearch = false;

public int sequentialSearch(int key) {

returnSearch = !returnSearch;

return returnSearch ? _search(key) : _count(key);

}

Now calling sequentialSearch alternates initial offset, key count, initial offset, key count...

This is the smelliest code I've written all day, by the way.

DrLaszloJamfa at 2007-7-11 16:09:46 > top of Java-index,Java Essentials,Java Programming...
# 12

Hehe, no doubt, the help is greatly appreaciated though. I am learning new things everyday in this class. I gave it a shot, but still nothing. I'm getting compiler errors as follows:

2 errors found:

File: C:\Documents and Settings\Administrator.HARR\My Documents\My Classes\COMP150\Labs\A08_07\ArrayPractice2.java [line: 83]

Error: cannot find symbol

symbol : method _search(int)

location: class ArrayPractice2

File: C:\Documents and Settings\Administrator.HARR\My Documents\My Classes\COMP150\Labs\A08_07\ArrayPractice2.java [line: 83]

Error: cannot find symbol

symbol : method _count(int)

location: class ArrayPractice2

I have the following code written:

public int sequentialSearch( int key )

{

// Note: To animate the algorithm, put this method call as the

// first statement in your for loop

// animate( i, 0 );

// where i is the index of the current array element

boolean returnSearch = false;

for( int i = 0; i < arr.length; i++ )

{

animate( i, 0 );

if( arr[i] == key )

{

returnSearch = !returnSearch;

return returnSearch ? _search(key) : _count(key);

return i;

}

}

return -1; // replace this statement with your return statement

} // end of sequentialSearch

kingrichard2005a at 2007-7-11 16:09:46 > top of Java-index,Java Essentials,Java Programming...
# 13
It should be a lot simpler, we have not encountered the following form of code:return returnSearch ? _search(key) : _count(key);I'll try to continue working with it, thanks again.
kingrichard2005a at 2007-7-11 16:09:46 > top of Java-index,Java Essentials,Java Programming...
# 14
Cannot find symbol means you forgot to define those methods.
DrLaszloJamfa at 2007-7-11 16:09:46 > top of Java-index,Java Essentials,Java Programming...
# 15
> return returnSearch ? _search(key) : _count(key);That is sometimes referred to as the ternary operator: http://java.sun.com/docs/books/tutorial/java/nutsandbolts/op2.html
DrLaszloJamfa at 2007-7-21 19:39:01 > top of Java-index,Java Essentials,Java Programming...
# 16
I suggest you check with your instructor to find out exactly what is required.I'm sure your function should only return the count.
DrLaszloJamfa at 2007-7-21 19:39:01 > top of Java-index,Java Essentials,Java Programming...