Java基础编程练习

本文指导如何在Java中实现一个Book类,包括书名获取、可用性判断、借书和还书功能,通过提供测试样例展示了类的使用方法。
摘要由CSDN通过智能技术生成

6-44 Implement Book分数 10

创建一个Book类。

拷贝如下的代码框架。该类定义了一些方法:获取书名、判断是否可用、借书和还书。然而,我们所提供的框架缺少这些方法的实现。请在方法体中填上合适的代码。

主方法用于测试这些方法,运行此程序,应该有如下的输出:

Title (should be The Da Vinci Code): The Da Vinci Code
Borrowed? (should be false): false
Borrowed? (should be true): true
Borrowed? (should be false): false

提示:查看主方法看每个方法如何使用,然后在每个方法中填入代码。

程序框架如下:

class Book {

     String title;
     boolean borrowed;
   
     // Creates a new Book
     public Book(String bookTitle) {
           // Implement this method
               
     }
     // Marks the book as rented
     public void rented() {
           // Implement this method
     }
    // Marks the book as not rented
    public void returned() {
        // Implement this method
          
    }
    // Returns true if the book is rented, false otherwise
    public boolean isBorrowed() {
         // Implement this method
           
    }
   // Returns the title of the book
   public String getTitle() {
       // Implement this method
       
   }
}

仔细阅读测试程序中的main()方法,根据上述的样例框架补充完整缺失的方法实现部分。

裁判测试程序样例:

public class Main {
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());
    }
}
/* 请在这里填写答案 */

输入样例:

在这里给出一组输入。例如:


输出样例:

在这里给出相应的输出。例如:

Title (should be The Da Vinci Code): The Da Vinci Code
Borrowed? (should be false): false
Borrowed? (should be true): true
Borrowed? (should be false): false

以下是答案



public class Pta_6_44 {
	public static void main(String args[]) {
		Book44 example = new Book44("The da Vinci Code");
		System.out.println("Title (should be The DaVinci Code):"+example.getTitle());
		System.out.println("Borrowed? (should be false): "+example.isBorrowed());
		example.rented();
		System.out.println("Borrowed? (should be ture): "+example.isBorrowed());
		example.returned();
		System.out.println("Borrowed? (should be false) :"+example.isBorrowed());
	}
}
//以下是答案,把Book44换成Book
class Book44{
	String title;
	boolean borrowed=false;
	public Book44(String bookTitle) {
		this.title=bookTitle;
	}
	public void rented() {
		borrowed = true;
	}
	public void returned() {
		borrowed = false;
	}
	public boolean isBorrowed() {
		return borrowed;
	}
	public String getTitle() {
		return this.title;
	}
}
  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值