1、实验目的与要求
(1) 掌握java异常处理技术;
(2) 了解断言的用法;
(3) 了解日志的用途;
(4) 掌握程序基础调试技巧;
2、实验内容和步骤
实验1:用命令行与IDE两种环境下编辑调试运行源程序ExceptionDemo1、ExceptionDemo2,结合程序运行结果理解程序,掌握未检查异常和已检查异常的区别。
//异常示例1 public class ExceptionDemo1 { public static void main(String args[]) { int a = 0; System.out.println(5 / a); } } |
//异常示例2 import java.io.*;
public class ExceptionDemo2 { public static void main(String args[]) { FileInputStream fis=new FileInputStream("text.txt");//JVM自动生成异常对象 int b; while((b=fis.read())!=-1) { System.out.print(b); } fis.close(); } } |
实验结果:未检查异常:
修改后:
实验2: 导入以下示例程序,测试程序并进行代码注释。
测试程序1:
l 在elipse IDE中编辑、编译、调试运行教材281页7-1,结合程序运行结果理解程序;
l 在程序中相关代码处添加新知识的注释;
l 掌握Throwable类的堆栈跟踪方法;
1 package stackTrace; 2 3 import java.util.*; 4 5 /** 6 * A program that displays a trace feature of a recursive method call. 7 * @version 1.01 2004-05-10 8 * @author Cay Horstmann 9 */ 10 public class StackTraceTest 11 { 12 /** 13 * Computes the factorial of a number 14 * @param n a non-negative integer 15 * @return n! = 1 * 2 * . . . * n 16 */ 17 public static int factorial(int n) 18 { 19 System.out.println("factorial(" + n + "):"); 20 Throwable t = new Throwable(); 21 StackTraceElement[] frames = t.getStackTrace(); 22 for (StackTraceElement f : frames) 23 System.out.println(f); 24 int r; 25 if (n <= 1) r = 1; 26 else r = n * factorial(n - 1); 27 System.out.println("return " + r); 28 return r; 29 } 30 31 public static void main(String[] args) 32 { 33 Scanner in = new Scanner(System.in); 34 System.out.print("Enter n: "); 35 int n = in.nextInt(); 36 factorial(n); 37 } 38 }
测试程序2:
l Java语言的异常处理有积极处理方法和消极处理两种方式;
l 下列两个简答程序范例给出了两种异常处理的代码格式。在elipse IDE中编辑、调试运行源程序ExceptionalTest.java,将程序中的text文件更换为身份证号.txt,要求将文件内容读入内容,并在控制台显示;
掌握两种异常处理技术的特点。
//积极处理方式 import java.io.*;
class ExceptionTest { public static void main (string args[]) { try{ FileInputStream fis=new FileInputStream("text.txt"); } catch(FileNotFoundExcption e) { …… } …… } } |
//消极处理方式
import java.io.*; class ExceptionTest { public static void main (string args[]) throws FileNotFoundExcption { FileInputStream fis=new FileInputStream("text.txt"); } } |
1 //积极处理方式 2 package aaa; 3 import java.io.*; 4 public class ExceptionTest { 5 public static void main(String args[]) 6 { 7 FileInputStream fis; 8 try { 9 fis = new FileInputStream("text.txt"); 10 int b; 11 while((b=fis.read())!=-1) 12 { 13 System.out.print(b); 14 } 15 fis.close(); 16 } catch (Exception e) { 17 // TODO 自动生成的 catch 块 18 e.printStackTrace(); 19 }//JVM自动生成异常对象 20 } 21 }
1 //消极处理方式 2 package aaa; 3 import java.io.*; 4 public class ExceptionTest { 5 public static void main(String args[]) throws IOException 6 { 7 FileInputStream fis; 8 fis = new FileInputStream("text.txt"); 9 int b; 10 while((b=fis.read())!=-1) 11 { 12 System.out.print(b); 13 } 14 fis.close(); 15 } 16 }
实验3: 编程练习
练习1:
l 编制一个程序,将身份证号.txt 中的信息读入到内存中;
l 按姓名字典序输出人员信息;
l 查询最大年龄的人员信息;
l 查询最小年龄人员信息;
l 输入你的年龄,查询身份证号.txt中年龄与你最近人的姓名、身份证号、年龄、性别和出生地;
l 查询人员中是否有你的同乡;
l 在以上程序适当位置加入异常捕获代码。
1 package test1; 2 3 import java.io.BufferedReader; 4 import java.io.File; 5 import java.io.FileInputStream; 6 import java.io.FileNotFoundException; 7 import java.io.IOException; 8 import java.io.InputStreamReader; 9 import java.util.ArrayList; 10 import java.util.Collections; 11 import java.util.Scanner; 12 13 public class Main{ 14 private static ArrayListstudentlist; 15 public static void main(String[] args) { 16 studentlist = new ArrayList<>(); 17 Scanner scanner = new Scanner(System.in); 18 File file = new File("F:\\身份证号.txt"); 19 try { 20 FileInputStream fis = new FileInputStream(file); 21 BufferedReader in = new BufferedReader(new InputStreamReader(fis)); 22 String temp = null; 23 while ((temp = in.readLine()) != null) { 24 25 Scanner linescanner = new Scanner(temp); 26 27 linescanner.useDelimiter(" "); 28 String name = linescanner.next(); 29 String number = linescanner.next(); 30 String sex = linescanner.next(); 31 String age = linescanner.next(); 32 String province =linescanner.nextLine(); 33 Student student = new Student(); 34 student.setName(name); 35 student.setnumber(number); 36 student.setsex(sex); 37 int a = Integer.parseInt(age); 38 student.setage(a); 39 student.setprovince(province); 40 studentlist.add(student); 41 42 } 43 } catch (FileNotFoundException e) { 44 System.out.println("学生信息文件找不到"); 45 e.printStackTrace(); 46 } catch (IOException e) { 47 System.out.println("学生信息文件读取错误"); 48 e.printStackTrace(); 49 } 50 boolean isTrue = true; 51 while (isTrue) { 52 System.out.println("选择你的操作,输入正确格式的选项"); 53 System.out.println("A.按姓名字典排序"); 54 System.out.println("B.输出年龄最大和年龄最小的人"); 55 System.out.println("C.寻找老乡"); 56 System.out.println("D.寻找年龄相近的人"); 57 System.out.println("F.退出"); 58 String m = scanner.next(); 59 switch (m) { 60 case "A": 61 Collections.sort(studentlist); 62 System.out.println(studentlist.toString()); 63 break; 64 case "B": 65 int max=0,min=100; 66 int j,k1 = 0,k2=0; 67 for(int i=1;i ) 68 { 69 j=studentlist.get(i).getage(); 70 if(j>max) 71 { 72 max=j; 73 k1=i; 74 } 75 if(j<min) 76 { 77 min=j; 78 k2=i; 79 } 80 81 } 82 System.out.println("年龄最大:"+studentlist.get(k1)); 83 System.out.println("年龄最小:"+studentlist.get(k2)); 84 break; 85 case "C": 86 System.out.println("老家?"); 87 String find = scanner.next(); 88 String place=find.substring(0,3); 89 for (int i = 0; i ) 90 { 91 if(studentlist.get(i).getprovince().substring(1,4).equals(place)) 92 System.out.println("老乡"+studentlist.get(i)); 93 } 94 break; 95 96 case "D": 97 System.out.println("年龄:"); 98 int yourage = scanner.nextInt(); 99 int near=agenear(yourage); 100 int value=yourage-studentlist.get(near).getage(); 101 System.out.println(""+studentlist.get(near)); 102 break; 103 case "F": 104 isTrue = false; 105 System.out.println("退出程序!"); 106 break; 107 default: 108 System.out.println("输入有误"); 109 110 } 111 } 112 } 113 public static int agenear(int age) { 114 int j=0,min=53,value=0,k=0; 115 for (int i = 0; i < studentlist.size(); i++) 116 { 117 value=studentlist.get(i).getage()-age; 118 if(value<0) value=-value; 119 if (value<min) 120 { 121 min=value; 122 k=i; 123 } 124 } 125 return k; 126 } 127 128 }
1 package test1; 2 3 public class Student implements Comparable{ 4 5 private String name; 6 private String number ; 7 private String sex ; 8 private int age; 9 private String province; 10 11 public String getName() { 12 return name; 13 } 14 public void setName(String name) { 15 this.name = name; 16 } 17 public String getnumber() { 18 return number; 19 } 20 public void setnumber(String number) { 21 this.number = number; 22 } 23 public String getsex() { 24 return sex ; 25 } 26 public void setsex(String sex ) { 27 this.sex =sex ; 28 } 29 public int getage() { 30 31 return age; 32 } 33 public void setage(int age) { 34 // int a = Integer.parseInt(age); 35 this.age= age; 36 } 37 38 public String getprovince() { 39 return province; 40 } 41 public void setprovince(String province) { 42 this.province=province ; 43 } 44 45 public int compareTo(Student o) { 46 return this.name.compareTo(o.getName()); 47 } 48 49 public String toString() { 50 return name+"\t"+sex+"\t"+age+"\t"+number+"\t"+province+"\n"; 51 } 52 }
注:以下实验课后完成
练习2:
l 编写一个计算器类,可以完成加、减、乘、除的操作;
l 利用计算机类,设计一个小学生100以内数的四则运算练习程序,由计算机随机产生10道加减乘除练习题,学生输入答案,由程序检查答案是否正确,每道题正确计10分,错误不计分,10道题测试结束后给出测试总分;
l 将程序中测试练习题及学生答题结果输出到文件,文件名为test.txt;
l 在以上程序适当位置加入异常捕获代码。
实验4:断言、日志、程序调试技巧验证实验。
实验程序1:
//断言程序示例 public class AssertDemo { public static void main(String[] args) { test1(-5); test2(-3); }
private static void test1(int a){ assert a > 0; System.out.println(a); } private static void test2(int a){ assert a > 0 : "something goes wrong here, a cannot be less than 0"; System.out.println(a); } } |
l 在elipse下调试程序AssertDemo,结合程序运行结果理解程序;
l 注释语句test1(-5);后重新运行程序,结合程序运行结果理解程序;
l 掌握断言的使用特点及用法。
1 package shiyan; 2 import java.util.Scanner; 3 import java.io.PrintWriter; 4 5 public class Main { 6 public static void main(String[] args) throws Exception{ 7 Scanner in=new Scanner(System.in); 8 PrintWriter output=new PrintWriter("E:/test.txt"); 9 int sum=0; 10 jisuanji js=new jisuanji(); 11 for (int i = 0; i < 10; i++) { 12 int a = (int) Math.round(Math.random() * 100); 13 int b = (int) Math.round(Math.random() * 100); 14 int n = (int) Math.round(Math.random() * 3); 15 16 switch(n) 17 { 18 case 1: 19 System.out.println(a+"/"+b+"="); 20 while(b==0){ 21 b = (int) Math.round(Math.random() * 100); 22 } 23 double c = in.nextDouble(); 24 output.println(a+"/"+b+"="+c); 25 if (c == js.chu(a,b)) { 26 sum += 10; 27 System.out.println("答案正确"); 28 } 29 else { 30 System.out.println("答案错误"); 31 } 32 33 break; 34 35 case 2: 36 System.out.println(a+"*"+b+"="); 37 int c1 = in.nextInt(); 38 output.println(a+"*"+b+"="+c1); 39 if (c1 == js.chen(a, b)) { 40 sum += 10; 41 System.out.println("答案正确"); 42 } 43 else { 44 System.out.println("答案错误"); 45 } 46 break; 47 case 3: 48 System.out.println(a+"+"+b+"="); 49 int c2 = in.nextInt(); 50 output.println(a+"+"+b+"="+c2); 51 if (c2 == js.jia(a, b)) { 52 sum += 10; 53 System.out.println("答案正确"); 54 } 55 else { 56 System.out.println("答案错误"); 57 } 58 59 break ; 60 case 4: 61 System.out.println(a+"-"+b+"="); 62 int c3 = in.nextInt(); 63 output.println(a+"-"+b+"="+c3); 64 if (c3 == js.jian(a,b)) { 65 sum += 10; 66 System.out.println("答案正确"); 67 } 68 else { 69 System.out.println("答案错误"); 70 } 71 break ; 72 73 } 74 75 } 76 System.out.println("成绩"+sum); 77 output.println("成绩:"+sum); 78 output.close(); 79 } 80 }
1 package shiyan; 2 3 public class jisuanji { 4 private int a; 5 private int b; 6 public int jia(int a,int b) 7 { 8 return a+b; 9 } 10 public int jian(int a,int b) 11 { 12 return a-b; 13 } 14 public int chen(int a,int b) 15 { 16 return a*b; 17 } 18 public int chu(int a,int b) 19 { 20 if(b==0) 21 { 22 return 0; 23 } 24 else 25 return a/b; 26 } 27 }