CS 111: Fundamentals of Programming 1

Lab 6

The goals are to

Step 1 - Log On

Copy the Lab6 folder from the course folder to your folder. Open the Strategy project in BlueJ. You may want to also open your OneIsZero project from Lab5.

In this lab, the emphasis is not on how well your GUI looks, but on how well your code is written and how well the program works.

Step 2 - scoreFromRoll

You should create a GUI with a single integer output field and a single button. You should have an instance variable for the score, called score. Now you should write a method called scoreFromRoll that has a single int parameter, called scoreValue, representing the current score of a turn. This method should simulate rolling a die, using rollDie, and compute the new score based on scoreValue and the value of the roll. This new score value should be returned by the method. This method should not use any instance variables. When the button is clicked, the current value of the variable score should be passed to scoreFromRoll and the value returned should become the new value of score. This should then be displayed on the GUI.

Step 3 - A type of strategy for OneIsZero

After playing OneIsZero for a few games, you are likely to adopt a strategy of the form: I'll keep rolling until either I get 15 points or else I roll a 1? Here 15 is serving as a limit that you must get before you quit. Wonder what the best limit would be?

You should now add an input field for the limit. Now when the button is clicked, the output field should display the result of simulating the playing of a single turn using the limit given as the strategy limit. So, if I put 18 in the limit field and click the button, the program should automatically keep rolling and adding the score until either it reaches 18 points or rolls a 1. It should display the result. Possible values for output would be 0,18,19,20,21,22,23. At this point, I don't think score is needed as an instance variable; so remove it and make it a local variable for the buttonClicked method. Also, you will need a local variable for the limit the user enters. Add or remove variables, if any, as appropriate. Now you should write a method called turnWithLimit that takes one int parameter, say limitValue, representing the limit and returns an int value for the result of playing a single turn with this limit. This method should not have anything to do with the GUI and should not make use of instance variables. It should make use of the scoreFromRoll method. The buttonClicked method would now simply get the limit from the GUI, pass this value to turnWithLimit and then display to value score that the method returns.

Step 4 - Many Turns

Once you have this working correctly, you should add an input field to your GUI. The second input field is for the number of turns that the strategy should be played. For example, if the limit field has 16 and the number of turns field has 1000, then the program should play 1000 turns using the "Limit 16" strategy. The output field should now be changed to a double field, and is for the average score per turn that we obtain when we play the given strategy for the given number of turns. Of course, the button is to cause this to happen. The program should update the output (average) field only after it has completed the given number of turns. Here you should have a method called manyTurns that has two int parameters and returns a double. The first parameter is the limit to use, say manyLimit, and the second parameter is the number of turns to play using this limit, say manyNum. The method should return the average per turn for playing the given number of turns with the specified limit. You should make use of turnWithLimit within manyTurns. The buttonClicked method will now need a local variable, say numTurns, for the number of turns to simulate. The buttonClicked method should get the two inputs from the GUI, pass them to manyTurns and display the value it gets back.

You should make use of a for-loop with this part of the program.

Step 5. Many limits

At this point you might want to make a copy of your folder for safekeeping in case anything gets messed up in the next part.

For the final version, your GUI should have three input fields (integer), and a text area for output. The first input field should specify a low value for a limit and the second input should specify a high value. The third input field should specify the number of turns to play each limit within the range from the low limit to the high limit. The results should be displayed in the output text area, one limit per row in the output. Each output row should give the limit and the average number of points per turn for that limit. You should practice using the Format class methods here.

For example, if we input 10 and 20 for the low and high limits and 1000 for the number of turns, then the first row of the output text area should have 10 and the average for playing 1000 turns with limit 10, the second row should have 11 and the average for playing 1000 turns with limit 11, etc. up through the final roll with limit 20. Now you should have a method called manyLimits that has three int parameters, representing the inputs, say lowValue, highValue, numValue and no return value. The method should cause all of the simulations to be done and the GUI updated as this is done. This method should make use of manyLimits. Again, you should make use of for-loops. The buttonClicked method should have local variables, say low, high, and turns. It should get these values from the GUI, and simply pass them to manyLimits.

In each part of the lab, you should make use of methods developed in earlier parts. None of the methods should use instance variables. If you would like me or the lab assistant to take a look when you have finished one of the parts, call us over.

Step 6 - Turn in your work

Turn in the worksheet indicating how much you got completed during the lab period.

Turn in a printout of your final program and put your Lab6 folder into your turnin folder.