[1]实验目的:了解Java中异常处理(exception)的作用及常用的异常类,掌握异常处理的设计方法。
[2]实验内容:
*1、用try-catch-finally结构实现异常处理。编译并运行程序,写出程序运行结果。
package experiment3;
public class Exp3_1 {
public static void main(String[] args){
int i=0;
String[] greeting={"Hello", "Only", "Test"};
while(i<4){
try{
System.out.println(greeting[i]);
}catch (ArrayIndexOutOfBoundsException e){
System.out.println("数组越界");
}finally {
System.out.println("总会运行");
}
i++;
}
}
}
2、设计一个Java程序,自定义异常类,从命令行(键盘)输入一个字符串,如果该字符串值为“XYZ”,则抛出一个异常信息“This is a XYZ”,如果从命令行输入ABC,则没有抛出异常。(只有XYZ和ABC两种输入)。
package experiment3;
import java.util.*;
class InputException extends Exception
{
public String toString()
{
return "异常:This is a XYZ";
}
}
public class Exp3_2 {
public static void main(String[] args) throws InputException {
Scanner scanner=new Scanner(System.in);
String s=scanner.next();
if(s.equals("XYZ"))
throw new InputException();
System.out.println("输入正常");
}
}
3、声明一个Average接口,继续完善学生信息录入程序,其中约定求平均值的方法;声明多个类实现Average接口,分别给出求平均值的方法实现。例如,在一组数值中,一种算法是,全部数值相加后求平均值,另一种算法是,去掉一个最高分和一个最低分后,再将总分求平均等;使用键盘输入数据时,对于不能转换成数值的字符串进行异常处理。
package experiment3;
import java.util.ArrayList;
import java.util.Scanner;
interface Average
{
public float getAve();
}
class GetAver1 implements Average//全部数值相加后求平均值
{
ArrayList<Stu> al;
float sum=0;
GetAver1(ArrayList<Stu> al) {
this.al = al;
}
public float getAve()
{
for(int i=0;i<al.size();i++)
{
sum+=al.get(i).getScore();
}
return sum/al.size();
}
}
class GetAver2 implements Average {
ArrayList<Stu> al;
float sum=0;
GetAver2(ArrayList<Stu> al) {
this.al = al;
}
public float getAve() {
double min = 101, max = -1;
for (int i = 0; i < al.size(); i++) {
if (al.get(i).getScore()< min) min = al.get(i).getScore();
if (al.get(i).getScore() > max) max = al.get(i).getScore();
sum+=al.get(i).getScore();
}
return (float) ((sum - max - min) / (al.size() - 2));
}
}
class MyDate{
int year;
int month;
int day;
MyDate(int y,int m,int d)
{
year=y;month=m;day=d;
}
boolean isValidYear()
{
return this.year >= 0 && this.year <= 2021;
}
boolean isValidMonth()
{
return this.month >= 1 && this.month <= 12;
}
public boolean isValidDay()
{
return this.day >= 1 && this.day <= DaysOfMonth();
}
public boolean isLeapYear()
{
return this.year % 400 == 0 || this.year % 100 != 0 && this.year % 4 == 0;
}
public int DaysOfMonth()
{
switch(this.month) {
case 1:case 3:case 5:case 7:case 8:case 10: case 12:
return 31;
case 4:case 6:case 9:case 11:
return 30;
case 2:
if(isLeapYear())return 29;
else return 28;
default: return -1;
}
}
public boolean isValidDate()
{
return isValidYear() && isValidMonth() && isValidDay();
}
}
class Stu extends MyDate {
private String name;
private int age;
private float score;
Stu(String name, int year, int month, int day, float score){
super(year,month,day);
this.name=name;
this.age=2021-year;
this.score=score;
}
public void getInfo(){
System.out.println("学生信息如下:\n姓名:" + this.name + " 出生日期:" + this.year + " " +this.month + " " + this.day +
" 年龄:" + this.age + " Java实验课成绩:" + this.score);
}
public float getScore(){
return this.score;
}
}
public class Exp3_3 {
public static void main(String[] args){
String name;
int year,month,day;
float score;
ArrayList<Stu> stu = new ArrayList<Stu>();
System.out.println("请输入10位学生的姓名、出生年月日、java课程实验成绩\n");
for(int i=0;i<10;i++){
Scanner ss=new Scanner(System.in);
try {
name = ss.next();
year = ss.nextInt();
month = ss.nextInt();
day = ss.nextInt();
score = ss.nextFloat();
}catch (Exception e){
System.out.println("格式错误!请重新输入:");
continue;
}
stu.add(new Stu(name,year,month,day,score));
if (!stu.get(i).isValidDate()){
System.out.println("日期信息有误,重新输入:");
stu.remove(i);
i--;
}
else {
stu.get(i).getInfo();
if(stu.get(i).isLeapYear())
System.out.println("生日在闰年");
else stu.get(i).isLeapYear();
System.out.println("生日不在闰年");
if(stu.get(i).DaysOfMonth()==31)
System.out.println("生日在大月");
else if (stu.get(i).DaysOfMonth()==30)
System.out.println("生日在小月");
else System.out.println("生日既不在大月,也不在小月");
}
}
GetAver1 aver1 = new GetAver1(stu);
GetAver2 aver2 = new GetAver2(stu);
System.out.println("全部数据求成绩平均值:"+aver1.getAve());
System.out.println("去掉最大最小求成绩平均值:"+aver2.getAve());
}
}
[3]实验分析:
掌握了Java中的异常处理过程和异常类,掌握了try-catch-finall的异常处理设计方法。