Question 1 We are writing a software for a library which lends out books, DVDs a
ID: 3915422 • Letter: Q
Question
Question 1
We are writing a software for a library which lends out books, DVDs and CDs.
We are going to develop the system so that the details that are displayed depend on the type of item. If it is a book, author and title will be displayed. For a DVD, the title (film name), certificate, and lead actor are displayed. For a CD, title and artist are displayed. Book, CD and DVD are classes which inherit from LibraryItem.
Your Tasks
1. Create a LibraryItem class so that it only contains a title attribute, representing the LibraryItem's title. It should also have a getDescription() method. This should just return the title. Give it a constructor which initialises the title.
2. Inherit Book, DVD and CD from LibraryItem. Each subclass should have additional attributes representing the specific data associated with that subclass: author for Book; certificate and lead actor for DVD; and artist for CD. Also give each class a series of "getter" (accessor) methods to return each attribute, and give each class a suitable constructor.
3.Give each class a getDescription() method of its own. This should return the full details as a string, e.g. a string containing film name, certificate and lead actor for the DVD.
4.Test out your system with a main() and write up your inheritance hierarchy, clearly explaining all aspects of your code relating to inheritance.
5. Adapt your main() to demonstrate polymorphism using this example. Describe clearly how your example demonstrates polymorphism, in around a paragraph.
Explanation / Answer
import java.util.*;
class LibraryItem
{
protected String title;
LibraryItem(String tempT)
{
this.title=new String(tempT);
}
public String getDescription()
{
return "Title : "+title;
}
}
class Book extends LibraryItem
{
private String author;
Book(String tempT , String tempA)
{
super(tempT);
this.author=new String(tempA);
}
public String getDescription()//overloaded method
{
return "Title : "+title+"Author : "+author;
}
public String getTitle()
{
return this.title;
}
public String getAuthor()
{
return this.author;
}
}
class DVD extends LibraryItem
{
private String certificate;
private String leadActor;
DVD(String tempT , String tempC , String tempLA)
{
super(tempT);
this.certificate = new String(tempC);
this.leadActor = new String(tempLA);
}
public String getDescription()//overloaded method
{
return ("Title : "+title+" Certificate : "+certificate+" Lead Actor "+leadActor);
}
public String getTitle()
{
return this.title;
}
public String getCertificate()
{
return this.certificate;
}
public String getLeadActor()
{
return this.leadActor;
}
}
class CD extends LibraryItem
{
private String artist;
CD(String tempT , String tempA)
{
super(tempT);
this.artist = new String(tempA);
}
public String getDescription()
{
return ("Title : "+title+" Artist : "+artist);
}
public String getTitle()
{
return this.title;
}
public String getArtist()
{
return this.artist;
}
}
class LibraryEx1
{
public static void main(String args[])
{
int choice,bookchoice;
Scanner sc=new Scanner(System.in);
do
{
System.out.println("Library System");
System.out.println("Which Library Item You want?");
System.out.println("1.Book 2.DVD 3.CD 4.Exit");
choice=sc.nextInt();
switch(choice)
{
case 1:
Book objB1=new Book("Fault in our Stars","John Green");
Book objB2=new Book("Angels & Demons","Dan Brown");
System.out.println(objB1.getDescription());
System.out.println("Title : "+objB2.getTitle());
System.out.println("Author : "+objB2.getAuthor());
break;
case 2:
DVD objD1=new DVD("3 Idiots","UA","Aamir Khaan");
DVD objD2=new DVD("Raazi","A","Aliya Bhutt");
System.out.println(objD1.getDescription());
System.out.println("Title : "+objD2.getTitle());
System.out.println("Certificate : "+objD2.getCertificate());
System.out.println("Lead Actor : "+objD2.getLeadActor());
break;
case 3:
CD objC1=new CD("Audio","LSD");
CD objC2=new CD("24k Magic","Bruno Mars");
System.out.println(objC1.getDescription());
System.out.println("Title : "+objC2.getTitle());
System.out.println("Artist : "+objC2.getArtist());
break;
case 4:
default :
System.out.println("Invalid Choice");
}
}while(choice!=4);
//To test Polymorphism
LibraryItem objL1=new Book("Tree Tops","Jim Corbette");
System.out.println(objL1.getDescription());
LibraryItem objL2=new DVD("Deadpool","A","Ryan Reynolds");
System.out.println(objL2.getDescription());
LibraryItem objL3=new CD("Aadat","Aatif Aslam");
System.out.println(objL3.getDescription());
}
}
/*
In this Program We First create a class LibraryItem. The Class LibraryItem contains only one field "title".
The other classes Book, DVD and CD inherit class LibraryItem with single inheritance level.
Book :-
So Now the Book Class have two Fields : Title ( Inherited from LibraryItem ) and Author ( New Field of class Book ).
It also has Methods like Constructor , getDescription(),getTitle(),getAuthor().
In the Constructor we use another keyword 'super' which is used to initialise the constructor ofbase class LibraryItem.
so the fields title and author both get initialized.
DVD :-
Similarly class DVD inherit class LibraryItem with only single level inheritance. So it has title field from LibraryItem and some new fields by its own.
The constructor of DVD class also uses super keyword.
CD :-
Same as above classes this class uses single level inheritance.
After the while loop we have made an example for polymorphism in java ,The function overloading.
For that we have created the reference of base class and created an object of all the derived classes seperately.
the reference objL1 points to Book class object. we can make the method in base class with a name and in derived class we can also make a method with same name and parameters .
when we call the getDescription() method it automatically calls the method from derived class instead of base class.
so the function/method overloading is satisfied .
Similarly we did overloading in all the derived classes.
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.