Question: IT IS URGENT HELP PLEASE ! I have a class code and I need to change it by using array. Can you help me? Here
IT IS URGENT HELP PLEASE !
I have a class code and I need to change it by using array. Can you help me?
Here is the code:
public class Library {
LibraryBook book1;
LibraryBook book2;
LibraryBook book3;
LibraryBook book4;
public Library()
{
book1 = null;
book2 = null;
book3 = null;
book4 = null;
}
public boolean Empty()
{
if(book1 == null && book2 == null && book3 == null && book4 == null)
{
return true;
}
else
{
return false;
}
}
public String toString() {
String newString = "";
if ( Empty())
{
return "no book ";
}
else
{
if(book1 != null)
{
newString = newString + book1.toString();
}
if(book2 != null)
{
newString = newString + book2.toString();
}
if(book3 != null)
{
newString = newString + book3.toString();
}
if(book4 != null)
{
newString = newString + book4.toString();
}
return newString;
}
}
public void add(String title, String author){
if(book1 == null )
{
LibraryBook temp = new LibraryBook(title,author,"",0);
book1 = temp;
System.out.println("Book added.");
return;
}
if(book2 == null )
{
LibraryBook temp = new LibraryBook(title,author,"",0);
book2 = temp;
System.out.println("Book added.");
return;
}
if(book3 == null )
{
LibraryBook temp = new LibraryBook(title,author,"",0);
book3 = temp;
System.out.println("Book added");
return;
}
if(book4 == null )
{
LibraryBook temp = new LibraryBook(title,author,"",0);
book4 = temp;
System.out.println("Book added");
return;
}
else
{
System.out.println("Book not added! No slot available!");
}
}
public boolean remove(LibraryBook book)
{
if((book1 != null) && book.equals(book1))
{
book1 = null;
}
else if((book2 != null) && book.equals(book2))
{
book2 = null;
}
else if((book3 != null) && book.equals(book3))
{
book3 = null;
}
else if((book4 != null) && book.equals(book4))
{
book4 = null;
}
else
{
return false;
}
return true;
}
public LibraryBook findByTitle(String title)
{
if ( book1 != null)
{
if(title.equals(book1.getTitle()))
{
return book1;
}
}
if ( book2 != null)
{
if(title.equals(book2.getTitle()))
{
return book2;
}
}
if ( book3 != null)
{
if(title.equals(book3.getTitle()))
{
return book3;
}
}
if ( book4 != null)
{
if(title.equals(book4.getTitle()))
{
return book4;
}
}
return null;
}
}
This is my Library class and I have another class called LibraryBook. There are four properties of LibraryBook(book1 book2 book3 book4). I need to replace them with a single property,an arraylist of LibraryBook. Then modify the constructor which takes no parameter and other methods isEmpty toString add remove findByTitle so they work with that new property. Note: you can't change any of the method signatures-only their implementations- so you will not need to change other classes ( that's why I shared only this class). Note: the ArrayList "manages" the collection of LibraryBook objects so you no longer need to check for or set the references to null.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
