CS 110: Introduction to Computing with Java

Lab 3


Write your answers for the following exercises before coming to lab.   First, you should try to figure out the answer without entering and running the code.  Then, you can use Dr Java to enter and run the code to check your answers.

1.  What happens when the following main method runs with key board input of:


5 25.0 alphabet


import java.util.Scanner;


public class Junk


 public static void main(String [] args)


   Scanner scan = new Scanner(System.in);


   int firstToken = scan.nextInt();

   double secondToken = scan.nextDouble();

   System.out.println("Answer is: " + secondToken / firstToken);

   int lastToken = scan.nextInt();




2.  What do you think is wrong with the following code fragment?  Explain your answer.


  if (i > MAXIMUM)

      System.out.println(“Too Large”);

  if (i < MINIMUM)

      System.out.println(“Too Small”);

  System.out.println(“Just Right”);


3.  What is wrong with the following code fragment?  Explain your answer.


  if (i < 12 && i > 53)

    System.out.println(“Value is between 12 and 53”);


4.  What is printed by the following code fragment?


     double a = 1.2, b = 2.4, c = 1.5, d = 3.3;

     System.out.println(Math.max(Math.min(a,b), Math.min(c, d)));

     System.out.println((a > b)?  c : d);

You will not be allowed to work on the lab if you have not already completed these tasks.



The computer and video game industry is huge, with some estimates at $20 Billion in sales every year.  Games are used for entertainment, education, and job training.  There even are universities dedicated to teaching computer game design and implementation.

In this lab we are exploring a simple game called NIM.  There are many variants of the game, but they all share the same basic structure.  (My favorite is the Fruit Game.)Two players take turns picking up objects.  The player who picks up the last object is considered either the winner or loser depending on the variant of the game you are playing.   NIM is an old game whose history is unclear, though the name is thought to come from the German word nimm meaning take. 

We are going to build a very simple version of the NIM game with the following rules:

·                      The game begins with 21 sticks in a pile.

·                      Each player can pick up 1, 2, or 3 sticks.

·                      The player who picks up the last stick loses.


This lab gives you some experience writing conditional statements, another skill that you will use again in programs that you write.


We have provided for you most of the code to play the game (Lab3.zip).  You should download, unzip this file, and on your removable media and create a Dr Java project for it as for Lab 1.  Starting with this class, your job is to complete the following three methods:

·                      boolean validMove(int numSticks) – returns true if it is valid to take the specified number of sticks.

·                      boolean gameOver() – returns true if the game is over (i.e. there are no more sticks).

·                      String otherPlayer(String p) – returns the identifier of the other player.  If p is “A”, this method returns “B”.  If p is “B”, this method returns “A”.

The instance variable sticksLeft contains the number of sticks left in the pile.

Sample Output

The following shows one iteration of the game:

Before you leave, have your TA check off that you completed the lab.  Make sure each person saves a copy of your work.

Lab Report

Write a document describing your experiences.   Your lab must be printed (not handwritten). 

Answer the following questions related to what you did in this week’s lab.  You may complete the code on your own, but the TA must certify that most of your work was done in the lab.

1.       Explain the Java language difference in syntax and semantics between the call to the welcome method and the call to the start method in main().

2.       There are two instance variables in the SimpleNIM class (sticksLeft and player).  What do these variables mean?  How does the value of these variables change? 

3.       Describe what happens in Dr Java when you select the menu Tools -> Preview Javadoc for Current Document for the SimpleNIM class.  What is produced?  Where does the content come from?

4.       Describe what you learned doing this lab.  Explain what was difficult and what was easy.

5.       Attach a listing of your completed SimpleNIM class.

Note:  You should work alone on the lab report.

Note:  The assignment is due at the BEGINNING of your next lab.  No late assignments will be accepted.  Emailed assignments will not be accepted.  If you are not going to be in lab on the due date, you can turn the assignment ahead of time to the CS110 TA box in the CS department office.