Computer Science 120
Introduction to Programming
Spring 2012, Siena College
In today's lab, you will do some practice using ArrayLists and arrays, then you will use arrays to complete a program that plays the children's memory game called Simon.
You may work alone or with a partner on this lab.
In Lab: ArrayList Practice
In this part of the lab, you will model a charge account system using ArrayLists and loops.
To get started, download and extract the files from this zip file into your folder for today's lab.
Open the project that is created in your folder in BlueJ. Use this project to complete the following exercises.
Your task is to complete the ChargeAcct class, an outline of which has been provided. This class is intended to store a list of valid credit cards. You will write methods that will list all cards, find a specified card in the list and remove a specified card from the list.
/** * Look through the card list and determine if the * specified card is in there. * @param cardNum number of card looking for * @return true if card is found * false if card is not found */ public boolean findIt(int cardNum)
Complete the findIt method. To look for a particular number in the list of cards, write a for loop that will visit each of the cards in the list and compare it to the desired number. Do not use the contains method.
There is no reason to continue looking at cards when we have found the card. You can simply return true as soon as you find it. If you get all the way through the loop, you did not find it and you can return false.
Add a few printouts to the main method to test out your findIt method.
/** * Remove the specified card from the card list. It should * print out the list of cards before and after the remove. * @param cardNum the number of the card to be removed * @return true if card is found and removed * false if card is not found */ public boolean removeIt(int cardNum)
Complete the removeIt method by implementing the following steps.
Manange this loop by utilizing the break; statement. This statement will exit the loop and continue on with the code following your loop. Use the break statement combined with a conditional to get the loop to exit as soon as it locates the card. Write the method so that there is only one return statement.
Add some more statements to your main method to test out your removeIt method.
Once you have verified that your program is working, add your name(s) as a comment at the top of the class, demonstrate it for your instructor and submit a printout (no need to include this part in your submission to the hw folder).
In Lab: Array Practice
In this part of the lab, you will work with array definitions and accessing various elements. Create a document (plain text or Word is fine) where you will record your responses.
Examine the code below.
int arrList[]; arrList = new int[15];
Curly bracket notation can be used for initializing an array at declaration time.
int origin[]; origin = {0, 0};
Examine the following code that initializes an array of int values.
int array[] = new int[5]; array[0] = 3; array[1] = 2; array[2] = 4; array[3] = 0; array[4] = 1;
array[0]
array[1 + 1 * 2]
array[array[ 3 ] ]
array[array[1 + 1] - 2 ]
array[array[array[array[1]]]]
Print and submit your document containing your responses to this section's questions.
Programming Task: Completing the Simon Game
Many of you are probably familiar with the electronic toy named "Simon". Simon is a simple solitaire memory game. The toy is composed of a plastic base with four colored plastic buttons on top. Each button has a different color, and a different musical note is associated with each button. The toy "prompts" the player by playing a sequence of randomly chosen notes. As each note is played, the corresponding button is illuminated. The player must then try to play the same "tune" by pressing the appropriate buttons in the correct order. If the player succeeds, the game plays a new sequence identical to the preceding sequence except that one additional note is added to the end. As long as the player can correctly reproduce the sequence played by the machine, the sequences keep getting longer. Once the player makes a mistake, the machine makes an unpleasant noise and restarts the game with a short sequence.
For this part of the laboratory exercise, you will write a Java program to play a simple game like "Simon". Like the original, our game will involve four buttons which the player will have to press in an order determined by the computer. We will keep the graphics simple by placing the four buttons in a 2 by 2 grid.
A working solution for this program will appear below. Click inside the applet to interact with it.
As soon as the buttons are displayed, your program should generate a sequence consisting of a single note/button. It should "play" a sequence by briefly highlighting the buttons that belong to the sequence in order. After a sequence is played, your program should wait while the player tries to repeat the sequence by clicking on the buttons in the appropriate order. If the player repeats the sequence correctly, the program should randomly pick a button to add to the end of the sequence and "test" the player on this new sequence. If the user makes a mistake, the program makes a "razzing" sound and then starts over with a one note sequence.
The bad news is that this is a complex program with several classes that interact with each other. The good news is that your task is limited to the management of the "songs" that are played by the Simon game.
Setting up for Sounds: JFugue
This program has the option to use sounds like a real Simon game. It uses a library called "JFugue" from David Koelle to make this task easier. Much like objectdraw, this library is not part of a standard Java or BlueJ installation, so you will need to add it.
First, visit http://www.jfugue.org/download.html and download the first file in the list (jfugue-4.0.3.jar). Save this to your account and then add it to BlueJ's list of libraries. You can accomplish this by going into BlueJ's preferences, choosing "Libraries", then adding the path to the JFugue library to the list of User Libraries. After doing this, quit and restart BlueJ so it can load the library correctly.
Starter Code
To get started, download and extract the files from this zip file into your folder for today's lab.
Provided Classes
Several classes are provided, most of which you will not need to modify or even look at (unless you wish to do so). The SimonController manages the game, NoisyButton and NoisyButtonListener provide the capability for our big, colorful buttons to be pressable, and the ButtonCollection keeps track of the four buttons and is able to pick one of the buttons it contains randomly.
Classes to be Completed
The Song and SongPlayer classes are where you will need to do your work. A skeleton of each class is provided.
The Song class needs to keep track of the sequence of notes/buttons that makes up the song that the player is trying to match.
The last three methods are used by the SimonController to keep track of whether the song being "entered" by the player trying to match what Simon played is correct. To do this, your Song class will need to keep track of a "current position" which indicates the next note in the song that should be played to continue to match the song.
The SongPlayer class is used to play the current song to the player of the game so he or she has a chance to repeat it back. Since we need to be able to do things like play notes and flash buttons for a given amount of time and pause between the notes, this needs to be an ActiveObject.
The provided code includes all of the instance variables you need and a complete constructor. Your task is to fill in the run method to pause for one second, then play each note in the song. This is done by calling each NoisyButton's flash method. When all of the notes have been played, the run method calls the SimonController's playComplete method to inform the game that it is OK to allow the player to start pressing buttons to try to match the sequence just played.
Fill in these methods. When your Song and SongPlayer classes work properly, you should be able to play Simon. If you have trouble with the sounds or just prefer to work quietly, you can set the PLAY_NOISE constant in the NoisyButton class to false.
You need not comment classes you did not modify, but you should augment the comments in the Song and SongPlayer classes to describe in some detail how they work. You should also make sure you name(s) are in each of those files.
Submitting Your Work
Before 4:00 PM, Friday, April 20, 2012, submit your Java program for grading. There are three things you need to do to complete the submission: (i) place a copy of your Java program into your csis120/hw folder under hw13, (ii) print a copy of your program and hand it to your instructor, and (iii) demonstrate the execution of your program for your instructor.
Don't forget to check your programs for compliance with the Style Guide for CSIS 120 Programs
Grading
This assignment is worth 100 points, which are distributed as follows:
> Feature | Value | Score |
ChargeAcct Program | ||
ArrayList declaration | 3 | |
ArrayList construction | 3 | |
ArrayList insertions | 2 | |
printCards method | 4 | |
findIt method | 4 | |
removeIt method | 4 | |
Testing code in main method | 4 | |
Array Practice | ||
Question Responses | 6 | |
Simon | ||
Documentation | 5 | |
Appropriate variable declarations | 5 | |
Naming conventions | 3 | |
Formatting | 3 | |
Song class array declaration | 5 | |
Song class array construction | 5 | |
Song class add method | 10 | |
Song class play method | 5 | |
Song class restart method | 4 | |
Song class atEnd method | 5 | |
Song class getNextButton method | 10 | |
SongPlayer class run method | 10 | |