CS 110: Introduction to Computing with Java

Lab 6

Pre-Lab

Do the following tasks before coming to lab.  You will not be able to work on the lab if you have not already completed these tasks.

Write a 1-2 page paper answering the following questions:

This lab has to do with the game called Othello (also called Reversi).  Do some web research (e.g. Google) and learn the rules of the game.  For example, you can visit http://en.wikipedia.org/wiki/Reversi which has an explanation of the rules.  You can also play against a computer at http://www.freegames.ws/games/boardgames/othello/othello.htm.   Summarize the rules of the game.

Explain the statement “Testing can only prove the presence of bugs, not their absence.”

Lab

Introduction

As you know from the pre-lab, we are working with a game called Othello.  The makers of Othello have an interesting catchphrase: “A minute to learn, a lifetime to master.”  In this lab, we provide you with a working, text-based version of the game. 

The code we provide is almost complete.  You have to write one (relatively) simple method to make the code work.  However, we will focus on using JUnit, a Java framework for unit testing that is built into Dr Java as well as many other tools.

Purpose

This lab gives you some experience using JUnit to test your classes.

Activities

Copy the Othello project.  A complete set of source files project for the Othello game can be found in Lab6.zip.  Extract these files to folder named Lab6 on the student drive.  As always, open Dr Java and create a project file named Lab6.  Open/save each Java source file in the project and save the project.  Use the menu Project->Compile Project to compile all the files.

Run the project.  Select the file Game.java in the side panel and use the Tools->Run Document’s Main Method Note what happens for your project report.

Look at the inRange method of the Board class.  This is the method that you will be writing.  The existing code is a “Stub” that always provides the return value false.  The method is supposed to verify that a given position on the board is valid.  If row and column are the number of rows and columns on the board, then a position (testRow, testCol) is in range iff:

0 ≤ testRow < row and 0 ≤ testCol < column

Write a test class and a test method for the Board class. Our strategy will be to write the test case first, then write the method.  We will use Dr Java to do this. 

In Dr Java, select menu FileàNew JUnitTest Case

Enter the test class name as TestBoard.

Use Project->Save to save the updated project file.

Edit the default name of the one method in TestBoard from testX to testInRange

Add code to the testInRange method to instantiate a Board object named “cut” (Class under Test) with a row and column range of 4 by 4.

Write some tests in the testInRange method.  Try to think of a complete set of test cases that should provide both true and false return values. Each test should call assertEquals with two arguments: a “should be” value of either true or false and a call to the cut.inRange method with row and column values.  Your TA will help you with this.

Run the tests.  Use the menu Tools->Test Current Document to run your test cases.  They should fail.  If they all pass with the provided stub version of the Board inRange method, then your test cases are not very good.  Add test cases until at least some of them do fail. 

Write the inRange method.  Continue the following two steps until all test cases succeed.

Edit the code in the Board inRange method.

Compile and run the TestBoard / testInRange test cases

Play Othello and enjoy.  Open the lower Interactions window so that you will be able to see the entire game board area.  Select the Game.java file in the sidebar and use menu Tools->Run Document’s Main Method.  If you have implemented and tested your implementation of Board inRange well, the game should work correctly.

Before you leave, have your TA check off that you completed the lab.  Make sure each person saves a copy of your work.  You may complete the code on your own, but the TA must certify that most of your work was done in the lab.

Sample Output

There is no sample output for this assignment.

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. 

What are the advantages of writing the tests first then writing the methods? 

Why is it important to have a stub version of the method that compiles when you write a test?  Explain.

Explain what you see in the Interactions pane when you run tests.  What are some possible outcomes of running tests? 

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

Attach a listing of your completed Board and TestBoard classes. 

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.