JAVA Programming Exercise 1) Super Class: Create a class Person private member v
ID: 3821670 • Letter: J
Question
JAVA Programming Exercise
1) Super Class: Create a class Person
private member variables firstName (String), lastName (String)
Add public setter , getter and constructor with parameters to class
Add public toString method
2) Inheritance: Create a class Author as a subclass of class Person
the class also has private member variable email (String)
Add public setter , getter and constructor with parameters to the class
Add public toString method by overriding the super class method
3) Composition: Create a class Book with
private member variables title (String), isbn (title), and author (Author). Note: though in real life a book can have a list of authors, in this lab we assumed that each book has a single author to simplify coding.
Add public setter , getter and constructor with parameters to the class
Add public toString method
4) Create a class Library
private member variables books (Book[] that is books is a one dimensional array of books), bookCount(int) where bookCount keeps track of actual number of books stored in the library.
Add a constructor with parameters to the class. Hint: The library has a capacity that can be set by the client program. So, you can write a constructor for class library that will take the capacity of the library as a parameter.
Add public method addNewBook method to the class: This method will add a new book to the array books. Note: When writing this method, also consider what to do when there is "no room" in the library to store a new book.
Add public method printBooks method to the class
5) Client Code: Create a class LibrarySimulation that will have the main function to do the following
create a new author object
create two book objects, both books have the same author created above
create a library object that can hold 10,000 books
add the two books to the library
print all the books in the library
Explanation / Answer
Person.java
package library;
/*
* class Person
*/
public class Person {
//member variables firstName (String), lastName (String)
private String firstName;
private String lastName;
//Constructor with parameters
public Person(String firstName, String lastName){
this.firstName=firstName;
this.lastName=lastName;
}
//public setter , getter
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
//toString method to read information related to Person
public String toString(){
return firstName+" "+lastName;
}
}
Author.java
package library;
/*
* Author a subclass of class Person
*/
public class Author extends Person {
//private member variable email
private String email;
//Constructor with parameters firstname, lastName and email
public Author(String firstName, String lastName, String email) {
super(firstName, lastName);
this.email=email;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
// toString that can be used for getting Author information readable
public String toString(){
return "Author: "+getFirstName()+" "+getLastName()+" "+"Email: "+email;
}
}
Book.java
package library;
public class Book {
//private member variables title (String), isbn (String), and author (Author)
private String title;
private String isbn;
private Author author;
//Constructor with parameters
public Book(String title, String isbn, Author author){
this.title=title;
this.isbn=isbn;
this.author=author;
}
// public setter , getter and constructor with parameters to the class
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public Author getAuthor() {
return author;
}
public void setAuthor(Author author) {
this.author = author;
}
//toString() method to get the data related to Book
public String toString(){
return "Title: "+title+ " "+author.toString()+" ISBN: "+isbn+" ";
}
}
Library.java
package library;
/*
* Library class with ,
*/
public class Library {
private Book[] books; //variable books (Book[] that is books is a one dimensional array of books)
private int bookCount;//bookCount(int) where bookCount keeps track of actual number of books stored in the library.
private int capacity;// capacity holds the capacity of library to hold books
public Library(int capacity){
this.capacity= capacity;
books=new Book[capacity];
}
/*
* This method will add a new book to the array books.
*/
public void addNewBook(Book book){
if(bookCount < capacity){
books[bookCount]=book;
bookCount++;
System.out.println("Added New Book to Library. ");
}else{
System.out.println("Library do not have capacity to add new books.");
}
}
/*
* Prints all the books
*/
public void printBooks(){
for (int i=0;i<bookCount;i++) {
System.out.println(books[i].toString()); //prints books in the given format as Book toString method
}
}
}
LibrarySimulation.java
package library;
public class LibrarySimulation {
public static void main(String[] args) {
//create a new author object
Author author=new Author("First","Last","testmailcom");
//create two book objects, both books have the same author created above using Parameterized Constructor
Book book1=new Book("SampleTitle1","testISBN1",author);
Book book2=new Book("SampleTitle2","testISBN2",author);
//create a library object that can hold 10,000 books
Library library =new Library(10000);
//add the two books to the library
library.addNewBook(book1);
library.addNewBook(book2);
//print all the books in the library
library.printBooks();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.