Site Navigation

Site Search

CS 110: Lab 11

Learning Objectives

After completion of this lab, you should be able to

Work collaboratively as a pair programming team

All labs in CS 110 will be done as pair programming teams. Your partner for today's lab is listed in the table below:

Hebeler 203
Grader: Vinh Tran; Assistant: Daniel Carpenter
Team 1
Aguilar Jr, Jaime
Olivares, Scott
Team 2
Amezcua Gutierrez, Edson
Shearer, Stetson
Team 3
Baird, Owen
Wagster, Nathan
Team 4
Berman, Jake
Smith, Stephanie
Team 5
Coudriet, Blake
Awan, Samara
Team 6
Erickson, Joel
Bloom, Thomas
Team 7
Goeke, Max
Olden, Greg
Team 8
Harris, Alexander
Cuddington, Chris
Team 9
Heflick, Liz
Straub-Walden, Andy
Team 10
McCauley, Rylee
Williamson, Sarah
Team 11
Millard, Ryan
Hastings, Jake
Team 12
Barrett Wright, Matthew
Quayle, Weston
Team 13
Millard, Mike
Hogan, Martin
Team 14
Field, Sarah
Ling, Nathan
Team 15
Akana, Chris
Nash, Chris
Fill in: Hansen, Mitchell

Note: Partners will change every week.

You may wish to review basic pair programming guidelines before you begin.

You should change roles every 10 to 15 minutes.

Write a simple game in Java

Write a simple game in Java called "Same or Different".

First, let the user choose either "Same" or "Different". Then, have the computer randomly flip two coins. If the person choose correctly (Same is either both heads or both tails) the user wins; otherwise the user loses.

Sample Session

Enter your choice (Same or Different): same
Computer flipped heads and heads
YOU WIN

Another Sample Session

Enter your choice (Same or Different): Different
Computer flipped tails and heads
YOU WIN

Another Sample Session

Enter your choice (Same or Different): DIFFERENT
Computer flipped tails and tails
YOU LOSE

Write a Coin class with toString and equals methods

Use a Coin class to simulate the actual coin.

The Coin class needs two instance variables and four instance methods.

import java.util.Random;

public class Coin
{
// Instance variable
   private boolean sideShowing;  //let true be "heads", false be "tails"
   private Random randomNumberGenerator;

// No-Arg Constructor method:
//   initialize the sideShowing to false and instantiate the randomNumberGenerator
   public Coin()
   {
      sideShowing = false;
      randomNumberGenerator = new Random();
   }

// Flip the coin by using the randomNumberGenerator object
   public void flip()
   {
      sideShowing = randomNumberGenerator.nextBoolean();
   }

// Returns true if sideShowing is equal to coin2's sideShowing
   public boolean equals(Coin coin2)
   {
      // TODO: you write this code
   }

// Returns a string (either "Heads" or "Tails") based on the value of sideShowing
   public String toString()
   {
      if (sideShowing == true)
         {
            return "heads";
         }
         else
         {
            return "tails";
         }
   }
}

To simulate a coin flip, use Java's Random class instance method, nextBoolean, to get an approximately random true or false value

   sideShowing = randomNumberGenerator.nextBoolean();

Change Pair Partner Roles

Change pair partner roles. The driver should become the navigator, the navigator should become the driver.

Write a Game class that uses Coin's toString and equals methods

Use a Game class with a main method to implement a game of "Same or Different".

The logic for the game is given as comments in the code below.

import java.util.Scanner;

public class Game
{
   public static void main(String args[])
   {
   // Declare a local variable to hold user input
      String userInput;
   
   // Create a Scanner object to read from the keyboard
      Scanner keyboard = new Scanner (System.in);
     
   // Declare and instantiate two Coin objects
      Coin coin1 = new Coin();
      Coin coin2 = new Coin();
       
   // Prompt for and read the user choice: either same or different
      // TODO: you write this code
     
   // Flip the two coins
      coin1.flip();
      coin2.flip();
       
   // Display the results of the coin flip
   // Hint: use the Coin class toString() method
      // TODO: you write this code
         
   // Display if the user wins or loses
   // If the user entered "same" and the coins are equal OR the user entered
   // "different" and the coins are not equal, they win; otherwise they lose
   // Hint: use Coin class equals() method
      // TODO: you write this code
}

Change Pair Partner Roles

Change pair partner roles. The driver should become the navigator, the navigator should become the driver.

Use a loop to allow re-plays

Modify your main method to allow the user to choose whether they want to play another round (yes or no)
Hint: use a do while loop

Sample Session

Enter your choice (Same or Different): same
Computer flipped tails and heads
YOU LOSE

Would you like to try again (yes or no): yes
Enter your choice (Same or Different): different
Computer flipped tails and heads
YOU WIN

Would you like to try again (yes or no): yes
Enter your choice (Same or Different): same
Computer flipped heads and heads
YOU WIN

Would you like to try again (yes or no): no
Game over

Use running totals to keep track of wins and losses

Modify your main method to keep track of the number of times the winner choose correctly and incorrectly. Displays these values when the user chooses to end playing.


...
You won 2 times
You lost 1 times

To Receive Credit

You are allowed to drop one lab grade for the entire quarter.