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

FoP 2 2 Variant

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

FoP 2 2 Variant

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

DISCLAIMER!!!

These are just exercises, which are assessed by SA


(Archil Melikidze), Which means it is not related to
actual Midterm Papers…

Completing these tasks, does not mean you


understand all topics, neither format nor topics will
be exactly matched for midterm

Please exercise further and revise remaining topics


by yourself
Problem 1:
Consider the interface for sequences of work of generic type T:

import java.util.Iterator;

public interface Work<T> extends Iterable<T> {

boolean noWork();

Work<T> addWork(T x);

T viewWork();

Work<T> removeWork();

class WorkIterator<T> implements Iterator<T> {

// todo

default Iterator<T> iterator() {

// todo

default Work<T> reverse();

** Provide an implementation of the class WorkIterator<T> which implements the interface Iterator<T>

by means of the methods provided in the interface Work<T>.

The implementation should allow to iterate over the elements stored as work.

Thereby assume that the method viewWork() returns the first piece of work of the data-structure (given
that there is work) -

which can then be removed by applying removeWork().


** Use the class WorkIterator<T> in order to provide a default implementation of the method
Iterator<T> iterator.

** Provide a default implementation of the method Work<T> reverse().

One implementation of the interface Work<T> is given by:

class NoWork<T> implements Work<T> {

public NoWork() { }

public boolean noWork() {

return true;

public T viewWork() {

throw new RuntimeException("No work!");

public Work<T> removeWork() {

throw new RuntimeException("No work!");

public Work<T> addWork(T x) {

return new SomeWork<T> (x,this);

public Work<T> reverse() {

return new NoWork<T>();

You yourself should now provide an implementation of the class

class SomeWork<T> implements Work<T> {


// todo: appropriate private attributes

public SomeWork(T x, Work<T> work) {

// todo

public boolean noWork() {

// todo

public T viewWork() {

// todo

public Work<T> removeWork() {

// todo

public Work<T> addWork(T x) {

// todo

public Work<T> reverse() {

// todo

** provide the attributes of SomeWork<T> objects which you require for your implementation.

** provide implementations of the methods boolean noWork, T viewWork(), Work<T> removeWork(),


and Work<T> addWork(T x).

Finally, consider the class Doit with main method only:

public class Doit {

public static void main(String[] arr) {


// todo

Implement that class in a way such that all commandline arguments are first all stored in a data
structure Work<String> work.

After reversing the data structure work, the strings stored in work should all successively be printed to
the console.

In this way, make sure that they are printed in the same order as they occur on the commandline.

Problem 2:
Advanced Library System

You are tasked with implementing an advanced library system that efficiently manages different types of
items, including books and DVDs. Each item has a unique identification number, a title, and a status
indicating whether it is available for borrowing. Additionally, the system should support features such as
checking in and checking out items, displaying available and checked-out items, and providing detailed
information about each item.

Subtasks:

Base Class - LibraryItem:

Create a base class called LibraryItem with the following attributes:

id (int): a unique identification number for the item.

title (String): the title of the item.

available (boolean): a flag indicating whether the item is available for borrowing.

Add a checkOut method that sets the item as unavailable.

Add a checkIn method that sets the item as available.

Make sure that each id is unique and also override equals and hashcode methods, which makes
sure every item is unique.

Generic Class - Library:

Create a generic class called Library to manage a collection of library items. Keep in mind that
this class should only take Library Items as generics
Use a HashMap for efficient item lookup, where each item would be found using ID.

Use a HashSet for tracking the checked out items.

Implement methods to:

Add an item to the library.

Remove an item from the library.

Display the list of available items.

Display the list of checked-out items using a HashSet for efficient tracking.

Book and DVD Subclasses:

Create two subclasses, Book and DVD, that inherit from the LibraryItem class.

For Book: Add an author (String) attribute and a getAuthorInfo method that returns a formatted
string with author information.

For DVD: Add a director (String) attribute and a getDirectorInfo method that returns a formatted
string with director information.

Override the toString method in both subclasses to provide a string representation of the item.

Problem 3:
Write a program, which will use division of integers, make sure that if some error is caused, it’s
full stack trace + your full name is printed, also no matter what happens this program should always
print “KIU” in the end (it should be last thing on console)

You might also like