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.

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.

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.
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.