201772020113李清华《面向对象程序设计(java)》第九周学习总结

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 ArrayList<Student> studentlist;
 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<studentlist.size();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 <studentlist.size(); 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<Student> {
 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 }

原文地址:https://www.cnblogs.com/bmwb/p/9865168.html

时间: 2024-07-30 18:33:02

201772020113李清华《面向对象程序设计(java)》第九周学习总结的相关文章

20155335 俞昆 2016-2017-2 《Java程序设计》第九周学习总结

学号 2016-2017-2 <Java程序设计>第九周学习总结 ##JDBC入门 在正式介绍JDBC前,已知JDBC是用来执行SQL的解决方案,开发人员使用JDBC的标准接口,开发人员不需接触底层数据库驱动程序的差异性. 本章,我们需要了解JDBC与API使用和概念,我们先要认识应用程序如何与数据库进行沟通,数据库本身是一种独立运行的应用程旭,程序员撰写的应用程序是利用通信协议对数据库进行指令交换,以进行数据的增加删除以及查找. 通常应用程序会利用一组专门与数据库进行通信协议的链接库,以简化

20145311 《Java程序设计》第九周学习总结

20145311 <Java程序设计>第九周学习总结 教材学习内容总结 第十六章 整合数据库 16.1JDBC 16.1.1JDBC简介 JDBC(Java DataBase Connectivity)是用于执行SQL的解决方案,开发人员使用JDBC的标准接口,数据库厂商则对接口进行操作,开发人员无须接触底层数据库驱动程序的差异性. 厂商在操作JDBC驱动程序时,依方式可将驱动程序分为4种类型: ·Type 1:JDBC-ODBC Bridge Driver·Type 2:Native API

20165329 《Java程序设计》第九周学习总结

20165329 <Java程序设计>第九周学习总结 教材学习内容总结 第十二章 URL类 构造方法1 try { URL url = new URL("http://www.google.com"); } catch(MalformedURLException e) { System.out.println("Bad URL:"+url); } 构造方法2:用public URL(String protocol,String host,String f

20165208 2017-2018-2 《Java程序设计》第九周学习总结

20165208 2017-2018-2 <Java程序设计>第九周学习总结 教材学习内容总结 第十三章 URL类 URL对象包含三部分信息:协议.地址和资源 创建URL对象两种方法: public URL (String spec) throws MalformedURLException public URL(String protocol,String host,String file) throws MalformedURLException 使用字符串初始化一个URL对象URL ur

20165235 祁瑛 2018-4 《Java程序设计》第九周学习总结

20165235 祁瑛 2018-4 <Java程序设计>第九周学习总结 教材学习内容总结 URL类 UR类是java.net包中的一个重要类,使用URL创建的对象的应用程序称作称作客户端程序. 一个URL类用如下的构造方法来创建一个类: public URL(String spec) throws MalformedURLException URL创建的对象是http协议,所包含的资源是默认的资源(主页) URL对象可以通过调用InputStream openStream()方法可以返回一个

20165324 《Java程序设计》第九周学习总结

学号 20165324 <Java程序设计>第九周学习总结 教材学习内容总结 第十三章 Java网络编程 URL类 使用URL创建对象的应用程序称为客户端 一个URL对象封装一个具体资源的引用 一个URL对象包含最基本的三部分信息:协议,地址和资源. URL类的构造方法: 该构造方法使用字符串初始化一个URL对象URL url=new URL("http://www.google.com"); public URL(String protocol,String host,S

20165318 2017-2018-2 《Java程序设计》第九周学习总结

20165318 2017-2018-2 <Java程序设计>第九周学习总结 目录 学习过程遇到的问题及总结 教材学习内容总结 第13章 Java网络编程 代码托管 代码统计 学习过程遇到的问题及总结 Q1:运行课本例子4时,代码中为汉字,但同样的输出的字符为乱码. 解决方法:检查后发现,使用的编码方式不一致,其他源文件使用的是GBK,而运行的Client4使用的是UTF-8,将这个源文件也改为GBK编码,就可以正确运行了. Q2:运行课本例子4后又运行例子3时,提示以下问题. 解决方法:我猜

20165225《Java程序设计》第九周学习总结

20165225<Java程序设计>第九周学习总结 1.视频与课本中的学习: 第十三章学习总结 URL类 URL对象包含三部分信息:协议.地址和资源 创建URL对象两种方法: public URL (String spec) throws MalformedURLException public URL(String protocol,String host,String file) throws MalformedURLException 使用字符串初始化一个URL对象URL url=new

20165204 Java第九周学习

20165204 Java第九周学习 学习笔记 学习笔记拍照如下 代码托管 脚本运行截图 码云链接 上周错题总结 错题一 错题二 原文地址:https://www.cnblogs.com/jph596299009/p/8971590.html

201621123040《Java程序设计》第九周学习总结

1.本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容 泛型部分思维导图 集合部分学习总结 java.util.Collection 是一个集合接口;java.util.Collections 是一个包装类,是实现Collection的工具类 2.书面作业 2.1List中指定元素的删除(题集题目) 2.1.1实验总结.并回答:列举至少2种在List中删除元素的方法 实验总结:输出List函数部分--通过split()实现分割:foreach实现遍历输出:删除Lis