Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In Java Files Provided: https://drive.google.com/drive/folders/11-jMQflpPFPqbwlOtWT74Ow_i2_wXWUO?usp=sharing Introduction Valentines Day is fast approaching, and the hero of our story, Cupid, is trying his best to spread some

In Java

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

Files Provided: https://drive.google.com/drive/folders/11-jMQflpPFPqbwlOtWT74Ow_i2_wXWUO?usp=sharing

Introduction Valentines Day is fast approaching, and the hero of our story, Cupid, is trying his best to spread some platonic love and friendship in these gloomy times. If you didn't know, Cupid is an archer, and archers often prefer staying still and spending their time taking good shots with their bow. Cupid is no different in this regard, but he's heard you've been learning about stacks and algorithm design and was wondering if you could help him simulate some scenarios so that he's in tip-top shape (He hasn't been getting out much with the pandemic). He also understands that your assignment isn't due until the 4h of March, but he's happy to have it for future years anyways. You are given a map that describes Cupid and the targets for his arrows, which is divided into rectangular cells to simplify the task of computing the required path. There are different types of map cells: The starting point that Cupid wants to use Map cells where the target is located, Map cells indicating roadblocks or other barriers Map cells containing pathways. There are 3 types of pathways: Cross paths. A cross path located in cellL can be used to interconnect all the neighbouring map cells of L. A cell L has at most 4 neighbouring cells that we denote as the north, south, east, and west neighbours. The cross path can be used to interconnect all neighbours of L: Vertical paths. A vertical path can be used to connect the north and south neighbours of a map cell; and Horizontal paths. A horizontal path can be used to connect the east and west neighbours of a map cell Figure 1 shows an example of a map divided into cells. Each map cell has up to 4 neighbouring cells indexed from 0 to 3. Given a cell the north neighbouring cell has index 0 and the remaining neighbouring cells are indexed in clockwise order. For example, in Figure 1 the neighbouring cells of cell 8 are indexed from 0 to 3 as follows: neighbour with index O is cell 1, neighbour with index 1 is cell 9, neighbour with index 2 is cell 13, and neighbour with index 3 is cell 7. Some cells have fewer than 4 neighbours and the Indices of these neighbours might not be consecutive numbers, for example, cell 4 in Figure 1 has 2 neighbours indexed 1 and 2 (no 0 or 3 neighbours) A path from Cupid (cell number 12 in the figure) to a target (cell number O) is the following: 12.7.2.1, 0. A path from Cupid to another target (cell number 4) is the following: 12,7. 2. 3. 4. Note that in some other maps, cells will be inaccessible because they are walls, or because the path is horizontal and the only way is vertical, or vice versa. Valid Paths When looking for a path the program must satisfy the following conditions: The path can go from Cupid, a cross path cell, or a target cell to the following neighbouring cells: A target cell A cross path cell The north cell or the south cell, if such a cell is a vertical path, or The east cell or the west cell, if such a cell is a horizontal path. The path can go from a vertical path cell to the following neighbouring cells: The north cell or the south cell, if such a cell is either Cupid, a cross path cell, a target cell, or a vertical path cell. The path can go from a horizontal path cell to the following neighbouring cells: The east cell or the west cell, if such a cell is either Cupid, a cross path cella target cell, or a horizontal path cell Map 3 2 5 6 7 + Figure 1. A sample of one of Cupid's maps represented as a grid of cells Limitation: If while looking for a path the program finds that from the current cell there are several choices as to which adjacent cell to use to continue the path your program must select the next cell for the path in the following manner An object in motion in motion will stay in motion unless acted on by an unbalanced force. The arrows will continue in the same direction they were heading if the path is available. The program prefers a target cell over the other cells. If there is no target cell adjacent to the current cell, then the program must prefer the cross path cell over the other cells and If there is no cross-path cell, then the program chooses the smallest indexed cell that satisfies the conditions described above. While Cupid's arrows are magic, they are only so magical. The arrows Cupid uses can backtrack up to 3 times before they are unable to do so again. Cupid's arrows can only turn so much as well. Keep track of the inertia of the arrow by tracking how many times it has traveled in the same direction. After it has taken 3 steps in the same direction, it can no longer turn. For simplicity, it may take 2 steps and turn as many times as needed for the pathfinding. Note: The initial step required to pick a direction does not count for inertia. Importantly, Cupid will only shoot an arrow down the same path once. Cupid's arrows should stop when they hit a target, and the stack should be popped until empty. . The distance Cupid can shoot an arrow is configurable and provided as an argument to the program Lastly, Cupid fires arrows one at a time. Provided files The following is a list of files provided to you for this assignment. You do not need alter these files in any way and should assume we will mark them with unmodified ones. Studying these will help improve your understanding of Java. Map.Java - This class represents the map of the neighbourhood, including Cupid's starting location and the target houses. The public methods from this class you might use include: Map (String inputFile) throws InvalidMapException FileNotFoundException, IOException. This method reads the input file and displays the map on the screen. An InvalidMapException is thrown if the inputFile has the wrong format. Map Cell getStart(). Returns a MapCall object for the cell that Cupid starts in Int quiverSize(). Returns the number of targets that can fit in Cupid's quiver. Note: Lines 45-47 set the delay times for showing the path. You may wish to enable line 47 when testing many maps to have it go quicker. Map Cell.java - This class represents the cells of the map. Objects of this class are created inside the class Map when its constructor reads the map file. The methods that you might use from this class are: Map Cell neighbourCell(int i) throws InvalidNeighbourindexException. As explained in Section 1, each cell of the map has up to four neighbouring cells, indexed from 0 to 3. This method retums either a MapCell object representing the i-th neighbour of the current cell or null if such a neighbour does not exist Remember that if a cell has fewer than 4 neighbouring cells, these neighbours do not necessarily need to appear at consecutive index values. So, it might be, for example, that this.getNeighbour(2) is null, but this getNeighbouri) for all other values of i are not null. An InvalidNeighbourindexException exception is thrown if the value of the parameter i is negative or larger than 3. boolean methods is Blocked), is CrossPath() is VerticalPath(), is HorizontalPath(). isCupid(), is Target(), return true if this MapCell object represents a cell corresponding to a blocked path, a cross path, a vertical path, a horizontal path, Cupid, or a target respectively. boolean isMarked() returns true if this MapCell object represents a cell that has been marked as inStack or outorStack. void markinStack() marks this MapCell object as inStack. void markOutStack() marks this MapCell object as outOfStack Other classes provided ArrayStackADT.java. CellColors.java. CellComponent.java. Invalid NeighbourindexException java, CelLayout java. InvalidMapException java, llegalArgumentException.java. Classes to implement A description of the dasses that you are required to implement for full marks in this assignment are given below. You may implement more classes if you want, you must submit the code for these with your assignment. In all these classes, you may implement more private (helper) methods as desired, but you may not implement more public methods. You may not add static methods or instance variables. Penalties will be applied if you implement these For this assignment, you may not use Java's Stack class, although reading and understanding the code there may help you better understand stacks. You also may not use any other pre- made Java collections libraries. The data structure required for this assignment is an array, described below: ArrayStack.java This class implements a stack using an array. The header for this class must be this: public class ArrayStack implements ArrayStackADT This class will have the following private instance variables: T stack This array stores the data items of the stack. int top This variable stores the position of the last data item in the stack. In the constructor this variable must be initialized to -1, this means the stack is empty. . Note that this is different from the way in which the variable top is used in the lecture notes This class will have the following public static variable: public static String sequence: Used for tracking the arrow path This class needs to provide the following public methods: Array Stack() Creates an empty stack. The default initial capacity of the array used to store the items of the stack is 14 (for Valentine's day) ArrayStack(int initial Capacity). . Creates an empty stack using an array of length equal to the value of the parameter void push(T dataltem) Adds dataltem to the top of the stack. If the array storing the data items is full, you will increase its capacity as follows: If the capacity of the array is smaller than 50, then the capacity of the array will be increased by 10. . Otherwise, the capacity of the array will increase by doubling the initial size. So, if, for example, the size of the array is 225 and the array is full, when a new item is added the size of the array will increase to 450. At the end of this method, add the following. This is used in Test Search to make sure you are following a correct path: if (dataltem instanceof MapCell) { sequence = "push" + ((Map Celt dataltem).getidentifier(); else { sequence += "push" + dataitem.toString); } T pop() throws EmptyStackException Removes and returns the data item at the top of the stack. An Empty StackException is thrown if the stack is empty. ff after removing a data item from the stack the number of data items remaining is smaller than one fourth of the length of the array you need to shrink the size of the array by one half, to a minimum of 14 To do this create a new array of size equal to half of the size (to a minimum of 14) of the original array and copy the data items there. . For example, if the stack is stored in an array of size 100 and it contains 25 data items, after performing a pop operation the stack will contain only 24 data items. Since 24 implements ArrayStackADT This class will have the following private instance variables: T stack This array stores the data items of the stack. int top This variable stores the position of the last data item in the stack. In the constructor this variable must be initialized to -1, this means the stack is empty. . Note that this is different from the way in which the variable top is used in the lecture notes This class will have the following public static variable: public static String sequence: Used for tracking the arrow path This class needs to provide the following public methods: Array Stack() Creates an empty stack. The default initial capacity of the array used to store the items of the stack is 14 (for Valentine's day) ArrayStack(int initial Capacity). . Creates an empty stack using an array of length equal to the value of the parameter void push(T dataltem) Adds dataltem to the top of the stack. If the array storing the data items is full, you will increase its capacity as follows: If the capacity of the array is smaller than 50, then the capacity of the array will be increased by 10. . Otherwise, the capacity of the array will increase by doubling the initial size. So, if, for example, the size of the array is 225 and the array is full, when a new item is added the size of the array will increase to 450. At the end of this method, add the following. This is used in Test Search to make sure you are following a correct path: if (dataltem instanceof MapCell) { sequence = "push" + ((Map Celt dataltem).getidentifier(); else { sequence += "push" + dataitem.toString); } T pop() throws EmptyStackException Removes and returns the data item at the top of the stack. An Empty StackException is thrown if the stack is empty. ff after removing a data item from the stack the number of data items remaining is smaller than one fourth of the length of the array you need to shrink the size of the array by one half, to a minimum of 14 To do this create a new array of size equal to half of the size (to a minimum of 14) of the original array and copy the data items there. . For example, if the stack is stored in an array of size 100 and it contains 25 data items, after performing a pop operation the stack will contain only 24 data items. Since 24

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_2

Step: 3

blur-text-image_3

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

More Books

Students also viewed these Databases questions

Question

5. How can you listen critically to others public speeches?

Answered: 1 week ago

Question

Commen Name with scientific name Tiger - Wolf- Lion- Cat- Dog-

Answered: 1 week ago

Question

3. An overview of the key behaviors is presented.

Answered: 1 week ago

Question

2. The model is credible to the trainees.

Answered: 1 week ago