Add a public class named FileManager to that contains the following methods: public static void writeToFile(String saveState,
Question:
Add a public class named FileManager to that contains the following methods:
public static void writeToFile(String saveState, String fileName) {
//TODO: a string to a new file synchronously
}
public static void writeToFileAsync (String saveState, String
fileName) {
//TODO: a string to a new file asynchronously
}
Implement the writeToFile() method using a FileWriter in a try-with-resources block. Make sure to import the io package: import.java.io.*
Implement the writeToFileAsync() method using a separate thread. Use the following code as a guide:
new Thread() {
public void run(){
writeToFile(saveState fileName);
}
}.start();
Part2.
In the main() method of the BoardGameTester project, add the following code:
FileManager.writeToFileAsync(ticTacToe.toString(),
"ttt.txt");
FileManager.writeToFileAsync(connectFour.toString(),
"c4.txt");
FileManager.writeToFileAsync(mastermind.toString(),
"mm.txt");
BoardGameTester.java:
package games.boards;
import games.boards.*;
public class BoardGameTester {
public static void main(String[] args) {
// board for tictactoe
Board ticTacToeBoard = new Board(3, 3);
//board for Connect Four
Board connectFourBoard = new Board(6,7);
//board for Mastermind
Board mastermindBoard = new Board(5,8);
//cell to a nought or cross on the Tic-Tac-Toe board
ticTacToeBoard.setCell(Mark.NOUGHT, 0, 0);
ticTacToeBoard.setCell(Mark.CROSS, 1, 1);
//cell to yellow or red on the Connect Four board
connectFourBoard.setCell(Mark.YELLOW, 2, 3);
connectFourBoard.setCell(Mark.RED, 3, 4);
//cell to yellow, red, green, blue, magenta, or orange on the Mastermind board
mastermindBoard.setCell(Mark.YELLOW, 0, 0);
mastermindBoard.setCell(Mark.RED, 0, 1);
mastermindBoard.setCell(Mark.GREEN, 0, 2);
mastermindBoard.setCell(Mark.BLUE, 0, 3);
mastermindBoard.setCell(Mark.MAGENTA, 0, 4);
mastermindBoard.setCell(Mark.ORANGE, 0, 5);
//boards for Tic-Tac-Toe, Connect Four, and Mastermind
System.out.println("TicTacToe board:");
System.out.println(ticTacToeBoard);
System.out.println("Connect four board:");
System.out.println(connectFourBoard);
System.out.println("Mastermind board:");
System.out.println(mastermindBoard);
}
}
The first argument in each method call should have the name used for the corresponding Board object. Remember to import the games.utilities package!
Java How To Program Late Objects Version
ISBN: 9780136123712
8th Edition
Authors: Paul Deitel, Deitel & Associates