Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

CSI247: Lab Test 2 (40 marks)

November 17, 2021

Lab Test Rules


1. Attempt all the sections.

2. Only use the materials handed out by the lecturer; notes, videos, text books.

3. Do not use any internet searches.

4. Only ask the TAs and Demonstrators questions related to compilation and syntax errors.

Project Structure
Follow the steps below to create appropriate project structure for this lab.

ˆ In your Z drive, create a directory called CSI247 (If it does not already exist).

ˆ Download the labtest2 project from the labtest link and extract it inside CSI247.

ˆ There is a file classes/lab/list/Checker.class, DO NOT delete it.

ˆ In order to be able to compile the Tester class without the Checker.java file, you have
to add -classpath classes to the compile command i.e. javac -d classes -classpath classes
-sourcepath src src/lab/Tester.java

ˆ Follow the instructions below to complete the lab.

ˆ When implementing your code, find the // TODO: comments and implement the appropri-
ate code at those locations.

ˆ When you finish, zip the project folder and upload it to moodle.

List Iteration
In this lab we are going to implement the list iterators by utilising two of predefined interfaces
from java called java.lang.Iterable and java.util.Iterator. Implementing these two interfaces as
instructed in the sub sections below.

1. List Interface
a) Modify the interface to inherit from the Iterable interface. You can check out
the specification for the Iterable interface at https://docs.oracle.com/en/java/
javase/11/docs/api/java.base/java/lang/Iterable.html

1
b) Modify the interface so that the generic formal parameter so that it inherits from
Comparable

2. LinkedList class
a) Modify the LinkedList class to use bounded generics
b) Implement the private inner class called ListIterator as follows:
i. Declare the private inner class called ListIterator. The class is not generic but
it does have access to the generic parameter declared in LinkedList interface.
ii. The class should inherit from the java.util.Iterator interface
iii. Add all the proper generic parameters.
iv. You can check out the specification in https://docs.oracle.com/en/java/
javase/11/docs/api/java.base/java/util/Iterator.html
v. Declare a variable to hold the current Node object.
vi. Create a default constructor that assigns the first Node in the list to current
Node variable.
vii. Implement the hasNext() method to return whether the current variable NOT
null.
viii. Implement the next() method as follows:
ˆ Get the data from the current node.
ˆ Change the current node to point to the next node.
ˆ Return the data.
ix. Do not forget to put the Override annotation on the methods.
c) Implement the iterator() method to create and return an instance of the ListIterator
class.
d) Make sure you compile and fix the errors.

3. Tester class
a) Locate the // TODOs in the code and implement as needed.
b) Create an instance of the LinkedList class and assign it to the List variable.
c) Use Math.random() to add MAX number of integers between 0 and 100 into the list.
d) Use a loop to replace the numbers in the list with numbers twice their size.
e) Calculate the sum of all the elements in the list.
f) Calculate the average of the items in the list.
g) Print the elements of the list using the for-each loop.
h) Print the elements of the list using the iterator loop.

You might also like