} } public class L02 { public static void main(String args[]) { Account a=new Account(\ System.out.println(\账户余额为\元\ a.deposit(500); System.out.println(\你存入\元\ System.out.println(\账户余额为\元\ a.withdraw(300); } } 3. package mymaths; interface Num{ public void Max(); public void Min(); } public class interface implements Num{ int a,b,c; public interface (int a,int b,int c){ this.a=a; this.b=b; this.c=c; } public void Max(){ int max; max=a>b?a:b; max=max>c?max:c; System.out.println(\三个数中最大值是:\ } public void Min(){ int min; min=a
public abstract class Cipher { public String encrypt(String s) { StringBuffer result = new StringBuffer(\ StringTokenizer words = new StringTokenizer(s); while (words.hasMoreTokens()) { result.append(encode(words.nextToken()) + \ } return result.toString(); } public String decrypt(String s) { StringBuffer result = new StringBuffer(\ StringTokenizer words = new StringTokenizer(s); while (words.hasMoreTokens()) { result.append(decode(words.nextToken()) + \ } return result.toString(); } public abstract String encode(String word); public abstract String decode(String word); } public class Caesar extends Cipher { public String encode(String word) { StringBuffer result = new StringBuffer(); for (int k = 0; k < word.length(); k++) { char ch = word.charAt(k); ch = (char)('a' + (ch -'a'+ 3) % 26); result.append(ch); } return result.toString(); } public String decode(String word) { StringBuffer result = new StringBuffer(); for (int k = 0; k < word.length(); k++) { char ch = word.charAt(k); ch = (char)('a' + (ch - 'a' + 23) % 26); result.append(ch); } return result.toString(); } } public class Transpose extends Cipher { public String encode(String word) { StringBuffer result = new StringBuffer(word); return result.reverse().toString(); 第7页
} public String decode(String word) { return encode(word); } } public class TestEncrypt { public static void main(String argv[]) { Caesar caesar = new Caesar(); //Cipher ci =new Cipher(); String plain = \ String secret = caesar.encrypt(plain); System.out.println(\ System.out.println(\ System.out.println(\ System.out.println(\ Transpose transpose = new Transpose(); secret = transpose.encrypt(plain); System.out.println(\ System.out.println(\ System.out.println(\ System.out.println(\ } } 四.实验结果 实验一的运行结果如下: 实验二的运行结果如下: 实验三的运行结果如下: 第8页
实验四的运行结果如下: 五.试验中遇到的问题及体会 在实验过程中的确遇到过很多问题,不像前几章那样较容易实现各功能,比如在做实验一时开始的时候各个计算的相关算法确实不容易想,后来在与同学进行探讨后才勉强实现实验要求,还有在做第三题时我还分不清类与接口之间的区别与联系,不过经过仔细看书,发现理解起来也不是很困难,至于题中的算法还相对比较容易实现,另外在实验四中一开始案例一的设计都没看懂,最后在别人的帮助下才得以完成实验内容要求。 经过这次实验我发现自己在知识方面还有很多的不足,在平时的练习中也不懂得扩展与举一反三,导致这次的实验做得马马虎虎,除此之外也体会到了面向对象程序设计相对于过程化程序设计的优越性,总之,语言是一门技巧,要想能熟练掌握,必须多加练习。
第9页
太原理工大学学生实验报告
学院名称 学生姓名 课程名称 计算机科学与技术 专业班级 实验日期 2011.10 学号 成绩 Java异常处理 Java语言程序设计 实验题目 一.实验目的 掌握异常的概念,以及如何定义,抛出和捕获处理异常 二.实验内容 1.做实验之前要求思考一下问题,作为预习内容: (1)错误和异常有何区别?查看java异常类的关系图。 (2)异常是如何抛出,捕获和处理的? (3)java捕获和处理异常的结构是怎样的? (4)try语句如何嵌套?throw语句有何作用?finally程序块的作用是什么? 2.运行下面的程序,理解异常的抛出,捕获与处理。 import java.io.*; public class ExceptionTest { public static void main(String args[]){ for(int i=0;i<4;i++){ int k; try{ switch(i){ case 0://divided by zero int zero=0; k=911/zero; break; case 1://null pointer int b[]=null; k=b[0]; break; case 2://array index out of bound int c[]=new int[2]; k=c[9]; break; case 3://string index out of bound char ch=\ break; } }catch(Exception e){ System.out.println(\ System.out.println(e); } } 实验地点 多学科楼六层606室 第10页
指导教师 相洁