讲解UML diagram、辅导CS/python程序、辅导Racer留学生、讲解java/c++

- 首页 >> Java编程

Assignment 5

You will use inheritance to create a general Player Class that can be used for a variety of games. For this

assignment, in addition to creating the Player Class, you will also create a sub-class called Racer. A Racer object

will either be a slow but steady racer or a fast and curious racer. A slow but steady racer always moves forward

but only moves one or two positions each turn. A fast and curious racer moves one to four positions for each

turn but can go forward and backwards. The Racer objects moves along a straight race track of a given size

moving forward (and backwards if appropriated) taking turns until one the objects wins (gets to the last

position of the track). Do not use any data structures for this assignment.

Remember – your assignments are to be your own work and your code will be checked for plagiarism.

Implement the following UML diagram. Note, you need to use the super keyword in this assignment when

creating Racer objects.

Use the following UML diagram to define a class called Player and Racer class.

Player

- name: String

- postion: int //current position, starts at 1

- maxPosition: int //size of playing area

+ Player (n : String, m: int): //set name, and maxPosition to m, set position to 1

+ getName (): String

+ getPosition() : int

+ getMaxPosition(): int

+ setName (n: String): void

+ setPosition (p: int): void

+ setMaxPosition (m: int): void

+ move (p: int) : void //moves the player to the position (make sure it is within bounds e.g., if less

//than 1 stay at position 1, if more than maxPosition move to max Position)

+ win ( ): boolean //checks to see if current player is at maxPosition

Racer

- steps: int //value that says how maximum steps in a move (slow but steady go up to 2 and fast and curious

//is up to 4

- direction: int //0 means can backwards and forward, and 1 only goes forward

- character: int //1 repr slow but steady 2 represents fast and curious

+ Racer (n : String, ch : int, m: int): //set name, maxPosition (m) using super and set character's steps and

//direction according to the int (1 is slow and steady and 2 is fast and curious)

+ getSteps (): int

+ getDirection() : int

+ getCharacter(): int

+ setSteps (s: int): void

+ setDirection (d: int): void

+ setCharacter (c: int): void

+ move (n: int, d: int) : void //set number places and direction to move based on character

2

The Racer Class

A Racer object has three attributes and sets name, position, maxPosition to it's superclass:

i. character – which is represented by an integer:

a. 1 means the racer is slow but steady always goes forward moving 1 or 2 steps each turn.

b. 2 means the racer is fast and curious meaning they can move from 1 to 4 steps each

turn but this can be going forward AND going backwards.

ii. steps– an integer which represent how many spots a racer can move in one turn.

a. A slow but steady racer goes at rate of 1 or 2 steps a turn.

b. A fast and curious racer goes at rate or 1 to 4 steps a turn.

iii. direction – an integer which represents whether a racer goes forward only or forward and

backwards

a. a slow but steady racer only goes forward no matter what (set to 1).

b. a fast and curious racer goes backwards and forwards (set to 0)

The constructor has three parameters ( name, character, maxPosition ). It uses super to set the name

and the maxPosition for the Racer. It also uses the character int to set the steps and distance (1

represents a slow but steady racer and 2 represents a fast and curious racer).

All the get/set methods for the Racer attributes

The Racer class overrides the Player Class's move method. The move method in the Racer class takes in

both the direction and number of steps to move. Direction will either be inputted as 0 (go backwards) or

1 (go forward). As well, number of steps to move will either be 1 or 2. You will use the Racer's own steps

and direction attributes to work with the inputted move method parameters.

The Race class overrides the Player class's move method. The Race class will take both a direction and a

number of steps to move. If the direction is backwards (<1) then you need to make sure that moving

won't take the Racer out of bounds. The Racer object should not move below 1 (move to 1 if the move

would take the racer out of bounds). If the direction to move is forward and number of spots to move is

larger than the maxPosition, then don't move. You need to reach the final position (maxPosition) with

an exact number.

For example, if the Racer is at spot 4 with a maximum size of 5, and it is told to move 2 spots. This is

higher than the max size, so the Racer will stay where it currently is. It needs to be told to move 1 spot

to get the final spot (maxPosition).

Then create a Racer demo class. You will read in the attributes of the two racer objects. Then you will

continue to read in a direction and number to move for racer object 1, followed by a direction number

to move racer object 2 while keeping track of each racer. Direction will always be entered in as 0

(backwards) or 1 (forward) no matter what type of character each Racer is. Move will always between 1

and 4 regardless of the character type of the Racer. You should handle all input limitations in the Racer

Class.

After each racer moves, check to see if there is a winner and add the moves (direction and read in steps)

to a String that will be printed out after a winner/tie is declared. Continue to read in numbers

representing direction/move for each racer until a winner is declared. Then print out the String with the

pairs of direction/moves for each racer and who won (or tied) and in (n) number of turns. See the

output. Please note, that after the input/output table, there are two images that goes with the first and

second input/output examples, so you can see visually how the two racers move along a straight line.

This is just to show you visually what happens after each move; however, for this assignment, you only

3

need to supply the final String of moves and the winner to avoid problems of output that can happen in

Codio.

Sample output

Sample input (values to create

2 racer objects, then pairs of

direction and move for each

racer object. Continue to read

in until winner is reached.

Sample output

String output representing the race (using the first letter of each

racer name's followed the value for direction (0 or 1) and number of

moves (1 to 2).

Sample 1 shows the race between two fast and curious racers (Rapid and Speedy)

Rapid 2 14 Speedy 2 14 1

2 1 4 1 3 0 1 1 1 1 3 1 2

1 4 1 4 1 2 0 1 1 2 1 1 1

2 1 1 1 1

R: 1 2 S: 1 4 R: 1 3 S: 0 1 R: 1 1 S: 1 3 R: 1 2 S: 1 4

R: 1 4 S: 1 2 R: 0 1 S: 1 2 R: 1 1 S: 1 2 R: 1 1 S: 1 1

Tie! in 8 rounds!

Sample 2 shows the race between a fast and curious racer (Rabbit) and a slow but

steady racer (Turtle)

Rabbit 2 10 Turtle 1 10 1

4 1 4 0 2 1 2 1 2 1 2 1 2

0 2 1 2 1 2 1 4 0 1

R: 1 4 T: 1 4 R: 0 2 T: 1 2 R: 1 2 T: 1 2 R: 1 2 T: 0 2

R: 1 2 T: 1 2 R: 1 4 T: 0 1

Turtle wins in 6 rounds!

Visual of 1st output

Visual of 2nd output:


站长地图