Question

In: Computer Science

TestLibrary.java Design and implement a class named Book. A book has a serial number, a title,...

TestLibrary.java

Design and implement a class named Book. A book has a serial number, a title, an author, and a publisher. Create appropriate constructor(s) and accessor and mutator methods for the Book class.

Add a static variable to the Book class and set its initial value to 100000000. When a new book is created, this static variable is automatically incremented by 1 and the new value is assigned as the serial number of the new book.

Design and implement a class named Library. A library has a library name (such as Surrey Central Library), a list of books (which is an array of Book objects. Assume that the maximum number of books for a library is 10000), and the actual number of books in the library. Create a constructor for creating a library object with a given library name. Create a method for adding a book to the library and another method for returning the array of all books. Add another method to get the number of books in the library.

In the main method, create a new library object and create a few books. Add the books to the library. Get the list of books and display their serial numbers, titles, author names, and publishers.

Solutions

Expert Solution

Book.java

public class Book {
   private int serial;
   private String title;
   private String author;
   private String publisher;
   private static int IV = 100000;  
  
   public Book(String title, String author, String publisher) {
       super();
       IV++;
       this.serial = IV;      
       this.title = title;
       this.author = author;
       this.publisher = publisher;
   }
   public String getTitle() {
       return title;
   }
   public void setTitle(String title) {
       this.title = title;
   }
   public String getPublisher() {
       return publisher;
   }
   public void setPublisher(String publisher) {
       this.publisher = publisher;
   }
   public int getSerial() {
       return serial;
   }
   public String getAuthor() {
       return author;
   }
   //I 've added this for demonstration. You can delete it if you want.
   @Override
   public String toString() {
       return "Book [serial=" + serial + ", title=" + title + ", author=" + author + ", publisher=" + publisher + "]";
   }

}

Library.java

public class Library {
   private Book[] books;
   private String name;
   private int numOfBooks;
   private static final int MAX = 10000;
   public Library(String name) {
       super();
       numOfBooks = 0;
       books = new Book[10000];
       this.name = name;
   }
  
   public void addBook(Book b) {
       if(numOfBooks<MAX) {
           books[numOfBooks] = b;
           numOfBooks++;
       }
   }
  
   public Book[] getBooks() {
       Book[] buks = new Book[this.numOfBooks];
       for(int i=0; i<numOfBooks; i++)
           buks[i] = this.books[i];
      
       return buks;
   }
  
   public int getNumOfBooks() {
       return this.numOfBooks;
   }
}

TestLibrary.java

public class TestLibrary {

   public static void main(String[] args) {
       //Create library
       Library lib = new Library("Surrey Central Library");
       //Create and add some books to library
       Book b = new Book("Book 1", "Author 1", "Publisher 1");
       lib.addBook(b);
       b = new Book("Book 2", "Author 2", "Publisher 2");
       lib.addBook(b);
       b = new Book("Book 3", "Author 3", "Publisher 3");
       lib.addBook(b);
       b = new Book("Book 4", "Author 4", "Publisher 4");
       lib.addBook(b);
       b = new Book("Book 5", "Author 5", "Publisher 5");
       lib.addBook(b);
      
       Book[] books = lib.getBooks();
       for(int i=0; i<books.length; i++)
           System.out.println(books[i]);
      
       System.out.println("Number of books in library : " + lib.getNumOfBooks());
   }

}



Related Solutions

Programming Exercise Implement the following class design: class Tune { private:    string title; public:   ...
Programming Exercise Implement the following class design: class Tune { private:    string title; public:    Tune();    Tune( const string &n );      const string & get_title() const; }; class Music_collection { private: int number; // the number of tunes actually in the collection int max; // the number of tunes the collection will ever be able to hold Tune *collection; // a dynamic array of Tunes: "Music_collection has-many Tunes" public: // default value of max is a conservative...
In java design and code a class named comparableTriangle that extends Triangle and implements Comparable. Implement...
In java design and code a class named comparableTriangle that extends Triangle and implements Comparable. Implement the compareTo method to compare the triangles on the basis of similarity. Draw the UML diagram for your classes. Write a Driver class (class which instantiates the comparableTriangle objects) to determine if two instances of ComparableTriangle objects are similar (sample output below). It should prompt the user to enter the 3 sides of each triangle and then display whether or not the are similar...
Design a class that holds the following data regarding a music album: artist, title, number of...
Design a class that holds the following data regarding a music album: artist, title, number of tracks, and year released. Write appropriate accessor and mutator methods. Also, write a program that creates three instances of the class. Each instance will hold information about a different music album. Make sure to display all of the information for all three albums to the screen in an organized manner. **Using python**
Implement in C++ Design a BookstoreManager class which creates a dynamic array of type Book (don’t...
Implement in C++ Design a BookstoreManager class which creates a dynamic array of type Book (don’t use vectors), and provide an implementation for the following operations on books in the array 1)isEmpty() returns true if the array is empty, otherwise false 2)isFull() returns true if the array is full, otherwise false 3)listSize() prints the number of books in the array 4)print() prints the content of the array 5)insert(Book) asks the user to enter new book info, and it adds the...
Implement in C++ Design a BookstoreManager class which creates a dynamic array of type Book (don’t...
Implement in C++ Design a BookstoreManager class which creates a dynamic array of type Book (don’t use vectors), and provide an implementation for the following operations on books in the array 1)isEmpty() returns true if the array is empty, otherwise false 2)isFull() returns true if the array is full, otherwise false 3)listSize() prints the number of books in the array 4)print() prints the content of the array 5)insert(Book) asks the user to enter new book info, and it adds the...
Design a class named Fan to represent a fan. The class contains: ■ Three constants named...
Design a class named Fan to represent a fan. The class contains: ■ Three constants named SLOW, MEDIUM, and FAST with the values 1, 2, and 3 to denote the fan speed. ■ A private int data field named speed that specifies the speed of the fan (the default is SLOW). ■ A private boolean data field named on that specifies whether the fan is on (the default is false). ■ A private double data field named radius that specifies...
In java, design a class named Motorcycle that has the following fields: • year – The...
In java, design a class named Motorcycle that has the following fields: • year – The year field is an int that holds the motorcycle’s year • make – The make field references a String object that holds the make of the motorcycle. • speed – The speed field is an int that holds the motorcycle’s current speed The class should have the following constructor and other methods: • Constructor – the constructor should accept the motorcycle’s year and make...
In Java, design a class named MyInteger. The class contains: An int data field named value...
In Java, design a class named MyInteger. The class contains: An int data field named value that stores the int value represented by this object. A constructor that creates a MyInteger object for the specified int A get method that returns the int Methods isEven(), isOdd(), and isPrime() that return true if the value is even, odd, or prime, respectively. Static methods isEven(int), isOdd(int), and isPrime(int) that return true if the specified value is even, odd, or prime, respectively. Static...
Design a class named Person with fields for holding a person's name, address, and telephone number(all...
Design a class named Person with fields for holding a person's name, address, and telephone number(all as Strings). Write a constructor that initializes all of these values, and mutator and accessor methods for every field. Next, design a class named Customer, which inherits from the Person class. The Customer class should have a String field for the customer number and a boolean field indicating whether the customer wishes to be on a mailing list. Write a constructor that initializes these...
Design a class named Person with fields for holding a person's name, address, and telephone number(all...
Design a class named Person with fields for holding a person's name, address, and telephone number(all as Strings). Write a constructor that initializes all of these values, and mutator and accessor methods for every field. Next, design a class named Customer, which inherits from the Person class. The Customer class should have a String field for the customer number and a boolean field indicating whether the customer wishes to be on a mailing list. Write a constructor that initializes these...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT