Arrays exercises

COM 1201 – Spring 2003 – Jeff Raab

Introduction

In this exercise set you will write code that uses Java arrays, and that implements linear structures built from arrays.  You must follow the course guidelines for type- or handwritten answers, diagrams, and writing code, as posted on the course website.

Written exercises

1.         Perform exercise 1 on pages 176 and 177 of the textbook.

2.         Perform exercise 2 on page 178 of the textbook.

3.         Perform exercise 3 on page 178 and 179 of the textbook.

Programming exercises

BingoNumbers

# boolean[] called

+ BingoNumbers()

+ void call(int num)

+ boolean isCalled(int num)

+ void newGame()

4.         Define the class named BingoNumbers based on the UML specification on the right.  This class maintains which numbers have been called in a game of Bingo, and represents that information using a bit array.  Each index in the array of boolean values represents whether or not the index has been called as a number during the course of the game.

            The data definition for this class must be a protected array of boolean values named called.

            The constructor must initialize the array so that it can keep track of bingo numbers in the range [1, 75].

            The method call must take a number as input and represent the fact that it has been called.  The preconditions for this method are that the given number is in the range [1, 75] and that the given number has not already been called.  If either of these preconditions is violated, you must throw a RuntimeException with an error message describing what went wrong.

            The method isCalled must take a number as input and return whether or not it has been called.  The precondition for this method is that the given number is in the range [1, 75].  If the precondition is violated you must throw a RuntimeException with an error message describing what went wrong.

            The method newGame must set the representation to reflect that none of the numbers in the range [1, 75] have been called.

            Add tests to the ArrayTests class that thoroughly test each of the methods of this class, in all cases you can identify.

            Run the application and click the RunBingo button to see your class in action.  For more information about the game of bingo, search the web for “bingo rules”.

GamingWheel

# Prize[] prizes

+ GamingWheel(int[] values)

+ void spin(int num)

void rotateRight()

+ Prize currentPrize()

+ int prizeCount()

5.         Define the class named GamingWheel based on the UML specification on the right.  This class maintains what prizes are available on a gaming wheel similar to that used on the television show Wheel Of Fortune and shown below.

wheel.jpg (38908 bytes)In the real world, a player would physically spin this circular wheel.  One of the prizes listed on the wheel would be selected by a fixed arrow at the top of the wheel.  The GamingWheel class roughly models this behavior through its data definition and its available methods.

The data definition for this class must be a protected array of Prize objects named prizes.  By default this reference should be set to null.

The constructor must take an array of int values as input and initialize the array of Prize objects to an array of the same length as the given array.  Once the array of Prize objects has been created, a new Prize object must be assigned to each location in that array.  Use the given array of int values as the dollar value passed to the constructor of a Prize.  If the given array of values is null, or has a length less than 2, you must throw a RuntimeException with an error message that describes what went wrong.

The spin method must “spin the wheel” as needed for the purposes of the game.  The effect of this must be that the Prize object previously at each index i in the array of prizes is moved to index i + num, where num is the input to the method.  If i + num is not in the range of valid indices in the array of prizes, you must “wrap around” to the index i + num – prizes.length to impart a circular effect on the array.  To simplify this process, first write the helper method rotateRight.

The rotateRight method is a helper method to make the spin method easier to understand.  Since it is a helper method, useful only to this class, it must be made private.  This method must move each Prize in the array from its index i to the index i + 1.  The Prize at index prizes.length – 1 must move to index 0.  In other words, if the array contained 3 objects in the order { A, B, C }, then after a call to rotateRight, the order would be { C, A, B }.  Another call would make it { B, C, A }, and so on.

Once you have written the rotateRight method, finish writing the spin method so that it uses the rotateRight method.

The currentPrize method must return the Prize that is currently “selected by the fixed arrow at the top of the wheel.”  Consider the top of the wheel to be index 0 in the array of prizes.

Finally, the prizeCount method must return the number of prizes in the array.

            Add tests to the ArrayTests class that thoroughly test each of the methods of this class, in all cases you can identify.

Run the application and click the RunGamingWheel button to see your class in action.  For an explanation of the game played in the application, browse:

http://www.casinocity.com/rule/wheel_of_fortune.htm

6.         Write a class named DynamicArrayVector that implements the given IVector interface and uses an array to hold its elements.  When the add or insertAtRank method is called on an object of your class, and its array is full, your class must grow the array to twice its length.  When the removeAtRank method is called on an object of your class, and the removal of the element causes the size of the vector to become less than 1/3 of the length of the array, your class must shrink the array to 1/2 its length.  You must not shrink the array under any circumstances if its length is less than 4.

            You must create protected methods in your class that return whether or not the vector is in a state that indicates it should grow or shrink.  The first of these methods must return true if the array is full, or false otherwise.  The second of these methods must return true if the array has a length greater than 4 and is less than 1/3 full, or false otherwise.  Use these helper methods in the add, insertAtRank, and removeAtRank methods.

            You must create private methods in your class that do the work of growing and shrinking the array.  The first of these methods must replace the array with a new array of twice the length, and copy all of the elements to this new array.  The second of these methods must replace the array with a new array of half the length, and copy all of the elements to this new array.  Use these helper methods in add, insertAtRank, and removeAtRank.

            Add tests to the ArrayTests class that thoroughly test each of the methods of this class, in all cases you can identify.

7.         Modify the createVector method in the VectorFactory class so that it produces objects of type DynamicArrayVector, rather than of type JavaVector.  Run the application and click the RunAudioPlayer button to see your class in action.

Submission

You must submit the following written and/or printed material:

           Type- or handwritten answers to written problems

           Printouts of classes and tests you wrote for programming problems

 

You must submit the following electronic material:

           A folder containing all of the provided files in the exercise

           Classes and tests you wrote for programming problems

 

Details for electronic submission are given on the course website.