题目1:
请通过代码封装,实现如下需求:
编写一个类Book,代表教材.
1.具有属性:名称〈title)、页数(pageNum)
2.其中页数不能少于200页,否则输出错误信息,并赋予默认值200
3.为各属性提供赋值和取值方法
4.具有方法:detail,用来在控制台输出每本教材的名称和页数
5.编写测试类BookTest进行测试:为Book对象的属性赋予初始值,并调用Book对象的detail
public class BookTest {
public static void main(String[] args) {
Book book01 = new Book("高三数学人教版",300);
//book.detail();
book01.setPageNumber(100);
book01.detail();
//教材为空,方法通过,可以通过修改无参方法
Book book02 = new Book();
book02.detail();
}
}
class Book {
//静态变量(属性)名称、页数
private String title;
private int pageNumber;
//detail方法(输出教材名称和教材页数)
/*
public void detail() {
System.out.println("教材名称是:" + getTitle());
System.out.println("教材页数为:" + getPageNumber());
}
*/
public void detail() {
System.out.println("书籍名称:" + title + ",总页数为" + pageNumber);
}
//构造方法
public Book(){
title = "未知";
pageNumber = 200;
}
public Book(String t,int p){
title = t;
if(p < 200){
pageNumber = 200;
System.out.println("页数小于200页,默认赋值200!");
}
else
pageNumber = p;
}
//静态方法(类加载时创建,空间开辟在方法区)
public void setTitle(String t){
title = t;
}
public String getTitle(){
return title;
}
//静态方法
public void setPageNumber(int p){
if (p < 200){
pageNumber = 200;
System.out.println("页数小于200页,默认赋值200!");
}
else
pageNumber = p;
}
public int getPageNumber(){
return pageNumber;
}
}
题目2:
一个名为的类模拟账户。
该类属性和方法如下所示。
该类包括的属性:账户id,余额balance,年利率annualInterestRate;
包含的方法:各属性的set和get方法。取款方法withdraw(),存款方法deposit()。
写一个测试程序
(1)创建一个Customer,
名字叫Jane Smith,他有一个账号为1000,余额为2000,年利率为1.
(2)对Jane Smith操作。
存入100元,再取出960元,再取出2000。
打印Jane smith的基本信息
信息如下显示:
成功存入:100
成功取出:960
余额不足,取钱失败
Customer [Smith,Jane] has a account : id is 1000 annualInterestRate is 1.23% balance
public class AccountTest {
public static void main(String[] args) {
//当没有创建Customer类
//Account JaneSmith = new Account(1000,2000,0.01);
//JaneSmith.deposit(100);
//JaneSmith.withdraw(960);
//JaneSmith.withdraw(2000);
//调用Customer类时
Account a1 = new Account("1000",2000,0.0123);
Customer c1 = new Customer("Jane Smith", a1);
c1.getAct().deposit(100);
c1.getAct().withdraw(960);
c1.getAct().withdraw(2000);
}
}
//需要定义一个Customer类
class Customer {
//名字
private String name;
//账户
private Account act;
//构造方法
//无参构造
public Customer() {
}
//有参构造
public Customer(String name, Account act) {
this.name = name;
this.act = act;
}
//getting and setting
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Account getAct() {
return act;
}
public void setAct(Account act) {
this.act = act;
}
}
class Account {
//账户id
private String id;
//余额balance
private double balance;
//年利率annulInterestRate
private double annualInterestRate;
//无参构造
public Account() {
}
//有参构造
public Account(String id, double balance, double annualInterestRate) {
this.id = id;
this.balance = balance;
this.annualInterestRate = annualInterestRate;
}
//getting和setting方法
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
//取款方法withdraw(返回账户余额)
public void withdraw(double money) {
//balance在此处可以换用this.getBalance()
if (balance < money) {
System.out.println("余额不足,取钱失败");
}
else{
System.out.println("成功取出:" + money);
}
//this可以省略,也可以调用setBalance()
this.balance = this.balance - money;
//调用方法修改余额
//this.setBalance(this.getBalance() - money);
return;
}
//存款方法deposit(返回账户余额)
public void deposit(double money) {
System.out.println("成功存入:" + money);
//this可以省略
this.balance = this.balance + money;
//也可以通过调用setBalance()修改余额
//this.setBalance(this.getBalance() + money);
}
}
题目3:
(封装)已知一个类Student代码如下:
class Student;
String name;
int age;
String address;
String zipCode;
String mobile;
要求:
1、把Student的属性都作为私有,并提供get/set方法以及适当的构造方法。
2、为Student类添加一个getPostAddress方法,要求返回Student对象的地址和邮编。
public class StudentTest01 {
public static void main(String[] args) {
//调用无参构造方法
Student01 s1 = new Student01();
System.out.println(s1.getName() + "," + s1.getPostAddress());
//为参数赋值
s1.setName("张三");
s1.setAge(22);
s1.setAddress("北京");
s1.setZipcode("123456");
s1.setMobile("13152648790");
System.out.println(s1.getName() + "," + s1.getPostAddress());
//调用有参构造方法
Student01 s2 = new Student01("李四",23,"上海","654321","15464845462");
System.out.println(s2.getName() + "," + s2.getPostAddress());
}
}
class Student01 {
//姓名(私有)
private String name;
//年龄
private int age;
//地址
private String address;
//邮编
private String zipcode;
//移动电话
private String mobile;
//无参构造
public Student01() {
}
//有参构造
public Student01(String name, int age, String address, String zipcode, String mobile) {
this.name = name;
this.age = age;
this.address = address;
this.zipcode = zipcode;
this.mobile = mobile;
}
//setting and getting
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getZipcode() {
return zipcode;
}
public void setZipcode(String zipcode) {
this.zipcode = zipcode;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getPostAddress() {
return "地址:" + this.getAddress() + ",邮编:" + this.getZipcode();
}
}