| CPSC 124 | Introduction to Programming | Spring 2008 |
Methods are fundamental building blocks of well-designed programs. This lab gives you a chance to write a few. This lab is largely based on a lab by Stina Bridgeman.
Create a lab06 directory in your cs124 directory to hold the files for this lab.
Copy the file from /classes/s08/cs124/labs/lab06/ to your lab06 directory. This contains two files: TextIO.java and Craps.java.
Here are the exercises for this week's lab, due next Thursday. But don't wait until just before it is due to do it all! You should continue to follow the good programming style rules from lab #2. Remember to indent statements contained in 'if' statements and loops. Also, remember to comment your code and to use reasonable variable names. In addition, in this lab you should add a comment above every method that you write, which describes what the method does, how it is called, what it takes as input, and what it outputs. Note: points will be taken off for poor indentation, uncommented code, or poor variable naming.
Write a subroutine named "stars" that will output a line of stars to standard output. (A star is the character "*".) The number of stars should be given as a parameter to the subroutine. For example, "stars(20)" would output:
********************Next, write a main() routine that reads a non-negative integer N from the user (you will need to do some error checking) and uses the stars subroutine to output N lines of stars with 1 star in the first line, 2 stars in the second line, and so on. For example, if N is 10 the output would be:
* ** *** **** ***** ****** ******* ******** ********* **********
Put both routines (main and stars) in a class called PrintStars and put this class in a file called PrintStars.java.
Write a function called getRandomInt which takes two parameters, low and high, and returns a random integer between 'low' and up to and including 'high'. (e.g. calling this function with 1 for 'low' and 6 for 'high' should result in either 1, 2, 3, 4, 5, or 6) Your function should not perform any input or output. You do not need to check for error conditions such a 'low' being larger than 'high', although you should state your assumptions in the comment that preceeds the method.
Next, write a function called rollDiceUntilSum which computes the number of times you have to roll a pair of dice until a particular sum is achieved. In order to be general-purpose, your function should accomodate dice with any number of sides. Your function should take the target sum and the number of sides for the dice as parameters, and should return the number of rolls it takes to get that sum. Use your random-number function to roll the dice. You do not have to worry about error-checking, such as if the sum is not actually attainable given the dice, although as with the getRandomInt, you should state your assumptions in the comment that preceeds the method.
Finally, write a main program which allows the user to enter a target sum and the number of sides the dice have, and then prints out the number of rolls it took to reach that sum. In main, you should do some error checking to make sure the inputted target sum and number of sides are both positive integers (>0).
Put all three subroutines in a class called RollForSum and put this class in a file called RollForSum.java.
Note: Test each function as you write it! This means writing a small main program which lets you try out each function separately. Don't just write the entire program and then wonder why it doesn't work.
Verify that your lab06 folder contains all of the files you created or modified for this lab (which should include your Craps.java, PrintStars.java, and RollForSum.java), then copy your entire lab06 folder to the handin directory ~mcorliss/handin/cs124/username (where username is replaced with your username).