老师帮忙看下是否满足题目要求?

来源:1-8 使用包进行类管理--创建包

weixin_慕先生706308

2019-07-30 09:12:55

package test;


public class Book {

// 属性:书名、作者、出版社、价格

private String bookNm;

private String author;

private String press;

private double price;


/**

要求:

1、 设计构造函数实现对属性赋值

2、 设置私有属性,get/set方法实现对属性的访问

3、 限定图书价格必须大于10,如果无效需进行提示,并强制赋值为10

4、 限定作者、书名均为只读属性

5、信息介绍方法描述图书所有信息

 * */


//构造方法:

public Book(String bookNm,String author,String press,double price ) {

this.bookNm=bookNm;

this.author=author;

this.setPress(press);

this.setPrice(price);

}

public Book( ) {

}

//书名方法,只读

public String getBookNm() {

return bookNm;

}

//作者方法,只读

public String getAuthor() {

return author;

}



//出版社方法

public String getPress() {

return press;

}


public void setPress(String press) {

this.press = press;

}

//价格方法

public double getPrice() {

return price;

}


public void setPrice(double price) {

this.price = price;

}

//下半段信息介绍方法,描述图书所有信息

public  void BookInt(String bookNm,String author,String press,double price ) {

System.out.println("书名:"+bookNm);

System.out.println("作者:"+author);

System.out.println("出版社:"+press);

System.out.println("价格:"+price+"元");

}


}





package test;


public class BookTest {

public static void main(String[]args) {

//实例化对象

Book one=new Book("红楼梦","曹雪芹","人民文学出版社",20.0);

System.out.println("图书价格最低10元");

System.out.println("书名:"+one.getBookNm());

System.out.println("作者:"+one.getAuthor());

System.out.println("出版社:"+one.getPress());

//限定图书价格必须大于10,如果无效需进行提示,并强制赋值为10

if(one.getPrice()<10.0)

System.out.println("图书价格无效,价格应为10元");//如果无效需进行提示

else 

    System.out.println("价格:"+one.getPrice()+"元");

System.out.println("========================");

//图书2介绍,实例化对象

Book two=new Book();

two.BookInt("小李飞刀","古龙","中国长安出版社",55.5);



}


}


写回答

1回答

好帮手慕酷酷

2019-07-30

同学你好,代码完成的不错,但是还有一个小问题:

  • 在限定图书价格时,可以将判断语句写入到Book类的setPrice()方法中,建议将if···else···语句中的语句块添加{},否则语句块中含有多条语句时,将只执行第一句。

如果我的回答解决了你的疑惑,请采纳!祝学习愉快!


0
heixin_慕先生706308
h 已改,谢谢老师
h019-07-30
共2条回复

0 学习 · 11489 问题

查看课程