Question
###Java prog question Phase 2: Loading BigCity from files We want to be able to load a city from a file. The format is as
###Java prog question
Phase 2: Loading BigCity from files We want to be able to load a city from a file. The format is as follows: First line - The number of rows and the number of columns of the city grid The next (# rows) lines - The city grid separated by whitespace The following line - The number of boxes Next line - The number of cheese boxes Next (numCheese) lines - The positions of cheese separated by a space in the format: row column For example: 6 5 s . . . . . . b . . . . . . . b b . . . . . . . b . . . . . 4 2 3 0 1 2 is a valid file with a 6x5 city grid with 4 boxes and 2 cheese at locations (3, 0) and (1, 2). To do this, add the following to your BigCity class: Following good programming practice, create the following final static chars for the relevant grid pieces: final static char BLANK = '.'; final static char SUZIE = 's'; final static char BOXES = 'b'; Add a second constructor BigCity(String fileName). This constructor reads the data in the text file specified by fileName. Do not forget to initialize your instance variables. This method should throw an IOException with an appropriate message if the data is not valid. Special exception messages include: File specified was not found Bad dimensions for the grid Inaccurate number of rows of data to read Inaccurate number of columns of data to read ASSIGNMENT 2: Multidimensional arrays, Files and Exceptions COMP 1020 Winter 2020 Your error messages should have as much detail as possible. See the sample output for what is expected. This method is quite long, due to the error handling, and will exceed the usual 15 lines of code rule-of-thumb. Sample output 1 Input file: s . . b 1 1 1 1 Exception Thrown: java.io.IOException: No dimension to read Sample output 2 Input file: 2 2 s . . b 1 1 Exception Thrown: java.io.IOException: Inaccurate number of rows of cheese positions in the file. Saw 0 expected 1 Sample output 3 Input file: 4 5 s . . . . b . . . . . . 1 1 1 1 Exception Thrown: java.io.IOException: Inaccurate number of columns of cheese in the file. Saw 4 expected 5 Do not bother error-checking the individual characters on the grid - the only thing you should check is if the number of rows and columns match what was provided. If they do, assume that the grid is good. Note: you do not need to do all the error handling in this phase to start working on the next phase
class BigCity { char grid[][]; int numBoxes; int numCheese; int cheesePos[][]; BigCity(int rows, int cols, int numBoxes, int numCheese, int [][] cheesePositions){ grid = new char[rows][cols]; this.numBoxes = numBoxes; this.numCheese = numCheese; cheesePos = cheesePositions; fillGrid(); } void fillGrid(){ //initialise grid with '.' int rows = grid.length; int cols = grid[0].length; for(int i=0;i Test Phase -2 .... CityGrid_GoodData 6 5 s . . . . . . b . . . . . . . b b . . . . . . . b . . . . . 4 2 3 0 1 2 CityGrid_BadData 3 3 s . . . . b 1 1 1 2 public class TestPhase2{ public static void main (String[]parms){ testGoodInput(); testBadInput(); } public static void testGoodInput () { System.out.println("Testing good input data... "); try { System.out.println("This should print BigCity without errors"); BigCity game = new BigCity("CityGrid_GoodData1.txt"); System.out.println(game); } catch (IOException ioe) { System.out.println(ioe.getMessage()); }
public static void testBadInput () { System.out.println(" Testing bad input data... "); try { System.out.println("This should print: The file NoSuchFile.txt was not found"); BigCity game = new BigCity("NoSuchFile.txt"); System.out.println(game); } catch (IOException ioe) { System.out.println(ioe.getMessage() + " "); } try { System.out.println("This should print: No dimension to read"); BigCity game2 = new BigCity("CityGrid_BadData1.txt"); } catch (IOException ioe) { System.out.println(ioe.getMessage() + " "); }
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