Question
(a) FileIO In the FileIO class add a public static method named writeCharacter. This method takes as input a Character to write, and a String
(a) FileIO
In the FileIO class add a public static method named writeCharacter. This method takes as input a Character to write, and a String which is the filename to write to. Within this writeCharacter method, use the FileWriter and BufferedWriter objects to write the characters information back to a file. Make sure to catch the IOException when writing a file, and throw an IllegalArgumentException with an appropriate error message. If you prefer, you can also use the throws statement to throw the IOException up to the calling playGame method. Then this Exceptions would be caught in a try/catch block there. The format of the file that is written must match the format that is expected when a character is read. That is, you should be able to write a character to a file, and then read a character from that same file. Recall that the format of a character in a file is each value on a separate line: Name of the character Attack value Maximum health Number of wins so far in the battle game Note that this means you will have to write getter methods for the attributes in the Character class.
(b) BattleGame
In the playGame method, after you have increased the wins for either the monster or the player, print how many wins each character has. As well, you must write the characters to the files you loaded them from. For example, write the player character to player.txt or the monster character to monster.txt. Call the writeCharacter method in the FileIO class, and pass the character who won and the file which stores that character as parameters. This will save the number of wins for that character, so that after playing the battle game multiple times, you will know which character has won more often. Here is some sample output for the finished program. Again, your output doesnt need to exactly match, as long as the same information is presented. Name: Odin Health: 30.00 Attack: 10.00 Number of Wins: 8 Name: Fenrir Health: 30.00 Attack: 12.00 Number of Wins: 12 Spells: Name: fireball Damage: 5.0-10.0 Chance: 50.0% Name: icestorm Damage: 1.0-7.0 Chance: 90.0% Name: meteorstrike Damage: 10.0-10.0 Chance: 5.0% Enter a command: fireball Odin tried to cast fireball, but they failed Fenrir attacks for 9.84 damage! Page 8 Odin takes 9.84 damage! Name: Odin Health: 20.16 Enter a command: ICESTORM Odin casted icestorm for damage of 6.50 Fenrir takes 6.50 damage! Name: Fenrir Health: 23.50 Fenrir attacks for 10.18 damage! Odin takes 10.18 damage! Name: Odin Health: 9.98 Enter a command: badbreath Odin tried to cast badbreath, but they dont know that spell Fenrir attacks for 10.09 damage! Odin takes 10.09 damage! Odin was knocked out! Name: Odin Health: 0.00 Oh no! You lost! Fenrir has won: 13 times
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