Assignment2 Instructions
Assignment2 Instructions
Learning Outcomes
Introduction
It’s Halloween! You know what that means, right? It’s time to go trick-or-treating! With all the
restrictions in place this year, it’s more important than ever to plan out the ideal routes. To
minimize the number of people on the doorsteps at once, everyone has agreed to only trick-or-
treat for so long and to take turns. The hero of our assignment, Bryan the Bold, needs your help
making sure that his friends and him can collect as much candy as possible before their
designated trick-or-treating time ends. He brought a map of the neighbourhoods he’s going to
go trick or treating in and heard that you’ve been studying some computer science recently that
will help him plan this all out. To help Bryan the Bold out, you will create a Java program to help
him navigate the neighbourhood. The program needs to determine whether Bryan or a friend
can reach enough candy to fill his bag from a starting point on the map, given a maximum
distance from the start point (Bryan and friends can only run so fast!).
You are given a map of the neighbourhood, which is divided into rectangular cells to simplify the
task of computing the required path. There are different types of map cells:
o Horizontal paths. A horizontal path can be used to connect the east and west
neighbours of a map cell.
Map cells containing candy are allowed to function like cross paths; Bryan the Bold can sneak
through backyards if need be (but not road blocks).
Figure 1 shows an example of a map divided into cells.
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 0 1 is cell 5, neighbour with index 1 is cell 9,
neighbour with index 2 is cell 11, and neighbour with index 3 is cell 7 (note that even though
there are 4 neighbours, only 2 of them are accessible: cell 5 and cell 11, because cell 8 is a
vertical path and thus does not connect on the east or west sides).
Some cells have fewer than 4 neighbours and the indices of these neighbours might not be
consecutive numbers; for example, cell 6 in Figure 1 has 2 neighbours indexed 1 and 2 (no 0 or
3 neighbours).
A path from Bryan (cell number 1 in the figure) to a candy (cell number 7) is the following:
1, 2, 3, 4, 5, 6, 7. A path from Bryan to another candy (cell number 10) is the following: 1, 2,
3, 4, 9, 10. Notice that the two other candies in the figure are inaccessible because of the path
types of their neighbouring cells.
Valid Paths
When looking for a path the program must satisfy the following conditions:
The path can go from Bryan, a cross path cell, or a candy cell to the following
neighbouring cells:
o A candy cell,
o A cross path cell,
o The north cell or the south cell, if such a cell is a vertical path, or
o 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:
o The north cell or the south cell, if such a cell is either Bryan, a cross path cell, a
candy cell, or a vertical path cell.
The path can go from a horizontal path cell to the following neighbouring cells:
o The east cell or the west cell, if such a cell is either Bryan, a cross path cell, a
candy cell, or a horizontal path cell.
CS1027
ASSIGNMENT 2 Computer Science Fundamentals II
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:
When considering path length, adding a new cell to the stack or removing a cell from the
stack counts as adding to the path length; Bryan takes time to move to and from a cell.
CS1027
ASSIGNMENT 2 Computer Science Fundamentals II
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 Bryan’s
starting location and the candy houses. The public methods from this class you might
use include:
MapCell.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:
Classes to implement
A description of the classes 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:
int top.
o 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.
o Note that this is different from the way in which the variable top is used in the
lecture notes.
ArrayStack()
o Creates an empty stack.
o The default initial capacity of the array used to store the items of the stack is 5.
ArrayStack(int initialCapacity).
o Creates an empty stack using an array of length equal to the value of the
parameter.
At the end of this method, at the following. This is used in TestSearch to make sure
you are following a correct path.:
}
else {
else {
boolean isEmpty().
o Returns true if the stack is empty and returns false otherwise.
int size()
o Returns the number of data items in the stack.
int length()
o Returns the capacity of the array stack.
String toString()
o Returns a String representation of the stack of the form:
“Stack: elem1, elem2, ..." where element i is a String representation of the i-th
element of the stack.
o If, for example, the stack is stored in an array called s, then element 1 is
s[0].toString(), element 2 is s[1].toString(), and so on.
You can implement other methods in this class, if you want to, but they must be declared
as private.
CS1027
ASSIGNMENT 2 Computer Science Fundamentals II
StartSearch.java
o Map neighbourhoodMap;
o This variable will reference the object representing the neighbourhood map
where Bryan and the candies are located. This variable must be initialized in the
constructor for the class, as described below.
You can write more methods in this class but they must be declared as private.
o Find the best unmarked neighbouring cell (use method bestCell from class
StartSearch to do this).
o If one exists:
Push the neighbouring cell into the stack and mark it as inStack.
(If max path length given) Increase the path length counter
If the best neighbouring cell is a candy, increase the number of candies
found.
o Otherwise, since there are no unmarked neighbouring cells that can be added to
the path, pop the top cell (and increase the path length counter) from the stack
and mark it as outOfStack.
While the stack is not empty perform the following:
o Pop the top cell from the stack and mark is as outOfStack.
Your program must print a message indicating how many candies were collected.
Note that your algorithm does not need to find the shortest path from Bryan to all the candies.
Where nameOfMapFile is the name of the file containing the desert map, and maxPathLength is
the longest path that Bryan can take to find candies.
You can use the following code to verify that the program was invoked with the correct number
of arguments:
In the text box for ‘Program arguments’, you can type your arguments in. Ex: map3.txt above, or
map3.txt 20 for map 3 with a maximum of 20 steps.
Marking notes
Marking categories
Functional specifications
o Does the program behave according to specifications?
o Does it produce the correct output and pass all tests?
o Are the class implemented properly?
o Are you using appropriate data structures?
Non-functional specifications
o Are there Javadocs comments and other comments throughout the code?
o Are the variables and methods given appropriate, meaningful names?
o Is the code clean and readable with proper indenting and white-space?
o Is the code consistent regarding formatting and naming conventions?
Penalties
o Lateness: 10% per day
o Submission error (i.e. missing files, too many files, ZIP, etc.): 5%
o "package" line at the top of a file: 5%
Remember you must do all the work on your own. Do not copy or even look at the work of
another student. All submitted code will be run through cheating-detection software.
Rules
Please only submit the files specified below. Do not attach other files even if they were
part of the assignment.
Submit the assignment on time. Late submissions will receive a penalty of 10% per day.
Forgetting to submit is not a valid excuse for submitting late.
Submissions must be done through Gradescope
Submitting the files in an incorrect submission page or website will receive a penalty.
Assignment files are NOT to be emailed to the instructor(s) or TA(s). They will not be
marked if sent by email.
You may re-submit code if your previous submission was not complete or correct,
however, re-submissions after the regular assignment deadline will receive a penalty.
Files to submit
ArrayStack.java
StartSearch.java
Any custom classes used.
Grading Criteria
Total Marks: [20]
Functional Specifications:
o [4] ArrayStack.java
o [4] StartSearch.java
o [8] Passing Tests
Non-Functional Specifications:
o [1] Meaningful variable names, private instance variables
o [1] Code readability and indentation
o [2] Code comments