(a) FileIO In the FileIO class add a public static method named writeCharacter.
ID: 3805468 • Letter: #
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 which is the filename to write to. Within this writeCharacter method, use the FileWriter and BufferedWriter objects to write the character’s 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 doesn’t 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 don’t 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
Explanation / Answer
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class FileWritingDemo {
public static void main(String[] args) {
// declare and initialize variables
Scanner input = new Scanner(System.in); // keyboard input
BufferedReader in = null; // input file buffer
BufferedWriter out = null; // output file buffer
String filename = ""; // string holding the filename be opened
StringBuilder prevContents = new StringBuilder(); // stores previous
// file contents
// ask the user for a filename until a valid file is selected
while (in == null) {
System.out.println("Enter a file name");
filename = input.next();
try {
in = new BufferedReader(new FileReader(filename));
} catch (FileNotFoundException e) {
System.out.println("File doesn't exist");
}
}
// read previous contents of the file
try {
while (in.ready()) {
prevContents.append(in.readLine());
if( in.ready())
prevContents.append(" ");
}
in.close(); // done with input file buffer so we can close it
} catch (IOException e) {
System.out.println("Error reading from input file");
return;
}
System.out.println(prevContents); // print file contents
// ask for contents to append to the file
System.out.println("Enter output filename:");
input.nextLine(); // clear trailing newline, make keyboard ready for
// reading
filename = input.nextLine();
// write to output file
try {
// initialize the output file buffer using the provided filename
out = new BufferedWriter(new FileWriter(filename));
// actually write updated contents to file
out.write(prevContents.reverse().toString());
System.out.println("Written to '" + filename + "'");
System.out.println(prevContents);
out.close(); // close output file buffer
} catch (IOException e) {
System.out.println("Error writing back to file");
input.close();
return;
}
input.close(); // close keyboard input scanner
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.