java code problem
level: introduction to java
these methods are in Streamline.java class
just the methods loadFromFile(String filename) and saveToFile() no GUI
The snapshots below is information to help with the methods above
In Streamline.java
In GameState.java class
loadFromFile0 protected void loadFromFile(String filename) throws IOException This helper method takes in the parameter filename. Read the file's content, and initialize the appropriate instance variables. After loading everything from the file, check whether the game is already over. You can assume that the file you read in should always be correct. The file to read in has the following format: saveToFile) format Explanation
print rows and columns of the game state> 0 To summarize what is in the file you are reading: 1. The first line is the board's height, a space, and then the board's width. 2. The second line is the player's position height, a space, and then the player's position width. 3. The third line is the goal's position height, a space, and then the goal's position width. 4. Finally, there is the printed the board. There should be a space for every empty part of the board and a capital X for every "block". The board here doesn't contain the goal and player positions nor the boundaries. In the example above, there are trailing spaces. Each line representing a row of the board has exactly the same number characters. Hints: - Always a good file: We will always use an existing and well-formatted file when testing your program. You do not need to consider any edge cases in this method. -Loadable: You can now also run the game by using java Streamline (e.g. java Streamline game.txt) to load a game from the file. Give me some space: Since the space character is part of the grid, we need to read the spaces, instead of ignoring them. Using next) from the Scanner class might not be a good idea since it skips all the spaces. Look at the documentation for Scanner to see which method(s) might be good for this purpose. saveToFile0 void saveToFile) This method writes the Streamline game to a file in the exact format mentioned above (See the diagram under loadFromFile) for the format and explanation). Use OUTFILE_NAME as the filename which you will save to (See the provided constant at the top of Streamline.java). Make sure that your formatting matches the formatting specified above exactly Once finished writing to the file, close it and print a message in the following format informing the user that the file was successfully saved Saved current state to: saved streamline_game Hints: To write to a file, you can use PrintWriter. Read the documentation to understand how it works and check out the methods you can use. Don't forget to close the stream public class Streanline final static int DEFAULT HEIGHT = final static int DEFAULT VIDTH = 5; final static String OUTFILE NANE "Saved strea line_gane"; GameState currentState: List previousStates; loadFromFile0 protected void loadFromFile(String filename) throws IOException This helper method takes in the parameter filename. Read the file's content, and initialize the appropriate instance variables. After loading everything from the file, check whether the game is already over. You can assume that the file you read in should always be correct. The file to read in has the following format: saveToFile) format Explanation print rows and columns of the game state> 0 To summarize what is in the file you are reading: 1. The first line is the board's height, a space, and then the board's width. 2. The second line is the player's position height, a space, and then the player's position width. 3. The third line is the goal's position height, a space, and then the goal's position width. 4. Finally, there is the printed the board. There should be a space for every empty part of the board and a capital X for every "block". The board here doesn't contain the goal and player positions nor the boundaries. In the example above, there are trailing spaces. Each line representing a row of the board has exactly the same number characters. Hints: - Always a good file: We will always use an existing and well-formatted file when testing your program. You do not need to consider any edge cases in this method. -Loadable: You can now also run the game by using java Streamline (e.g. java Streamline game.txt) to load a game from the file. Give me some space: Since the space character is part of the grid, we need to read the spaces, instead of ignoring them. Using next) from the Scanner class might not be a good idea since it skips all the spaces. Look at the documentation for Scanner to see which method(s) might be good for this purpose. saveToFile0 void saveToFile) This method writes the Streamline game to a file in the exact format mentioned above (See the diagram under loadFromFile) for the format and explanation). Use OUTFILE_NAME as the filename which you will save to (See the provided constant at the top of Streamline.java). Make sure that your formatting matches the formatting specified above exactly Once finished writing to the file, close it and print a message in the following format informing the user that the file was successfully saved Saved current state to: saved streamline_game Hints: To write to a file, you can use PrintWriter. Read the documentation to understand how it works and check out the methods you can use. Don't forget to close the stream public class Streanline final static int DEFAULT HEIGHT = final static int DEFAULT VIDTH = 5; final static String OUTFILE NANE "Saved strea line_gane"; GameState currentState: List previousStates