Object Oriented Practice Questions

  1. Require:
    (1) Program according to the variable name, type and method name given in the title, modification is prohibited;
    (2) The programming environment is optional, either Eclipse or JDK
    ① Library interface (Library)
    Member method:
    borrow(), to borrow books
    revert(), return the book
    ② Book s
    Member variables:
    Book name (name) String type
    Publisher (publisher) String type
    Construction method:
    Initialize book name (name) and publisher (publisher) through formal parameters
    Normal method:
    (1) Set the getter and setter methods to get and set the value of the name variable in the class;
    (2) Override the Equals method, if and only if the title (name) and publisher (publisher) are equal, it is the same book.
    (3) Rewrite the toString method to return the information of the book title (name) and publisher (publisher) in the following style:
    "Title: Java Programming, Publisher: Tsinghua University Press"
    ③ CollectionBook, which inherits from the Book class and implements the Library interface
    Member variables:
    Book number (bNo) String type
    The library where it is located (stacks) String type
    Whether to borrow (isBorrow) boolean type When the book status is borrowed, the value is true
    Construction method:
    Call the parent class constructor to initialize the book name (name) and publisher (publisher) information, and then initialize the book number (bNo) and the line book library (stacks)
    Normal method:
    (1) Implement the borrow method in the interface
    If the status of the book is borrowed, output "Sorry, the book has been borrowed", otherwise, modify the status of the book to be borrowed, and output "borrowed successfully"
    (2) Implement the revert method in the interface
    If the book status is available for borrowing, output "the book has been returned", otherwise, change the book borrowing status to not borrowed, and output "book returned successfully"
    ④ Pass the main function test
    (1) Create two CollectionBook objects book1 and book2, output book1 and book2 respectively, and call their equals method to determine whether the two objects are equal
    (2) Enter an integer through the keyboard, enter 0, then borrow book1, enter 1, return book1
import java.util.*;

interface Library{
    public abstract void borrow();
    public abstract void revert();
class Book{
    String name;
    String publisher;
    Book(String name,String publisher){
    public void getter(){
    public String setter(String str1){
        return null;

    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Book)) return false;
        Book book = (Book) o;
        return Objects.equals(name, book.name) &&
                Objects.equals(publisher, book.publisher);

    public String toString() {
        return ""Title:"+name+",publishing house"+publisher+""";
    class CollectionBook extends Book implements Library{
        String bNo;
        String stacks;
        boolean isBorrow;
        CollectionBook(String name,String publisher,String bNo,String stacks){
        public  void borrow(){
            if (isBorrow){
                System.out.println("Sorry, the book is already on loan");
            }else {
                System.out.println("Borrowed successfully");
        public  void revert(){
            if (isBorrow){
                System.out.println("return book successfully");
                System.out.println("The book has been returned");


   public class test3{
    public static void main(String[]args){
        CollectionBook book1=new CollectionBook("java programming","Tsinghua University Press","11111","Line Library 1");
        CollectionBook book2=new CollectionBook("java programming","Tsinghua University Press","11111","Line Library 1");
            System.out.println("Comparing results:"+book1.equals(book2) );
            System.out.println("Please enter 0 or 1");
            System.out.println("please enter:");
         Scanner sc =new Scanner(System.in );
         int a=sc.nextInt();
        }else {



The result is

``Comparing results: true
 Please enter 0 or 1
 please enter:
 The book has been returned`

Tags: Java

Posted by redmonkey on Thu, 05 May 2022 20:58:33 +0300