Assignment 05

Task

Write your own class to track a player's score in a simple computer game.

Concepts: Constructors, instance variables, encapsulation
Textbook: Chapter 3

Steps

Step 1: PlayerScore

You are going to write a PlayerScore class. Instances of this class could be used to track a player's score in a simple arcade computer game. The idea is that the player will enter their name when they start the game. Then, as they complete objectives in the game, their score can go up. Their score is reduced by dying, and it can be reset if they start a new game.

Therefore, your PlayerScore class should have two private instance variables: a String to hold the player's name and an int to hold their score. (Since they are private, you can give these two variables whatever name you want to.)

Your class should then have the following public interface (set of methods). A short explanation explains what each should do.

Note how we are using encapsulation here. Because the name instance variable is private, there is no way to change it once a PlayerScore object is constructed. Similarly, the score value can be affected by another class only through well-controlled means: the reset(), die(), and add() methods.

Step 2: UsernameA05

The value of breaking a problem into encapsulated objects like this is that they can be tested separately and then plugged into a larger program. In the next assignment, we'll split testing a class and using that class in a program into two separate steps. For now, here's a short little program that describes a typical use of a PlayerScore object in the context of a game.

Save this file into the same directory as your PlayerScore class: UsernameA05.java

The UsernameA05 file should compile and run. If it does not compile or does not run, it most likely means your PlayerScore class does not match the specifications given above.

Look over the code of UsernameA05. When you run this program, it will print out a few lines of text describing a game session. It will also be testing your class. Whenever you see a test printed in the form [value == value], both sides of the == should be identical. If they are not, there is an error in the logic of your PlayerScore class.

Once you are satisfied that your PlayerScore class is performing correctly, rename UsernameA05 to use your actual username in place of "Username". (Remember to change both the filename and the class name! Leave my name as @author on this class, but put your name in the /** javadoc */ description of your PlayerScore class.)

What to Submit

As you did in A04, you should submit both of your classes in one file. Paste the PlayerScore class into your Username05.java. (Remember to remove the public modifier from the PlayerScore class and do not accidentally paste one class within the other. )

Then, upload your complete UsernameA05.java file to Tamarin.

Grading [4 points]

1 - Compiles
Your program compiles successfully (no errors).
0.5 - UsernameA05
Includes the provided main method. (You may tweak/customize the output if you want to, such as by changing the sample player's name.)
2.5 - PlayerScore
All instance variables are private (0.3). Uses no static variables (0.2). Has the requested constructor (0.5) and five methods (0.3 each). The correct constructor must exist to get credit for the methods.

FAQs

None yet...