Arrays - Hedgehogs


Classwork

Part A

Create a project and add a class with a main.

☑Create a class Hedgehog, which has instance variables for name and number of quills.  Add the standard methods.  Do not allow negative quills.

☑Create a class HogList. Give it  an array of Hedgehogs, hogs, and a firstEmpty.  In the default constructor, set up hogs to be length 10.

☑Give HogList an add method that takes a Hedgehog and adds it to hogs, using firstEmpty to keep track.  If there is no more room in the array, create a new array that is twice the size of the previous one, copy all the Hedgehogs over from the old array to the new one, and then make hogs point at this new array, before finishing the rest of the add method. 

So the first time this happens, if firstEmpty is already at 10 (of course we wouldn't use the literal number...), we make a new array of size 20, copy over the first 10 hedgehogs, and then add the 11th hedgehog in the new array.

☑Set up toString  for HogList so that it shows only the added Hedgehogs, numbered starting at 1.

☑In your main, create a HogList and then use a loop to add 42 Hedgehogs with number of quills in order from 1 to 42, and make sure your adding and toString works.

Part B

☑ Add to Hoglist a method swap, which takes two indices.  If they are not legal for the array or are not currently in use for Hedgehogs, just print an error.  Otherwise, swap the positions of the two Hedgehogs in the array.  So if we had {A, B, C, D, E} and did swap(1, 3) we would end up with {A, D, C, B, E}

☑ Also add a method checkOrder, which returns true if the Hedgehogs in hogs are in non-decreasing order by quills (so {1, 3, 3, 3, 5, 7, 9} is okay { 1, 3, 4, 3, 3, 9, 7} is not), and false otherwise.  (If there are no Hedgehogs added, or only 1, it counts as being in order)

(hint: if you find two that are out of order, you know it has failed immediately, but if two are in order, you still have to keep checking until you get to the end.)

Part C

☑ In main, create another HogList and this time add 10 Hedgehogs, each with random numbers of quills from 1 to 50, and add them to the HogList. 

Then tell the user "Welcome to the Hedgehog ordering game!"

☑ Until the array is in order, repeatedly show the user the HogList, and ask them to enter two numbers for Hedgehogs to swap (remember to translate these numbers from human numbers to CS indices).  When they get the array into order, congratulate them on winning the game.


[EC] Add a method remove in Hoglist that takes an index. If the index is not valid for an existing Hedgehog, return null. Otherwise, store the chosen hedgehog to be returned at the end of the method. If the hedgehog to remove is the last HedgeHog currently in the array, just move firstempty up by one.  Otherwise, copy the last one added to the array to the place where the one to be removed currently is, and then move firstempty up by one.  (so if the array was {A, B, C, D, E} and we are removing B, it would become {A, E, C, D} and we'd return B). Test in main that this works.