题目:
(1)按照book.java代码中的提示完成该类。该类主要提供借书、还书、查询书的借阅状态、获取书名等功能。当运行该代码的时候,输出为:
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)完成Library.java,该类中的main()方法已经给出。但类成员和类方法需要完善。完成该类的时候请注意以下要求:
A.该类中有类成员方法,也有实例成员方法;
B. 不能改变main()方法的内容。
C.main( )方法的输出为:
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
代码如下:
1.Book类:
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类:
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();
}
}