java实验2实验报告.doc
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
实验指导书 实验名称:包、接口与异常处理 学时安排:2 实验类别:综合设计型实验 实验要求:1人1组  ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ 一、实验目的 1、掌握java字符串处理方法。 2、掌握java面向对象的概念。 3、掌握java自定义异常和处理。 4、 通过实验,对Java程序的开发有一个深入了解。 二、实验内容 1.阅读并分析程序TestException.java。(见附件PPT文件) 2.设计一个程序,其功能是从命令行输入整数字符串,再将该整数字符串转换为整数, 输入的数据可能具有以下格式:12345、123 45、123xyz456 3.编写一个四则运算程序,要求用户输入一个x值和一个y值,以及一个四则运算符。如 果用户输入正确,则给出运算结果,如果输入错误,则给出出错警告。(利用异常处理 编程,尽量使用java现成的异常类,如没有可自定义异常类 ) 三、程序代码 1. public class TestException { public TestException() { } boolean testEx() throws Exception{ boolean ret = true; try{ ret = testEx1(); }catch (Exception e){ System.out.println("testEx, catch exception"); ret = false; throw e; } finally{ System.out.println("testEx, finally; return value="+ret); return ret; } } boolean testEx1() throws Exception{ boolean ret = true; try{ ret = testEx2(); if (!ret){ return false; } System.out.println("testEx1, at the end of try"); return ret; }catch (Exception e){ System.out.println("testEx1, catch exception"); ret = false; throw e; } finally{ System.out.println("testEx1, finally; return value="+ret); return ret; } } boolean testEx2() throws Exception{ boolean ret = true; try{ int b=12; int c; for (int i=2;i>=-2;i--){ c=b/i; System.out.println("i="+i); } return true; }catch (Exception e){ System.out.println("testEx2, catch exception"); ret = false; throw e; } finally{ System.out.println("testEx2, finally; return value="+ret); return ret; } } public static void main(String[] args) { TestException testException1 = new TestException(); try{ testException1.testEx(); }catch(Exception e){ e.printStackTrace(); } } } 2. import java.util.*; public class test { public static void main(String args[]) { int i, number=0; String str; try { for(i=0;i<args.length;i++) { str=args[i]; number=Integer.parseInt(str); System.out.printf("第%d个字符转换为整数后:%d\n",i+1,number); } } catch(NumberFormatException e) { System.out.println("输入字符串不正确!"); } } } 3. import java.io.*; import java.util.*; public class ExceptionExam { void addition(double x,double y) //加法运算
- 粉丝: 167
- 资源: 3万+
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 最全空间计量实证方法(空间杜宾模型和检验以及结果解释文档).txt
- 5uonly.apk
- 蓝桥杯Python组的历年真题
- 2023-04-06-项目笔记 - 第一百十九阶段 - 4.4.2.117全局变量的作用域-117 -2024.04.30
- 2023-04-06-项目笔记 - 第一百十九阶段 - 4.4.2.117全局变量的作用域-117 -2024.04.30
- 前端开发技术实验报告:内含4四实验&实验报告
- Highlight Plus v20.0.1
- 林周瑜-论文.docx
- 基于MIC+NE555光敏电阻的声光控电路Multisim仿真原理图
- 基于JSP毕业设计-基于WEB操作系统课程教学网站的设计与实现(源代码+论文).zip