∨ The programming task — Find your dogs!You have two Yorkshire Terriers, let’s call them Bistol and Poo (to protect their identities). They’re on the run from Deputy Prime-Minister and HiFi store magnate Jarnaby Boyce, and you’ve got to find them before he does!You will search the states of Australia for your beloved doggies. When you see them you’ll try to catch them so that they’re safe. To do this you have to lure them towards you. But they’re playful and will only come the third time you call. If, however, you encounter Jarnaby while looking for them: you’re in trouble buddy! You’ll be so hurt by his rebuke that you will be dazed for two moves — run into him again while you’re dazed and go home Yankee, go home! You can call the dogs while dazed but you will stay dazed; you must move twice to clear your head. But dazed or not, you can only erroneously call out to the puppies three times — if you shout in the wrong direction three times, you will be found by Jarnaby and deported. And who will look after the puppies then Captain? Game over. Indeed.When you are not dazed it is possible for you to have some idea of your location, the states around you, and the location of your dogs (because their continuous annoying yapping gives their location away).The on-line version of this document has a link to some sample output of both a stage 2 and stage 4 implementation of this game.The completed implementation will consist of three files, only one of which must be written by you. These files are:AssigTwo216.java This is the driver program (with a main() method). The code of this is complete and it MUST NOT be changed (unless Stage 4 is reached). The code in this file is very simple — it declares and instantiates an object of thePirateDog class and calls its play() method. (The on-line version of this document has a link to the code.)R2JCD2.java This is a library class and this file contains resources that you are to use in developing your implementation of the task. The code is complete and MUST NOT be changed. (The on-line version of this document has a link to the code.)PirateDog.java This is an organiser class and is the file that you are to write; the on-line version of this document has a link to a ‘skeleton’ version of the code — use this as the starting point for your program.There will be no main() method in the class, it will contain methods that organise the task. This will include all the interactions with the user.You should make sure that you do the following:Create and use object(s) of the R2JCD2 class. You will LOSE MARKS if you write code in PirateDog.java that duplicates things that could be done using methods of the R2JCD2 class.Use the trace() method (provided in the skeleton) to include tracing messages in your program — switch the messages off before submission.Use separate methods to implement the separate activities within the task.Use instance variables to store data that is used or changed by more than one method and/or needs to persist for the life of the object.Use local variables to store the data that is used by just one method.This program (like all programs) should be developed in stages, with each stage fully implemented and tested before the next stage is attempted. To provide you with some guidance about this, the detailed specification below is divided into four stages, you should aim to submit a program that implements at least stage 1.A program that correctly implements stage 1 and provides all the required external documentation, both to a high standard will be able to score a maximum of 48 of the 60 marks. This is 80% — a high distinction. If you have done all of this in plenty of time and want to try for an even higher mark, then you should begin to implement stage 2, and if you have time stages 3 and 4. Note that you will receive NO marks for your attempts to implement later stages if the marker finds that your implementation of earlier stages does not function as specified in this document.VERY IMPORTANT: You must state in the header comments of your source code which stage of implementation your program has reached. If you do not do this then the marker may assume that you have only implemented stage 1 and you may not receive any marks for implementation of any other stages.Details – Stage 1(Note: even within stage 1, you should plan, implement, and test your program in sub-stages. It is part of your task to work these sub-stages out yourself.)Write code in PirateDog.java to do the following:”Housekeeping” tasks — PirateDog()Instantiate the R2JCD2 and Scanner classes.Switch on the debugging/tracing messages in the PirateDog and R2JCD2 classes. (Switch them off before you submit your program for assessment.)Introduction — explain()Provide the user with a general explanation of the game.Play the game — the user is prompted to make “moves” until the game is over.The game will end when any one of the following happens:You try to move to the state occupied by Jarnaby while you are dazed.You call the dogs when they’re not there three times.You call the dogs successfully three times.You choose to quit the game.Before each move you are provided with information. If you are dazed then you are only reminded that you need to find the dogs, otherwise you are told:Your current location in Australia.The numbers of the four connecting states.The number of times you have called the dogs both successfully and unsuccessfully.Any available information about the location of the dogs — i.e. whether you can hear their barking.Any available information about the location of Jarnaby — i.e. whether music from JB’s HiFi can be heard.You will be asked what you want to do for this turn. The options are:Fly into another state (by number).You will be asked to enter the number of the state you want to fly into.If the state is not connected to your current location you will not be able to fly, and should be told so.Otherwise if the state is connected to you current location, you will enter the new state andif the new state doesn’t contain the dogs or Jarnaby the game will continue (with you becoming slightly less dazed if you were dazed)if the new state does contain Jarnaby and you were already dazed then the game is over, otherwise you become dazed and Jarnaby leaves to give a press conference.if the new state does contain the dogs then they will run away.Call into another state (by number).You will be asked to enter the number of the state you want to shout into.If the state is not connected to your current location, you will be told this.If the state is connected and contains the dogs then it is noted that they have been called and the game continues. When this occurs for the third time, the dogs come to you and the game ends, otherwise the dogs run away.If the state is connected but doesn’t contain the dogs then it is noted that you’ve drawn attention to yourself unnecessarily and the game continues. When this occurs three times, the Australian Federal Police will arrest you and deport you and the game ends.Reset the Game.If this option is chosen then many game parameters are reset:your position is changed.the dogs’ position is changed.Jarnaby’s position is changed.your head is cleared (i.e. you are not dazed)the shouting tallies are reset to 0.Quit the game.The game will end.Details – Stage 2RememberYou should only consider implementing this if you have completed and tested stage 1 and have time to spare.You must state in the header comments of your source code that you have implemented stage 2.Extra functionality at this stage:Before playing, you are asked whether or not you want to play, if not, there is no game played.After each game is over, you are asked whether you want to play again.The numbers of the states that the user enters should also be remembered (to a maximum of 20 moves and then the most recent 20 should be remembered). These values (in chronological order) should be displayed at the end of the game. Seesample output for more information.Details – Stage 3RememberYou should only consider implementing this if you have completed and tested stage 2 and have time to spare. There is a lot of extra work in this Stage for very few marks.You must state in the header comments of your source code that you have implemented stage 3.Extra functionality at this stageDesign and build a graphical user interface (GUI) for the output of the program (display of messages after each move, and the final messages). The input (including prompts to the user) will be from the console (using methods of the Scanner class) (as in stages 1 and 2).NOTE: You are not expected to, and should not attempt to write your own code to open, position, arrange, or close the GUI.Write extra code in PirateDog.java (which should now extend QuickGUI)”Housekeeping” tasks — PirateDog()Create the GUI (consisting of Fly, Call, Reset, and Quit buttons, and (perhaps) a read-only text field and two ordinary text fields).Show the information to the player and present them with their choices — paintComponent()Add the functionality of displaying the user’s status on the GUI within a new method: paintComponent().Place calls to repaint() within the program.You may find some Tutorials helpful.Details – Stage 4RememberYou should only consider implementing this if you have completed and tested stage 3 and have time to spare. There is a huge amount of extra work in this Stage for very few marks.You must state in the header comments of your source code that you have implemented stage 4.Extra functionality at this stageMake the graphical user interface interactive, that is all interaction between the user and the program should be through the GUI.NOTE: You are not expected to, and should not attempt to write your own code to open, position, arrange, or close the GUI.Write code in PirateDog.java (which should now also implement ActionListener) to do the following:”Housekeeping” tasks — PirateDog()Ensure ActionEvents will be listened for by the buttons and a text field.Comment out the call to play() in the AssigTwo216.java file.Manage the pressing of buttons and typing in the text field — actionPerformed()If the user has elected to fly, shout, reset, or quitManage this appropriately (similar to the play() method).repaint() the screen.You may find some Tutorials helpful.▷ Planning and Documentation∨ Program StyleThe code you write for this assignment must be a single class called PirateDog, with the code in a file called PirateDog.java. This class should not have a main() method, execution of the code will be initiated from the “driver” program AssigTwo216.java(supplied). It is expected that the code in PirateDog.java will instantiate an R2JCD2 object (code supplied) and will call its methods.Your program should follow the coding conventions introduced in this unit, especially:Variable identifiers should start with a lower case letterfinal variable identifiers should be written all in upper case and declared before non-final variablesAll local variables (with the exception of loop counters in for loops) should be declared at the start of methods and before any other Java statementsEvery if-else statement should have a block of code for both the if part and the else part (if used)Every loop should have a block of codeThe program should use final variables as much as possibleOpening and closing braces of a block should be alignedAll code within a block should be aligned and indented 1 tab stop from the braces marking this blockCommenting:There should be a block of header comment which includes at leastfile name, student name and ID, stage of development reachedEach variable declaration should be commentedThere should be header comments for each method indicating:What the method doesThe purpose of the parameters (if there are any)What the return value (if any) is for∨ Help and hints:Read the specification carefully and make sure that you know what your program needs to do. Work out some pencil and paper examples.Break the problem down into subtasks. For each of these tasksProduce an algorithm before writing any code.When you start to code add only a small amount of code at a time.Compile and run to check that this part is working before proceeding.Test thoroughly to see that the program you have written performs this task correctly.You will waste a lot of your time  if you do not follow a step-by-step method of solving a problem such as this. It is a mistake to think that this step-by-step method is slower than trying to tackle the whole problem at once.  It never is.Once you think you have completed your program, test it thoroughly to ensure it works correctly. Repeat these tests if you make any modifications to your program.DO NOT neglect your tutorial work in the unit to work on the assignment.  Some of the tutorial activities may lead you to see what needs to be done in the assignment.You may seek help with this assignment in normal consultation times for this unit or via email to your lecturer.  Here are some hints to make best use of help.Make sure you know (or think you know) what your problem is.Have details of the work you have done so far and the progress you have made on hand when you seek help.The more specific you can be in your request for help the more immediately useful the help is likely to be.
piratedogtable.doc

r2jcd2table.doc

assigtwo216.java.txt

piratedog.java.txt

r2jcd2.java.txt

Unformatted Attachment Preview

KIT101 Programming Fundamentals
Semester 2, 2016
Assignment 2
Name:
ID:
Instance variables in my PirateDog class
(add a row for each instance variable in your PirateDog.java file)
identifier
type
What is this used for?
Which methods used this variable?
Methods in my PirateDog class
(add a row for each method in your PirateDog.java file)
identifier return parameters what does the
type
method do?
instance variables
used
Development history for this
method
implementation testing of
of code (of this
code (of this
method) date
method) date
coding
testing
completed
completed
KIT101 Programming Fundamentals
Semester 2, 2016
Assignment 2
Name:
ID:
Methods in the R2JCD2 class
(use one row for each method in the R2JCD2.java file)
method
identifier
return
type
parameters instance
variables used
by this method
what does the method do?
/**
* KIT101 Assignment 2
*
* Pirate Dog — Driver class
*
* AssigTwo216.java
*
* @author Julian Dermoudy
* @version September 2016
*
* Notes:
* 1. Students should not change this program unless STAGE 4 is reached
* 2. Students need not submit this program
*/
public class AssigTwo216
{
public static void main(String[] args)
{
PirateDog g=new PirateDog();
g.play(); // Comment out this line if STAGE 4 is reached
}
}
/**
* KIT101 Assignment 2
*
* Pirate Dog — Organiser Class
*
* PirateDog.java
*
* @author ???
* @version ???
*
* Stage Reached: ???
*/
import java.util.Scanner;
public class PirateDog
{
// finals
// non-finals
private boolean tracing;
private R2JCD2 bot;
private Scanner sc;
public PirateDog()
{
}
public void play()
{
}
public void playOne()
{
}
public void explain()
{
}
public void setTracing(boolean onOff)
{
tracing = onOff;
}
}
public void trace(String message)
{
if(tracing)
{
System.out.println(“PirateDog: ” + message);
}
}
/**
* KIT101 Assignment 2
*
* Pirate Dog — Library class
*
* R2JCD2.java
*
* @author Julian Dermoudy
* @version September 2016
*
* Notes:
* 1. Students should not change this program
* 2. Students need not submit this program
*/
import java.util.Random;
public class R2JCD2
{
// final instance variables
public final int CAPTURED = 1;
public final int SUCCESS = 0;
public final int FAILURE = -1;
public final int IMPOSSIBLE = -2;
not allowed
public final int DAZED = -3;
public final int STARTLED = -4;
public final int DEPORTED = -5;
public final int DISTANT_DOGS=2;
state
public final int FAINT_DOGS=4;
public final int DISTANT_JB=3;
state
public final int FAINT_JB=5;
//
//
//
//
dogs captured
operation successful
dogs not present
operation unsuccessful because it was
//
//
//
//
player is dazed
player encounters dogs
player encounters Jarnaby Boyce
dogs are neither here nor an adjacent
// dogs are in an adjacent state
// Jarnaby neither here nor an adjacent
// Jarnaby in an adjacent state
private final String[] STATES={“Northern Territory”,”Canberra”,”New South
Wales”,”Victoria”,”Queensland”,
“South Australia”,”Western Australia”,”Tasmania”,”NZ North Island”,”NZ
South Island”};
private final int MAX_DAZED_MOVES=2;
private final int MAX_CALLING_ATTEMPTS=3;
// turns to take when dazed
// turns to take to capture dogs
private int numStates=9;
// number of states in the vicinity
private int[] east={1,2,0,4,5,3,7,8,6}; // states to east of current
location (index)
private int[] west={2,0,1,5,3,4,8,6,7}; // states to west of current
location (index)
private int[] north={6,7,8,0,1,2,3,4,5}; // states to front of current
location (index)
private int[] south={3,4,5,6,7,8,0,1,2}; // states to rear of current
location (index)
//non-final
private int
private int
private int
private int
private int
instance variables
currentState;
//
dogPos;
//
jarnabyPos;
//
dazedRemaining;
//
callsRemaining;
//
private Random generator;
private boolean tracing;
current location of player
state where the dogs can be found
state where Jarnaby Boyce can be found
number of turns remaining when dazed
number of shouts needed to capture
// to use for random placement in states
// switch for tracing messages
/**
* creates a bot for a Pirate Dog game
* @param traceOnOff whether or not tracing output should be shown
*/
public R2JCD2(boolean traceOnOff)
{
tracing = traceOnOff;
trace(“R2JCD2() starts…”);
generator = new Random();
generator.setSeed(101);
trace(“…R2JCD2() ends”);
}
/**
* provide the identity of the current state
* @return which state player is within
*/
public String getCurrentState()
{
trace(“getCurrentState() starts… …and ends with value ” +
currentState + “, ” + STATES[currentState]);
return currentState + “, ” + STATES[currentState];
}
/**
* provide the identity of the specified state
* @return which state is of a given number
*/
public String getSpecificState(int stateNum)
{
trace(“getSpecificState() starts… …and ends with value ” + stateNum
+ “, ” + STATES[stateNum]);
return stateNum + “, ” + STATES[stateNum];
}
/**
* provide the count of dogs capture attempts
* @return the fraction of capture attempts made
*/
public String getCallCount()
{
String count; // formatted total
trace(“getCallCount() starts…”);
count=””+(MAX_CALLING_ATTEMPTS-callsRemaining)+”/”+MAX_CALLING_ATTEMPTS;
trace(“getCallCount() …ends with value ” + count);
return count;
}
/**
* randomly determines unique player location (currentState), dogs position
(dogPos), and Jarnaby position (jarnabyPos)
* @param male whether or not the dogs to be stalked is male
* @return the starting location (state)
*/
public int newGame()
{
int pos; // player’s position
trace(“newGame() starts…”);
// determine player’s position
pos = generator.nextInt(numStates);
currentState = pos;
System.out.println(“player starts at ” + currentState);
// determine dogs’ position
trace(“calling setDogPosition()”);
setDogPosition();
// determine Jarnaby’s position
trace(“calling setJarnabyPosition()”);
setJarnabyPosition();
// define player’s status
trace(“player is not dazed”);
dazedRemaining=0;
trace(“dogs are free”);
callsRemaining=MAX_CALLING_ATTEMPTS;
trace(“…newGame() ends with value ” + currentState);
return currentState;
}
/**
* randomly determines unique location for dogs (dogPos)
* @return the dogs location (state)
*/
private void setDogPosition()
{
int pos; // dogs position
trace(“setDogPosition() starts…”);
pos = generator.nextInt(numStates);
while (pos == currentState)
{
trace(“clash detected”);
// avoid clash with current location
pos = generator.nextInt(numStates);
}
dogPos = pos;
System.out.println(“dogs position is ” + dogPos);
trace(“…setDogPosition() ends”);
}
/**
* randomly determines unique location for Jarnaby (jarnabyPos)
* @return jarnaby’s location (state)
*/
private void setJarnabyPosition()
{
int pos; // jarnaby’s position
trace(“setJarnabyPosition() starts…”);
pos = generator.nextInt(numStates);
while ((pos == currentState) || (pos == dogPos))
{
trace(“clash detected”);
// avoid clash with current location
pos = generator.nextInt(numStates);
}
jarnabyPos = pos;
System.out.println(“Jarnaby’s position is ” + jarnabyPos);
trace(“…setJarnabyPosition() ends”);
}
/**
* determine if dogs are nearby
* @return status of dogs location
* DAZED: if player is currently dazed
* FAINT_DOGS: if in connected state
* DISTANT_DOGS: if elsewhere
*/
public int dogsNear()
{
int closeness; // closeness of dogs
trace(“dogsNear() starts…”);
if (dazedRemaining != 0)
{
// the player is dazed
closeness = DAZED;
trace(“player is still dazed (” + dazedRemaining + ” moves to go)”);
}
else
{
if ((east[currentState ] == dogPos) ||
(west[currentState ] == dogPos) ||
(north[currentState ] == dogPos) ||
(south[currentState] == dogPos))
{
// the dogs are nearby
closeness = FAINT_DOGS;
trace(“dogs are close”);
}
else
{
// the dogs are elsewhere
closeness = DISTANT_DOGS;
trace(“dogs are distant”);
}
}
trace(“…dogsNear() ends with value ” + closeness);
return closeness;
}
/**
* determine if Jarnaby is nearby
* @return status of Jarnaby’s location
* DAZED: if player is currently dazed
* FAINT_JB: if Jarnaby in connected state
* DISTANT_JB: if elsewhere
*/
public int jarnabyNear()
{
int closeness; // closeness of Jarnaby
trace(“jarnabyNear() starts…”);
if (dazedRemaining != 0)
{
// the player is dazed
closeness = DAZED;
trace(“player is still dazed (” + dazedRemaining + ” moves to go)”);
}
else
{
if ((east[currentState ] == jarnabyPos) ||
(west[currentState ] == jarnabyPos) ||
(north[currentState ] == jarnabyPos) ||
(south[currentState] == jarnabyPos))
{
// the dogs are nearby
closeness = FAINT_JB;
trace(“Jarnaby is close”);
}
else
{
// the dogs are elsewhere
closeness = DISTANT_JB;
trace(“Jarnaby is distant”);
}
}
trace(“…jarnabyNear() ends with value ” + closeness);
return closeness;
}
/**
* try to fly the player to another state
* @param into indicates the state to try to move into
* @return status of movement
* SUCCESS: move was successful (current position changed)
* STARTLED: move was successful but player encountered dogs
* DAZED: move was successful but player encounted Jarnaby
* DEPORTED: move was successful but dazed player encountered Jarnaby again
* IMPOSSIBLE: move was impossible (current position not changed)
*/
public int tryFly(int into)
{
int result; // outcome of walk attempt
trace(“tryFly() starts…”);
if ((east[currentState] == into) ||
(north[currentState] == into) ||
(west[currentState] == into) ||
(south[currentState] == into))
{
// state connected
trace(“move into state ” + into );
currentState = into;
if ((currentState != dogPos) && (currentState != jarnabyPos))
{
// empty state
trace(“neither dogs nor Jarnaby found”);
result = SUCCESS;
if (dazedRemaining != 0)
{
dazedRemaining–;
}
}
else
{
if (currentState == jarnabyPos)
{
// Jarnaby found
if (dazedRemaining == 0)
{
// not dazed
trace(“Jarnaby encountered”);
result = DAZED;
dazedRemaining = MAX_DAZED_MOVES;
setJarnabyPosition();
}
else
{
// already dazed
trace(“Jarnaby encountered again”);
result = DEPORTED;
}
}
else
{
// dogs found
trace(“dogs encountered”);
setDogPosition();
setJarnabyPosition();
result = STARTLED;
}
}
}
else
{
}
// state not connected
trace(“move not possible”);
result = IMPOSSIBLE;
trace(“…tryFly() ends with value ” + result);
return result;
}
/**
* try to call to dogs from the current state
* @return status of attempt
* CAPTURED: dogs captured
* SUCCESS: dogs were present but not captured yet
* IMPOSSIBLE: state not connected
* FAILURE: dogs not present
*/
public int tryCall(int into)
{
int result; // outcome of shout attempt
trace(“tryCall() starts…”);
if ((east[currentState] == into) ||
(north[currentState] == into) ||
(west[currentState] == into) ||
(south[currentState] == into))
{
// state connected
trace(“shout into state ” + into );
if (into != dogPos)
{
// not at dogs location
result = FAILURE;
trace(“dogs not present”);
}
else
{
// at dogs location
callsRemaining–;
if (callsRemaining == 0)
{
// last shout needed for capture
trace(“dogs captured”);
result = CAPTURED;
}
else
{
// not the last shout
result = SUCCESS;
setDogPosition();
setJarnabyPosition();
trace(“dogs found but not yet captured”);
}
}
}
else
{
}
// not at valid location
result = IMPOSSIBLE;
trace(“state not present”);
trace(“…tryCall() ends with value ” + result);
return result;
}
/**
* determine number of adjacent state given its direction
* @param char indicates the direction required (w – west, e – east, n north, s – south)
* @return number of the state in that direction or IMPOSSIBLE if invalid
parameter
*/
public int nextState(char direction)
{
final char NORTH = ‘n’; // north
final char SOUTH = ‘s’; // south
final char EAST = ‘e’; // east
final char WEST = ‘w’; // west
int nextIs; // state number of state in indicated direction
trace(“nextstate() starts…”);
// examine adjacent states
switch (direction)
{
case WEST:
}
trace(“determining number of state
nextIs = west[currentState];
break;
case NORTH: trace(“determining number of state
nextIs = north[currentState];
break;
case EAST: trace(“determining number of state
nextIs = east[currentState];
break;
case SOUTH: trace(“determining number of state
nextIs = south[currentState];
break;
default: nextIs = IMPOSSIBLE;
to the west”);
north”);
to the east”);
south”);
trace(“…nextstate() ends with value for ‘” + direction + “‘ of ” +
nextIs);
}
return nextIs;
/**
* reset all game values
*/
public void reset()
{
trace(“reset() starts…”);
// reset all game values
trace(“resetting all game values”);
newGame();
trace(“…reset() ends”);
}
/**
* turn tracing messages on or off (if off it is assumed that debugging is
not occurring and so a new (unseeded) RNG is provided
* @param onOff indicates the required state of messages (true on, false
off)
*/
public void setTracing(boolean onOff)
{
if (! onOff) // not tracing so get an unseeded RNG
{
generator=new Random();
}
}
tracing = onOff;
/*
* displays tracing messages
* @param message the message to be displayed if instance variable tracing
is true
*/
public void trace(String message)
{
if (tracing)
{
System.out.println(“R2DCJ2: ” + message);
}
}
}

Purchase answer to see full
attachment