Question

In: Computer Science

Question 2 A text-based adventure game has several types of player. The code for the game...

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]

Solutions

Expert Solution

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


Related Solutions

JAVA 1) You are provided an implementation of a text-based adventure game. Currently the game responds...
JAVA 1) You are provided an implementation of a text-based adventure game. Currently the game responds to directional movements that allow you to move your adventurer around a maze (that is read from a text file). Add the ability to save (and load) the game using object serialization. Write the serialization of the file out to a default filename SavedGame.dat. Make sure that you take care of any exceptions that may occur when reading or writing to a file (for...
Consider a game with two players, each of whom has two types. The types of player...
Consider a game with two players, each of whom has two types. The types of player 1 are T1 = (a,b). The types of player 2 are T2 = (c,d). Suppose the beliefs of the types are p1(c/a) = p2(a/c) = 0.25 and p1(c/b) = p2(a/d) = 0.75. Is there a common prior? If yes, construct one; if no, prove why not.
Coin taking game This game is played between 2 players, player 1 and player 2. There...
Coin taking game This game is played between 2 players, player 1 and player 2. There are two piles of coins. The values of a coin can be any integer. Both players know the values of all coins in both piles. Player 1 makes the first move, and play alternates between the players. A move consists of taking a coin from the top of either of the piles (either player can take from either pile). The game ends when both...
Question 4: Jar Game Consider the following game: Players: 2 - We designate player #1 to...
Question 4: Jar Game Consider the following game: Players: 2 - We designate player #1 to be the one that starts with the jar. Actions: - Each round at the same time both players deposit between 1 to 4 pennies into the jar. - Then both players are able to count the pennies in the jar. - If there are 21 or more pennies, the person with the jar is the winner. - If there are 20 or less pennies,...
Consider the following two-player game, in which Player 1 is the IMF, and Player 2 is...
Consider the following two-player game, in which Player 1 is the IMF, and Player 2 is a debtor country. Reform Waste Aid 3, 2 -2, 3 No Aid -2, 1 0, 0 a) Compute all (pure and mixed) Nash equilibria. b) Do you think that the above game is the case of a resource curse? Interpret the game with a story of a resource curse.
Consider a game between a tax collector (player 1) and a tax payer (player 2).  Player2 has...
Consider a game between a tax collector (player 1) and a tax payer (player 2).  Player2 has an income of 200 and may either report his income truthfully or lie.  If he reports truthfully, he pays 100 to player 1 and keeps the rest.  If player 2 lies and player 1 does not audit, then player 2 keeps all his income.  If player 2 lies and player 1 audits then player 2 gives all his income to player 1. The cost to player 1...
Mario Brothers, a game manufacturer, has a new idea for an adventure game. It can either...
Mario Brothers, a game manufacturer, has a new idea for an adventure game. It can either market the game as a traditional board game or as an interactive DVD, but not both. Consider the following cash flows of the two mutually exclusive projects. Assume the discount rate for both projects is 8 percent. Year Board Game DVD 0 –$ 1,250 –$ 2,800 1 700 1,800 2 1,000 1,580 3 220 850 a. What is the payback period for each project?...
Wii Brothers, a game manufacturer, has a new idea for an adventure game. It can market...
Wii Brothers, a game manufacturer, has a new idea for an adventure game. It can market the game either as a traditional board game or as an interactive DVD, but not both. Consider the following cash flows of the two mutually exclusive projects for the company. Assume the discount rate is 12 percent.    Year Board Game DVD 0 –$ 1,450 –$ 3,200 1 740 2,000 2 1,200 1,620 3 260 1,050    a. What is the payback period for...
Mario Brothers, a game manufacturer, has a new idea for an adventure game. It can either...
Mario Brothers, a game manufacturer, has a new idea for an adventure game. It can either market the game as a traditional board game or as an interactive DVD, but not both. Consider the following cash flows of the two mutually exclusive projects. Assume the discount rate for both projects is 12 percent. Year Board Game DVD 0 –$ 1,200 –$ 2,700 1 690 1,750 2 950 1,570 3 210 800 a. What is the payback period for each project?...
Wii Brothers, a game manufacturer, has a new idea for an adventure game. It can market...
Wii Brothers, a game manufacturer, has a new idea for an adventure game. It can market the game either as a traditional board game or as an interactive DVD, but not both. Consider the following cash flows of the two mutually exclusive projects for the company. Assume the discount rate is 12 percent. Year Board Game DVD 0 –$ 1,200 –$ 2,700 1 690 1,750 2 950 1,570 3 210 800 a. What is the payback period for each project?...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT