欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java基础练习题

程序员文章站 2024-03-05 12:50:30
...

Java 类的继承练习题

适用于java初学者

1、

写一个名为Account的类模拟账户。该类的属性和方法如下图所示。该类包括的属性:账号id,余额balance,年利率annualInterestRate;
包含的方法:访问器方法(getter和setter方法),
返回月利率的方法getMonthlyInterest(),取款方法withdraw(),存款方法deposit()。

Account

private int id
private double balance
private double annualInterestRate

public Account (int id, double balance, double annualInterestRate )

public int getId()
public double getBalance()
public double getAnnualInterestRate()
public void setId( int id)
public void setBalance(double balance)
public void setAnnualInterestRate(double annualInterestRate)
public double getMonthlyInterest()
public void withdraw (double amount)
public void deposit (double amount)
写一个用户程序测试Account类。在用户程序中,创建一个账号为1122、余额为20000、年利率4.5%的Account对象。使用withdraw方法提款30000元,并打印余额。
再使用withdraw方法提款2500元,使用deposit方法存款3000元,然后打印余额和月利率。

提示:在提款方法withdraw中,需要判断用户余额是否能够满足提款数额的要求,如果不能,应给出提示。
运行结果如图所示:

java基础练习题
解答代码:

//首先定义一个Account类模拟账户

public class Account {//定义类的属性
private int id;// 账号
protected double balance;// 余额
private double annualInterstRate;// 年利率

public Account(int id, double balance, double annualInterstRate) {
	super();
	this.id = id;
	this.balance = balance;
	this.annualInterstRate = annualInterstRate;
}

// 返回月利率的方法
public double getMonthlyInterest() {
	return annualInterstRate / 12;

}

// 取款方法
public void withdraw(double amount) {
	if (amount > balance) {
		System.out.println("余额不足");
		System.out.println("余额为:" + balance);
	} else {
		balance = balance - amount;
		System.out.println("余额为:" + balance);
	}
}

// 存款方法
public void deposit(double amount) {
	balance = balance + amount;
	System.out.println("余额为:" + balance);

}

public int getId() {// getter和setter方法
	return id;
}

public void setId(int id) {
	this.id = id;
}

public double getBalance() {
	return balance;
}

public void setBalance(double balance) {
	this.balance = balance;
}

public double getAnnualInterstRate() {
	return annualInterstRate;
}

public void setAnnualInterstRate(double annualInterstRate) {
	this.annualInterstRate = annualInterstRate;
}

}

定义一个CheckAccount类继承Account类

public class CheckAccount extends Account {

private double overdraft;	// 可透支限额

public CheckAccount(int id, double balance, double annualInterstRate,
		double overdraft) {
	super(id, balance, annualInterstRate);
	this.overdraft = overdraft;
}

public double getOverdraft() {
	return overdraft;
}

public void setOverdraft(double overdraft) {
	this.overdraft = overdraft;
}

// balance:余额
// amount:要取的钱数
// overdraft:可透支限额
public void withdraw(double amount) {
	if (amount < getBalance()) {
		balance = balance - amount;
		System.out.println("余额:" + super.balance);
		System.out.println("可透支余额为:" + overdraft);
	} else {
		if (balance > 0) {
			if (amount - balance <= 5000) {
				overdraft = overdraft - (amount - balance);
				balance = 0;
				System.out.println("余额为:" + balance);
				System.out.println("可透支余额为:" + overdraft);
			}
		} else {
			if (amount - balance > 5000) {
				System.out.println("余额:" + super.balance);
				System.out.println("超过可透支额的限额");
			} else if (overdraft - amount >= 0) {
				overdraft = overdraft - amount;
				System.out.println("余额:" + super.balance);
				System.out.println("可透支余额为:" + overdraft);
			} else if (overdraft - amount < 0) {
				System.out.println("超过可透支额的限额");
				System.out.println("余额:" + super.balance);
				System.out.println("可透支余额为:" + overdraft);
			}
		}
	}
}

}

定义一个用户程序测试TestAccount类

public class TestAccount {

public static void main(String[] args) {

Account a = new Account(1122, 20000.0, 0.045);

a.withdraw(30000);
a.withdraw(2500);
a.deposit(3000);

System.out.println(“月利率:”+a.getMonthlyInterest());
}
}

运行结果:

java基础练习题

2、

创建Account类的一个子类CheckAccount代表可透支的账户,该账户中定义一个属性overdraft代表可透支限额。在CheckAccount类中重写withdraw方法,其算法如下:
如果(取款金额<账户余额),
可直接取款
如果(取款金额>账户余额),
计算需要透支的额度
判断可透支额overdraft是否足够支付本次透支需要,如果可以
将账户余额修改为0,冲减可透支金额
如果不可以
提示用户超过可透支额的限额

要求:写一个用户程序测试CheckAccount类。在用户程序中,创建一个账号为1122、余额为20000、年利率4.5%,可透支限额为5000元的CheckAccount对象。
使用withdraw方法提款5000元,并打印账户余额和可透支额。
再使用withdraw方法提款18000元,并打印账户余额和可透支额。
再使用withdraw方法提款3000元,并打印账户余额和可透支额。

提示:
(1)子类CheckAccount的构造方法需要将从父类继承的3个属性和子类自己的属性全部初始化。
(2)父类Account的属性balance被设置为private,但在子类CheckAccount的withdraw方法中需要修改它的值,因此应修改父类的balance属性,定义其为protected。
运行结果如下图所示:

java基础练习题
解答代码:

//首先定义一个Account类模拟账户

public class Account {//定义类的属性
private int id;// 账号
protected double balance;// 余额
private double annualInterstRate;// 年利率

public Account(int id, double balance, double annualInterstRate) {
	super();
	this.id = id;
	this.balance = balance;
	this.annualInterstRate = annualInterstRate;
}

// 返回月利率的方法
public double getMonthlyInterest() {
	return annualInterstRate / 12;

}

// 取款方法
public void withdraw(double amount) {
	if (amount > balance) {
		System.out.println("余额不足");
		System.out.println("余额为:" + balance);
	} else {
		balance = balance - amount;
		System.out.println("余额为:" + balance);
	}
}

// 存款方法
public void deposit(double amount) {
	balance = balance + amount;
	System.out.println("余额为:" + balance);

}

public int getId() {// getter和setter方法
	return id;
}

public void setId(int id) {
	this.id = id;
}

public double getBalance() {
	return balance;
}

public void setBalance(double balance) {
	this.balance = balance;
}

public double getAnnualInterstRate() {
	return annualInterstRate;
}

public void setAnnualInterstRate(double annualInterstRate) {
	this.annualInterstRate = annualInterstRate;
}

}

定义一个CheckAccount类继承Account类

public class CheckAccount extends Account {

private double overdraft;	// 可透支限额

public CheckAccount(int id, double balance, double annualInterstRate,
		double overdraft) {
	super(id, balance, annualInterstRate);
	this.overdraft = overdraft;
}

public double getOverdraft() {
	return overdraft;
}

public void setOverdraft(double overdraft) {
	this.overdraft = overdraft;
}

// balance:余额
// amount:要取的钱数
// overdraft:可透支限额
public void withdraw(double amount) {
	if (amount < getBalance()) {
		balance = balance - amount;
		System.out.println("余额:" + super.balance);
		System.out.println("可透支余额为:" + overdraft);
	} else {
		if (balance > 0) {
			if (amount - balance <= 5000) {
				overdraft = overdraft - (amount - balance);
				balance = 0;
				System.out.println("余额为:" + balance);
				System.out.println("可透支余额为:" + overdraft);
			}
		} else {
			if (amount - balance > 5000) {
				System.out.println("余额:" + super.balance);
				System.out.println("超过可透支额的限额");
			} else if (overdraft - amount >= 0) {
				overdraft = overdraft - amount;
				System.out.println("余额:" + super.balance);
				System.out.println("可透支余额为:" + overdraft);
			} else if (overdraft - amount < 0) {
				System.out.println("超过可透支额的限额");
				System.out.println("余额:" + super.balance);
				System.out.println("可透支余额为:" + overdraft);
			}
		}
	}
}

}

定义一个用户程序测试TestAccount类

public static void main(String[] args) {

	CheckAccount w = new CheckAccount(1122, 20000, 0.045,5000);
	w.withdraw(5000);

	w.withdraw(18000);
	
	w.withdraw(3000);

}

}

运行结果:

java基础练习题

3.

练习(基础):
(1)编写一个圆类Circle,该类拥有:
①一个成员变量
Radius(私有,浮点型); // 存放圆的半径;
②两个构造方法
Circle( ) // 将半径设为0
Circle(double r ) //创建Circle对象时将半径初始化为r
③ 三个成员方法
double getArea( ) //获取圆的面积
double getPerimeter( ) //获取圆的周长
void show( ) //将圆的半径、周长、面积输出到屏幕
(2)编写一个圆柱体类Cylinder,它继承于上面的Circle类。还拥有:
①一个成员变量
double hight(私有,浮点型); // 圆柱体的高;
②构造方法
Cylinder (double r, double h ) //创建Circle对象时将半径初始化为r
③ 成员方法
double getVolume( ) //获取圆柱体的体积
void showVolume( ) //将圆柱体的体积输出到屏幕
编写应用程序,创建类的对象,分别设置圆的半径、圆柱体的高,计算并显示圆半径、圆面积、圆周长,圆柱体的体积。
//如何进行封装
/*1.属性私有化化:private
2.提供get和set方法
*/

//一个圆类Circle

public class Circle {// 一个成员变量 Radius(私有,浮点型)
protected double Radius;// 圆的半径

public double getRadius() {
	return Radius;
}

public void setRadius(double radius) {
	Radius = radius;
}

// 两个构造方法
public Circle() {
	Radius = 1;
}

public Circle(double radius) {
	super();
	Radius = radius;
}

// 三个成员方法
public double getArea() {// 获取圆的面积
	double s = Radius * Radius * 3.14;
	return s;

}

public double getPerimeter() {// 获取圆的周长
	double c = 2 * 3.14 * Radius;
	return c;
}

public void show() { // 将圆的半径、周长、面积输出到屏幕
	System.out.println("圆的半径:" + getRadius());
	System.out.println("圆的周长:" + getPerimeter());
	System.out.println("圆的面积:" + getArea());
}

}

// Cylinder类

public class Cylinder extends Circle {// 继承Circle类
// 成员变量
private double hight;// 圆柱体的高

// 构造方法

public Cylinder(double radius, double hight) {
	super();

	this.hight = hight;
}

public Cylinder() {

}

// 成员方法
public double getVolume() {// 获取圆柱体的体积
	return super.getArea() * hight;

}

public void showVolume() { // 将圆柱体的体积输出到屏幕
	System.out.println("圆柱的高:" + getHight());
	System.out.println("圆柱体的体积:" + getVolume());
}

// getter,setter
public double getHight() {
	return hight;
}

public void setHight(double hight) {
	this.hight = hight;
}

}

//测试类

public class TestCircle {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	Cylinder c = new Cylinder();
	//输入半径2,圆柱的高为2
	c.setRadius(1);
	c.setHight(2);
	//调用计算面积,体积的方法
	c.show();
	c.showVolume();
}

}

运行结果:

java基础练习题

4.

定义一个人的类(属性有名字,年龄,性别。写一个能输出各个属性值的方法showInfo()),定义一个学生类(属性有学号),学生继承人类,要求:
(1)父类的属性赋值用构造方法来实现(分别用有参数构造方法和无参数构造方法实现);
(2)子类的属性也用构造方法来赋值;
(3)在子类中重写父类的showInfo()方法
(4)声明学生类的对象,调用学生的显示信息的方法。

//Person类

public class Student extends Person{
private int sNo;

public int getsNo() {
return sNo;
}

public void setsNo(int sNo) {
this.sNo = sNo;
}
public Student(){
sNo = 1002;
}
public Student(double sNo){
super();
sNo = sNo;
}
public void showInfo(){
System.out.println(“姓名:”+getName()+",年龄:"+getAge()+",性别:"+getSex()
+",学号:"+getsNo());
}
}

//Student类

public class Student extends Person{
private int sNo;

public int getsNo() {
return sNo;
}

public void setsNo(int sNo) {
this.sNo = sNo;
}
public Student(){
sNo = 1002;
}
public Student(double sNo){
super();
sNo = sNo;
}
public void showInfo(){
System.out.println(“姓名:”+getName()+",年龄:"+getAge()+",性别:"+getSex()
+",学号:"+getsNo());
}
}

//测试类

public class TestPerson {

public static void main(String[] args) {
	Student s = new Student();
	s.setName("老宋");
	s.setAge(20);
	s.setSex("男");
	s.setsNo(1002);
	s.showInfo();

}
}
运行截图:
java基础练习题