已阅读5页,还剩2页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
任课教师:宋瑾钰任课教师:宋瑾钰 JAVA 程序设计程序设计 (2011-2012 学年第学年第 2 学期)学期) 实实 验验 报报 告告 学号:学号:E10700217 姓名:廖远东姓名:廖远东 班级:数字媒体班级:数字媒体 2 班班 浙江理工大学信息学院 实验指导书 实验名称:包、接口与异常处理 学时安排:2 实验类别:综合设计型实验 实验要求:1 人 1 组 一、实验目的 1、掌握 java 字符串处理方法。 2、掌握 java 面向对象的概念。 3、掌握 java 自定义异常和处理。 4、 通过实验,对 Java 程序的开发有一个深入了解。 二、实验内容 1.阅读并分析程序 TestException.java。 (见附件 PPT 文件) 2.习题 2 编程 。 (见 P85) 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.Scanner; /import java.util.Input public class experience2 public static void main(String args) int year = 0; try Scanner scan = new Scanner(System.in); year = scan.nextInt(); /System.out.printf(“%dn“,year); catch(Exception e) System.out.println(“ERROR“); System.out.printf(“%dn“,year); 3.package friddle.test; import java.util.Scanner; public class experience3 public static void main(String args) boolean run=true; while (run) double num1,num2,result; num1=num2=result=0; String function=new String(); try System.out.println(“please input the nmber1“); Scanner scan = new Scanner(System.in); num1 = scan.nextDouble(); System.out.println(“please input the nmber2“); scan = new Scanner(System.in); num2 = scan.nextDouble(); catch(Exception e) System.out.println(“the put number is wrong;“); try System.out.println(“please input you function“); Scanner scan=new Scanner(System.in); function=scan.next(); catch(Exception e) System.out.println(“the put number is wrong;“); if(function.equals(“+“)result=num1+num2; if(function.equals(“-“)result=num1-num2; if(function.equals(“*“)result=num1*num2; if(function.equals(“/“)result=num1/num2; System.out.printf(“%fn“,result); System.out.println(“continue? Y/N“); Scanner right=new
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 5月《界面设计职业技能等级证书》试卷试题及答案
- Lesson 1说课稿-2025年小学英语一年级下册人教版(新起点)
- 基因工程期末试题及答案
- 雨课堂学堂在线学堂云《Introduction to World's Famous Spirits(暨南 )》单元测试考核答案
- 4.19 北魏政治与北方民族大交融 说课稿 2023-2024学年部编版七年级历史上册
- 多旋翼植保机无人机真题及答案5
- 2025年上海市安全员A证考试练习题及模拟考试
- Unit 4 Entertainment说课稿-2025年高中英语高一上册牛津上海版(试用本)
- 七年级英语上册 Unit 9 My favorite subject is science Section B 1a-2c说课稿(新版)人教新目标版
- 中级会计考试试题题库
- 《锅炉定期排污》课件
- GB/T 5072-2023耐火材料常温耐压强度试验方法
- 都是亲人(小品)
- 科研伦理与学术规范-课后作业答案
- 11《答谢中书书》知识点整理
- 2009-2022历年广东省航道事务中心所属事业单位招聘真题带答案详解2023上岸甄选资料
- 进食障碍-课件
- 四川省2023年普通高等学校高职教育单独招生文化考试(普高类)数学试题【含答案】
- 基于BIM基数的机电安装工程降本提质增效
- 《在你们离开以前》读书笔记PPT模板思维导图下载
- 原发性肝癌放疗进展-门脉癌栓放疗
评论
0/150
提交评论