DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
WORKSHEET 1
Student Name: Subodh Aryan
UID: 22BCS12707 Branch: CSE
Section/Group: 705-A Semester: 3rd
Date of Performance: 28/08/23 Subject Code:22CSH-201
Subject Name: Java Programming
1. Aim: Being a student, you read from CSE Textbooks and DVD from
your online lecture, Literature review and Novels from online Library.
You want to implement a bookshelf with this class and methods.
2. Source Code:
import java.util.ArrayList;
import java.util.List;
class Item {
private String title;
private String type;
public Item(String title, String type) {
this.title = title;
this.type = type;
}
public String getTitle() {
return title;
}
public String getType() {
return type;
}
}
class Book extends Item {
private String author;
public Book(String title, String author) {
super(title, "Book");
this.author = author;
}
}
class Textbook extends Book {
private String subject;
public Textbook(String title, String author, String subject) {
super(title, author);
this.subject = subject;
}
}
class DVD extends Item {
private String director;
public DVD(String title, String director) {
super(title, "DVD");
this.director = director;
}
}
class OnlineLecture extends Item {
private String instructor;
private String platform;
public OnlineLecture(String title, String instructor, String platform) {
super(title, "Online Lecture");
this.instructor = instructor;
this.platform = platform;
}
}
class LiteratureReview extends Item {
public LiteratureReview(String title) {
super(title, "Literature Review");
}
}
class Bookshelf {
private List<Item> contents;
public Bookshelf() {
contents = new ArrayList<>();
}
public void addItem(Item item) {
contents.add(item);
System.out.println("Added " + item.getTitle() + " to the bookshelf.");
}
public void removeItem(Item item) {
if (contents.contains(item)) {
contents.remove(item);
System.out.println("Removed " + item.getTitle() + " from the bookshelf.");
} else {
System.out.println(item.getTitle() + " not found on the bookshelf.");
}
}
public void listContents() {
if (contents.isEmpty()) {
System.out.println("The bookshelf is empty.");
} else {
System.out.println("Bookshelf Contents:");
for (int i = 0; i < contents.size(); i++) {
Item item = contents.get(i);
System.out.println((i + 1) + ". " + item.getTitle() + " (" + item.getType() + ")");
}
}
}
}
public class Main {
public static void main(String[] args) {
Bookshelf bookshelf = new Bookshelf();
Textbook textbook = new Textbook("Computer Science Textbook", "John Smith",
"Computer Science");
DVD dvd = new DVD("Online Lecture DVD", "Jane Doe");
LiteratureReview review = new LiteratureReview("Literature Review");
Book novel = new Book("Great Novel", "Author Subodh");
bookshelf.addItem(textbook);
bookshelf.addItem(dvd);
bookshelf.addItem(review);
bookshelf.addItem(novel);
bookshelf.listContents();
bookshelf.removeItem(dvd);
bookshelf.listContents();
}
}
3. Screenshot of Outputs:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
4. Learning Outcomes
(i) We learn about java programming.
(ii) We learn about Constructor.
(iii)We learn about java methods.
(iv)We learn about inheritance.