In: Computer Science
Question 2
A text-based adventure game has several types of player.
The code for the game uses the following class, Character to define all the shared elements
for different types of player characters (Warrior, Wizard etc).
1. public class Character {
2. private String name;
3. private int life;
4. protected int hitPoints;
5. public Character(String name, int life, int hitPoints) {
6. this.name = name;
7. this.life = life;
8. this.hitPoints = hitPoints;
9. }
10. public void setHitPoints(int hitPoints) {
11. this.hitPoints = hitPoints;
12. }
13.
14. public int getHitPoints() {
15. return hitPoints;INTERNATIONAL STUDY CENTRE IY1 EXAMINATION - SUMMER 2020
Page 3 of 6
16. }
17.
18. }
Use the class here as a superclass and work in NetBeans to:
a. Add a toString method to the character class that prints out:
The name of the character, their hit points & their remaining life
[5 marks]
b. In the class Character, the field life is private, but the field hitPoints is
protected.
Explain the difference between the access modifiers private and protected, and
explain why it useful for hitPoints to be protected, but this may not be needed for
the field life.
[5 marks]
One of the types of player is a Warrior.
c. Define the Warrior class as a subclass of Character. Warrior has only two methods
currently
setHitPoints(int) which overrides the method in the super class and
fightEnemy which can take any character subclass as a parameter
The warrior has a maximum of 20 hitPoints & a minimum of 10 hitPoints.
Add appropriate data types in the fields to store the min and max hitPoints
Make sure Warrior has an appropriate constructor
[10 marks]
d. Write the new setHitpoints method for the Warrior class that has a parameter of
the new number of hitPoints. Check to see if the number of hitPoints is within
the valid range
[6 marks]
e. Write appropriate methods for life field in the Character class. If life is less than
or equal to 0 the character is dead.
[4 marks]
f. Extend the fightEnemy method so that the enemy will damage the players life by
their hitPoints multiplied by a random percentage that range between between 0 &
50%
[10 marks]
g. Define a fightEnemy method which uses a valid method in Java to determine
whether the Warrior or the enemy character should attack first
[10 marks]
b.
The access level of a private variable is only within the class. It cannot be accessed from outside the class. The access level of a protected variable is within the package and outside the package through child class. The child can access the protected variables from the super class. Here the variable life is private, because the child class or any other class is not allowed to access it directly. The variable hitPoints is protected because the child classes can access this variable directly. Different subclasses of Character may have different hit points range.
/**
* Character class
*/
public class Character {
private String name;
private int life;
protected int hitPoints;
public Character(String name, int life, int
hitPoints) {
this.name = name;
this.life = life;
this.hitPoints =
hitPoints;
}
public void setHitPoints(int hitPoints)
{
this.hitPoints =
hitPoints;
}
public int getHitPoints() {
return hitPoints;
}
public void setLife(int life){
this.life = life;
}
public int getLife(){
return this.life;
}
@Override
public String toString() {
return "Name: " + name +
", hitPoints: " + hitPoints + ", Life: " + life ;
}
}
/*
* Warrior class
*/
import java.util.concurrent.ThreadLocalRandom;
public class Warrior extends Character{
final int maxHitPoints = 20;
final int minHitPoints = 10;
public Warrior(String name, int life, int
hitPoints) {
super(name, life,
hitPoints);
}
@Override
public void setHitPoints(int hitPoints) {
if(hitPoints<=maxHitPoints &&
hitPoints>=minHitPoints)
this.hitPoints = hitPoints;
}
public void fightEnemy(Character c){
double percentage =
ThreadLocalRandom.current().nextDouble(0.0, .50);
int selecter =
ThreadLocalRandom.current().nextInt(0, 2);
if(selecter==1){
this.setLife((int) (this.getLife()-percentage*c.hitPoints));
c.setHitPoints(c.getHitPoints()+1);
}
else {
c.setLife((int) (c.getLife()-percentage*this.hitPoints));
this.setHitPoints(this.getHitPoints()+1);
}
}
}
/*
* Test class
*/
public class Test {
public static void main(String[] args) {
Warrior w1 = new
Warrior("A", 10, 10);
Warrior w2 = new
Warrior("B", 10, 10);
while(w1.getLife()>=0
&& w2.getLife()>=0){
w1.fightEnemy(w2);
}
if(w1.getLife()<=0
&& w2.getLife()>0){
System.out.println(w1+" dead");
System.out.println(w2+" won the game");
}
else
if(w2.getLife()<=0 && w1.getLife()>0){
System.out.println(w2+" dead");
System.out.println(w1+" won the game");
}
else
System.out.println("No winner");
}
}
Sample output