Course Hero Logo

The questions in this part of the assignment will be graded....

The questions in this part of the assignment will be graded. Question 1: Battle Game (50 points) For this question, you will write a number of classes to create a battle game between a player and a monster. Your code for this assignment will go in multiple .java files. Note that in addition to the required methods below, you are free to add as many other private methods as you see fit. It is up to you to figure out if the methods from the Character class and the Spell class should be static or not. BattleGame and FileIO are utilities classes, therefore all their methods are static. We strongly recommend that you complete the warm-up questions before starting this problem. (a) Character Class Write a class that represent a character in our battle game. The monster and the player will both be characters, 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 The class also contains the following public methods. • 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. • A getName(), getAttackValue(), getMaxHealth(), getCurrHealth(), and getNumWins() to retrieve the corresponding attributes' values. • A toString() to returns a String consisting of the character's 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. • A getAttackDamage(): This method takes an integer as input and calculates how much attack damage one character does when they attack. The method uses the input to generate a Random object with the given input as seed. The method then computes the damage as follows: take the character's attack value and multiply it by a random value between 0.7 (inclusive) and 1.0 (exclusive). • A takeDamage(): This method takes the damage done to this character as an input of type double. It then subtract this value from the character's current health and returns a double indicating the current health of the character. • A increaseWins(): 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. Page 4 (b) FileIO Class must contain the following public static method: • A 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 in order to open the file specified by the filename. Make sure to catch FileNotFoundException and IOException when reading from the file. If either exception is raised, print an appropriate error message and return null. You can assume that the files that readCharacter receives as input have all the same format: they contain 4 lines, with the following information - Name of the character - Attack value - Maximum health - Number of wins so far in the battle game Examples of such files are the player.txt and the monster.txt files are provided with the assignment. Use the readLine() method of the BufferedReader to retrieve the content of the file. Use the information retrieved to create and return the appropriate object of type Character. (c) BattleGame class The code for this part will go in a file named In this class, add a private static attribute of type Random and initialize it in place with a reference to a Random object created with no seed. (You can fix a seed for helping you debug your program if you want to) The BattleGame class has only one public static method called playGame(). You are highly encouraged to add private helper methods that allow you to organize your code well. The playGame() method takes two Strings as input containing the name of two files: the first containing the information about a player, the second containing the information about a monster. The method should do the following: • Create the player and their enemy (the monster), using the readCharacter() method from the FileIO class and the inputs received. If the method does not return two valid references to objects of type Character, then print a message saying that the game cannot be played and terminate the method. • Display the information of the two characters in the game including: the character's name, current health, attack value, and number of wins. • Create a Scanner object to take input from the user. • Until both the player and the monster have health above zero the method does the following: - Ask the user for a command. For the moment, the only options will be attack and quit. - If the user enters attack: ∗ Generate a random integer (using the class variable and the method nextInt() with no input) and get the attack damage of the player. ∗ Print out a statement with the player's name and how much damage they do. To make your output nicer, we suggest using a String formatting statement, though this is not Page 5 necessary. For example, you could write this to only show two decimal places of the attack damage: String attackStr = String.format("%1$.2f", attack);, where attack is a variable containing the damage value. ∗ Apply the damage made by the player to the monster. If the current health of the monster after taking the damage is still above 0, print a message with their name and current health. Note that you can take advantage of having a toString() method in the Character class to do this. If on the other hand, the current health is less than or equal to 0, print a message saying that the player was knocked out and exit the loop. ∗ Repeat the above steps swapping the roles of the player and the monster (i.e. the monster should now be attacking the player back). - If the user enters quit: ∗ Print a goodbye message and terminate the method. - If the user enters any other command: ∗ 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 increase the number of wins of either the player or the monster, depending on who won. Page 6 Here is some sample output produced by running the playGame method after Question 1 has been finished. Output for the finished assignment is found at the bottom of this document. Note that for this assignment, your output doesn't need to match exactly the samples provided. You are free to change these statements as you wish, as long as the required information still appear. 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 current health is 20.15. Fenrir attacks for 9.86 damage! Odin current health is 20.14. Enter a command: attack Odin attacks for 9.30 damage! Fenrir current health is 10.85. Fenrir attacks for 9.94 damage! Odin current health is 10.20. Enter a command: quit Goodbye! Page 7 Question 2: Extending the BattleGame (35 points) For this question, you will modify the above classes and add one new class, in order to add magical spells for the player to use. Note that you only hand in one set of files for this assignment. (a) Spell Class Write a class A Spell has the following private attributes: • A String name • A double minimum damage • A double maximum damage • A double chance of success for the spell (from 0 to 1) The Spell class also contains the following public methods: • A constructor that takes as input the name, minimum and maximum damage, and chance of success for the spell. Note that, an IllegalArgumentException must be thrown if the minimum damage is less than 0 or greater than the maximum damage, or if the chance of success is less t

Answer & Explanation

Unlock full access to Course Hero

Explore over 16 million step-by-step answers from our library

Get answer

Our verified expert tutors typically answer within 15-30 minutes.