Answered step by step
Verified Expert Solution
Question
1 Approved Answer
1 ) You should compile your code after nearly every step in the lab. This allows you to make sure you have no syntax errors,
You should compile your code after nearly every step in the lab. This allows you to make sure
you have no syntax errors, and it saves your work. Double bonus!!!
Create a new class click on the New Class button Name this class BoardPiece.
Create a new class. Name this class PlayerPiece.
We want the PlayerPiece class to inherit methods from the BoardPiece class. To do
this, double click on PlayerPiece. Find near the top where it says public class
PlayerPiece. On that same line, add to the end of the line, extends BoardPiece.
The PlayerPiece class can be thought of as a more specialized version of the
BoardPiece class. It can do anything the BoardPiece class can do and more.
Click Compile. There should be no errors. If there are, fix them
Now that you know how to create classes, create three new classes. Name these classes
Human, Elf, and Dwarf. These will be the classes to represent our heroes. Your BlueJ
window should look similar to below when you are done. Note that you can drag your classes
around to arrange them how you likeThe arrow from PlayerPiece to BoardPiece signals
that PlayerPiece extends BoardPiece
Now, we want the Human, Elf, and Dwarf classes to inherit from the PlayerPiece class.
Follow the same concept from step to make this happen add extends PlayerPiece to
the class definitions for Human, Elf, and Dwarf You should have something similar to
below. Note I've moved my hero classes below PlayerPiece
We need one more class now to make our game semi complete. Can you guess? A
GameBoard class of course!!! Our heroes must have a game board to do battle upon. Create
a GameBoard class. Conceptually we will be putting game pieces onto our GameBoard.
Now its time to add some instance variables to our classes. These will store things like
position, hit points, and attack power. Instance variables are like the variables we have studied
so far, except they belong to the class rather than to a specific method. You will need to use
the keyword private to declare instance variables. For example,
private int maxHealth ; declaration initialization
Double click on the PlayerPiece class. This brings up the editor window for the
PlayerPiece class. You should see where an instance variable x was created for you,
along with a sample method sampleMethod Using the example as a guideline, you
should create the following instance variables and initialize them to the values below.
Comment your code. Make all of these instance variables private. Instance variables should
be declared OUTSIDE of methods, but inside your class.
currentHealth type: int, initial value:
maxHealth type: int, initial value:
locX type: int, initial value:
locY type: int, initial value:
attackPower type: int, initial value:
defensePower type: int, initial value:
Compile to save your work and check for errors.
Look at the method declaration below:
public means any class can call this method. Your methods should be public. int
is the type returned by the method. You can see the one line of code at the bottom that says
return xy; to verify this Next comes the name of the method, sampleMethod
Then open parenthesis. Then you list the parameters input of the method. In this case there
is one parameter, an int, that will be called y inside this method. Then finally inside the
brackets you place the code for the method.
Using the sampleMethod as a guide, create the following methods inside the
PlayerPiece class, with parameters and return types as specified. Compile your code after
EVERY STEP here.
getMaxHealth no parameters, returns an int. Add line of code return
maxHealth; inside the method body.
getCurrentHealth no parameters, returns an int. Add line of code return
currentHealth;
getLocX no parameters, returns an int. Add line of code return locX;
getLocY no parameters, returns an int. Add line of code return locY;
getAttackPower no parameters, returns an int. Add line of code return
attackPower;
getDefensePower no parameters, returns an int. Add line of code return
defensePower;
Each of the methods created above are accessor methods. They simply give access to the
instance variables. They make no changes.
Now we need to create some mutator methods. These will be useful to be able to move our
game pieces around. We are conceptually using a x board that starts at coord in the
upper left corner, and ends at coord in the lower right corner. Draw this so you can
visualize it Create the following methods. Compile after you make each method.
moveUp no arguments, no return value, no code
moveDown no arguments, no return value, no code
moveLeft no arguments, no return value, no code
moveRight no arguments, no return value, no code
We will worry about implementing these methods in another lab assignment.
At thi
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started