Define a constructor for class game that takes the initial


Tasks

This section describes each piece of functionality you must implement, and the order in which they must be implemented. These tasks must be completed in the order listed below. Please read Section "Marking Scheme" for more details on how these tasks would be marked.

Task 1: Classes and fields

Read the following system description and identify the classes and their fields. There are 3 classes of objects A game has 1 player and 3 dots (called dot1, dot2 and dot3). The player has an x and y value (both integers) which together describe the position of the player. The coordinate system of the x and y position is as follows: the x-coordinate increases from left to right, and the y-coordinate increases from top to bottom. Theplayer also keeps a counter of the number of dots collected (called collectedDots). Each dot has an x and y position with the same coordinate system as the player, and also has a boolean status called "exists" which is initially true and is set to false whenever the dot has been collected by the player.

In a new BlueJ project, define some classes and fields based on the above description. The names of your classes and fields should exactly match the noun phrases mentioned in the above system description, and must follow Java's naming conventions (i.e. class names begin with an uppercase letter, field names begin with a lowercase letter, compound nouns are joined without a space and the second word begins with an uppercase letter).

This is a multi-class program. You may find it helpful to refer to examples of "compositional" class designs in lecture 3 and tutorial 3.

All classes must be public and all fields must be private (taught in week 4).

Submit your project to PLATE now to receive your marks and feedback. You can resubmit unlimited times before the due date. Use the feedback from plate to improve your code and then resubmit to improve your mark.  


Task 2: Constructors

In this task, you will add constructors to each class to initialise the program.

2.1 Dot

Define a constructor for class Dot that takes the initial x and y values as parameters, and copies these initial values into the x and y fields.

The Dot constructor should also initialise the "exists" field to true.

2.2 Player

Define a constructor for class Player that takes the initial x and y values as parameters, and copies these initial values into the x and y fields. The collectedDots fields should also be initialised to 0.

Submit your code to PLATE to receive marks for this part and do the same for each subsequent part below. You are required to submit your progress to plate while you are making progress.

2.3 Game

Define a constructor for class Game that takes the initial x and y positions of the player as parameters and creates and initialises the player, dot1, dot2 and dot3 fields. The player should be created at the position given by the parameters. dot1, dot2 and dot3 must be created at positions (1,1), (2,2) and (3,3) respectively.

Preamble to Task 3 and Task 4

Tasks 3 and 4 can mostly be completed in any order. It is recommended that you do the easy parts first and leave the hard parts (labelled "advanced") until after you have completed the easy parts.

Task 3: move() method

Review the examples of class composition in the week 3 lecture notes and tutorial notes before attempting this task.

Compositionally define a move() method in class Game taking two int parameters called dx and dy. This method should tell the player to move by the given relative distance (dx,dy). That is, if the player is currently at position (x,y), the player should be moved to position (x+dx, y+dy). For example, if the player is currently at position (3,4) and the move method is used with parameters dx=2 and dy=3, then the player should move to position (5,7).

Advanced: (You can try this after completing the easy part of Task 4) The move() method should also tell the player to collect a dot if possible. The player can collect a dot only if the player is at the same position as the dot. When the player collects a dot, the player's "collectedDots" count should be increased by 1, and the dot should disappear. When a dot disappears, its "exists" field should be set to false.

Try not to put too much code in one class. In a good compositional design, the responsibility should be distributed throughout the system.

Task 4: toString() function

Define a toString() function for class Game that returns (but does not print) a string of the form:
 
Player[0](0,0) Dot(1,1) Dot(2,2) Dot(3,3)

The above string indicates the positions of the player and 3 dots, as well as the number of dots collected by the player. Here, [0] indicates the player has collected 0 dots, and each (x,y) indicates the position of a player or dot.

Advanced: If the player moves to position (2,2) and collect dot2, the string should appear as follows:

Player[1](2,2) Dot(1,1) Dot(-,-) Dot(3,3)

Showing the dot's position as (-,-) indicates that the dot has been collected and has disappeared.

This function must be defined compositionally. That is, Game.toString() should call upon toString() functions in other classes:

-  Player.toString() should return a string such as "Player[0](0,0)".
-  Dot.toString() should return a string such as "Dot(3,3)"

Solution Preview :

Prepared by a verified Expert
PHP Web Programming: Define a constructor for class game that takes the initial
Reference No:- TGS0655306

Now Priced at $70 (50% Discount)

Recommended (94%)

Rated (4.6/5)