实验五 异常处理

(1.编写一个计算学生平均成绩的程序。

要求:程序实现输入学生名字和成绩,当用户输入-1时表示输入结束。根据正确输入的成绩计算平均成绩和及格率。成绩用double类型表示,范围在[0.0,

100.0]之间。自定义一个checked异常InvalidScoreException,如果用户输入成绩时,输入一个非法值,如-80、大于100或字符等,程序抛出该异常,并捕获异常。请使用try和catch语句实现对输入、计算过程中出现的异常进行处理,某个学生的成绩输入错误时,应提示重新输入该学生的成绩。程序运行过程中不能使程序非法退出。

用户输入完学生姓名和成绩后,首先显示学生的成绩列表,然后显示全部学生的平均成绩和及格率(平均成绩和及格率均保留小数点后一位),要求程序输出格式如下所示:

张三       80.0

李四       40.0

王五       60.0

马六       71.0

赵七       22.0

====

平均成绩:54.6

及格率:60.0%


提示:

1. 用户可能在名字和成绩中任意一处输入-1。

2. 合理利用上一次实验中的ReList类。

3. 成绩列表可能为空。


运行结果   
工程截图


InvalidScoreException.java

package shiyan5_1;

public class InvalidScoreException extends Exception{

private static final long serialVersionUID = 1L;

public InvalidScoreException(String s) {

       System.out.println(s);

}

}


myList.java

package shiyan5_1;

public class myList {

private double Average;

private double pass_rate;

private int number=0;

Student[]liStudents;

public double getAverage() {

       return Average;

}

public void setAverage(double average) {

       Average = average;

}

public double getPass_rate() {

       return pass_rate;

}

public void setPass_rate(double pass_rate) {

       this.pass_rate = pass_rate;

}

public myList() {

       Average=0.0;

       pass_rate=0.0;

       number=0;

       liStudents = newStudent[100];

}

public void Display(){

       double sum=0;

       for(int i=0;i

              System.out.println(liStudents[i].getName()+"  "+liStudents[i].getScore());

              sum+=liStudents[i].getScore();

              if(liStudents[i].getScore()>=60){

                     pass_rate++;

              }

       }

       this.Average=sum/number;

       this.pass_rate=pass_rate/number;

       System.out.println("平均成绩:"+(double)Math.round(Average*10)/10);

       System.out.println("及格率:"+(double)Math.round(pass_rate*1000)/10+"%");

}

public void add(Student a){

       liStudents[getNumber()]=a;

       setNumber(number+1);

}

public int getNumber() {

       return number;

}

public void setNumber(int number) {

       this.number = number;

}

}


Student.java

package shiyan5_1;

public class Student {

private String name;

private double score;

public Student() {

       name="wangshuai";

       score=100.0;

}

public String getName() {

       return name;

}

public void setName(String name) {

       this.name = name;

}

public double getScore() {

       return score;

}

public void setScore(double score) {

       this.score = score;

}

}


Test.java

package shiyan5_1;

import java.util.Scanner;

import javax.naming.NamingException;

public class Test {

       public static void main(String[] args) throws NamingException {

              System.out.println("程序开始!");

              myListlists = new myList();

              Studentstu = new Student();

              Scannerinput = new Scanner(System.in);

              int f=0;

              while(true){

                     if(f%2==0){

                            f++;

                            System.out.println("请输入学生姓名:");

                            Stringitname = input.next();

                            if(itname.compareTo( "-1")==0){

                                   System.out.println("程序停止运行!");

                                   lists.Display();

                                   input.close();

                                   System.exit(0);

                            }

                            stu.setName(itname);

                     }else{

                            f++;

                            try {

                                   System.out.println("请输入学生成绩:");

                                   double itsorce;

                                   if(input.hasNextInt()){

                                          itsorce= Double.parseDouble(input.next());

                                          if(itsorce==-1){

                                                 System.out.println("程序停止运行!");

                                                 lists.Display();

                                                 input.close();

                                                 System.exit(0);

                                          }

                                   }else {

                                          input = new Scanner(System.in);

                                          throw new InvalidScoreException("输入的数据类型不符合,请重新输入~");

                                   }

                                   if(itsorce<0||itsorce>100){

                                          throw new InvalidScoreException("请重新输入0-100以内的数据~");

                                   }

                                   stu.setScore(itsorce);

                                   lists.add(stu);

                                   stu = new Student();

                            }catch (InvalidScoreException e) {

                                   f--;

                            }

                     }

              }

       }

}

(2.编写一个程序引起JVM的OutOfMemoryError。

要求:在程序中不断分配内存,并引起JVM的OutOfMemoryError错误,然后用try…catch捕捉处理这个异常,在异常处理中查看虚拟机总内存和空闲内存并尝试恢复错误。在错误恢复后,再次查看总内存和空闲内存。

提示:

1. 合理利用上一次实验中的ReList类。

2. 可以使用Runtime类的freeMemory()方法查看空闲内存。

3. 使用Runtime类的totalMemory()方法查看总内存。(maxMemory()方法可以查看最大可占用内存。)

4. 试图恢复这个异常的时候可以使用System.gc()方法。



TestMemory.java

package shiyan5_2;

public class TestMemory {

       static final int SIZE=1700*1024*1024;

       public static void main(String[] args) {

                     System.out.println("程序运行开始时:");

                            System.out.println("空闲内存:"+Runtime.getRuntime().freeMemory() / 1024 / 1024);

                            System.out.println("总内存:"+Runtime.getRuntime().totalMemory() / 1024 / 1024);

                            System.out.println("最大内存:"+Runtime.getRuntime().maxMemory() / 1024 / 1024);

                            System.out.println("=============");

                            System.out.println("分配一个超过最大内存的数组为1700*1024*1024后");

                            try {

             int[] i = new int[SIZE];

                            }catch (OutOfMemoryError ee) {

                                   System.out.println("产生了OutOfMemoryError!异常");

                            }

                            finally {

                                   System.gc();

                            }

                            System.out.println("使用System.gc()方法后恢复异常:");

                            System.out.println("空闲内存:"+Runtime.getRuntime().freeMemory() / 1024 / 1024);

                            System.out.println("总内存:"+Runtime.getRuntime().totalMemory() / 1024 / 1024);

                            System.out.println("最大内存:"+Runtime.getRuntime().maxMemory() / 1024 / 1024);

                            System.out.println("=============");

       }

}

你可能感兴趣的:(实验五 异常处理)