图书类
分数 20
全屏浏览
切换布局
作者 温彦
单位 山东科技大学
构建一个书类Book,包括名称(字符串),价格(整型),作者(字符串,多个作者当做一个字符串处理),版本号(整型),提供带参数的构造函数Book(String name, int price, String author, int edition),提供该类的toString()和equals()方法,toString方法返回所有成员属性的值的字符串形式,形如“name: xxx, price: xxx, author: xxx, edition: xxx”,当两个Book对象的名称(不关心大小写,无空格)、作者(不关心大小写,无空格)、版本号相同时,认为两者表示同一本书。
Main函数中,读入两本书,输出他们是否相等,打印两本书的信息。
输入描述:
两本书信息
输出描述:
两本书的打印信息
两本书是否相等
裁判测试程序样例:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
Book b1 = new Book(s.next(),
s.nextInt(),
s.next(),
s.nextInt());
Book b2 = new Book(s.next(),s.nextInt(),s.next(),s.nextInt());
System.out.println(b1);
System.out.println(b2);
System.out.println(b1.equals(b2));
}
}
/* 你的代码被嵌在这里 */
输入样例:
在这里给出一组输入。例如:
ThinkingInJava
86
BruceEckel
4
CoreJava
95
CayS.Horstmann
10
输出样例:
在这里给出相应的输出。例如:
name: ThinkingInJava, price: 86, author: BruceEckel, edition: 4
name: CoreJava, price: 95, author: CayS.Horstmann, edition: 10
false
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
class Book {
String name;
int price;
String author;
int edition;
public Book(String name, int price, String author, int edition) {
this.name = name;
this.price = price;
this.author = author;
this.edition = edition;
}
@Override
public String toString() {
return "name: " + this.name + ", price: " + this.price + ", author: " + this.author + ", edition: "
+ this.edition;
}
@Override
public boolean equals(Object obj) {
Book b = (Book) obj;
return this.name.equalsIgnoreCase(b.name) && this.author.equalsIgnoreCase(b.author)
&& this.edition == b.edition;
}
}