Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

IT2030 - Object Oriented Programming-Version F

Uploaded by

it20046798
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

IT2030 - Object Oriented Programming-Version F

Uploaded by

it20046798
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

/ Student ID:

00269

Machine No:

Sri Lanka Institute of Information Technology

B.Sc. Honours Degree in Information Technology


Specialized in Information Technology

Final Examination (Computer Based)


Year 2, Semester 1 (2023)
Paper Version F

IT2030 -Object Oriented Programming

Durationi 3 Hours

November 2023
Instructions to Candidates:
• This paper contains four questions. Answer All Questions.
• Marks for each question are given on the paper. Total Marks: loo.
EE
• Create a folder on the Desktop with your student registration number and store all your
program files inside that.
• Create a separate Project for each question. The name of the project is provided in the
question.
• This paper contains 06 pages including the Cover Page.

Instructions to Candidates when submitting:


• Save all your work.
• Delete all unnecessary files from each project folder (There should be 4 folders for 4
piejects named as Questiono i , Question02, Question03 and Question04 inside your ID
folder, and each folder should contain the source code (.java files)) only.

• Zip the Student ID folder (Zipped folder also should be named with Student ID number).
• Upload the zipped folder to the correct link.
oo26\
Question 1 . (30 marks) .
This question is based on the Collection Framework and Generics.

a) Write a Java program that allows the user to manag-e a list of books in a library using a

PriorityQueue. The program should perform the following tasks.


i) Create an empty priorityQueue of book titles (Strings) to store the book names.

Prompt the user to enter the titles of books one by one. Continue accepting book
titles as keyboard inputs until the user enters "done." Add each entered book title

to the created priorityQueue. (04 marks)


ii) Once the user has finished entering book titles, ask the user to enter the title of the

book they want to remove. If the book title is found in the PriorityQueue, remove
it.Displayamessageindicatingwhetherthebookwassuccessfullyremovedornot,
in the case the title was not found. (06 marks) i
iii) Find and display the total number of books in the priorityQueue. -(02 marks)
iv) Print all the remaining book titles in the priorityQueue. (03 marks)

Save the project as QuestionolA

b) Create a Java program that manages the inventory of products using a HashMap. The

program should allow user to add products, update their quantities, remove products, and
view the current inventory.
i) Create a HashMap to manage the inventory, where the keys are unique product
codes (strings) and the values are objects representing each product. Each product
object should include the product name (string), the quantity in stock (integer), and
the price per unit (a double) (06 marks) \-
ii) Implement a method to add aproduct to the inventory. The method should take the

productcode,productname,quantity,andpriceperunitasparameters.Ifaproduct
with the same code already exists, update its details. (04 marks)
iii) Create a method to remove a product from the inventory based on the product code.

(02 marks)
c) In the main method, create a sample inventory with 5 products, and display the product

details. Then remove 2 products and display the remaining products again.

(03 marks)

Save the project as QuestionolB


00269
Question 2 (30 marks)
This question is based on the Design Patterns implementation.

a) You are going to implement the Abstract Factory- Pattern based on the scenario of
Vehicle factory that turns out Cars or Busses for the specified model. Refer the
Screenshot of the Console Output and the VehicleDemo class. You should implement
the rest of the other classes.

i). Implement Vehicleproducer class that returns either CarFactory or


BusFactory outputs through getvehicleo operation. Refer the screenshot in
Demo class. (02 marks)

ii). Design CarFactory and BusFactory classes according to the Singleton


ed Pattern and let getModel(String vehicle) select the factories.
(06 marks)
iii). Design 03 classes for Car factory called (BMW, Benz, and RollsRoys)
` according to the singleton pattern. Each car should have a displayvehicleo

method you have to override.

(03 marks x 03 = 09 marks)

iv). Create Design 03 classes for Bus factory called (Volvo, Fuso, and TATA)
according to the singleton pattern. Each bus should have a displayvehicleo
method you must override.

(03 marks x 03 = 09 marks)

`=, b) Now design three interfaces to IBus, Icar, and VehicleFactory to override common
behaviors.

i). Now design IBus and Icar interfaces and declare the displayvehicleo
operation in each.

(01 mark x 2 = 02 marks)

ii). Design vehicleFactory as an Interface and declare the getModel operation to


select vehicle option either Bus or Car

(02 marks)

Hint: Rofer to the main program and package hierarchy given below

Save the project as Question02


oo26\
Main Program:

3 public class VchicleselectionDfro {


4
5 private static final String Gap = -Car'-;
6 private static final String Bus = -Bus-;
7
88 public static void irain(String[] args) {

( (Icar) Vehicleproducer. getychtcte(Can) gctModel(-RollsRoys-)).displayychicle{};


( (Icar) Vehicleprochicer. getvehicte(Can) getMdel{-Benz-)).displayvehicle();
( {Icar) VchicleprodLicer. getl/ehicfe(Can) getMedel(-BW-)).displayvchicle{);

( (IBtis ) Vehicleprodiicer. getyehicte(4us) getHndel(tyolvo-)).displayvchicl:();


( (IBug} vehicleprodrcer. getyehicte{aus) getNedel(-Fuse-)).displayvchicle();
( (IBtis ) Vchicleprochicer. grtyghicLe(4us) getMdel(-TATA-)).displayvthicle();
7)

haA-lC: Fae5\AM\inlAQi44\binhiapiaeHH€ife29.2D23.1aecomo
Factory t`ms art Rollstrys ar
Fjrfury ttrms art Bcoz Car
Factory trms atrt ml Car
Factory tiims Out volvo 8Lis
Fatory tins otrt Fuse Bus
Factory tit ms ait TAIA utis

Package Hierarchy:

I oap-a-q1
> E 8drjara
> ill Brm.jRE
> E@ 8LEiFrdoryjarva
> in aradoryjanra
> in I-jama
> EE BLBjava
> ill kin.jima
> ill RoillsRftygiv
> in munjan.
> E V±atory.jRE
> in vehi~er.java
> in vrfujiFya
00269
Question 3 (20 marks)
This question is based on the Threads implementation.

You are tasked with simulating a resource allocation system for a multi-user environment. The
system manages a set of resources, and users can request to allocate and deallocate these resources
from the Resource Manager.
a) Create a class naned ResourceManager that manages a pool of resources. (02 marks)
i). Implement the requestResource() method where a user can request a resource. Ifa
resource is available, it is allocated to the user. If not, the user waits until a resource
becomes available. (04 marks)
ii). Implement the releaseResource() method where users can request to release the
resource they have acquired. (04 marks)
RE
b) Create a class called User that simulates a user. Each user should simulate users requesting
to acquire and release resources from a Resource Manager with a sleeping time of 1000
milliseconds in between acquiring and releasing. Each user needs to have a unique userld.

(06 marks)
c) The main class should initialize the ResourceManager object with 3 resources and 5
user threads, and the program should display messages indicating when users request and
release resources. Refer to the partial code of the Main class and the output below.

(4 marks)
public class Main {
public static void main(String[] args) {
int TResources = 3;
int TUsers = 5,.
ResourceManager rm = new ResourceManager (TResources) ,.

for (int i = 1,. i <= TUsers; i++) ,{


UserThread userThread = new UserThread(i, rm) ,.
userThread . start ( ) ,.
)))

Save the project as Question03

Sample Output:

user 1 acquires Resource 0


User 5 acquires Resource 1
lJser 4 acquires ResoL]rce 2
User 3 is waiting for a resource.
User 2 is waiting for a resource.
User 4 releases Resource 0
User 2 acquires Resource 0
user 3 is waiting`for a resource.
User 1 releases Resource 0
oo269`
Question 4 (20 marks)
This question is based on the OOP concepts.

Create an abstract class called "Person" with attributes as ID (int) and a name (String). This class
should also include a default constructor and an overloaded constructor to initialize these
attributes. Additionally, add an abstract method called "DisplayDetails()". Implement another

abstract class called "Course" class with attributes such as code (String) and a course name

(String). It should include a default constructor and an overloaded constructor to initialize these
attributes. Add an abstract method called "DisplaycourseDetailso".

The concrete "Student" class should be inherited from the "Person" class. It should have an
additional attribute as eurolledcourses which can hold a list to hold the enrolled courses. This
class should include a default constructor and an overloaded constructor to initialize the attributes.
Additionally, implement the following methods:

• Eurollhcourse(Course c): This method should allow students to euroll in a course by

adding the course to their list of emolled courses.


• DisplayDetails(): This method should override the abstract method from the "Person" class

to display the student's details, including their ID, name, and the list of enrolled courses.

The concrete "Teacher" class should also be inherited from the "Person" class and have an
attributecalledcoursesTaughtwhichcanholdalistofcoursestheyteach.Thisclassshouldinclude
a default constmctor and an overloaded constructor to initialize the attributes. Additionally,
implement the following methods :

• Teachcourse(Course c): This method should allow teachers to add a course to th;ir list of

courses they teach.


• DisplayDetails(): This method should override the abstract method from the "Person" class

to display the teacher's details, including their ID, name, and the list of courses they teach.

Implement a class called "InstitutionManagementApp" with the main method. In the main
method, create 2 instances each of "Student" and "Teacher" classes and initialize their attributes

using the respective constructQrs. Allow 3 students to enroll in courses and teachers to add courses

they teach. Display the details of students, teachers, and courses using the provided methods,

taking advantage of the abstract class stmcture for common attributes and methods.

Save the project as Question04

You might also like