Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java Program it is Java Assignment overview Suzie is a mouse in the big city. She needs to collect a lot of cheese that will

Java Program

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

it is Java

Assignment overview Suzie is a mouse in the big city. She needs to collect a lot of cheese that will last her throughout the cold winter months. Each day, Suzie travels to a new city to gather up all the cheese crumbs she can find Cheese crumbs are gathered in boxes, so Suzie has to open each box to steal the cheese. News of the cheese prowler has spread across neighbouring cities like wildfire. Concerned cities determined to protect their precious cheese have hired exterminators to take out the notorious Suzie. So, she has to be extra careful now. Unbeknown to Suzie, exterminators now hide "traps in some of these boxes, to trap Suzie when she opens it. Your goal is to lead Suzie to the cheese in these dangerous times Unless specified otherwise, all instance variables must be private, and all methods should be public. Also, unless specified otherwise, there should be no println statements in your classes. Objects usually do not print anything, they only return String values from toString methods Your code should be DRY- don't repeat yourself. If you have a method that already accomplishes a task, or has some logic, that logic should be used. You may add any private methods you see fit to help accomplish this task The description of the assignment has methods that are required. There are suggested names for the parameters, which you may change. Do not change the signatures of the methods. Your classes and objects should work with the provided test files You may not use an ArrayList in this assignment. You may write your own stack class, but that is not the expectation for this assignment Testing Your Coding We have provided sample test files for each phase to help you see if your code is working according to the assignment specifications. These files are starting points for testing your code. Part of developing your skills as a programmer is to think through additional important test cases and to write your own code to test these cases. Different, more vigorous tests will be used to test your program. Sample output is provided in some cases for you to match as closely as possible. Other output is left out for you to practice reading code, understand a program and practice seeing output is correct or not based on inputs Phase 1: The BigCity A city is defined by an MxNgrid M may not be equal to N). The city will show the starting position of Suzie (denoted by belowl. Suzie's starting position is always at (0, 0). The city will also show the boxes (denoted by t'). Valid paths through the city are denoted by the character The following is a 4x5 city grid (4 rows and 5 columns), with 3 boxes which may contain cheese or a trap! 5. b. . b . . b First create the city grid, named class BigCity. The Bigcity class should have . The following instance variables: . a 2D array of char to hold the grid - an int to hold the number of boxes in the grid an int to hold the number of cheese boxes. This value is less than or equal to Ure number of bures in the grid a 20 array of int to hold the positions of the boxes that contain cheese A constructor Bigcitylint rows, int cols, int numBoxes, int numCheese, int 01 cheese Positions) which creates a grid. It assigns the 2D array instance of cheese positions to the cheese Positions parameter passed A private method fillGrid that: - M iks Sude's slal positional (0, 0) with's marks the positions in cheese Positions as b. cheese Positions will never contain (0,0) marks (numBoxes minus numCheese) random positions in the grid as 'b' It cannot mark (0,0) or any positions in cheese Positions. You can create a private method to do this. marks the remaining positions as This method should be called from the constructor you defined above A standard toString method, which returns a text representation of the city Given the grid above, the constructor may be passed the following: Ow = 4 cols-5 numBoxe 3 numCheece-2 could be 31. or but not greater than 3) cheese Positions - (3, 1), (0.3)) (the number of rows is always numCheese, the number of columns is always 2. Each row is are coordinates of a piece of cheese.) Given this sample input, you can tell that the box at (3.4) contains a trap for the grid shown above, as it is not in the list of cheese Positions provided 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 (numCheesel lines - The positions of cheese separated by a space in the format: row column For example: 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 Higcity(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: rile 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 Your M e s 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 puteti Input file: Exception Thrown: java.io.IOException: No dimension to read Sample output 2 Input file: 22 Exception Thrown: java.io.IOException: Inaccurate number of rows of cheese positions in the file Sow O expected 1 Sample output 3 Input file: 45 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 accume that the gnd is good. Note: you do not need to do all the error handling in this phase to start working on the next phase Phase 3: Moving through Big City Add the following to enable moving along the city grid Create 2 count variables: one to track how many cheese crumbs Suzie has collected, and one to track how many moves Sure has made moving through the grid. Do not forget to update both constructors to m alue them Create 1 boolean instance variable to indicate that Suzie still loose and roaming the city Create a method vold move(char direction). This will move Suzie in the direction specified. The following characters will represent the movements along the grid . w up s . down left . right Throw appropriate IndexOutOfBoundsException for moving off the grid. If the moveis valid, then process the move using the following helper method Create method private void process Movelchar direction), which determines the next outcome of the game based on the direction from the move() method A ventu a valid path() increases the number of moves Sucie Tamale A move onto a 'b' can either. increase Suzie's cheese collection by 1 (as well as her move count), or end the game and increase her move count) by calling the endTerror() method you will implement in the next phase Update the grid to show Suzie's movements. If she moves, replace her last position in the grid with '.' and set the char at the new position to 's IP Suzie collects a cheese, replace the 'b' with 's' to show where she currently is. When she moves from that location update the grid accordingly. Phase 4: Game ending condition Suzie has to keep scouring the city for boxes of cheese crumbs. She only stops when any of these happens Suzie has collected all the cheese in the city. or Suzie is caught by a trap in a box. To do this: Create method private void endTerror() that changes the value of the variable indicating that Suzie is still roaming the city. Create a method boolean isRoaming City) that tells the outside world of Susie is still on the loose in the Big City! Update the process Move method to check if the game is over, and calling endTerror to end the game Phase 5: Extracting data First, create your own Exception type named DataDoes NotExistException for this class implement only a constructor that receives a String message Then add two methods to your Big City class: char[] extractRowlint row Num) that extracts the row specified by the passed integer. O will fetch the first row. 1 the second index-by-zero rules) charextractColumn(int colNum), which extracts a single column, much like extract row Both methods should throw a DataDoes NotExistException when passed a row or column number that does not exist in your city grid For example, if your city is a 3x3 grid and the extractRow() method is passed a-1 Exception Thro: Database Existexception City grid does not have a wide op 3 Phase 6: Update the toString with Suzie's stats! When Sucie's reign of terror is done in a Big City. update the output of your toString() method to include one of the following on a new line in addition printing out the map. For an extra, optional challenge: watch out for singular and plural tenses. Sample output Surse outsmarted the exterminators and got my Moves - 5 cheese collected - Z traps escaped - 3 Susie's reign of terror cane to an end when she met a trap instead of cheese Moves -20 cheese collected-1 If the game is still running, print out just the map, as described in Phase 1 Hint. You will know if Suzie was captured if the she is no longer roaming the city and somehow there are still cheese crumbs left Phase 7: Suzie's Guardian Angel, Acqueline Suzie's guardian angel. Acqueline is always watching out for her she sees her every move and keeps track of them. Acqueline can only keep track of the last 5 moves Suzie makes This enables Suzie go back in time a maximum of 5 steps at once. To bring Acqueline to life, implement the following: Add a partially-filled array to the class that holds a maximum of 5 2D array of char Again, remember to initialize it in both contructors - You do not have to set the dimension of the inner 20 Tays Create a method private void saveGrid that stores a copy of the city grid at index O of this partially filled array if there are elements in the partially filled array, move them over to the right if the array is full and you need to insert at index o, discard the last element in the array by moving the rest over to make room at index 0 For example: consider adding values a, b, cd, e, fto a char array in the same manner Adding a: la.... size: 1 Adding h: [b,a,..] size: 2 Adding c: (c.b.a..) size: 3 Adding d: [d.c.b.a.) size: 4 Adding e: (e,d,c,b,a] size: 5 Adding f: [f.e,d.c.b) size: 5 Modify the processMove() method to call saveGrid() before modifying the contents of the grid. Now, create a method void undo(), which replaces the 2D grid instance with the 2D array at index 0 of this partially filled array. If the partially-filled array is empty, it should do nothing. When this is done, element of the partially-filled array is removed, and the remaining 2D arrays must shuffle left to fill the space. Starting array: [fie,d,c,b) size: 5 After undo: [e,d.c.b, ) size: 4 Note: an undo() should do the following: *if Suzie is not roaming the city or the partially-filled array is empty, do nothing. Else: * change the 2D instance grid to point to first item in the partially-filled array * remove that first item from the array and shuffle the rest left * go through the replaced grid to set the new x and y positions of Suzie to that of 's' in this replaced grid *reduce the numberOfMoves counter variable by 1 * if the new frame put back a box on the grid, it MUST be a cheese. In that case, also reduce the numCheeseCollected counter variable by 1 Assignment overview Suzie is a mouse in the big city. She needs to collect a lot of cheese that will last her throughout the cold winter months. Each day, Suzie travels to a new city to gather up all the cheese crumbs she can find Cheese crumbs are gathered in boxes, so Suzie has to open each box to steal the cheese. News of the cheese prowler has spread across neighbouring cities like wildfire. Concerned cities determined to protect their precious cheese have hired exterminators to take out the notorious Suzie. So, she has to be extra careful now. Unbeknown to Suzie, exterminators now hide "traps in some of these boxes, to trap Suzie when she opens it. Your goal is to lead Suzie to the cheese in these dangerous times Unless specified otherwise, all instance variables must be private, and all methods should be public. Also, unless specified otherwise, there should be no println statements in your classes. Objects usually do not print anything, they only return String values from toString methods Your code should be DRY- don't repeat yourself. If you have a method that already accomplishes a task, or has some logic, that logic should be used. You may add any private methods you see fit to help accomplish this task The description of the assignment has methods that are required. There are suggested names for the parameters, which you may change. Do not change the signatures of the methods. Your classes and objects should work with the provided test files You may not use an ArrayList in this assignment. You may write your own stack class, but that is not the expectation for this assignment Testing Your Coding We have provided sample test files for each phase to help you see if your code is working according to the assignment specifications. These files are starting points for testing your code. Part of developing your skills as a programmer is to think through additional important test cases and to write your own code to test these cases. Different, more vigorous tests will be used to test your program. Sample output is provided in some cases for you to match as closely as possible. Other output is left out for you to practice reading code, understand a program and practice seeing output is correct or not based on inputs Phase 1: The BigCity A city is defined by an MxNgrid M may not be equal to N). The city will show the starting position of Suzie (denoted by belowl. Suzie's starting position is always at (0, 0). The city will also show the boxes (denoted by t'). Valid paths through the city are denoted by the character The following is a 4x5 city grid (4 rows and 5 columns), with 3 boxes which may contain cheese or a trap! 5. b. . b . . b First create the city grid, named class BigCity. The Bigcity class should have . The following instance variables: . a 2D array of char to hold the grid - an int to hold the number of boxes in the grid an int to hold the number of cheese boxes. This value is less than or equal to Ure number of bures in the grid a 20 array of int to hold the positions of the boxes that contain cheese A constructor Bigcitylint rows, int cols, int numBoxes, int numCheese, int 01 cheese Positions) which creates a grid. It assigns the 2D array instance of cheese positions to the cheese Positions parameter passed A private method fillGrid that: - M iks Sude's slal positional (0, 0) with's marks the positions in cheese Positions as b. cheese Positions will never contain (0,0) marks (numBoxes minus numCheese) random positions in the grid as 'b' It cannot mark (0,0) or any positions in cheese Positions. You can create a private method to do this. marks the remaining positions as This method should be called from the constructor you defined above A standard toString method, which returns a text representation of the city Given the grid above, the constructor may be passed the following: Ow = 4 cols-5 numBoxe 3 numCheece-2 could be 31. or but not greater than 3) cheese Positions - (3, 1), (0.3)) (the number of rows is always numCheese, the number of columns is always 2. Each row is are coordinates of a piece of cheese.) Given this sample input, you can tell that the box at (3.4) contains a trap for the grid shown above, as it is not in the list of cheese Positions provided 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 (numCheesel lines - The positions of cheese separated by a space in the format: row column For example: 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 Higcity(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: rile 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 Your M e s 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 puteti Input file: Exception Thrown: java.io.IOException: No dimension to read Sample output 2 Input file: 22 Exception Thrown: java.io.IOException: Inaccurate number of rows of cheese positions in the file Sow O expected 1 Sample output 3 Input file: 45 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 accume that the gnd is good. Note: you do not need to do all the error handling in this phase to start working on the next phase Phase 3: Moving through Big City Add the following to enable moving along the city grid Create 2 count variables: one to track how many cheese crumbs Suzie has collected, and one to track how many moves Sure has made moving through the grid. Do not forget to update both constructors to m alue them Create 1 boolean instance variable to indicate that Suzie still loose and roaming the city Create a method vold move(char direction). This will move Suzie in the direction specified. The following characters will represent the movements along the grid . w up s . down left . right Throw appropriate IndexOutOfBoundsException for moving off the grid. If the moveis valid, then process the move using the following helper method Create method private void process Movelchar direction), which determines the next outcome of the game based on the direction from the move() method A ventu a valid path() increases the number of moves Sucie Tamale A move onto a 'b' can either. increase Suzie's cheese collection by 1 (as well as her move count), or end the game and increase her move count) by calling the endTerror() method you will implement in the next phase Update the grid to show Suzie's movements. If she moves, replace her last position in the grid with '.' and set the char at the new position to 's IP Suzie collects a cheese, replace the 'b' with 's' to show where she currently is. When she moves from that location update the grid accordingly. Phase 4: Game ending condition Suzie has to keep scouring the city for boxes of cheese crumbs. She only stops when any of these happens Suzie has collected all the cheese in the city. or Suzie is caught by a trap in a box. To do this: Create method private void endTerror() that changes the value of the variable indicating that Suzie is still roaming the city. Create a method boolean isRoaming City) that tells the outside world of Susie is still on the loose in the Big City! Update the process Move method to check if the game is over, and calling endTerror to end the game Phase 5: Extracting data First, create your own Exception type named DataDoes NotExistException for this class implement only a constructor that receives a String message Then add two methods to your Big City class: char[] extractRowlint row Num) that extracts the row specified by the passed integer. O will fetch the first row. 1 the second index-by-zero rules) charextractColumn(int colNum), which extracts a single column, much like extract row Both methods should throw a DataDoes NotExistException when passed a row or column number that does not exist in your city grid For example, if your city is a 3x3 grid and the extractRow() method is passed a-1 Exception Thro: Database Existexception City grid does not have a wide op 3 Phase 6: Update the toString with Suzie's stats! When Sucie's reign of terror is done in a Big City. update the output of your toString() method to include one of the following on a new line in addition printing out the map. For an extra, optional challenge: watch out for singular and plural tenses. Sample output Surse outsmarted the exterminators and got my Moves - 5 cheese collected - Z traps escaped - 3 Susie's reign of terror cane to an end when she met a trap instead of cheese Moves -20 cheese collected-1 If the game is still running, print out just the map, as described in Phase 1 Hint. You will know if Suzie was captured if the she is no longer roaming the city and somehow there are still cheese crumbs left Phase 7: Suzie's Guardian Angel, Acqueline Suzie's guardian angel. Acqueline is always watching out for her she sees her every move and keeps track of them. Acqueline can only keep track of the last 5 moves Suzie makes This enables Suzie go back in time a maximum of 5 steps at once. To bring Acqueline to life, implement the following: Add a partially-filled array to the class that holds a maximum of 5 2D array of char Again, remember to initialize it in both contructors - You do not have to set the dimension of the inner 20 Tays Create a method private void saveGrid that stores a copy of the city grid at index O of this partially filled array if there are elements in the partially filled array, move them over to the right if the array is full and you need to insert at index o, discard the last element in the array by moving the rest over to make room at index 0 For example: consider adding values a, b, cd, e, fto a char array in the same manner Adding a: la.... size: 1 Adding h: [b,a,..] size: 2 Adding c: (c.b.a..) size: 3 Adding d: [d.c.b.a.) size: 4 Adding e: (e,d,c,b,a] size: 5 Adding f: [f.e,d.c.b) size: 5 Modify the processMove() method to call saveGrid() before modifying the contents of the grid. Now, create a method void undo(), which replaces the 2D grid instance with the 2D array at index 0 of this partially filled array. If the partially-filled array is empty, it should do nothing. When this is done, element of the partially-filled array is removed, and the remaining 2D arrays must shuffle left to fill the space. Starting array: [fie,d,c,b) size: 5 After undo: [e,d.c.b, ) size: 4 Note: an undo() should do the following: *if Suzie is not roaming the city or the partially-filled array is empty, do nothing. Else: * change the 2D instance grid to point to first item in the partially-filled array * remove that first item from the array and shuffle the rest left * go through the replaced grid to set the new x and y positions of Suzie to that of 's' in this replaced grid *reduce the numberOfMoves counter variable by 1 * if the new frame put back a box on the grid, it MUST be a cheese. In that case, also reduce the numCheeseCollected counter variable by 1

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning ASP.NET 2.0 And Databases

Authors: John Kauffman, Bradley Millington

1st Edition

0471781347, 978-0471781349

More Books

Students also viewed these Databases questions

Question

=+ How about one you felt had acted in a hypocritical way?

Answered: 1 week ago