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

LAB Exercise-3

COSC1047
Lab will Covers Concept on polymorphism and abstract classes
1. Write a program to create a class named shape. In this class we have three sub classes
circle, triangle and square each class has two-member function named draw () and
erase (). Create these using polymorphism concepts.
2. You are tasked with creating a program that models a library with different types of
items, such as books and magazines. Implement the program using polymorphism to
handle different item types uniformly.Lab Exercise Instructions:
 Create an abstract class called LibraryItem with the following attributes: title
(String) and year (int).
 Implement a constructor in the LibraryItem class that takes parameters for the
title and year and initializes the corresponding attributes.
 Declare an abstract method called displayItemInfo in the LibraryItem class.
 Create two concrete subclasses of LibraryItem: Book and Magazine.
 The Book class should have an additional attribute called author (String).
 Implement a constructor in the Book class that takes parameters for the title,
year, and author, and initializes the corresponding attributes.
 Override the displayItemInfo method in the Book class to display the book's
title, author, and year.
 The Magazine class should have an additional attribute called issueNumber
(int).
 Implement a constructor in the Magazine class that takes parameters for the
title, year, and issue number, and initializes the corresponding attributes.
 Override the displayItemInfo method in the Magazine class to display the
magazine's title, issue number, and year.
 In the main method, create an array of LibraryItem objects.
 Populate the array with a combination of Book and Magazine objects.
 Iterate over the array and call the displayItemInfo method on each object to
display the information of the items.

Submission Instructions:
Please ensure you submit an original work and should not be copied or zero will be given for
copying. Ensure that your code follows Java language programming style and guidelines.
Your code must compile without errors and execute as per the specifications in the problem
description. Carefully write comments in the source code to have an understanding and don’t
forget to write your name in comments within the source code

Marking Scheme:
Code 6
Comments 1
Snapshot of output 3

You might also like