Menu

[solved]-Hello Trouble Java Program Assignment Create Library Allows Either Addbooks Library Check Q39086364

Hello, I am havingtrouble with my java program. The assignment is to create a Librarythat allows you to either addbooks to the Library or check themout. I am having trouble with my methods “addBook” and “addBooks”.we already have an array called “books” that stores all the booksin the library. I am having trouble adding to that already existingarray in methods “addBook” and “addBooks”

I will post my codebelow.

public class Library {

/** Unique books in the library. */
private Book[] books;

/** Number of copies for each book. */
private int[] copies;

/** Number of copies currently checked out for each book.*/
private int[] checkedOut;

/** Number of unique books in the library. */
private int numBooks;

/** Construct a new empty Library. */
public Library(int librarySize) {
books = new Book[librarySize];
copies = new int[librarySize];
checkedOut = new int[librarySize];
numBooks = 0;
}

/**
* Get the number of total copies of all books that exist inthe
* library.
* @return Total number of copies in the library.
*/
public int getTotalCopies() {
   int Totalcop = 0;
   for(int i = 0; i < copies.length; i ++) {
       Totalcop =+ copies[i];
   }
return Totalcop;
}

/**
* Get the number of copies currently checked out.
* @return Total number of copies checked out.
*/
public int getNumCheckedOut() {
   int Totalnum = 0;
   for(int i = 0; i < checkedOut.length; i++) {
       Totalnum =+ checkedOut[i];
   }
return Totalnum;
}

/**
* Get a String representing the status of the library.
* @return Status string.
*/
public String getStatus() {
   if(books.length != 0){
       return “Total unique books: ” +numBooks + “n” + “Total number of copies: “+ getTotalCopies() +”n” + “Total checked out: ” + getNumCheckedOut();
   }
   return “Total unique books: 0n” + “Total number ofcopies: 0n” + “Total checked out: 0”;
  
}

/**
* Add all the books in the array to the library. Adds one copyof
* each book.
* @param newBooks Books to add.
*/
public void addBooks( Book[] newBooks ) {
   int currentSize = numBooks;
   int newSize = currentSize + newBooks.length;
  

       int[] tempCopies = newint[newSize];       //sets temp array forcopies
       int[] tempCheckedOut = newint[newSize];   //sets temp array for checkout
       Book[] tempBooks = newBook[newSize];       // sets temp arrayfor books
       for(int i = 0; i < numBooks;i++) {       //loops number of uniquebooks in library
           tempBooks[i] =books[i];              //copies element from book to tempbook
           tempCopies[i] =copies[i];              //copies element from copies to tempcopies
          tempCheckedOut[i] = checkedOut[i];      //copies element from checkedout to tempcheckout
       }
       copies = tempCopies;                 //copies tempcopie into copies (withnewsize)
       checkedOut =tempCheckedOut;          //copies tempcheckedout into checkedout with new size
       books = tempBooks;                     //copies temp book into bookswith new size
  
       for(int j = 0; j <newBooks.length; j++) {   //loops for numb ofnewbooks
           books[j+numBooks] = newBooks[j];   //adds element from newBooksat the end of books
       }
       numBooks +=newBooks.length;   //updates number of unique books inlibrary
}

/**
* Add a single book the library.
*
* If the book is already present, adds another copy.
* If the book is new, add it after the existing books.
* @param b Book to add.
*/
public void addBook( Book b ) {
   addBooks(new Book[] {b});

}

Expert Answer


Answer to Hello, I am having trouble with my java program. The assignment is to create a Library that allows you to either addbook… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *