Java基础习题(五)

十一、多态、内部类和包

1.定义People类,有属性name及方法show ( ),该方法仅输出星号并换行。
People类的子类American,重写show ( )方法,按美国人姓名书写方式输出姓名;且子类American还有show(String msg )方法输出msg信息打招呼。
People类的子类Chinese,重写show ( )方法,按中国人姓名书写方式输出姓名;且子类Chinese还有show(String msg )方法输出msg信息打招呼。

//父类People
public class People {
	String name;
	void show() {
		System.out.println("*");
	}
}
//子类American 继承 父类People
public class American extends People{
	void show() {
		System.out.println("I'm "+name);
	}
	void show(String msg) {
		System.out.println(msg);
	}
}
//子类Chinese 继承 父类People
public class Chinese extends People{
	void show() {
		System.out.println("我叫"+name);
	}
	void show(String msg) {
		System.out.println(msg);
	}
}
//测试类
public class PeopleTest {
	public static void main(String[] args) {
		People p1 = new American();		//实现多态
		p1.name = "suyiYang";
		American a = (American) p1;		//使父类可以访问子类的强制转换
		a.show();
		a.show("Hello!");
		
		People p2 = new Chinese();
		p2.name = "李小静";
		Chinese c = (Chinese) p2;
		c.show();
		c.show("你好呀!");
	}
}

2.定义接口EspecialCar,包括抽象方法cautionSound( )。PoliceCar、AmbulanceCar、FireCar三个类实现了EspecialCar接口,分别重写了接口的cautionSound( )方法。利用接口、方法重写等知识编写程序展示各特种汽车警示声的多态性。

//接口EspecialCar
public interface EspecialCar {
	abstract void cautionSound( );
}
//类PoliceCar 实现 接口EspecialCar
public class PoliceCar implements EspecialCar{
	@Override
	public void cautionSound() {
		System.out.println("警车警示声");
	}
}
//类 AmbulanceCar 实现 接口EspecialCar
public class AmbulanceCar implements EspecialCar{
	public void cautionSound() {
		System.out.println("救护车警示声");
	}
}
//类FireCar 实现 接口EspecialCar
public class FireCar implements EspecialCar{
	@Override
	public void cautionSound() {
		System.out.println("消防车警示声");
	}
}
//测试类
public class EspecialCarTest {
	public static void main(String[] args) {
		EspecialCar car1 = new PoliceCar();		//实现多态
		car1.cautionSound();

		EspecialCar car2 = new AmbulanceCar();		//实现多态
		car2.cautionSound();

		EspecialCar car3 = new FireCar();		//实现多态
		car3.cautionSound();
	}
}

3.定义一个Father和Child类,并进行测试,要求如下:
Father类为外部类,类中定义一个私有的String类型的属性name,name的值为“zhangjun”。
Child类为Father类的成员内部类,其中定义一个introFather()方法,方法中调用Father类的name属性。
定义一个测试类Test,在Test类的main()方法中,创建Child对象,并调用introFather()方法。

public class Father {
	private String name = "zhangjun";
	//内部类
	class Child{
		void introFather() {
			System.out.println(name);
		}
	}
}
//测试类
public class Test {
	public static void main(String[] args) {
		Father f = new Father();
		Father.Child p1 = f.new Child();	//外部类访问内部类
		p1.introFather();
	}
}
十二、 异常

1.try…catch…catch捕获除数为0异常及输入格式错误异常 。要求:用户输入被除数和除数(输入非数值作为被除数、0作为除数),java异常处理机制处理异常,最后输出程序结束。

import java.util.Scanner;
public class Text1 {
	public static int divide(int x,int y) {
		try {		//可能异常的内容
			int result = x / y;
			return result;
		}catch(Exception e) {		//获取异常
			System.out.println("数据异常,捕获信息为:"+e.getMessage());
		}
		return -1;		//处理异常
	}
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int a = in.nextInt();
		int b = in.nextInt();
		int c = divide(a,b);
		if(c==-1) 
			System.out.println("错误!");
		else
			System.out.println(c);
	}
}

2.编写一个数组越界的异常处理程序。要求:定义一个数组,数组大小是5,分别给数组元素赋值,然后输出数组元素的下标是5的元素的值,就会出现异常,处理异常,最后显示程序结束。

public class Text2 {
	public static void main(String[] args) {
		int[] a;
		try {					//可能出现异常的代码
			a = array();
			int b = a[5];
			System.out.println(b);
		}catch(Exception e) {		//获取异常
			System.out.println("程序错误,捕获信息:"+e.getMessage());
		}
		System.out.println("程序结束");
	}
	public static int[] array() throws Exception{
		int[] arr = new int[5];
		arr[0] = 1;
		arr[1] = 2;
		arr[2] = 3;
		arr[3] = 4;
		arr[4] = 5;
		return arr;
	}
}

3.自定义一个异常类NoThisSongException和Player类,在Player类的play()方法中使用自定义异常。要求:NoThisSongException继承Exception类,类中有一个无参和一个接收一个String类型参数的构造方法,构造方法中都使用super关键字调用父类的构造方法。Player类中定义一个play(int index)方法,方法接收一个int类型的参数,表示播放歌曲的索引,当index>10时,play()方法用throw关键字抛出NoThisSongException异常,创建异常对象时,调用有参的构造方法,传入“您播放的歌曲不存在”。
测试类中创建Player对象,并调用play()方法测试自定义的NoThisSongException异常,使用try…catch语句捕获异常,调用NoThisSongException的getMessage()方法打印出异常信息。

//异常类的固定写法
public class NoThisSongException extends Exception{
	public NoThisSongException() {
		super();
	}
	public NoThisSongException(String message) {
		super(message);
	}
}
//Player类
public class Player {
	public void play(int index) throws NoThisSongException{		//可能出现的异常,固定写法
		if(index>10) {
			throw new NoThisSongException("您播放的歌曲不存在");		//异常的处理方法,固定写法
		}
		else
			System.out.println("即将播放第"+index+"首歌曲");
	}
}
//测试类
import java.util.Scanner;
public class PlayerTest {
	public static void main(String[] args) {
		System.out.println("请选择歌曲的序号:");
		Scanner in = new Scanner(System.in);
		int num = in.nextInt();
		Player p = new Player();
		try {		//在用到可能遇到异常的函数时try,谁用谁try
			 p.play(num);
		}catch(NoThisSongException e) {
			System.out.println("捕获的异常信息为:"+e.getMessage());
		}
	}
}

4.定义Bank类,该类包含money属性和income(int in,int out)方法,对象调用income方法时,必须向参数in传递正数,向out参数传递负数,并且in+out必须大于等于0,否则就抛出异常。因此Bank类在声明income方法时须使用throws关键字声明要产生的异常BankException,在BankException类中须声明能获取错误信息的方法。在测试类中创建Bank对象,并调用income()方法测试自定义的NoThisSBankException异常,使用try…catch语句捕获异常,调用BankException的方法打印出异常信息。

//Bank类
public class Bank {
	int money;
	void income(int in,int out) throws BankException{	//可能异常的情况
		if(in<=0)
			throw new BankException("第一个数据传输错误,应为正数");
		if(out>=0)
			throw new BankException("第二个数据传输错误,应为负数");
		if((in+out)<0)
			throw new BankException("数据错误,两数相加必须大于等于0");
	}
}
//异常类
public class BankException extends Exception{
	public BankException() {
		super();
	}
	public BankException(String message) {
		super(message);
	}
}
//测试类
import java.util.Scanner;
public class BankTest {
	public static void main(String[] args) {
		Bank p = new Bank();
		p.money = 10000;
		int in,out;
		Scanner in0 = new Scanner(System.in);
		in = in0.nextInt();
		out = in0.nextInt();
		try {
			p.income(in, out);
		} catch (BankException e) {
			System.out.println("捕获的异常信息为:"+e.getMessage());		
		}
		System.out.println(p.money+in+out);
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值