Title:
(1) Follow the prompts in the book.java code to complete this class. This category mainly provides functions such as borrowing books, returning books, checking the borrowing status of books, and obtaining book titles. When running this code, the output is:
Title (should be The Da Vinci Code): The Da Vinci Code
Rented? (should be false): false
Rented? (should be true): true
Rented? (should be false): false
(2) Complete Library.java, the main() method in this class has been given. But class members and class methods need to be improved. Please note the following requirements when completing this class:
A. There are class member methods and instance member methods in this class;
B. The content of the main() method cannot be changed.
The output of the C.main() method is:
Library hours:
Libraries are open daily from 9am to 5pm.
Library addresses:
10 Main St.
228 Liberty St.
Borrowing The Lord of the Rings:
You successfully borrowed The Lord of the Rings
Sorry, this book is already borrowed.
Sorry, this book is not in our catalog.
Books available in the first library:
The Da Vinci Code
Le Petit Prince
A Tale of Two Cities
Books available in the second library:
No book in catalog
Returning The Lord of the Rings:
You successfully returned The Lord of the Rings
Books available in the first library:
The Da Vinci Code
Le Petit Prince
A Tale of Two Cities
The Lord of the Rings
The code is as follows:
1.Book class:
public class Book { String title; boolean borrowed; // Creates a new Book public Book(String bookTitle) { // Implement this method this.title = bookTitle; borrowed = false; } // Marks the book as rented public void rented() { // Implement this method borrowed = true; } // Marks the book as not rented public void returned() { // Implement this method borrowed = false; } // Returns true if the book is rented, false otherwise public boolean isBorrowed() { // Implement this method return borrowed; } // Returns the title of the book public String getTitle() { // Implement this method return title; } public static void main(String[] arguments) { // Small test of the Book class Book example = new Book("The Da Vinci Code"); System.out.println("Title (should be The Da Vinci Code): " + example.getTitle()); System.out.println("Borrowed? (should be false): " + example.isBorrowed()); example.rented(); System.out.println("Borrowed? (should be true): " + example.isBorrowed()); example.returned(); System.out.println("Borrowed? (should be false): " + example.isBorrowed()); } }
2.Libarary class:
public class Library { String address; Book[] books = new Book[5]; //Add the missing implementation to this class public Library(String adress) { address = adress; books = new Book[5]; } public static void printOpeningHours() { System.out.println("Libraries are open daily from 9am to 5pm."); } public void printAddress() { System.out.println(address); } public void addBook(Book b) { for (int i = 0; i < books.length; i + + ) { if (books[i] == null) { books[i] = new Book(b.getTitle()); books[i].borrowed = false; break; } } } public void borrowBook(String name) { int i = 0; int cnt = 1; for (i = 0; books[i] != null; i + + ) { if (name.equals(books[i].getTitle())) { cnt = 0; break; } } if (cnt != 0) { System.out.println("Sorry, this book is not in our catalog."); } else { if (books[i].isBorrowed()) { System.out.println("Sorry, this book is already borrowed."); } else { System.out.println("You successfully borrowed " + name); books[i].rented(); } } } public void printAvailableBooks() { int cnt = 0; for (int i = 0; books[i] != null; i + + ) { if (!books[i].isBorrowed()) { System.out.println(books[i].getTitle()); cnt + + ; } } if (cnt == 0) { System.out.println("No book in catalog"); } } public void returnBook(String name) { for (int i = 0; books[i] != null; i + + ) { if (name.equals(books[i].getTitle())) { books[i].returned(); System.out.println("You successfully returned " + name); } } } public static void main(String[] args) { // Create two libraries Library firstLibrary = new Library("10 Main St."); Library secondLibrary = new Library("228 Liberty St."); // Add four books to the first library firstLibrary.addBook(new Book("The Da Vinci Code")); firstLibrary.addBook(new Book("Le Petit Prince")); firstLibrary.addBook(new Book("A Tale of Two Cities")); firstLibrary.addBook(new Book("The Lord of the Rings")); // Print opening hours and the addresses System.out.println("Library hours:"); printOpeningHours(); System.out.println(); System.out.println("Library addresses:"); firstLibrary.printAddress(); secondLibrary.printAddress(); System.out.println(); // Try to borrow The Lords of the Rings from both libraries System.out.println("Borrowing The Lord of the Rings:"); firstLibrary.borrowBook("The Lord of the Rings"); firstLibrary.borrowBook("The Lord of the Rings"); secondLibrary.borrowBook("The Lord of the Rings"); System.out.println(); // Print the titles of all available books from both libraries System.out.println("Books available in the first library:"); firstLibrary.printAvailableBooks(); System.out.println(); System.out.println("Books available in the second library:"); secondLibrary.printAvailableBooks(); System.out.println(); // Return The Lords of the Rings to the first library System.out.println("Returning The Lord of the Rings:"); firstLibrary.returnBook("The Lord of the Rings"); System.out.println(); // Print the titles of available from the first library System.out.println("Books available in the first library:"); firstLibrary.printAvailableBooks(); } }
Run results:
1.Book class:
2.Library class:
The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Java Skill TreeHomepageOverview 138116 people are learning the system