Java实验3 接口与异常处理
(1)飞机和小鸟都有飞的功能,小鸟用翅膀来飞,而飞机是用空气动力学原理来飞,抽象一个Fly接口,接口中有一个fly抽象方法,定义飞机和小鸟类均实现Fly接口,写个测试类来进行测试。
//定义接口
package fly;
public interface Canfly {
public void fly();
}
//定义类,实现接口
class Bird implements Canfly {
@Override
public void fly() {
System.out.println("小鸟靠翅膀起飞");
}
}
class Plane implements Canfly {
@Override
public void fly() {
System.out.println("飞机靠燃油起飞");
}
}
//测试类
class text {
public static void makeFly(Canfly c)
{
c.fly();
}
public static void main(String[] args) {
Plane p=new Plane();
Bird b=new Bird();
text.makeFly(p);
text.makeFly(b);
}
运行结果:
(2)为复数类的除法加上自定义异常处理。
package complex;
import java.util.Scanner;
public class complextest { // 复数类
double real; // 实部
double image; // 虚部
complextest(){ // 不带参数的构造方法
Scanner input = new Scanner(System.in);
double real = input.nextDouble();
double image = input.nextDouble();
complextest(real,image);
}
private void complextest(double real, double image) { // 供不带参数的构造方法调用
// TODO Auto-generated method stub
this.real = real;
this.image = image;
}
complextest(double real,double image){ // 带参数的构造方法
this.real = real;
this.image = image;
}
public double getReal() {
return real;
}
public void setReal(double real) {
this.real = real;
}
public double getImage() {
return image;
}
public void setImage(double image) {
this.image = image;
}
complextest div(complextest a){ // 复数相除
double real2 = a.getReal();
double image2 = a.getImage();
if((real2==0)&&(image2==0)) //检查异常:如果real2==0&&image2==0则属于异常情况
throw(new RuntimeException("输入的数值必须为正整数"));//报告异常
double newReal = (real*real2 + image*image2)/(real2*real2 + image2*image2);
double newImage = (image*real2 - real*image2)/(real2*real2 + image2*image2);
complextest result = new complextest(newReal,newImage);
return result;
}
public void print(){ // 输出
if(image > 0){
System.out.println(real + " + " + image + "i");
}else if(image < 0){
System.out.println(real + "" + image + "i");
}else{
System.out.println(real);
}
}
}
// 测试类
package complex;
public class main {
public static void main(String[] args) {
System.out.println("请用户输入第一个复数的实部和虚部:");
complextest data1 = new complextest ();
System.out.println("请用户输入第二个复数的实部和虚部:");
try{ //启用异常处理机制
complextest data2 = new complextest();
complextest result_div = data1.div(data2);
result_div.print();
}
catch(RuntimeException e){ //捕获并处理异常
System.out.println(e.getMessage());
}
}
}
运行结果:
(3)写个程序,从命令行获取两个数值,求这两个数值的和,定义异常类处理可能的各种异常。
package text;
import java.util.Scanner;
public class sum {
public static void main(String[] args) {
// ArithmeticException 算数异常
// NumberFormatException 数字格式异常
Scanner scanner = new Scanner(System.in);
while (true) {
try {
System.out.println("请输入第一个数:");
int n1 = Integer.parseInt(scanner.nextLine());
System.out.println("请输入第二个数:");
int n2 = Integer.parseInt(scanner.nextLine());
int n3 = n1 + n2;
System.out.println(n1 + "+" + n2 + " = " + n3);
}
catch (NumberFormatException e) {
e.printStackTrace();
continue;
}
catch (ArithmeticException e) {
e.printStackTrace();
continue;
}
finally {
System.out.println("每次都输出的!");
}
}
}
}
运行结果:
下一篇:
Go语言值不值得学,发展前景怎么样?
