Adele - Ismaila - Cosc301indvl
Adele - Ismaila - Cosc301indvl
U22DLCS20019
COMPUTER SCIENCE
300L.
INDIVIDUAL ASSIGNMENT
QUESTION 1 .
Consider the vehicle class with three instance member and their
associated accessor
(i)vehicle {
(vii) }
(viii) }
ANSWER 1
// Constructor given
Public Vehicle(String licensePlate, String ownerName, int speed)
{
This.licensePlate = licensePlate;
This.ownerName = ownerName;
This.speed = speed;
// Getters
Return licensePlate;
Return ownerName;
Return speed;
// Setters
This.licensePlate = licensePlate;
This.ownerName = ownerName;
This.speed = speed;
}
(a) Create a visitor class called speedList visitor. This
visitor should visit each vehicle instance to check those
with a speed greater than 70 mph and then display
information in this format;
CODE:
Class SpeedListVisitor {
System.out.println(
Vehicle.getLicensePlate() + “ driven by “ +
vehicle.getOwnerName() + “ is exceeding the speed limit”);
CODE:
// Create a VehicleContainer
// Create a SpeedListVisitor
Container.accept(visitor);
QUESTION 2:
ANSWER
(a) Approximate Time Complexity:
O(log n)
ANSWER
The exact time complexity is also O(log n) because the inner loop
does not significantly contribute to the overall time complexity.
Although the inner loop is nested within the outer loop, it only runs
once for each iteration of the outer loop due to the condition `j <=
i`. This means that the inner loop's runtime is constant with respect
to the input size `n`, and it does not affect the overall time
complexity.
QUESTION 3
ANSWER
//The method that calculates and returns the sum of all the integer
values stored in the nodes of the linked list instance.
Int sum = 0;
Node current = head;
While (current != null) {
Sum += current.data; current = current.next;
}
Return sum;
}
(b) Write a method that return a new linked list where the
element are in reverse order .for example ,a list of the form
( 1,2,3,4,5) should be transformed into ( 5, 4, 3, 2, 1). This
method should have the following Signature,
ANSWER
reversedList.insertAtBeginning(current.data);
current = current.next;
Return reversedList;