Java教程
Java标识符与关键字
Java变量
Java数据类型
Java运算符
Java控制语句
Java方法
Java面向对象
Java对象的创建和使用
Java封装
Java中static和this
Java继承
Java方法覆盖和多态
Java super
Java基础练习题

Java基础编程练习题

十、 Java封装练习题

● 定义“人”类,“人”类包括这些属性:姓名、性别、年龄等。使用封装的方式编写程序,要求对年龄进行安全控制,合法的年龄范围为[0~100],其他值表示不合法。

public class People {
	private String name;
	private boolean sex;
	private int age;
	public String getName() {
		return name;
	}
	public void setName(String _name) {
		name = _name;
	}
	public boolean isSex() {
		return sex;
	}
	public void setSex(boolean _sex) {
		sex = _sex;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int _age) {
		if(_age < 0 || _age > 100){
			throw new RuntimeException("年龄不合法!");
		}
		age = _age;
	}
}
public class PeopleTest {
	public static void main(String[] args) {
		People p = new People();
		p.setName("张三");
		p.setAge(20);
		p.setSex(true);
		
		System.out.println("姓名 = " + p.getName());
		System.out.println("性别 = " + (p.isSex() ? "男" : "女"));
		System.out.println("年龄 = " + p.getAge());
		
		p.setAge(-100);
	}
}
执行结果如下图所示:

Java开发

十一、 this和static关键字练习题

● 选择题(B)

public class Test {
	static int value = 9;
	public static void main(String[] args) throws Exception{
		new Test().printValue();
	}
	public void printValue(){
		int value = 69;
		System.out.println(this.value);
	}
}
A. 编译错误

B. 打印9

C. 打印69

D. 运行时抛出异常

● 判断下面代码的输出结果,并说明原因

public class User {
	private String name;
	public User(){
	}
	public void setName(String name){
		name = name;
	}
	public String getName(){
		return name;
	}
}
public class UserTest {
	public static void main(String[] args) {
		User user = new User();
		user.setName("zhangsan");
		System.out.println(user.getName());
	}
}
最终的输出结果是:null

原因:setName方法体当中的name = name是把局部变量name赋值给局部变量name,和实例变量name无关,所以getName()方法获取的实例变量值是null。

● 找出下面代码的错误,并说明为什么错了

public class Test {
	int i;
	static int j;
	public void m1(){
		System.out.println(i);
		System.out.println(j);
		m2();
		m3();
	}
	public void m2(){
	}
	public static void m3(){
		System.out.println(i);
		System.out.println(j);
		m2();
		m4();
	}
	public static void m4(){
	}
}
第13,15行编译报错,因为静态方法中无法直接访问实例变量i和实例方法m2()。

十二、Java继承练习题

●  定义猴子类,猴子有名字和性别等属性,并且定义猴子说话的方法,定义人类,人有名字和性别等属性,并且定义人说话的方法。使用继承,让代码具有复用性。

public class Monkey {
	private String name;
	private boolean sex;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public boolean isSex() {
		return sex;
	}
	public void setSex(boolean sex) {
		this.sex = sex;
	}
	public Monkey() {
		super();
	}
	public Monkey(String name, boolean sex) {
		super();
		this.name = name;
		this.sex = sex;
	}
	public void speak(){
		System.out.println(name + "咿咿呀呀!");
	}
}
public class People extends Monkey{
	public void speak(){
		System.out.println(this.getName() + ",呦,小伙会说话了!");
	}
}
● 定义动物类,动物有名字属性,并且定义动物移动的方法,定义鱼类,鱼有名字属性,有颜色属性,并且定义鱼移动的方法。使用继承,让代码具有复用性。
public class Animal {
	private String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Animal() {
		super();
	}

	public Animal(String name) {
		super();
		this.name = name;
	}
	
	public void move(){
		System.out.println(name + " is moving!");
	}
}
public class Fish extends Animal{
	public void move(){
		System.out.println(this.getName() + "正在水里欢快的游来游去!");
	}
}
十三、 Java方法覆盖和多态练习题

● 实现愤怒的小鸟

我们有很多种小鸟,每种小鸟都有飞的行为,还有一个弹弓,弹弓有一个弹射的行为,弹射时把小鸟弹出去,之后小鸟使用自己飞行的行为飞向小猪(不要求编写小猪的代码)。不同种类的小鸟有不同飞行的方式:

红火:红色小鸟,飞行方式:正常

蓝冰:蓝色小鸟,飞行方式:分成3个

黄风:黄色小鸟,飞行方式:加速。

//小鸟
public class Bird {
	//飞
	public void fly(){
		
	}
}
public class RedBird extends Bird{
	public void fly(){
		System.out.println("正常的飞翔");
	}
}
public class BlueBird extends Bird{
	public void fly(){
		System.out.println("变成了3只小鸟一起飞");
	}
}
public class YellowBird extends Bird{
	public void fly(){
		System.out.println("加速飞翔");
	}
}
//弹弓
public class Slingshot {
	//射
	public void shot(Bird bird){
		//鸟儿飞
		bird.fly();
	}
}
public class Test {
	public static void main(String[] args) {
		Bird redBird = new RedBird(); 
		Bird blueBird = new BlueBird(); 
		Bird yellowBird = new YellowBird(); 
		Slingshot ss = new Slingshot();
		ss.shot(redBird);
		ss.shot(blueBird);
		ss.shot(yellowBird);
	}
}
执行结果如下图所示:

Java应用

● 计算不同类型的员工薪资

定义员工类Employee,员工包含姓名name、出生月份birthMonth两个属性,员工有获取指定月份工资的方法(getSalary(int month)),如果该月员工生日,公司补助250元。

定义有固定工资的员工类SalariedEmployee,有月薪monthlySalary属性。

定义小时工类HourlyEmployee,包含工作小时数hours和每小时的工资hourlySalary属性,如果每月工作超过160小时,超过的部分按1.5倍工资发放。

定义销售人员类SalesEmployee,包含月销售额sales和提成比例comm属性。

public class Employee {
	private String name;
	private int birthMonth;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getBirthMonth() {
		return birthMonth;
	}
	public void setBirthMonth(int birthMonth) {
		this.birthMonth = birthMonth;
	}
	public double getSalary(int month){
		return 0.0;
	}
}
public class SalariedEmployee extends Employee {
	private double monthlySalary;
	public double getMonthlySalary() {
		return monthlySalary;
	}
	public void setMonthlySalary(double monthlySalary) {
		this.monthlySalary = monthlySalary;
	}
	public double getSalary(int month){
		if(month == this.getBirthMonth()){
			return monthlySalary + 250;
		}
		return monthlySalary;
	}
}
public class HourlyEmployee extends Employee {
	private int hours;
	private double hourlySalary;
	public int getHours() {
		return hours;
	}
	public void setHours(int hours) {
		this.hours = hours;
	}
	public double getHourlySalary() {
		return hourlySalary;
	}
	public void setHourlySalary(double hourlySalary) {
		this.hourlySalary = hourlySalary;
	}
	public double getSalary(int month){
		double money = 0.0;
		if(hours <= 160){
			money = hourlySalary * hours;
		}else{
			money = hourlySalary * 160 + hourlySalary * (hours - 160) * 1.5;
		}
		if(month == this.getBirthMonth()){
			money += 250;
		}
		return money;
	}
}
public class SalesEmployee extends Employee {
	private double sales;
	private double comm;
	public double getSales() {
		return sales;
	}
	public void setSales(double sales) {
		this.sales = sales;
	}
	public double getComm() {
		return comm;
	}
	public void setComm(double comm) {
		this.comm = comm;
	}
	public double getSalary(int month){
		if(month == this.getBirthMonth()){
			return sales * comm + 250;
		}
		return sales * comm;
	}
}
public class Test {
	public static void main(String[] args) {
		SalariedEmployee e1 = new SalariedEmployee();
		e1.setName("张三");
		e1.setBirthMonth(2);
		e1.setMonthlySalary(5000.0);
		System.out.println(e1.getName() + "的2月份工资是" + e1.getSalary(2));
		HourlyEmployee e2 = new HourlyEmployee();
		e2.setName("李四");
		e2.setBirthMonth(2);
		e2.setHourlySalary(50);
		e2.setHours(170);
		System.out.println(e2.getName() + "的2月份工资是" + e2.getSalary(2));
		SalesEmployee e3 = new SalesEmployee();
		e3.setName("王五");
		e3.setBirthMonth(2);
		e3.setSales(10000.0);
		e3.setComm(0.5);
		System.out.println(e3.getName() + "的2月份工资是" + e3.getSalary(2));
	}
}
执行结果如下图所示:

Java应用

●  某汽车租赁公司有多种汽车可以出租,计算汽车租金

Vehicle是所有车的父类,属性:品牌、车牌号,有返回总租金的方法:public double getSumRent(int days){}

小轿车类Car是Vehicle的子类,属性:车型(两厢、三厢、越野),两厢每天300,三厢每天350,越野每天500。

多座汽车类Bus是Vehicle的子类,属性:座位数,座位数<=16的每天400,座位数>16的每天600。

编写测试类,根据用户选择不同的汽车,计算总租金。

public class Vehicle {
	private String brand;
	private String licensePlateNumber;
	public String getBrand() {
		return brand;
	}
	public void setBrand(String brand) {
		this.brand = brand;
	}
	public String getLicensePlateNumber() {
		return licensePlateNumber;
	}
	public void setLicensePlateNumber(String licensePlateNumber) {
		this.licensePlateNumber = licensePlateNumber;
	}
	public Vehicle() {
		super();
	}
	public double getSumRent(int days){
		return 0.0;
	}
}
public class Car extends Vehicle {
	
	//车型:两厢(300/天)、三厢(350/天)、越野(500/天)
	private String models;

	public String getModels() {
		return models;
	}

	public void setModels(String models) {
		this.models = models;
	}

	public Car() {
		super();
	}
	
	public double getSumRent(int days){
		if("两厢".equals(models)){
			return 300 * days;
		}else if("三厢".equals(models)){
			return 350 * days;
		}else{
			return 500 * days;
		}
	}
}
public class Bus extends Vehicle {
	//座位数
	private int seats;

	public int getSeats() {
		return seats;
	}

	public void setSeats(int seats) {
		this.seats = seats;
	}

	public Bus() {
		super();
	}
	
	public double getSumRent(int days){
		if(seats <= 16){
			return 400 * days;
		}else{
			return 600 * days;
		}
	}
}
public class Test {
	public static void main(String[] args) {
		Car c = new Car();
		c.setBrand("宝马");
		c.setLicensePlateNumber("京A 88888");
		c.setModels("三厢");
		System.out.println("品牌:" + c.getBrand() + ",车牌号:" + c.getLicensePlateNumber() + ",10天总租金:" + c.getSumRent(10));
		
		Bus b = new Bus();
		b.setBrand("海格");
		b.setLicensePlateNumber("京B 88888");
		b.setSeats(60);
		System.out.println("品牌:" + b.getBrand() + ",车牌号:" + b.getLicensePlateNumber() + ",10天总租金:" + b.getSumRent(10));
	}
}
执行结果如下图所示:

十四、 super关键字练习题

● 请判断以下代码的输出结果

public class Text {
	public static int k = 0;
	public static Text t1 = new Text("t1");
	public static Text t2 = new Text("t2");
	public static int i = print("i");
	public static int n = 99;
	public int j = print("j");
	static {
		print("静态块");
	}
	public Text(String str) {
		System.out.println((++k) + ":" + str + "   i=" + i + "    n=" + n);
		++i;
		++n;
	}
	public static int print(String str) {
		System.out.println((++k) + ":" + str + "   i=" + i + "    n=" + n);
		++n;
		return ++i;
	}
	public static void main(String args[]) {
		new Text("init");
	}
}
全部教程