Java Development

1 Questions which i have outlined below.

The work coding needs to be completed within the blue J project file i have uploaded below and to the specification that the questions ask it be. A lot of the project is completed already.

QUESTIONS:

This question involves completing a class called WizardController, instrances of which coordinate an exhibition performed by two wizards. The exhibition consists of a series of actions performed by each wizard in turn and repeated a number of times selected by the user. The actions are “fly” and “slither” and these incorporate jumps, making use of methods in Wizard to achieve these.

Wizards are represented by objects of the Wizard class which is provided.

Wizards have an instance variable persona of type Triangle, which enables them to be visible in the Shapes window. They are located in cells. They move from cell to cell horizontally or vertically. Cells have width CELL_SIZE_X and height CELL_SIZE_Y. The persona of a wizard in cell (1,3) for example would have xPos = CELL_SIZE_X and yPos = 3 * CELL_SIZE_Y. They have attributes cellX and cellY which show the current cell X and Y positions they are in.

Wizards remember their starting cell using the instance variables startCellX and startCellY so that they can return to these. startCellX is always 0 but startCellY is initialised using an argument passed to the constructor.

You are provided with a method in Wizard called getPersona(). This is solely so that you can display the wizard. You should not make use of this method in the WizardController class. Doing so would break encapsulation.

Launch BlueJ and open the project TMA03_Q1 which was provided in the zip file containing this TMA, then immediately save it as TMA03_Q1_Sol. This project contains partial implementations of WizardController as well as the Wizard class and before proceeding further, you should open both classes and familiarise yourself with their contents. You do not need to alter the Wizard class and should not attempt to do so.

At different points in this question you will be directed to look at specific parts of the WizardController class. We begin with the methods getNumOfRepeats(), isValidNumOfRepeats() and promptForNumOfRepeats().

A.

The public instance method getNumOfRepeats() takes no arguments and returns an integer between 1 and 3 (inclusive).
getNumOfRepeats() makes use of two helper methods:

promptForNumOfRepeats(), which prompts the user for a number between 1 and 3 and attempts to parse the response as an integer, and
isValidNumOfRepeats(), which checks that the number the user has input is in the correct range 1 to 3 (inclusive).
If the input is out of range the user is prompted again, and the method continues to loop until a number in the required range is entered.

But what if the user makes a mistake and enters something that cannot be parsed as an integer at all? In this case, the parseInt() method will throw a NumberFormatException and the program as it stands will simply stop executing.

To handle this case more gracefully the promptForNumOfRepeats() method can be modified to make use of a try-catch block. If an exception is thrown the catch statement can assign 0 to the number of moves, and since this is outside the range 1 to 3 the user will be prompted to try again. (Of course any integer outside the required range could be used instead of 0 but it seems a natural choice).

Modify the promptForNumOfRepeats() method so that it handles the possible exception in the way described above.

(3 marks)

B.

The WizardController class has the following private instance variables:
Two variables of type Wizard:
wizard1 and wizard2

which represent the wizards performing the moves;

A variable of type int:
numOfRepeats

which holds the number of times the moves are to be repeated;

It also has three int class constants:

MIN_NUM_REPEATS and MAX_NUM_REPEATS
which hold the minimum and maximum number of times the moves can be repeated.

LAST_CELL
which holds the maximum cell number permitted in the X direction.

The class has a single constructor that takes two arguments of type Wizard and assigns them to the two Wizard instance variables.

You are now going to write the various methods in WizardController that animate the wizards. In order to follow the motion of the wizards properly you will need to slow the animation down. To help you do this we have provided a class method delay() in the WizardController class.

e.g. If you want a delay of 100 ms you would insert the following statement at the appropriate point in your code.:

WizardController.delay(100);
Remember to use delay() at the appropriate point in each of the methods you write to animate the wizards.’

You should remind yourself of methods the Wizard class provides now as you will need to use these in your code.

Remember that you cannot make use of the Wizard method getPersona() within the WizardController class.

I.

Write a public class method jump() that returns nothing. It should make the wizard passed as argument jump up one cell position from its current cell then return to its original cell.
Now create an instance of wizard and execute the jump() method passing it as argument to check this works as expected. You’ll find code in the README file that will help you with this.

(3 marks)

II.

Next you are going to create a public class method travel() that takes a single argument of type Wizard and returns no value.
The wizard should move left or right as appropriate one cell at a time.

If the wizard’s cellX value is currently LAST_CELL it should move so that its cellX value is 0.
Otherwise it should move so that its value is LAST_CELL.
Test your code to make sure the wizard behaves as expected.

(5 marks)

iii.

Add an additional boolean argument isMagic to your travel() method, that governs whether the wizard moves straight to its destination or cell by cell in a loop.
Amend the code body of travel() so that for each path, if the argument isMagic is true then the wizard moves straight to its final destination so that cellX is 0 or LAST_CELL as before.

Otherwise the wizard should move as in part (b)(ii).

Test your code to make sure the altered behaviour of the wizard is as expected.

(3 marks)

iv.

Write a public class method switchTrack() that takes two arguments of type Wizard and returns no value. The method should switch the cellY values of the two wizards.
(3 marks)

c.

Now you can complete the code for WizardController to put the movements together.
Uncomment the provided methods:

setUpWizards() which resets each wizard persona to its original cellX and cellY and asks the user how many repeats they want.
flyWizards() which invokes jump(), travel()and jump() on each wizard in turn, moving magically. This is the “fly” action
slitherWizards() which invokes travel()and jump() on each wizard in turn, moving without magic. This is the “slither” action.
Now write a public instance method performMoves() which takes no argument and returns no value.

It should perform the following sequence of actions the number of times requested by the user:

“fly”
the wizards should switch track with each other
“slither”
the wizards should switch tracks again
“fly”
(5 marks)

d.

All the instance methods you have written are public in this question. This is to enable you to test your code easily. Explain with reference to Object Oriented principles which of these instance methods in WizardController needed to be public and why others should have been private.
You are not required to actually change any access modifiers for this question, only discuss what they ought to be.
(2 marks)

The Files need to be put into one folder and then open the project via blue j and the folder with work and open the project.