(On preview: I agree with the comment about using the word key. Key is a terrible term for this, and has a very specific meaning in a data structures context.)
Well, I’m not a Java guy…but basically, let me try to walk you through the concepts.
You see the section where it says
public Boolean find(long searchKey)
{ // find specified value
int j;
...
That’s a method. You need to add a new method, called getMax().
Notice that find takes a long as a parameter, and returns a boolean. Your getMax() won’t need to take any parameters - hence the () instead of the (long …). And it should return the same data type that the array holds. In this case:
class HighArray
{
private long [] a; // ref to array a
it looks like you should return a long.
Now…what does getMax() do? It needs to find the largest value in the array.
The classic way to do this is to define a local variable, initially set to 0. Walk through the entire array, similar to the way find does. At every step, check if the current element is larger than your local variable. If so, reset the local to match the value of the current element. Once you’re done going through the array, your local variable will have the value of the highest element. You can return this value, using the return statement.
Still with me? Ok! You also need to check if the array is empty…this should be done before you walk through the array. If it’s empty, as per the assignment, you return -1.
(You can optimize this a bit, but I wanted to keep it all broken out and seperate for the purpose of this post.)
Last part…adding code to exercise it. Well, calling the function is pretty straight forward. It will be similar to how you call find. You can create a variable of type long, and assign it the return value from getMax. Then use the println function to give some pretty output. You’ll want to call this after the insert calls…or maybe do it more than once, after a few of the inserts and then at the end, to show the change.
Call it once before any inserts, to prove the empty case works.