COMP2100/6442 Exam 2021
Implement the minimum number of JUnit test cases for the method findSomething() in
BranchComplete.java that is Branch Complete.
Note that each invocation of the method findSomething() counts as a single test case. For example, if you
call twice findSomething() method in the BranchCompleteTest.java , then the number of test cases is
considered as 2. All test cases must pass the JUnit test to get full marks. You must use JUnit4 to write your
test cases. Otherwise, you may be subject to a certain amount of mark loss.
You are expected to upload:
BranchCompleteTest.java
Q2 – Tokenisation and Parsing
The code provided implements a simple compiler that compiles and executes a series of commands listed
below. There are two types of commands with the syntax as below:
LOAD object_key FROM file_name;
SAVE object_key TO file_name;
where object_key is a string key referencing a certain object stored in a database and file_name is a
string that is made of a file name and the XML file extension .xml .
This compiler aims to use the two commands to load and save a list of persons objects, where each
Person object has four fields, name , gender , age , occupation . For example,
LOAD persons FROM persons.xml, which loads a list of persons from a persons.xml file into the
SAVE persons TO persons.xml, which saves a list of persons to a persons.xml file from the
LOAD , FROM , SAVE , TO , TERMINATOR , PARAMETER are the defined keywords of the commands. persons ,
persons.xml are the parameters of the commands. The terminator is a semi-colon ; . Note that
object_key and file_name are not fixed to the given persons and persons.xml as we will use different
parameters in the marking test cases to test the robustness of your solutions. But the objects to be stored
in the database will be fixed to Person instances.
Part 1) Implement the next() method of the Tokeniser class to perform a tokenisation process. The
next() method should be able to identify the keywords and parameters of the commands. The tokens
and the types are defined in the Token.java class.
Please find more details in the given TokeniserTest.java .
Part 2) Implement the parseCmds() method of the Parser class to create two types of commands from
the tokens:
LoadCommand contains two parameters, a key indicating the object_key, e.g. persons, and a fileName
indicating the file_name, e.g. persons.xml.
SaveCommand contains two parameters, a key indicating the object_key, e.g. persons, and a fileName
indicating the file_name, e.g. persons.xml.
Please find more details in the given ParserTest.java .
Part 3) Implement the loadFrom() , saveTo() methods of the Executor class to execute the parsed
You are required to follow the format of the given example xml file example.xml . The variables KEY_XXX in
Person.java class give the tag names of the elements for the xml file. Note that indent property of the
xml transformer is optional, you are allowed to uncomment it in saveTo() method of Executor.java
class to make the files more readable.
Please find more details in the given ExecutorTest.java .
IMPORTANT NOTES:
The keywords are all case insensitive, whereas the parameters are case sensitive. HINT: you can use
toLowerCase() or toUpperCase() , etc. defined in the String.class to compare strings without care for
case sensitivity.
Each single command is terminated by a semi-colon ; .
Any number of spaces or tabs between the keywords are allowed.
A series of single commands can be concatenated together in any order.
All the commands in the given and marking test cases are valid. You don’t need to consider the
invalidity of the commands.
We give some examples of valid commands below:
LOAD persons FROM persons.xml;
save persons to persons.xml;
LOAD persons from persons.xml; save persons to persons.xml;
We may not give all the valid test cases. Feel free to add more test cases to test the robustness of your
solutions.
You are expected to complete:
next() method in the Tokeniser.java class
parseCmds() method in the Parser.java class
loadFrom() , saveTo() methods in the Executor.java class
You are expected to upload:
Tokeniser.java
Parser.java
Executor.java
You are allowed to create helper methods in the required java classes. You are only allowed to implement
your code in the designated area between “START YOUR CODE” and “END YOUR CODE”. Some test cases are
provided to assist your understanding, but it does not guarantee you will get full marks. Remember that we
use different test cases to mark your solution. You are free to add your own test cases to increase your
confidence that your solution is robust. You may recognise the Parcel class (in parcel folder) from the
mid-semester exam, it has been modified slightly for the purpose of this exercise and now contains the
following fields:
id : an integer that identifies the parcel
weight : the weight of the parcel
allowedDays : the maximum allowed days for the parcel to be delivered
sender : the sender of the parcel
recipient : the recipient of the parcel
The sender and recipient fields are now of type class Person which has the following field:
location : the location of the person denoted by an X and Y coordinate in kilometres
All parcels must pass through a common warehouse located at [20,10] (as seen in general case below).
Distance is calculated using the standard Euclidean distance:
General case:
Example 1: Sender is at position [-10,50], Recipient is at position [70,130]
Example 2: Sender is at position [32,5], Recipient is at position [180,-290]
For Tasks 1 and 2: Some test cases are provided in ParcelTreeTest to assist your understanding, but it
does not guarantee you will get full marks. Remember that we use different test cases to mark your
solution. You are free to add your own test cases to increase your confidence that your solution is robust.
Impose a ‘natural order’ on Parcel using the CompareTo method in the Parcel class. The ordering of
parcels to be implemented is defined by:
The distance from the Warehouse to the Recipient (highlighted in green in the above image) is in
ascending order
Hence since 130km is less than 340km, then we know that the parcel in Example 1 should come before the
parcel in Example 2.
The CompareTo(Parcel other) method compares two parcels. If the other parcel should come before
this parcel then the CompareTo method should return a positive int . Conversely, if the other parcel
should come after this parcel then the CompareTo method should return a negative int . And finally, if
the ordering between two parcels is arbitrary (ie. if the relevant distance is equal) then the CompareTo
method should return zero.
Hint: The getDistance method in the CartesianCoordinate class might be helpful.
You are only allowed to add methods and variables to the Parcel.java file for this task, do not change the
class structure.
Create an Iterator that implements a pre-order walk over a Binary Search Tree of parcels. The class for the
iterator is already created for you (see inner class IteratorPreOrder in the ParcelBST ), your job is to
implement the methods hasNext() and next() .
You may recognise the implementation of the immutable BinarySearchTree class from your labs. The
ParcelBST class is essentially a BinarySearchTree that holds Parcels. Your iterator implementation needs
to somehow store which Parcel it is up to. When the method next() is called, your iterator implementation
should return the next parcel in the pre-order walk.
If next() is called and there are no parcels left in the iteration, then it should throw a new instance of
NoSuchElementException . The hasNext() method should return true if there are parcels left in the
iteration and false if not.
You are only allowed to add methods and variables to the ParcelBST.java file for this task, do not change
the class structure.
You are expected to complete:
CompareTo method in the Parcel.java class (Task 1)
hasNext() and next() methods in ParcelBST.java (Task 2)
You are expected to upload:
Parcel.java
ParcelBST.java
Open-ended Question (Question 4):
Based on the knowledge learned in this course, explain how a team should design a highly reliable and high-
performance software with low maintenance. We expect you to coherently explain and interrelate various
concepts approached in our course with clarity and brevity. Note that solely listing topics will not reward you
with any marks.
Word limit: [min: 100, max: 400].## Open-ended Question (Question 5): List all the methods in the life
cycle of Android activity and describe when they are called by the underlying system.
https://docs.oracle.com/javase/8/docs/api/java/util/NoSuchElementException.html
Word limit: [min: 100, max: 400].
COMP2100/6442 Exam 2021
You are expected to upload:
Q2 – Tokenisation and Parsing
You are expected to complete:
You are expected to upload:
You are expected to complete:
You are expected to upload:
Open-ended Question (Question 4):