CS101 - Lab 7

In this lab you will work on methods. Hopefully the problems will convince you that programming is fun, and methods make it easy!

Always start by thinking of the problem top-down. Split it into logical steps, and write one step at a time. Then compile it, and check if it does what you want. Write your code incrementally! This will save you much trouble.

As usual, work individually, and call me if you need help. You are encouraged to discuss ideas and techniques broadly with other class members, but not specifics. Discussions should be limited to questions that can be asked and answered without using any written medium (e.g. pencil and paper or email). You should at no point look at the screen of your colleagues.

Enjoy!


  1. Bank account.

    In this problem you will write a program to maintain a bank account balance.

    Your program will first describe itself, then ask the user to enter an initial balance (use a method to do this).

    Then the program should display a menu of options and ask the user (repeatedly) to enter a transaction type:

    What would you like to do? 
    1 - deposit
    2 - withdrawal
    3 - balance inquiry
    4 - quit
    
    If the user enters anything else than 1,2,3,4 the program should print:
    This is not a valid option. Try again. 
    What would you like to do? 
    1 - deposit
    2 - withdrawal
    3 - balance inquiry
    4 - quit
    

    If the user presses 1, the program should call a method that performs a deposit. This method will ask the user for the deposit amount and return this value. The program should use this value to update the current value of the balance.

    If the user presses 2, the program should call a method that performs an withdrawal. This method will ask the user for the withdrawal amount and return this value. The program should use this value to update the current value of the balance.

    If the user presses 3, the progam should call a method to display the current balance.

    If the user presses 4, the program should print the final balance, say Good Bye, and quit. Otherwise it should go back to asking the user to enter a transaction type.

    This is it. Go back, read it all over again, and make sure you understand what your program should do.

    Now, you'll need to write your program using methods. Methods encapsulate logical units of code. Of course, the meaning of logical may vary a bit from one logical person to another. For this exercise you will have to design your methods and structure your program so that the it looks as follows:

    public static void  main( String []) {
    
       int option = 0; //some dummy initial value
    
       //this variable holds the account balance
       double balance;  
    
       //describe program to user
       describeProgram(); 
    
       //ask for initial balance; 
       balance = getInitialBalance(); 
    
       while (option != 4) {
    
          //this function should make sure the option is valid
          option = getOption();
          
          if (option == 1) {...};
          if (option == 2) {...};
          if (option == 3) {...};
          if (option == 4) {...};
        }
    } //end of main
    
    To start this program, think what functions do you need to write, and what are their arguments? Note that I am not giving you the exact form of the methods, you will have to come up with it.

    I recommend that you start top-down, by writing the main() method, and the while loop that reads user options. And then add one method at a time, compile it, and check that it works.

  2. Playing Craps

    In this problem you will write a program to play Craps.

    A game of Craps is played as follows. The player rolls the dice once.

    So, in order to "play" a game of Craps you need to generate dice rolls repeatedly until the player wins or loses according to the above rules. Note that a dice rolls should be simulated by generating two random integers from 1 to 6 with equal probability and adding those two numbers. Generating a single random integer from 1 to 12 with equal probabiliy does not give you the same probabilities as a real dice roll. To see this, consider there is only one way to roll a total of 2 with real dice, whereas there are multiple different ways of rolling a 7, so the probabilities of these two rolls are not equal.

    To generate random numbers, use the class Random (you have seen it before). First, you need to include at the top of your program:

    //import  class (type) Random, which provides support for generating random nb
    import java.util.Random;
    
    To generate random numbers you will have to declare a variable of type Random
    Random rand = new Random(); // a random number generator
    
    You can put this declaration in a method, or at the top, visible to all methods. The class Random has a method called nextInt(int n), which returns a random number in the range 0 ..(n-1). Thus, when calling
    rand.nextInt(10)
    
    this will return a random number in the range 0..9 inclusive.

    If you are interested, check out more documentation on Java Random class.

    Design a method that plays a single Craps game, and structure it as follows:

    
    
    public static return-type playSingleCrapsGame(parameter-list) {
        
    	//call a function generateRoll that generates and returns the total value 
    	//of a random dice roll
    
    	//call a function printRoll that prints out this initial value in an 
    	//appropriate way  (see sample output)
    
    	loop (as long as the game has not been won or lost) {
    	    
    		//call generateRoll function to get another dice roll
    		
    		//send this value to printRoll function to print it
    
    		//send the necessary values to a function checkGameStatus that
    		//checks to see if the player has won or lost on the current roll 
    		//and returns a value indicating the current game status
    
    	}//end loop
    
    	//return appropriate value indicating whether the player won or lost
    }
    
    
    The output should look something like this:
    You got 11 and won.
    
    You got 8, 11, 12, 4, 8 and made your point. 
    
    You got 3 and lost.
    
    You got  5, 9, 8, 7 and crapped out. 
    
    

    Your (final) program should allow the user to play as many games of Craps as they want and report the total number of games they played and how many times they won. The user should be able to say 'no', they don't want to play, right away and the program should terminate without playing a single game and print out the appropriate statistics. Design your program so that it looks as follows:

    
    public static void main(String[]) {
        
    	//variable declaration 
    
    	loop (as long as the user want to play) {
    	    
    		//call a function playSingleCrapsGame that plays a single game of craps
    		//and returns a value indicating the outcome (win/lose)
    
    		//use this value to update a counter variable (or variables)  so that 
    		//you can print out the gane statistics when the user is done playing 
    
    		//ask if the user wants to play again and call a function that validates
    		//and returns a legal response getUserResponse()
    
    	}//loop
    
    } //end main
    
    Your output should look something like the following:
    
    Game 1: You got 11 and won!
    
    Do you want to play another game? (y/n) y
    
    Game 2: You got 8, 11, 12, 4, 8 and made your point. 
    
    Do you want to play another game? (y/n) y
    
    Game 3: You got 3 and lost!
    
    Do you want to play another game? (y/n) y
    
    Game 4: You got  5, 9, 8, 7 and crapped out. 
    
    Do you want to play another game? (y/n) n
    
    You played 4 games and won 2 of them. 
    
    
    Comments: This program is more complex (and more fun!) than anything you have seen. To make it even harder, you are not given the exact structure of the methods, you will have to come up with it. I recommend the following strategy: Start by writing the playSingleCrapsGame() method. Then test it. Once it works, add the loop that allows the user to play multiple times. Then add the statistics.


What to turn in: