Madeline in the Big City Madeline is a mouse in the big city. She needs to collect a lot of cheese that will last her th

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

Madeline in the Big City Madeline is a mouse in the big city. She needs to collect a lot of cheese that will last her th

Post by answerhappygod »

Madeline in the Big City Madeline is a mouse in the big city.She needs to collect a lot of cheese that will last her throughoutthe cold winter months. Each day, Madeline travels to a new city togather up all the cheese crumbs she can find. Cheese crumbs aregathered in boxes, so Madeline has to open each box to steal thecheese. News of the Maddy the mouse has spread across neigbouringcities like wildfire. Concerned cities determined to protect theirprecious cheese have hired extermi- nators to take out thenotorious Madeline. So she has to be extra careful now. Unbeknownto Madeline, exterminators now hide “traps” in some of the boxes,to trap Madeline when she opens it. Your objective Your goal is tolead Madeline to find all the cheese in these dangerous times.Phase 1: The BigCity A city is defined by an MxN grid (M may not beequal to N). The city will show the starting position of Madeline(denoted by s below). Madeline’s starting position is always at (0,0). The city will also show the boxes (denoted by ‘b’). Valid“paths” through the city are denoted by the ‘.’ character. Thefollowing is a 4x5 city grid (4 rows and 5 columns), with 3 boxes.Each box might contain cheese or it might be a trap! s . . b . . .. . . . . . . . . b . . b 1 First create the city grid, named classBigCity. The BigCity class should have: • The following instancevariables: – a 2D array of char to hold the grid – an int to holdthe number of boxes in the grid – an int to hold the number ofcheese in the boxes. This value is less than or equal to the numberof boxes in the grid – a 2D array of int to hold the positions ofthe boxes that contain cheese • A constructor BigCity(int rows, intcols, int numBoxes, int num- Cheese, int [ ][ ] cheesePositions)which creates a grid. It assigns the 2D array instance of cheesepositions to the cheesePositions parameter passed. Cheese positionsis an n x 2 2D array. cheesePositions[x][0] refers to row that thexth cheese is on and cheesePosition[x][1] is the column. Together,those two values give you the coordinates of the xth cheese (wherex is one of the cheeses). • A private method fillGrid() that: –marks Madeline’s start position at (0, 0) with ‘s’ – marks thepositions in cheesePositions as ‘b’. cheesePositions will nevercontain (0,0). – marks (numBoxes minus numCheese) random positionsin the grid as ‘b’. It cannot mark (0, 0) or any positions incheesePosi- tions. Create a private method to do this. – marks theremaining positions as ‘.’ This method should be called from theconstructor you defined above. • A standard toString method, whichreturns a text representation of the city. Given the grid above,the constructor may be passed the following: • rows = 4 • cols = 5• numBoxes = 3 • numCheese = 2 (could be 3, 1, or 0 but not greaterthan 3) • cheesePositions = {{3, 1}, {0, 3}} (the number of rows isalways num- Cheese, the number of of columns is always 2. Each rowis 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 gridshown above, as it is not in the list of cheesePositions provided.Phase 2: Loading BigCity from files We want to be able to load acity from a file. The format is as follows: • First line - Thenumber of rows and the number of columns of the city grid • Thenext (# rows) lines - The city grid separated by whitespace 2 • Thefollowing line - The number of boxes • Next line - The number ofcheese • Next (numCheese) lines - The positions of cheese separatedby a space in the format: row column For example: 6 5 s . . . . . .b . . . . . . . b b . . . . . . . b . . . . . 4 2 3 0 1 2 is avalid file with a 6x5 city grid with 4 boxes and 2 cheese atlocations (3, 0) and (1, 2). To do this, add the following to yourBigCity class: • Following good programming practice, create thefollowing final static chars for the relevant grid pieces: – finalstatic char BLANK = ‘.’; – final static char MADELINE = ‘s’; –final static char BOXES = ‘b’; • Add a second constructorBigCity(String fileName). This constructor reads the data in thetext file specified by “fileName”. Do not forget to initialize yourinstance variables. This method should throw an IOException with anappropriate message if the data is not valid. Special exceptionmessages include: – File specified was not found – Bad dimensionsfor the grid, including a 0x0 grid – Number of boxes <= 0 –Inaccurate number of rows of data to read – Inaccurate number ofcolumns of data to read Your error messages should have as muchdetail as possible. See the sample output for what is expected.This method is quite long, due to the error handling, and willexceed the usual “15 lines of code” rule-of-thumb. Sample output 1Input file: 3 s . . b 1 1 1 1 Exception Thrown:java.io.IOException: No dimension to read Sample output 2 Inputfile: 0 0 1 1 Exception Thrown: java.io.IOException: No grid toread Sample output 3 Input file: 2 2 s . . . 0 0 Exception Thrown:java.io.IOException: No boxes on grid Sample output 4 Input file: 22 s . . b 1 1 Exception Thrown: java.io.IOException: Inaccuratenumber of rows of cheese positions in the file. Saw 0 expected 1Sample output 5 Input file: 4 5 s . . . . b . . . . . . 1 1 1 1 4Exception Thrown: java.io.IOException: Inaccurate number of columnsof cheese in the file. Saw 4 expected 5 Do not bothererror-checking the individual characters on the grid - the onlything you should check is if the number of rows and columns matchwhat 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 tostart working on the next phase. Phase 3: Moving through BigCityAdd the following to enable moving along the city grid. Create 2count variables: one to track how many cheese crumbs Madeline hascollected, and one to track how many moves Madeline has made movingthrough the grid. Do not forget to update both constructors toinitialize them. Create 1 boolean instance variable to indicatethat Madeline is still loose and roaming the city. Create a methodvoid move(char direction). This will move Madeline in the directionspecified. The following characters will represent the movementsalong the grid • ‘w’ - up • ‘s’ - down • ‘a’ - left • ‘d’ - rightThrow appropriate IndexOutOfBoundsException for moving off thegrid. If the move is valid, then process the move using thefollowing helper method. Create method private voidprocessMove(char direction), which deter- mines the next outcome ofthe game based on the direction from the move() method. • A moveonto a valid path (‘.’) increases the number of moves Madeline hasmade. • A move onto a ‘b’ can either: – increase Madeline’s cheesecollection by 1 (as well as her move count), or – end the game (andincrease her move count) by calling the endTer- ror() method youwill implement in the next phase. Update the grid to showMadeline’s movements. If she moves, replace her last position inthe grid with ‘.’ and set the char at the new position to ‘s’. IfMadeline collects a cheese, replace the ‘b’ with ‘s’ to show whereshe currently is. When she moves from that location, update thegrid accordingly. 5 Phase 4: Game ending condition Madeline has tokeep scouring the city for boxes of cheese crumbs. She only stopswhen any of these happens: • Madeline has collected all the cheesein the city, or • Madeline is caught by a trap in a box. To dothis: Create method private void endTerror(), that changes thevalue of the variable indicating that Madeline is still roaming thecity. Create a method boolean isRoamingCity() that tells theoutside world if Madeline is still on the loose in the Big City!Update the processMove method to check if the game is over, andcalling endTerror to end the game. Phase 5: Extracting data First,create your own Exception type named DataDoesNotExistException. Forthis class, implement only a constructor that receives a Stringmessage. Then, add two methods to your BigCity class: • char [ ]extractRow(int rowNum) that extracts the row specified by thepassed integer. 0 will fetch the first row, 1 the second(index-by-zero rules). • char [ ] extractColumn(int colNum), whichextracts a single column, much like extract row. Both methodsshould throw a DataDoesNotExistException when passed a row orcolumn 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 Thrown: DataDoesNotExistException: BigCity grid doesnot have a row index of -1 Phase 6: Update the toString withMadeline’s stats! When Madeline’s reign of terror is done in a BigCity, update the output of your toString() method to include one ofthe following on a new line in addition printing out the map. Foran extra, optional challenge: watch out for singular and pluraltenses. Sample output: Madeline outsmarted the exterminators andgot away. moves - 5 6 cheese collected - 2 traps escaped - 3Madeline’s reign of terror came to an end when she met a trapinstead of cheese. moves - 10 cheese collected - 0 If the game isstill running, print out just the map, as described in Phase 1.Hint: You will know if Madeline was captured if the she is nolonger roaming the city and somehow there are still cheese crumbsleft. Phase 7: Madeline’s Guardian Angel, Acqueline Madeline’sguardian angel, Acqueline, is always watching out for her; she seesher every move and keeps track of them. Acqueline can only keeptrack of the last 5 moves Madeline makes. This enables Madeline goback in time a maximum of 5 steps at once. To bring Acqueline tolife, implement the following: • Add a partially-filled array tothe class that holds a maximum of 5 2D array of char. Again,remember to initialize it in both contructors. – You do not have toset the dimension of the inner 2D arrays. • Create a method privatevoid saveGrid() that stores a copy of the city grid at index 0 ofthis partially-filled array. If there are elements in thepartially-filled array, move them over to the right. If the arrayis full and you need to insert at index 0, discard the last elementin the array by moving the rest over to make room at index 0. Forexample: consider adding values a, b, c, d, e, f to a char array inthe same manner Adding a: [a, , , , ] size: 1 Adding b: [b,a, , , ]size: 2 Adding c: [c,b,a, , ] size: 3 Adding d: [d,c,b,a, ] size: 4Adding 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 modifyingthe contents of the grid. • Now, create a method void undo(), whichreplaces the 2D grid instance with the 2D array at index 0 of thispartially filled array. If the partially-filled array is empty, itshould do nothing. 7 When this is done, element 0 of thepartially-filled array is removed, and the remaining 2D arrays mustshuffle left to fill the space. Starting array: [f,e,d,c,b] size: 5After undo: [e,d,c,b, ] size: 4 Hand in Submit your two Java files(BigCity.java, DataDoesNotExistException.java). Do not submit.class or .java~ files! You do not need to submit theTestPhaseN.java files that were given to you. If you did notcomplete all phases of the assignment, use the Comments field whenyou hand in the assignment to tell the marker which phases werecompleted, so that only the appropriate tests can be run. Forexample, if you say that you completed Phases 1-2, then the markerwill compile your files with appropriate test cases. If it fails tocompile and run, you will lose all of the marks for the test runs.The marker will not try to run anything else, and will not edityour files in any way. Make sure none of your files specify apackage at the top! 8
Pls in java.
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply