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类的堆栈跟踪方法;

 package stackTrace;

 import java.util.*;

 /**
* A program that displays a trace feature of a recursive method call.
* @version 1.01 2004-05-10
* @author Cay Horstmann
*/
public class StackTraceTest
{
/**
* Computes the factorial of a number
* @param n a non-negative integer
* @return n! = 1 * 2 * . . . * n
*/
public static int factorial(int n)
{
System.out.println("factorial(" + n + "):");
Throwable t = new Throwable();
StackTraceElement[] frames = t.getStackTrace();
for (StackTraceElement f : frames)
System.out.println(f);
int r;
if (n <= 1) r = 1;
else r = n * factorial(n - 1);
System.out.println("return " + r);
return r;
} public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter n: ");
int n = in.nextInt();
factorial(n);
}
}

测试程序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");

}

}

 //积极处理方式
package aaa;
import java.io.*;
public class ExceptionTest {
public static void main(String args[])
{
FileInputStream fis;
try {
fis = new FileInputStream("text.txt");
int b;
while((b=fis.read())!=-1)
{
System.out.print(b);
}
fis.close();
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}//JVM自动生成异常对象
}
}
 //消极处理方式
package aaa;
import java.io.*;
public class ExceptionTest {
public static void main(String args[]) throws IOException
{
FileInputStream fis;
fis = new FileInputStream("text.txt");
int b;
while((b=fis.read())!=-1)
{
System.out.print(b);
}
fis.close();
}
}

实验3: 编程练习

练习1:

l 编制一个程序,将身份证号.txt 中的信息读入到内存中;

l 按姓名字典序输出人员信息;

l 查询最大年龄的人员信息;

l 查询最小年龄人员信息;

l 输入你的年龄,查询身份证号.txt中年龄与你最近人的姓名、身份证号、年龄、性别和出生地;

l 查询人员中是否有你的同乡;

l 在以上程序适当位置加入异常捕获代码。

 package test1;

 import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner; public class Main{
private static ArrayList<Student> studentlist;
public static void main(String[] args) {
studentlist = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
File file = new File("F:\\身份证号.txt");
try {
FileInputStream fis = new FileInputStream(file);
BufferedReader in = new BufferedReader(new InputStreamReader(fis));
String temp = null;
while ((temp = in.readLine()) != null) { Scanner linescanner = new Scanner(temp); linescanner.useDelimiter(" ");
String name = linescanner.next();
String number = linescanner.next();
String sex = linescanner.next();
String age = linescanner.next();
String province =linescanner.nextLine();
Student student = new Student();
student.setName(name);
student.setnumber(number);
student.setsex(sex);
int a = Integer.parseInt(age);
student.setage(a);
student.setprovince(province);
studentlist.add(student); }
} catch (FileNotFoundException e) {
System.out.println("学生信息文件找不到");
e.printStackTrace();
} catch (IOException e) {
System.out.println("学生信息文件读取错误");
e.printStackTrace();
}
boolean isTrue = true;
while (isTrue) {
System.out.println("选择你的操作,输入正确格式的选项");
System.out.println("A.按姓名字典排序");
System.out.println("B.输出年龄最大和年龄最小的人");
System.out.println("C.寻找老乡");
System.out.println("D.寻找年龄相近的人");
System.out.println("F.退出");
String m = scanner.next();
switch (m) {
case "A":
Collections.sort(studentlist);
System.out.println(studentlist.toString());
break;
case "B":
int max=0,min=100;
int j,k1 = 0,k2=0;
for(int i=1;i<studentlist.size();i++)
{
j=studentlist.get(i).getage();
if(j>max)
{
max=j;
k1=i;
}
if(j<min)
{
min=j;
k2=i;
} }
System.out.println("年龄最大:"+studentlist.get(k1));
System.out.println("年龄最小:"+studentlist.get(k2));
break;
case "C":
System.out.println("老家?");
String find = scanner.next();
String place=find.substring(0,3);
for (int i = 0; i <studentlist.size(); i++)
{
if(studentlist.get(i).getprovince().substring(1,4).equals(place))
System.out.println("老乡"+studentlist.get(i));
}
break; case "D":
System.out.println("年龄:");
int yourage = scanner.nextInt();
int near=agenear(yourage);
int value=yourage-studentlist.get(near).getage();
System.out.println(""+studentlist.get(near));
break;
case "F":
isTrue = false;
System.out.println("退出程序!");
break;
default:
System.out.println("输入有误"); }
}
}
public static int agenear(int age) {
int j=0,min=53,value=0,k=0;
for (int i = 0; i < studentlist.size(); i++)
{
value=studentlist.get(i).getage()-age;
if(value<0) value=-value;
if (value<min)
{
min=value;
k=i;
}
}
return k;
} }
 package test1;

 public class Student implements Comparable<Student> {

     private String name;
private String number ;
private String sex ;
private int age;
private String province; public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getnumber() {
return number;
}
public void setnumber(String number) {
this.number = number;
}
public String getsex() {
return sex ;
}
public void setsex(String sex ) {
this.sex =sex ;
}
public int getage() { return age;
}
public void setage(int age) {
// int a = Integer.parseInt(age);
this.age= age;
} public String getprovince() {
return province;
}
public void setprovince(String province) {
this.province=province ;
} public int compareTo(Student o) {
return this.name.compareTo(o.getName());
} public String toString() {
return name+"\t"+sex+"\t"+age+"\t"+number+"\t"+province+"\n";
}
}

注:以下实验课后完成

练习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 掌握断言的使用特点及用法。

 package shiyan;
import java.util.Scanner;
import java.io.PrintWriter; public class Main {
public static void main(String[] args) throws Exception{
Scanner in=new Scanner(System.in);
PrintWriter output=new PrintWriter("E:/test.txt");
int sum=0;
jisuanji js=new jisuanji();
for (int i = 0; i < 10; i++) {
int a = (int) Math.round(Math.random() * 100);
int b = (int) Math.round(Math.random() * 100);
int n = (int) Math.round(Math.random() * 3); switch(n)
{
case 1:
System.out.println(a+"/"+b+"=");
while(b==0){
b = (int) Math.round(Math.random() * 100);
}
double c = in.nextDouble();
output.println(a+"/"+b+"="+c);
if (c == js.chu(a,b)) {
sum += 10;
System.out.println("答案正确");
}
else {
System.out.println("答案错误");
} break; case 2:
System.out.println(a+"*"+b+"=");
int c1 = in.nextInt();
output.println(a+"*"+b+"="+c1);
if (c1 == js.chen(a, b)) {
sum += 10;
System.out.println("答案正确");
}
else {
System.out.println("答案错误");
}
break;
case 3:
System.out.println(a+"+"+b+"=");
int c2 = in.nextInt();
output.println(a+"+"+b+"="+c2);
if (c2 == js.jia(a, b)) {
sum += 10;
System.out.println("答案正确");
}
else {
System.out.println("答案错误");
} break ;
case 4:
System.out.println(a+"-"+b+"=");
int c3 = in.nextInt();
output.println(a+"-"+b+"="+c3);
if (c3 == js.jian(a,b)) {
sum += 10;
System.out.println("答案正确");
}
else {
System.out.println("答案错误");
}
break ; } }
System.out.println("成绩"+sum);
output.println("成绩:"+sum);
output.close();
}
}
 package shiyan;

 public class jisuanji {
private int a;
private int b;
public int jia(int a,int b)
{
return a+b;
}
public int jian(int a,int b)
{
return a-b;
}
public int chen(int a,int b)
{
return a*b;
}
public int chu(int a,int b)
{
if(b==0)
{
return 0;
}
else
return a/b;
}
}

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

  1. 20155312 2016-2017-2 《Java程序设计》第九周学习总结

    20155312 2016-2017-2 <Java程序设计>第九周学习总结 课堂内容总结 两个类有公用的东西放在父类里. 面向对象的三要素 封装 继承 多态:用父类声明引用,子类生成对象 ...

  2. 20155303 2016-2017-2 《Java程序设计》第九周学习总结

    20155303 2016-2017-2 <Java程序设计>第九周学习总结 目录 学习内容总结(Linux命令) 教材学习中的问题和解决过程 代码调试中的问题和解决过程 代码托管 上周考 ...

  3. 20155321 2016-2017-2 《Java程序设计》第九周学习总结

    20155321 2016-2017-2 <Java程序设计>第九周学习总结 教材学习内容总结 JDBC简介 厂商在实现JDBC驱动程序时,依方式可将驱动程序分为四种类型: JDBC-OD ...

  4. 20145302张薇《Java程序设计》第九周学习总结

    20145302 <Java程序设计>第九周学习总结 教材学习内容总结 第十六周 JDBC简介 撰写应用程序是利用通信协议对数据库进行指令交换,以进行数据的增删查找 JDBC目的:让Jav ...

  5. 20145213《Java程序设计》第九周学习总结

    20145213<Java程序设计>第九周学习总结 教材学习总结 "五一"假期过得太快,就像龙卷风.没有一点点防备,就与Java博客撞个满怀.在这个普天同庆的节日里,根 ...

  6. 21045308刘昊阳 《Java程序设计》第九周学习总结

    21045308刘昊阳 <Java程序设计>第九周学习总结 教材学习内容总结 第16章 整合数据库 16.1 JDBC入门 16.1.1 JDBC简介 数据库本身是个独立运行的应用程序 撰 ...

  7. 20145337 《Java程序设计》第九周学习总结

    20145337 <Java程序设计>第九周学习总结 教材学习内容总结 数据库本身是个独立运行的应用程序 撰写应用程序是利用通信协议对数据库进行指令交换,以进行数据的增删查找 JDBC可以 ...

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

    20145224 <Java程序设计>第九周学习总结 第十六章 整合数据库 JDBC入门 ·数据库本身是个独立运行的应用程序 ·撰写应用程序是利用通信协议对数据库进行指令交换,以进行数据的 ...

  9. 20145236 《Java程序设计》第九周学习总结

    20145236 <Java程序设计>第九周学习总结 教材学习内容总结 第十六章 整合数据库 JDBC简介 1.JDBC是java联机数据库的标准规范.它定义了一组标准类与接口,标准API ...

  10. 201521123061 《Java程序设计》第九周学习总结

    201521123061 <Java程序设计>第九周学习总结 1. 本周学习总结 2. 书面作业 本次PTA作业题集异常 1.常用异常 题目5-1 1.1 截图你的提交结果(出现学号) 1 ...

随机推荐

  1. Linux 下安装nodejs

    linux 版本 uname -a Linux -29deepin-generic # SMP Fri Jul :: UTC x86_64 GNU/Linux Nodejs 版本:node-v10.1 ...

  2. 16. Antimalware (反病毒 3个)

    Malwarebytes反恶意软件是为Windows的恶意软件扫描程序. 作者声称使用各种技术来查找其他恶意软件扫描仪检测不到的恶意软件. 有一个有限选项的免费试用版和支持能够运行扫描计划的完整版本, ...

  3. Linux第七节课学习笔记

    RHEL7用户身份有以下这些: 1.管理员 root UID:0 权限最大: 2.系统用户 UID:1-999: 3.普通用户 UID:1000+. 一个用户基本组只有一个,扩展组可多个,创建扩展组用 ...

  4. 【linux日常】 ACL权限管理

    ACL ((Access Control Lists) setfacl命令 这里引用一个非常详细的命令介绍. 要点: getfacl -R        递归获取acl权限,可以存储为文件以备还原 g ...

  5. sed命令讲解

    sed命令选项及作用 -n 不打印所有的行到标准输出 -e 表示将下一个字符串解析为sed编辑命令 -f 表示正在调用sed脚本文件 sed编辑命令 p 打印匹配行 = 打印文件行号 a\ 在定位行号 ...

  6. mongdb 报错“Cannot connect to the MongoDB at 192.179.1.6:27017. Error: Network is unreachable.”

    1.命令行输入talnet 192.179.1.6 27017 看能拼通不 2.防火墙添加入站规则  27017 3.修改mongdb配置文件 mongodb.config dbpath=c:\Mon ...

  7. 2018总结-->2019新目标

    2018完成的事情: ①考到了驾照: ②刷了很多题,春季找到了实习,赚到了去日本旅游的经费和2019毕业租房的预算,最后签了offer: ③去了西安.天津.山西,看到了不一样的人和事: ④发了小论文, ...

  8. 防止shell script多次运行

    一个思路是在script初期检测系统中是否存在同名进程. ] then echo "This script is already running. Exit." else whil ...

  9. .Net Core跨平台应用研究-CustomSerialPort(增强型跨平台串口类库)

    .Net Core跨平台应用研究-CustomSerialPort -增强型跨平台串口类库 摘要 在使用SerialPort进行串口协议解析过程中,经常遇到接收单帧协议数据串口接收事件多次触发,协议解 ...

  10. Spring编程式事务管理和声明式事务管理

    本来想写一篇随笔记一下呢,结果发现一篇文章写的很好了,已经没有再重复写的必要了. https://www.ibm.com/developerworks/cn/education/opensource/ ...