Question
(a) Character Class Character.java represents a character in our battle game. The monster and the player will both be instances of the Character class, each
(a) Character Class
Character.java represents a character in our battle game. The monster and the player will both
be instances of the Character class, each with their own attributes. The Character class should contain the following (private) attributes:
A String name A double attack value A double maximum health value A double current health value A int number of wins in the battle game
Here are the required public methods for this class. Note that you will also have to add getters and setters for the instance attributes as needed.
1) A constructor
The constructor for the Character class takes one String, two doubles, and one int as input. These parameters represent the name, attack value, maximum health, and number of wins in the battle game for the character, in that order. Note that the current health of a new character is the same as the maximum health.
2) The toString method
This method returns a String consisting of the characters name and current health. Format the String in any way you want. This method will be very handy for debugging your code, and will be used during the battle game to keep track of the health of each character.
3) calcAttack method
This method calculates how much attack damage one character does in a battle. The calculation is as follows: Take the characters attack value and multiply it by a random value between 0.3 (inclusive) and 0.7 (exclusive). Return this value as a double. Use the Random class to generate the random numbers, and do not use a seed.
4) takeDamage method In this method, we take the damage done to this character as a double parameter, and then we
subtract this value from the characters current health. This method does not return anything. 5) increaseWins method
This method will increase the number of wins by the character by one, and does not return anything. This method will be called when the character wins the battle game.
(b) FileIO FileIO.java must contain a static method readCharacter.
This method takes as input a filename as a String parameter, and returns a new Character, using the constructor defined in the Character class.
The readCharacter method must use a FileReader and a BufferedReader to open the file specified by filename. Make sure to have two catch blocks to catch both FileNotFoundException and IOException when reading from the file. In these cases, throw an IllegalArgumentException with an appropriate message that the file was not found, or that there was an IO exception. Note that the catch block for the FileNotFoundException must be directly above the catch block for the IOException.
If you prefer, you can also use the throws statement to throw the FileNotFoundException and IOException up to the calling playGame method. Then these Exceptions would be caught in a try/catch block there.
Example player.txt and monster.txt files are provided with the assignment. The format of these files is exactly this, one value per line:
Name of the character Attack value Maximum health Number of wins so far in the battle game
Use the readLine() method of the BufferedReader to get the lines from the file. Do not use the Scanner class. Then, use the Double.parseDouble() and Integer.parseInt() methods to parse the lines. Finally, use those values to create and return a new Character.
(c) BattleGame The code for this part will go in a file named BattleGame.java.
The BattleGame class only contains the playGame method, and the doAttack method, though you are allowed to create as many other private methods as you want.
1) playGame method must do the following things: Create the player and their enemy, using the FileIO.readCharacter method
After a character is created, print the characters name, current health, attack value, and number of wins.
We recommend you place this print statement in a public method inside the Character class
Use a Scanner to take input from the user Loop while both the player and the monster have health above zero
Ask the user for a command
For this question, the only options will be attack and quit
If the input is attack:
Call the doAttack method (in the BattleGame class) with the player as first parameter and the monster as second parameter
Call the doAttack method with the monster as first parameter and the player as second parameter
If the input is quit
Print a goodbye message and return from the method
If the input is anything else
Print a message that the input was not recognized and suggest the attack or quit commands
If the loop stops because one of the character's health is zero or below, then that character is knocked out. Print an appropriate message either congratulating the player, or saying how they lost. Also make sure to call the increaseWins method on either the player or the monster, depending who won.
2) The doAttack method takes two Characters as input, and returns nothing as output. This method must:
Get the damage from the first Character by calling the calcAttack method
Print out a statement with the first characters name and how much damage they do.
Apply the damage to the second Character with the takeDamage method
If the second characters current health is still above zero, print a message with their name and current health. If their current health is zero or below, print out a message saying that the second character was knocked out.
To make your output nicer, we suggest using a String formatting statement, though this is not necessary. For example, you could write this to only show two decimal places of the attack damage: String attackStr = String.format(%1$.2f, attack);
Here is some sample output produced by running the playGame method after Question 1 has been fin- ished. Output for the finished assignment is found at the bottom of this document. Note that for this assignment, your output doesnt need to match this exactly. You are free to change these statements as you wish, as long as the required information still appears.
Name: Odin Health: 30.00 Attack: 10.00 Number of Wins: 0 Name: Fenrir Health: 30.00 Attack: 12:00
Number of Wins: 0
Enter a command:
attack
Odin attacks for 9.85 damage! Fenrir takes 9.85 damage! Name: Fenrir Health: 20.15
Fenrir attacks for 9.86 damage! Odin takes 9.86 damage! Name: Odin Health: 20.14
Enter a command:
attack
Odin attacks for 9.30 damage! Fenrir takes 9.30 damage! Name: Fenrir Health: 10.85
Fenrir attacks for 9.94 damage! Odin takes 9.94 damage! Name: Odin Health: 10.20
Enter a command:
quit
Goodbye!
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