Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Advanced Object-Oriented Programming using Java Assignment 4: Exception Handling and Testing in Java Introduction - This assignment is meant to introduce you to design and

Advanced Object-Oriented Programming using Java Assignment 4: Exception Handling and Testing in Java

Introduction - This assignment is meant to introduce you to design and implementation of exceptions in an object-oriented language. It will also give you experience in testing an object-oriented support class.

You will be designing and implementing a version of the game Nim. Specifically, you will design and implement a NimGame support class that stores all actual information about the state of the game, and detects and throws exceptions when problems occur.

Basic Rules of Nim - Most versions of the game Nim involves 3 rows of sticks. For example:

| | |

| | | | |

| | | | | | |

Players alternate turns, each removing between 1 and 3 sticks from some row of their choice. For example, if player 1 takes 3 sticks from row 2, the board will then look like: | | |

| |

| | | | | | |

Of course, a player cannot take more sticks from a row than there are left. For example, a player cannot remove 3 sticks from row 2 of the above board.

The game continues until some player is forced to take the last stick, leaving none on the board. That player loses. In your application, a human player will alternate moves with a computer player. To simplify things, the computer player will choose a random number of sticks (between 1 and 3) from a random row. Of course, the number of sticks removed must be legal.

The NimGame Support Class - Required Constructors and Methods:

Your NimGame support class is required to have the following constructors and methods:

(1) public NimGame(int[] initialSticks) - This constructor should create a new game with the initial number of sticks in each row set to the corresponding elements of initialSticks.

For example, to create a game with initially 3, 5, and 7 sticks, this would be called as new NimGame(new int[]{3, 5, 7});

(2) public int getRow(int r) - This inspector method returns the current number of sticks in row r. Note that the user will enter a number between 1 and 3, not between 0 and 2 (this is more user friendly).

(3) public void play(int r, int s) - This modifier method should remove s sticks from row r. Again, note that the user will enter a number between 1 and 3 for the row.

It should also do validation on the number of sticks, throwing one of the following exceptions if the number of sticks, returning the following exceptions:

(a) NoSuchRowException if the row is not between 1 and 3.

(b) IllegalSticksException if the number of sticks taken is not between 1 and 3.

(c) NotEnoughSticksException if the number of sticks taken is between 1 and 3, but more than the number of sticks remaining in that row.

(4) public boolean isOver() - This inspector returns true if there are no sticks left in any row, false otherwise.

(5) public void AIMove() - This modifier takes a random number of sticks (between 1 and 3) from a random row, in order to make the computers move.

- Note that this must also do validation, making sure that it does not take more sticks than there are left in a row. A simple way to do this is generate and test: while (not enough sticks in that row) { generate a random row and number of sticks }

(6) public String toString() - This should return the state of the object (that is, the number of sticks in each row) as a string. - Note that it is not used in the visual application, but will be used for the JUnit testing described below.

IMPORTANT DESIGN NOTE: Your NimGame methods will only throw exceptions. They will not be catching any exceptions. All exceptions will be caught within the actionPerformed method of the NimApp class.

Designing Additional Exceptions - The above situations are not the only in which an exception can occur. You are to look over your NimGame class methods and constructors and identify at least two other instances where it would be appropriate for an exception to be thrown. This is the main design exercise of this assignment.

In each case, create an appropriate exception class, and modify the code to throw the exception if it occurs.

The NimApp Application Class - I have provided a simple command line application called NimApp for running your support class. When run (with a working NimGame class), it looks like the following:

image text in transcribed

When the player enters the row and sticks, the application calls the play method and redraws the sticks by calling the getRow method for each row). It then calls the AIMove method to make the computer move, and then redraws the sticks again.

This continues until there are no more sticks (checked by isOver calling the method), at which point whoever made the last move loses:

image text in transcribed

Carefully examine the main method in the application to see how this works, particularly with respect to how the play method is called.

Handling NimGame Exceptions - Note that there is currently no exception handling in this method. You are to modify this code to handle the three exceptions that may be thrown by the play method. In each case, it should pop up an appropriate error message. For example:

image text in transcribed

Also note that if the player makes an illegal move, it must remain their turn. That is, the AIMove method must not be called in that case. Hint: Think carefully about what code should be inside the try block.

Handling Other Exceptions - Aside from the exceptions thrown by the play method, there is one other type of exception that might be thrown in the main loop in this application due to player error. Add code to catch this exception and to display an appropriate message.

- HINT: When you play the game, think about what unusual input a player could try to enter.

Creating JUnit Testing Cases - Finally, you are to create a set of testing tools for your NimGame class using the JUnit testing tool.

To start, right-click the NimGame in NetBeans, and go to Tools Create JUnit Tests. This will set up a file called NimGameTest under the Test Packages folder.

It will generate a test method for each of your methods. To simplify things, you are only required to create tests for the methods:

getRow

play

You can comment out the rest of the tests if you like.

Testing the getRow Method - Since this will be easier, I suggest that you start with this one. Your tester will need to do the following (some of which is already built in):

(1) Construct an instance object of a NimGame.

(2) Call your getRow method for some row to determine whether it returns the correct number of sticks in that row.

Testing the play Method - You are to provide two tests for this method:

(1) Taking a legal number of sticks from a legal row.

(2) Taking sticks from an illegal row.

Test Case for legal play - Since this is a modifier, you will need to make sure the state has changed correctly. More specifically:

(1) Construct an instance object of a NimGame.

(2) Call the play method, with a legal row and number of sticks.

(3) Use your toString method to compare the expected state of the object to the actual state.

Test Case for illegal row - Since your method will be expected to throw an exception if the player chooses an illegal row, the structure of these testing methods will be different:

(1) Construct an instance object of a NimGame.

(2) Inside a try block:

(a) Call the play method for some illegal row.

(b) Call fail (since we would have exited the block if an exception were thrown).

(3) Inside the catch block:

(a) Use your toString method to make sure the state of the game has not changed.

DOCUMENTATION - Be sure to document the exception handling and testing components of your code. Specifically:

(1) Document each method of your NimGame class, particularly the sections related to detecting and throwing exceptions. - NOTE THAT: You are to use Javadoc format for this.

(2) Document the changes you make to the main method related to handling the exceptions thrown by NimGame methods.

(3) Document the two testing methods you create in JUnit, describing what they are testing and how.

Test Results Output-3701 assignments (run) a Which row to remove sticks from: 2 How many sticks to remove: 3 AI makes move Which row to remove sticks from

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Current Trends In Database Technology Edbt 2004 Workshops Edbt 2004 Workshops Phd Datax Pim P2panddb And Clustweb Heraklion Crete Greece March 2004 Revised Selected Papers Lncs 3268

Authors: Wolfgang Lindner ,Marco Mesiti ,Can Turker ,Yannis Tzitzikas ,Athena Vakali

2005th Edition

3540233059, 978-3540233053

More Books

Students also viewed these Databases questions