0% found this document useful (0 votes)
5 views6 pages

Adele - Ismaila - Cosc301indvl

The document contains a programming assignment focused on creating a Vehicle class with attributes and methods, including a SpeedListVisitor class to check for vehicles exceeding speed limits. It also discusses time complexity analysis of a code fragment, concluding that both approximate and exact time complexities are O(log n). Additionally, it includes methods for a MyLinkedList class to calculate the sum of integers in the list and to reverse the list's order.

Uploaded by

ashiwaj22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views6 pages

Adele - Ismaila - Cosc301indvl

The document contains a programming assignment focused on creating a Vehicle class with attributes and methods, including a SpeedListVisitor class to check for vehicles exceeding speed limits. It also discusses time complexity analysis of a code fragment, concluding that both approximate and exact time complexities are O(log n). Additionally, it includes methods for a MyLinkedList class to calculate the sum of integers in the list and to reverse the list's order.

Uploaded by

ashiwaj22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

ADELE ISMAILA ADEDEJI

U22DLCS20019

COMPUTER SCIENCE

300L.

INDIVIDUAL ASSIGNMENT

QUESTION 1 .

Consider the vehicle class with three instance member and their
associated accessor

(i)vehicle {

(ii) Private string license plate;

(iii) Private string owner name;

(iv) Private int speed ;

(v) Public vehicle ( string licensePlate, string Owner name, int


speed ){

(vi) //….. More code code left out ….

(vii) }

(viii) }

ANSWER 1

// Rewrite the program according to the question given

Public class Vehicle {

// Three instance member given

Private String licensePlate;

Private String ownerName;

Private int speed;

// Constructor given
Public Vehicle(String licensePlate, String ownerName, int speed)
{

This.licensePlate = licensePlate;

This.ownerName = ownerName;

This.speed = speed;

// Getters

Public String getLicensePlate() {

Return licensePlate;

Public String getOwnerName() {

Return ownerName;

Public int getSpeed() {

Return speed;

// Setters

Public void setLicensePlate(String licensePlate) {

This.licensePlate = licensePlate;

Public void setOwnerName(String ownerName) {

This.ownerName = ownerName;

Public void setSpeed(int speed) {

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;

“ licensePlate driven by OwnerName is exceedingl the speed limit”

CODE:

Class SpeedListVisitor {

Public void visit(List<Vehicle> vehicles) {

For (Vehicle vehicle : vehicles) {

If (vehicle.getSpeed() > 70) {

System.out.println(

Vehicle.getLicensePlate() + “ driven by “ +
vehicle.getOwnerName() + “ is exceeding the speed limit”);

( b) create the TestSpeedListVisitor class that insert number of


vehicle objects ( instance) into a My container instance and then
applies the SpeedLimitVisitor on the element of container…

CODE:

Public class TestSpeedListVisitor {

Public static void main(String[] args) {

// Create a VehicleContainer

VehicleContainer container = new VehicleContainer(new


ArrayList<>());
// Add Vehicle objects to the container

Container.getVehicles().add(new Vehicle(“WBC123”, “Mr


Adelee”, 90));

Container.getVehicles().add(new Vehicle(“DEF456”, Bola.


Kola”, 69));

Container.getVehicles().add(new Vehicle(“KHI789”, “Dada


peter”, 56));

Container.getVehicles().add(new Vehicle(“JKL012”, “Akin omo”,


45));

// Create a SpeedListVisitor

SpeedListVisitor visitor = new SpeedListVisitor();

// Apply the visitor to the elements of the container

Container.accept(visitor);

QUESTION 2:

Consider the following cod

For( int i= n; I >0; I /=2,){

For ( int j = I ; j<= I; j++){

System .out .println(“processing”);

(a) What is the approximate time complexity of the code


fragment ?

ANSWER
(a) Approximate Time Complexity:
O(log n)

The approximate time complexity is O(log n) because the outer


loop dominates the algorithm’s runtime. The outer loop iterates
from `n` to `1`, reducing the value of `i` by half in each iteration
(`I /= 2`). This is a logarithmic operation, as the number of
iterations grows logarithmically with the size of the input `n`.
Therefore, the outer loop has a time complexity of O(log n).

(b) What is the exact time complexity of the code fragment


in the term of n

ANSWER

Exact Time Complexity: O(log n)

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

(a) Write a method for the MylinkedList class that calculates


and returns the sum of all the integer values stored with node
of the list instance.

ANSWER

//The method that calculates and returns the sum of all the integer
values stored in the nodes of the linked list instance.

Public int sum() {

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,

“ Public MylinkedList reverseList ( ).

ANSWER

Public MyLinkedList reverseList( ) {

MyLinkedList reversedList = new MyLinkedList();

Node current = this.head;

While (current != null) {

reversedList.insertAtBeginning(current.data);

current = current.next;

Return reversedList;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy