Answered step by step
Verified Expert Solution
Link Copied!

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,

1) 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!!!
2) Create a new class (click on the New Class ... button). Name this class BoardPiece.
3) Create a new class. Name this class PlayerPiece.
4) 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.
5) Click Compile. (There should be no errors. If there are, fix them).
6) 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 like).(The arrow from PlayerPiece to BoardPiece signals
that PlayerPiece extends BoardPiece).
7) Now, we want the Human, Elf, and Dwarf classes to inherit from the PlayerPiece class.
Follow the same concept from step 3 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).
8) 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.
9) 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 =100; //declaration, initialization
10) 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.
1) currentHealth (type: int, initial value: 100)
2) maxHealth (type: int, initial value: 100)
3) locX (type: int, initial value: 15)
4) locY (type: int, initial value: 13).
5) attackPower (type: int, initial value: 12)
6) defensePower (type: int, initial value: 10)
11) Compile to save your work and check for errors.
12) Look at the method declaration below:
13) 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 x+y; 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.
14) 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.
1) getMaxHealth() no parameters, returns an int. Add line of code return
maxHealth; inside the method body.
2) getCurrentHealth() no parameters, returns an int. Add line of code return
currentHealth;
3) getLocX() no parameters, returns an int. Add line of code return locX;
4) getLocY() no parameters, returns an int. Add line of code return locY;
5) getAttackPower() no parameters, returns an int. Add line of code return
attackPower;
6) getDefensePower() no parameters, returns an int. Add line of code return
defensePower;
15) Each of the methods created above are accessor methods. They simply give access to the
instance variables. They make no changes.
16) 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 32x32 board that starts at coord (0,0) in the
upper left corner, and ends at coord (31,31) in the lower right corner. Draw this so you can
visualize it. Create the following methods. Compile after you make each method.
1) moveUp()- no arguments, no return value, no code
2) moveDown()- no arguments, no return value, no code
3) moveLeft()- no arguments, no return value, no code
4) moveRight()- no arguments, no return value, no code
17) We will worry about implementing these methods in another lab assignment.
18) At thi

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Expert Performance Indexing In SQL Server

Authors: Jason Strate, Grant Fritchey

2nd Edition

1484211189, 9781484211182

More Books

Students also viewed these Databases questions

Question

=+3. Is pay for performance effective? Why or why not?

Answered: 1 week ago

Question

What do Dimensions represent in OLAP Cubes?

Answered: 1 week ago