Sie sind auf Seite 1von 5

LAB 2:

Classes and Objects


Dr. Muhammad Safyan
Atif Ishaq
Muhammad Asjid
12 June 2020

1 Problem Statement
The libraries of SmallTownX need a new electronic rental system, and it is up to
you to build it. SmallTownX has two libraries. Each library offers many books
to rent. Customers can print the list of available books, borrow, and return
books.
There are three classes Book , Author and Library, that provide the func-
tionality for the book database. You must implement the methods to make
these classes work.

2 Implement class Book


First we need a class to model books. Start by creating a class called Book.
class Book will have Following Attributes:

Book:
– title: String
– author: String
– borrowed:boolean
– bookNumber: int
Book class Contains Following methods:

1. Add a constructor with all the attributes of the class Book. (Hint: It
should look like Book(Title, borrowed, bookNumber)
2. Add the getter and setter methods for the class Book.(Your methods
should be named properly for example setter and getter method for ti-
tle will be named as setName and getName)

1
3. Add another method called void display() that prints out all the values
of the attributes.
4. Implement these two methods
1 // Marks t h e book a s r e n t e d
2 p u b l i c v o i d borrowed ( ) {
3 // Implement t h i s method
4 }
5 // Marks t h e book a s not r e n t e d
6 public void returned ( ) {
7 // Implement t h i s method
8 }
Listing 1: Implement these methods

Inside the Main Class/Main Method:


5. Create an object of type Book with an empty constructor/default con-
structor. [What error do you get and try to resolve it ]
6. Create two instances of type Book and set it’s attributes using the setter
methods.
7. Create another two instances of type Book using the argument constructor.
8. Call the display method of class Book for each object.
9. Instead of giving four different calls to display() method for each Book in-
stance, use an array to store Book instances and write the call to display()
method only once.

3 Implement Class Author


Author Class Has following Attributes.
Author:
– name: String
– email: String
Author class Contains Following methods:
1. Add an argument constructor with all the attributes of the Author.
2. Add the getter/setters methods for the attributes.
3. Add a method as Boolean updateEmail(string newEmail) for the class
Author.
4. Also add a method called void display() that prints out all the values of
the attributes.
5. create a Main class. Create two different instances of type Author using
the argument constructor and display it’s values using the display method.

2
4 Change class Book.author attribute
Change class Book.author attribute from String to the Author type as: Book:
– title: String
– author: Author
– borrowed:boolean
– bookNumber: int

5 Main method for book class


write and main method for book class and Run few tests
1 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arguments ) {
2 // Small t e s t o f t h e Book c l a s s
3 Book example = new Book ( ” Harry p o t t e r ” ) ;
4 System . out . p r i n t l n ( ” T i t l e ( s h o u l d be Harry p o t t e r ) : ” + example .
getTitle () ) ;
5 System . out . p r i n t l n ( ” Borrowed ? ( s h o u l d be f a l s e ) : ” + example .
isBorrowed ( ) ) ;
6 example . r e n t e d ( ) ;
7 System . out . p r i n t l n ( ” Borrowed ? ( s h o u l d be t r u e ) : ” + example .
isBorrowed ( ) ) ;
8 example . r e t u r n e d ( ) ;
9 System . out . p r i n t l n ( ” Borrowed ? ( s h o u l d be f a l s e ) : ” + example .
isBorrowed ( ) ) ;
10 }

6 Implement class library


Next we need to build the class that will represent each library, and manage a
collection of books. All libraries have the same hours: 9 AM to 5 PM daily.
However, they have different addresses and book collections (i.e., arrays of Book
objects).

library class will have following attributes:

LIBRARY :
– Books[10]: Book(The Size of Book array is 10)
– libraryHours : String
– address : String
Library class Contains Following methods:

1. Add an argument constructor which takes parameter of library Address.

3
2. Add the getter/setters methods for the address and library hours.
3. write a method addbook(Book book) which takes a book as a argument
and add this to books array.
4. write a method printOpeningHours()that prints the library hours.

5. write a method printAddress(); that prints the address of library Now,


go back to the Main class.
6. write a method printAvailableBooks() that prints all books that are in
books array

7. write a method boolean borrowBook(string bookname) it will find


the book with this name in the books array and set the value of borrowed
variable of that book to true.and returns true if the book is borrowed other
vise false.
8. write a method boolean returnBook(string bookname) it will find
the book with this name in the Books array and set the value of borrowed
variable of that book to false.and returns true if the book is returned other
vise false.
1 // Marks t h e book a s r e n t e d
2 p u b l i c Boolean borrowBook ( S t r i n g BookName ) {
3 // Implement t h i s method
4
5 // code t o match two s t r i n g s
6 i f ( BookName==”The o t h e r S t r i n g ” )
7 System . out . p r i n t l n ( ” s t r i n g matched ” ) ;
8 else
9 System . out . p r i n t l n ( ” s t r i n g not matched ” ) ;
10
11 }
12 // Marks t h e book a s not r e n t e d
13 p u b l i c Boolean r e t u r n e d ( S t r i n g BookName ) {
14 // Implement t h i s method
15 }
Listing 2: Implement these methods

7 Main method for Library class


write and main method for book class and Run few tests
1. create two libraries

2. Add 4 books to each of them


3. Print opening hours and the addresses of library
4. Borrow some Books

4
5. Print all books in library one
6. Return two books

8 Submission guidelines
• you had to submit three files
– Book.java
– Author.java
– Library.java

• Each class will have its own main method in which you will perform the
given tests.
• Your files name should be as:
– XXX-BSCS-20XXBook.java
– XXX-BSCS-20XXAuthor.java
– XXX-BSCS-20XXLibrary.java
• At the top of the every file put title in comments as:
IN-LAB 2 : Solution
Your name
your roll no. XXX-BSCS-20XX (Section XX)
• No collaboration/group work is required. Copied assignments will get no
marks.
• Labs Submitted After Deadline or not following submission guidelines will
get No marks.

Das könnte Ihnen auch gefallen