Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a class “Book” to model books. This book object will have two instance var

ID: 3699203 • Letter: W

Question

Write a class “Book” to model books. This book object will have two instance variables: a title (String) and authors (Array of Strings). Write a default constructor which sets title to “Test” and authors to null. Write a constructor receiving values for these fields. Lastly, implement getter and setter methods for your instance variables. In “Book”, write a method “bookToString”, which returns a string with the books information (in the format below. Use ” to have a quote character in a string. “The Lord of the Rings” by J.R.R. Tolkien “Nixonland” by Rick Perlstein “Berenstain Bears” by Stan Berenstain & Jan Berenstain Write a class “Bookshelf” to model a bookshelf. This bookshelf object will have two instance variables: size (int) and books (ArrayList of Books). Write a default constructor which sets the size to 2 and initializes the ArrayList. Write a parameterized constructor which receives a single parameter for size and initializes the ArrayList. Write a getter method for the size instance variable. (Don’t write a setter method, you can’t change the size of a bookshelf after it’s been created!) Write a getter method for the books instance variable. Rather than a single setter method, write three specific methods to modify the contents of the bookshelf: a public void method addBook which takes a single book parameter and adds it to the shelf variable if there is room in the shelf, a public void method removeBook which takes no parameters and removes the first book on the shelf (the book at position 0 in the books ArrayList), and a public void method emptyBookshelf which takes no parameters and removes all of the books from the shelf (empties the books ArrayList). Neither the “Book” class or the “Bookshelf” class can have a main method. If you want to test your “Book” and “Bookshelf” classes, you may make a separate class with a main method. You do not need to submit this separate class.

Explanation / Answer

Book.java

public class Book {

private String title;

private String authors[];

public Book() {

this.title="Test";

authors=null;

}

public Book(String title, String[] authors) {

super();

this.title = title;

this.authors = authors;

}

public String getTitle() {

return title;

}

public void setTitle(String title) {

this.title = title;

}

public String[] getAuthors() {

return authors;

}

public void setAuthors(String[] authors) {

this.authors = authors;

}

public String bookToString()

{

String str="";

if(authors.length>1)

for(int i=0;i<authors.length;i++)

{

str+="""+title+"" by "+authors[i];

if(i!=authors.length-1)

{

str+="&";

}

}

else

{

str+="""+title+"" by "+authors[0];

}

return str;

}

}

_________________

package org.students;

import java.util.ArrayList;

public class BookShelf {

private int size;

private ArrayList<Book> books;

public BookShelf() {

super();

this.size = 2;

this.books = new ArrayList<Book>();

}

public BookShelf(int size, ArrayList<Book> books) {

super();

this.size = size;

this.books = books;

}

public int getSize() {

return size;

}

public ArrayList<Book> getBooks() {

return books;

}

public void addBook(Book b)

{

if(books.size()<2)

books.add(b);

else

System.out.println(":: Book Shelf is full ::");

}

public void removeBook()

{

if(books.size()>0)

books.remove(0);

else

System.out.println(":: Book Shelf is empty ::");

}

public void emptyBookShelf()

{

books.clear();

}

}

______________________

Test.java

import java.util.ArrayList;

import java.util.Scanner;

public class Test {

public static void main(String[] args) {

// Declaring variables

String bookname, author;

int nos;

/*

* Creating an Scanner class object which is used to get the inputs

* entered by the user

*/

Scanner sc = new Scanner(System.in);

BookShelf bs = new BookShelf();

while (true) {

System.out.println(" :: Menu ::");

System.out.println("1.Add Book");

System.out.println("2.Remove Book");

System.out.println("3.Display Books");

System.out.println("4.Clear Shelf");

System.out.println("5.Exit");

System.out.print("Enter Choice :");

int choice = sc.nextInt();

sc.nextLine();

switch (choice) {

case 1: {

System.out.print("Enter the Book name :");

bookname = sc.nextLine();

System.out.print("No of Authors :");

nos = sc.nextInt();

String authNames[] = new String[nos];

sc.nextLine();

for (int i = 0; i < nos; i++) {

System.out.print("Enter Name Author#" + (i + 1) + ":");

authNames[i] = sc.nextLine();

}

bs.addBook(new Book(bookname, authNames));

continue;

}

case 2: {

bs.removeBook();

continue;

}

case 3: {

ArrayList<Book> arl = bs.getBooks();

if (arl.size() == 0) {

System.out.println("** Book Shelf is empty **");

} else {

for (int i = 0; i < arl.size(); i++) {

System.out.println(arl.get(i).bookToString());

}

}

continue;

}

case 4: {

bs.emptyBookShelf();

continue;

}

case 5: {

System.out.println("** Program Exit **");

break;

}

default: {

System.out.println("** Invalid Choice **");

continue;

}

}

break;

}

}

}

____________________

Output:


:: Menu ::
1.Add Book
2.Remove Book
3.Display Books
4.Clear Shelf
5.Exit
Enter Choice :1
Enter the Book name :The Lord of the Rings
No of Authors :1
Enter Name Author#1:J.R.R Tolklen

:: Menu ::
1.Add Book
2.Remove Book
3.Display Books
4.Clear Shelf
5.Exit
Enter Choice :1
Enter the Book name :Berenstain
No of Authors :2
Enter Name Author#1:Stan Berenstain
Enter Name Author#2:Jan Baresstain

:: Menu ::
1.Add Book
2.Remove Book
3.Display Books
4.Clear Shelf
5.Exit
Enter Choice :3
"The Lord of the Rings" by J.R.R Tolklen
"Berenstain" by Stan Berenstain&"Berenstain" by Jan Baresstain

:: Menu ::
1.Add Book
2.Remove Book
3.Display Books
4.Clear Shelf
5.Exit
Enter Choice :1
Enter the Book name :Nixonland
No of Authors :1
Enter Name Author#1:Rick Perlstein
:: Book Shelf is full ::

:: Menu ::
1.Add Book
2.Remove Book
3.Display Books
4.Clear Shelf
5.Exit
Enter Choice :2

:: Menu ::
1.Add Book
2.Remove Book
3.Display Books
4.Clear Shelf
5.Exit
Enter Choice :1
Enter the Book name :Nixonland
No of Authors :1
Enter Name Author#1:Rick Perlstein

:: Menu ::
1.Add Book
2.Remove Book
3.Display Books
4.Clear Shelf
5.Exit
Enter Choice :3
"Berenstain" by Stan Berenstain&"Berenstain" by Jan Baresstain
"Nixonland" by Rick Perlstein

:: Menu ::
1.Add Book
2.Remove Book
3.Display Books
4.Clear Shelf
5.Exit
Enter Choice :4

:: Menu ::
1.Add Book
2.Remove Book
3.Display Books
4.Clear Shelf
5.Exit
Enter Choice :3
** Book Shelf is empty **

:: Menu ::
1.Add Book
2.Remove Book
3.Display Books
4.Clear Shelf
5.Exit
Enter Choice :5
** Program Exit **

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote