求一道java作业题

题目是12题,setbook这方法想不到要怎么实现,printbook本来是想把三个信息存在数组里然后返回的,但是这样的话销量就要改成string型的了,而且好像不能直接... 题目是12题,setbook这方法想不到要怎么实现,printbook本来是想把三个信息存在数组里然后返回的,但是这样的话销量就要改成string型的了,而且好像不能直接输出返回的数组,求大佬解答 展开
 我来答
zhoudha
生活家

2020-09-24 · 知世故而不世故地生活
知道大有可为答主
回答量:2.4万
采纳率:91%
帮助的人:166万
展开全部

下面是我写的代码,分两个类写的。

①Book.java


public class Book {

private String bookname;

private String author;

private int sales;

public Book() {

this.bookname = "";

this.author = "";

this.sales = 0;

}

public Book(String bookname, String author, int sales) {

this.bookname = bookname;

this.author = author;

this.sales = sales;

}

public void setBook(String bookname, String author, int sales) {

this.bookname = bookname;

this.author = author;

this.sales = sales;

}

public void printBook() {

System.out.print(this.bookname);

System.out.print("\t");

System.out.print("\t");

System.out.print(this.author);

System.out.print("\t");

System.out.print("\t");

System.out.println(this.sales);

}

}

②TestBook.java

public class TestBook {

public static void main(String[] args) {

Book b1 = new Book();

Book b2 = new Book();

Book b3 = new Book();

Book b4 = new Book();

Book b5 = new Book("万古神帝", "飞天鱼", 342);

Book b6 = new Book("阴阳师", "小墨鱼", 470);

Book b7 = new Book("斗罗大陆", "唐家三少", 207);

Book b8 = new Book("完美世界", "辰东", 214);

Book b9 = new Book("亮剑", "都梁", 78);

b1.setBook("天龙八部", "金庸", 150);

b2.setBook("盗墓笔记", "南派三叔", 420);

b3.setBook("元尊", "天蚕土豆", 368);

b4.setBook("都灵", "红色警戒", 42);

System.out.print("书名");

System.out.print("\t");

System.out.print("\t");

System.out.print("作者");

System.out.print("\t");

System.out.print("\t");

System.out.println("月销售量");

b1.printBook();

b2.printBook();

b3.printBook();

b4.printBook();

b5.printBook();

b6.printBook();

b7.printBook();

b8.printBook();

b9.printBook();

}

}

下面是我做的测试结果的截图,麻烦帮忙看一下,是否能够符合要求。

神官0麦依0
科技发烧友

2020-09-24 · 有一些普通的科技小锦囊
知道小有建树答主
回答量:1145
采纳率:75%
帮助的人:373万
展开全部

public class Book
{
private String name;
private String author;
private int sales;
public Book()
{
}
public Book(String name,String author,int sales)
{
this.name = name;
this.author = author;
this.sales = sales;
}

public void setBook(String name,String author,int sales)
{
this.name = name;
this.author = author;
this.sales = sales;
}
public void printBook()
{
System.out.println("书名:" + name  +
", 作者:" + author  +
", 月销量:" + sales);
}
}

===================================================

public class TestBook
{
public static void main(String[] args) {
Book book = new Book();
book.setBook("《西游记》","吴承恩",1115);
book.printBook();
}
}

已赞过 已踩过<
你对这个回答的评价是?
评论 收起
pieryon
2020-09-24 · 知道合伙人数码行家
pieryon
知道合伙人数码行家
采纳数:14410 获赞数:166868
获取软件设计师高级职称 万达金融最佳创新奖

向TA提问 私信TA
展开全部
12题的完整代码,仅供参考:

public class Book {
private String bookName;
private String author;
private int salesCount;

public Book() {}

public Book(String bookName, String author, int salesCount) {
this.bookName = bookName;
this.author = author;
this.salesCount = salesCount;
}

public void setBook(String bname, String auth, int count) {
this.bookName=bname;
this.author=auth;
this.salesCount=count;
}

public String printBook() {
return "书名:"+this.bookName+"\t 作者:"+this.author+"\t 销量:"+this.salesCount;
}

}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
IT是我的强迫症
2020-09-23
知道答主
回答量:12
采纳率:100%
帮助的人:2.2万
展开全部
public void setBook(String name,String author,int monthlySales){
this.name=name;……
}
printBook()应该只是为了显示用System.out.println()就好了,不是为了返回值。
追问
大佬能不能给个完整的代码参考参考
追答
Book类:
public class Book {
private String name=null;
private String author=null;
private int monthlySales=0;

public Book(){}

public Book(String name,String author,int monthlySales){
this.name=name;
this.author=author;
this.monthlySales=monthlySales;
}

public void set(String name,String author,int monthlySales){
this.name=name;
this.author=author;
this.monthlySales=monthlySales;
}

public void printBook(){
System.out.println("《"+name+"》作者:"+author+",月销量:"+monthlySales);
}
}

TsetBook类:
public class TestBook {
public static void main(String[] args) {
Book book1=new Book();
book1.set("红楼梦","曹雪芹",37000);
Book book2=new Book("三国演义","罗贯中",29000);

book1.printBook();
book2.printBook();
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式