版权声明:本文为Trinity原创文章,未经Trinity允许不得转载 https://blog.csdn.net/Caoyang_He/article/details/83451014
(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。
- 合理利用上一次实验中的ReList类。
- 成绩列表可能为空。
ReList .java
package test5;
public interface ReList {
public void add(Object obj);
public Object get(int index);
public void clear();
public boolean isEmpty();
public int size();
public int capacity();
}
Student.java
package test5;
public class Student {
private String name;
private double grade;
public Student(String name, double grade) {
super();
this.name = name;
this.grade = grade;
}
public String getName() {
return name;
}
public double getGrade() {
return grade;
}
}
StudentList.java
package test5;
import java.util.Arrays;
import test4.ReList;
public class StudentList implements ReList {
private int num = 0;
private Student[] stulist;
private int i = 0;
public StudentList(int num) {
super();
this.num = num;
stulist = new Student[num];
}
public void add(Object obj) {
// TODO Auto-generated method stub
if(i==stulist.length)
{
stulist=Arrays.copyOf(stulist, stulist.length+1);
}
this.stulist[i]=(Student) obj;
i++;
}
public Object get(int index) {
// TODO Auto-generated method stub
if(index>=i)
{
System.err.println("error index");
return null;
}
else
{
return this.stulist[index];
}
}
public void clear() {
// TODO Auto-generated method stub
for(int i = 0;i<this.i;i++ )
{
this.stulist[i]=null;
}
this.i = 0;
}
public boolean isEmpty() {
// TODO Auto-generated method stub
if(i==0)
return true;
else
return false;
}
public int size() {
// TODO Auto-generated method stub
return i;
}
public int capacity() {
// TODO Auto-generated method stub
return i;
}
}
Test.java
package test5;
import java.util.Scanner;
public class Test {
public static void main(String[] args) throws InvalidScoreException {
// TODO Auto-generated method stub
System.out.println("input class student number");
Scanner input= new Scanner(System.in);
int stunum =input.nextInt();
StudentList stulist = new StudentList(stunum);
System.out.println("input class student name and grade");
boolean flag = true;
while(flag)
{
Scanner input1= new Scanner(System.in);
String inputstr = input1.nextLine();
String[] str = inputstr.split(" ");
double g = 0.0;
if(str[0].equals("-1")||str[1].equals("-1"))
{
flag = false;
break;
}
g=Double.parseDouble(str[1]);
if(g<0.0||g>100.0)
{
throw new InvalidScoreException();
}
else
{
stulist.add(new Student(str[0],Double.parseDouble(str[1])));
}
}
Double sumgrade = 0.0;
int passnum = 0;
for(int i = 0;i<stulist.size();i++)
{
System.out.println(((Student)stulist.get(i)).getName()+" "+((Student)stulist.get(i)).getGrade());
sumgrade = sumgrade +((Student)stulist.get(i)).getGrade();
if(((Student)stulist.get(i)).getGrade()>=60.0)
passnum++;
}
System.out.println("=============================");
System.out.println("平均成绩:"+sumgrade/stulist.size());
System.out.println("及格率:"+passnum*100.0/stulist.size()+"%");
}
}
(2)编写一个程序引起JVM的OutOfMemoryError。
要求:在程序中不断分配内存,并引起JVM的OutOfMemoryError错误,然后用try…catch捕捉处理这个异常,在异常处理中查看虚拟机总内存和空闲内存并尝试恢复错误。在错误恢复后,再次查看总内存和空闲内存。
提示:
- 合理利用上一次实验中的ReList类。
- 可以使用Runtime类的freeMemory()方法查看空闲内存。
- 使用Runtime类的totalMemory()方法查看总内存。(maxMemory()方法可以查看最大可占用内存。)
- 试图恢复这个异常的时候可以使用System.gc()方法。
OutOfMenmoryErrorTest.java
package test5;
public class OutOfMenmoryErrorTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
try
{
int len = Integer.MAX_VALUE;
int largArray[] = new int[len];
System.out.println(len);
}
catch (OutOfMemoryError ex)
{
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(ex.getMessage());
}
finally
{
System.gc();
}
}
}
InvalidScoreException .java
package test5;
public class InvalidScoreException extends Exception {
private static final long serialVersionUID = 1L;
public InvalidScoreException()
{
super("输入错误:请输入数字0-100");
}
}