1. Homepage
2. Programming
3. CS 1027B: Computer Science Fundamentals II Assignment 3 - National Park Bike Paths

# CS 1027B: Computer Science Fundamentals II Assignment 3 - National Park Bike Paths

UWOCS 1027BComputer Science Fundamentals IINational Park Bike PathsJava

# CS 1027B: Computer Science Fundamentals II

Online Section (650) Summer 2023

## Assignment 3 – National Park Bike Paths

Due Mon Jul 24 at 23:55pm ET

### Learning Outcomes

In this assignment, you will gain hands -on experience with: • implementing doubly linked lists, • implementing an extended stack abstract data type (ADT) using doubly linked lists, and • finding a path in a map using a stack.

### Introduction

The management of a National Park wants to improve the accessibility for visitors by building bike paths by creating a new National Park aro und the current Pyramid Creek Falls Provincial Park in British Columbia (which is a beau tiful area, if you ever get the chance to go see it). The park consists of a vast area with various terrains, including forests, hills, and lakes. The goal is to create a network of bike paths that connect different natural treasures within the park, such as scenic viewpoints, picnic areas, and historical sites. To achieve this, you need to develop a program that finds the optimal paths for the bike paths while considering the park's topography and safety. This Photo by Unknown Author is licensed under CC BY -SA Figure 1: Pyramid Falls in British Columbia, Canada

Let’s model the inside of the par k space as interconnected hexagonal chambers. Many of the chambers have dangerous wildlife or natural hazards in them, so it is very important that the bike paths only go through through chambers where enough light enters so the park visitors can bike through them saf ely. There are several types of chambers in the park: • Sealed chambers : chambers where the cyclists cannot enter. • Lighted chambers : chambers that have a skylight, a hole in the ceiling where sunlight enters. The cyclists can safely bike through these c hambers as there is enough light in them to spot the natural ha zards . • Dim chambers : these are chambers that are adjacent to a lighted chamber. Enough light enters these chambers to make it safe to bike through them. • Dark chambers : these are chambers where there is n o light, so the cyclysts should not enter them as they would not be safe. A dark chamber is one that has no adjacent lighted chambers. • Entrance chamber : this is the chamber through which the cyclist can enter the park. The entrance chamber is lighted. • Treasure chambers : these are chambers that hold invaluable natural relics that will le ave the bikers or hikers with lifetimes of memories . Treasure chambers are lighted. For this assignment you need to write a program that finds a path from the entrance chamber to all the treasure chambers that does not include any sealed or d ark chambers. As the path is being computed, the following constraints must be satisfied : • if the program has a choice between moving into a treasure chamber or a chamber without a treasure your program must prefer the treasure chamber • if the program has a choice between moving to a lighted chamber or moving to a dim chamber the program must prefer the lighted chamber.

The following figure shows an example of the possible chambers of the park. The entrance chamber has an arrow inside of it, lighted chambe rs have a yellow skylight in the middle, dim chambers do not have a skylight, dark chambers are black, sealed chambers are filled with rocks, and treasure chambers have a treasure in them. The path from the entrance to all the treasure chambers is marked i n blue. Note that the blue path in the figure includes two lighted chambers (near the entrance) and one dim chamber (between the two treasure chambers; you recognize the dim chamber because it does not have a skylight). Each chamber has up to 6 neighbors indexed from 0 to 5 . The neighbor with index 0 is in the upper right side of a chamber. The remaining neighboring chambers are increasingly indexed in clockwise direction. The figure shows the indices for the neighbors of a chamber adjacent to the entrance.

### Classes to implement

For this assignment, you must implement two Java classes: DLStack.java and PathFinder .java . Follow the guidelines for each one below. In these two classes, you can implement more private (helper) methods , if you want to, but you may not implement more public methods . You may not add instance variables other than the ones specified below nor change the variable types or accessibility (i.e. making a variable public when it should be private). Penalties will be applied if you implement additional instance variables or change the variable types from what is described here.

#### DLStack.java

This class represents an extended stack ADT implemented using a doubly linked list. An example of a stack is shown in the figure below; note the position of the top of the stack. The nodes of the doubly linked list are of the provided class DoubleLinkedNod e; this is the same class you used in Lab 4. You are also provide d with a java interface DLStackADT.java file that specifies the operations of this ADT. Hence, your implementation will declare class DLStack as follows: public class DLStack implements DLSta ckADT The class must have the following private instance variables: • DoubleLinkedNode top : This a reference to the node at the top of the stack. • int numItems : The value of this variable is the number of data items stored in the stack.

The class must have th e following public methods: • public DLStack() : Creates an empty stack, so top must be set to null and numItems to zero . • public void push(T dataItem) : Adds the given dataItem to the top of the stack . • public T pop() throws EmptyStackException : Removes and ret urns the data item at the top of the stack. An EmptyStackException is thrown if the stack is empty. • public T pop(int k) throws InvalidItemException : Removes and returns the k-th data item from the top of the stack. So if k = 1 the method must remove the da ta item at the top of the stack, if k = 2 the method must remove the second item from the top of the stack, and so on. An InvalidItemException is thrown if the value of k is larger than the number of data items stored in the stack or if k is less than or e qual to zero. The following figure illustrates this operation.

• public T peek() throws EmptyStackException : Returns the data item at the top of the stack without removing it. An EmptyStackException is thrown if the stack is empty. • public boolean isEmpty() : Returns true if the stack is empty and it returns false otherwise . • public int size() : Returns the number of data items in the stack. • public DoubleLinkedNode getTop() : Returns top. • pubic toString() : Returns a string of the form “[data1 data2 … datan]” , where data 1 is the data item at the top of the stack, and data n is the data item at the bottom of the stack.

### Algorithm for Exploring the Park

Here is a description in pseudocode of an algorithm for trying to find a bike path from the entrance to the treasu re chambers. Make sure you understand the algorithm before you implement it. You do not have to use this algorithm if you do not want to. You are strongly encouraged to design your own algorithm, but your algorithm must use a stack of the class DLStack to keep track of the chambers that have been visited. • Create an empty stack. • Get the starting chamber and number N of treasure chambers from class Map, described below. • Push the starting chamber into the stack and mark the chamber as pushed. Read below the description of the provided class Chamber to learn how to mark a chamber. • Now, while the stack is not empty perform the following steps: o Peek at the top of the stack to get the current chamber. o If the current chamber is a treasure chamber and the number of treasure chambers that have been already found is equal to N, then exit the while loop. o Find the best neighboring chamber c to move to using method bestChamber from class PathFinder . If c is not null, push it into the stack and then mark it as pushed; otherwise pop the top chamber from the stack and mark it as popped. • After the while loop terminates return the stack Notice that your algorithm does not need to find the shortest path from the starting chamber to the exit. You CANNOT use class Stack from t he Java libraries.

### Provided Files

You are given several java classes that allow your program to display the chambers on the screen. You are encouraged to study the given code, so you learn how it works. Below is a description of some of these classes that you will need to use in your code. Other java classes, sample input files, and image files are also provided. Map.java This class represents the chambers of the park. The methods that you will use from this class are the following: • public Map (String inputFile) throws InvalidMapCharacterException, FileNotFoundException, IOException : This method reads the input file and displays the park chambers on the screen. An InvalidMapCharacterException is thrown if the inputFile contains an invalid character. Look at the sample input files to learn which characters are allowed. • public Chamber getEntrance() : Returns a Chamber object representing the entrance to the park. • public int getNumTreasures() : Returns the number of chambers that contain treasures and that your program must reach. Chamber.java

This class represents the park chambers. Objects of this class are cre ated inside class Map when the input file is read. The methods that you might use from this class are the following: • public Chamber getNeighbour (int i) throws InvalidNeighbourIndexException : Each park chamber has up to six neighbouring chambers, indexed from 0 to 5. For each value of i, from 0 to 5, the method might return either a Chamber object representing a chamber or null. Note that if a chamber has fewer than 6 neighboring chambers, these neighbors do not necessarily need to appear at consecutive index values. So, it might be that this.getNeighbour (0) and this.getNeighbour (3) are null, but this.getNeighbour (i) for all other values of i are not null. An InvalidNeighbourIndexException is thrown if the value of the parameter i is negative or larger than 5. • public boolean methods : isSealed (), isLighted (), isTreasure () return true if this Chamber object represents a chamber that is sealed, lighted, or it contains treasure, respectively. • public boolean isMarked() : Returns true if this Chamber object represents a chamber that has been marked as pushed or popped. • public void markPushed() : Marks this Chamber object as pushed. • public void markPopped() : Marks this Chamber object a s popped.

### Sample Input Files and Running the Program

1. You are given several input files (map1.txt – map5.txt) that you can use to test your code. The main method is in the provided class Pyramid.java. • To run the program from a terminal , put all provid ed files and your java classes in the same directory : From that directory compile the code by typing o javac *.java Then, r un the program by typing o java Pyramid inputFile where inputFile is one of the provided sample input files: map1.txt, map2.txt, ... • To run the program from Eclipse , first put all the provided files and your java classes in the same directory. Then in Eclipse select Run→Run Configurations; in the window that opens sele ct Arguments and under “Working directory” select “Other”. Click on “File System” and choose the folder where you have placed all your files.

After having configured Eclipse, to run the program , select class Pyramid.java in the Package Explorer, then select Run→Arguments and in the Program Arguments box enter the name of the input file (map1.txt, map2.txt, ...). Then click Run. If you use another IDE you will have to read its documentation to fi gure out where to put the provided files and your java code. If you cannot figure that out, please run the program from a terminal.

1. You can use class TestStackMap.java to test your implementation of the extended stack and some of the methods of class PathFinder .java.

### Marking Notes:

Functional Specifications • Does the program behave according to specifications? • Does it produce the correct output and pass all tests? • Are the classes implemented properly? • Will the code run properly on Gradescope (even if it runs on Eclipse, it is up to you to ensure it works on Gradescope to get the test marks). Note that Gradescope will not provide you any results of the autograder when you are submitting, except a general confirmation that the file was accepted. • You are expected to perform additional testing (create your own test harness class to do this) to ensure that your code works for other scenarios as well . We are providing you with some tests but we may use additional tests that you haven't seen before for marking. • Does the program produces compilation or run -time errors on Gradescope? • Does the program fail to follow the instructions (i.e. changing variable types, etc.)

Non-Functional Specific ations • Are there comments throughout the code (Javadocs or other comments)? • Are the variables and methods given appropriate, meaningful names? • Is the code clean and readable with proper indenting and white -space? • Is the code consistent regarding formatting and naming conventions? • Submission errors (i.e. missing files, too many files, etc.) will receive a minimum penalty of 5% • Including a “package” line at the top of a file will receive a minimum penalty of 5% • 150 Word Summary o Students are required to provid e a 150 -word explanation within the JavaDoc of their PathFinder class, elaborating their approach towards designing the cod e of the entire p rogram , any challenges faced and the steps taken for testing the solution. The explanation should be precise (5), logical (5), and demonstrate clear understanding of the code functionality (10).

Remember you must do all the work on your own. Do not copy or even look at the work of another student, and please develop your code yourself rather than relying on automated tools. All submitted code wi ll be run through similarity -detection software. Assignments must be submitted to Gradescope, not on OWL.