java上机实验答案与解析(2)
Animal类的子类,统计鱼的数量 count,获得鱼数量的方法 getCount()。定义Tiger类,是Animal类的子类,统计老虎的数量 count,获得老虎数量的方法 getCount()。定义SouthEastTiger类,是Tiger类的子类,统计老虎的数量 count,获得老虎数量的方法 getCount()。 public class Animal { String name; int legs; static int count; Animal(){count++;} void setLegs(int legs){ this.legs=legs; } int getLegs(){ return legs; } void setKind(String name){ this.name=name; } String getKind(){ return name; } int getCount(){ return count; } }
public class Fish extends Animal{
static int countFish; Fish(){countFish++;}
int getCount(){ return countFish; } }
public class Tiger extends Animal{
static int countTiger; Tiger(){countTiger++;}
int getCount(){ return countTiger; } }
public class SouthEastTiger extends Tiger{}
public class A { public static void main(String args[]){ Fish f=new Fish(); System.out.println(f.getCount()); Tiger t=new Tiger(); System.out.println(t.getCount()); SouthEastTiger st=new SouthEastTiger(); System.out.println(st.getCount()); } }
实验四 异常处理
1.建立exception包,编写TestException.java程序,主方法中有以下代码,确定其中可能出现的异常,进行捕获处理。
for(inti=0;i<4;i++){ intk; switch(i){ case0:intzero=0;k=911/zero;break; case1:intb[]=null;k?=b[0];break; case2:int c[]=new int[2];k=c[9];break; case3:charch=\ } }
package Exception;
public class TestException { public static void main(String[] args){ for(int i=0;i<4;i++){ try{ int k; switch(i){ case 0: int zero=0;k=911/zero;break; case 1: int b[]=null;k=b[0];break; case 2: int c[]=new int[2];k=c[9];break; case 3: char ch=\ } } catch(ArithmeticException e){ System.out.println(e.getMessage()); } catch(java.lang.NullPointerException e){ System.out.println(e.getMessage()); }
catch(ArrayIndexOutOfBoundsException e){ System.out.println(e.getMessage()); } catch(java.lang.StringIndexOutOfBoundsException e){ System.out.println(e.getMessage()); } } } }
运行结果: / by zero null 9
String index out of range: 99
2.建立exception包,建立Bank类,类中有变量double balance表示存款,Bank类的构造方法能增加存款,Bank类中有取款的发方法withDrawal(double dAmount),当取款的数额大于存款时,抛出InsufficientFundsException,取款数额为负数,抛出NagativeFundsException,如new Bank(100),表示存入银行100元,当用方法withdrawal(150),withdrawal(-15)时会抛出自定义异常。 提示: InsufficientFundsException,NagativeFundsException为自定义的类,分别产生余额不足异常和取款为负数异常,需继承Exception类。
通过输出结果了解java异常的产生,并将该Java程序放在exception包中。 package Exception;//注解先建立一个package,然后在包中建立各种类 public class InsufficientFundsException extends Exception{ String s1; InsufficientFundsException(String t){ s1=t; }
public String getMassage1(){ return s1; } }
package Exception;
public class NagativeFundsException extends Exception{ String s; NagativeFundsException(String t){ s=t; }
public String getMassage(){
return s; } }
package Exception;
public class Bank extends Exception{ static double ba=0; Bank(double r){ ba=ba+r; } void withDrawal(double dAmount) InsufficientFundsException,NagativeFundsException{ if(dAmount>ba) throw new InsufficientFundsException(\取款余额不足\ else if(dAmount<0) throw new NagativeFundsException(\取款为负数\ else System.out.print(\银行里还剩余:\ } }
package Exception; import java.util.*; public class A{
public static void main(String args[] ){ Bank b=new Bank(100); Scanner sc=new Scanner(System.in); try { System.out.println(\请输入一个数\ b.withDrawal(sc.nextInt()); } catch(InsufficientFundsException e){ System.out.println(e.getMassage1()); } catch(NagativeFundsException e){ System.out.println(e.getMassage()); } } }
运行结果: 请输入一个数 80
银行里还剩余:20.0
实验五 输入输出
throws
1.编写TextRw.java的Java应用程序,程序完成的功能是:首先向TextRw.txt中写入自己的学号和姓名,读取TextRw.txt中信息并将其显示在屏幕上。 import java.io.*;
public class TextRw {
public static void main(String[] args) throws IOException { File f=new File(\ FileWriter fw=new FileWriter(f); fw.write(\学号 姓名\ fw.close(); FileReader fr=new FileReader(f); int i; while((i=fr.read())!=-1) System.out.print((char)i); fr.close(); } }
2.编写IoDemo.java的Java应用程序,程序完成的功能是:首先读取IoDemo.java源程序文件,再通过键盘输入文件的名称为iodemo.txt,把IoDemo.java的源程序存入 import java.io.*;
public class TextRw {
public static void main(String[] args) throws IOException{ String f=\
FileReader r=new FileReader(f); int i;
while((i=r.read())!=-1){ System.out.print((char)i); }
r.close();
BufferedReader w=new BufferedReader(new InputStreamReader(System.in)); System.out.print(\请目录和输入文本名\ String f1=w.readLine();
FileWriter w1=new FileWriter(f1); FileReader r1=new FileReader(f); int j;
while((j=r1.read())!=-1){ w1.write((char)j); }
w1.close();
…… 此处隐藏:1853字,全部文档内容请下载后查看。喜欢就下载吧 ……相关推荐:
- [实用模板]第八章:法国“新浪潮”与“左岸派”
- [实用模板]2021年北京上半年临床医学检验技师生物
- [实用模板]SAP GUI 7.10客户端安装配置文档
- [实用模板]2001年临床执业医师资格考试综合笔试试
- [实用模板]36机场工作实用英语词汇总结
- [实用模板](一)社会保险稽核通知书
- [实用模板]安全教育主题班会材料
- [实用模板]濉溪县春季呼吸道传染病防控应急演练方
- [实用模板]长沙房地产市场周报(1.30-2.3)
- [实用模板]六年级数学上册典中点 - 图文
- [实用模板]C程序设计(红皮书)习题官方参考答案
- [实用模板]中国证监会第一届创业板发行审核委员会
- [实用模板]桥梁工程复习题
- [实用模板]2011学而思数学及答案
- [实用模板]初中病句修改专项练习
- [实用模板]监理学习知识1 - 图文
- [实用模板]小机灵杯四年级试题
- [实用模板]国贸专业毕业论文模板
- [实用模板]教育学概论考试练习题-判断题4
- [实用模板]2015届高考英语一轮复习精品资料(译林
- 00Nkmhe_市场营销学工商管理_电子商务_
- 事业单位考试法律常识
- 诚信教育实施方案
- 吉大小天鹅食品安全检测箱方案(高中低
- 房地产销售培训资料
- 高一地理必修1复习提纲
- 新概念英语第二册lesson_1_练习题
- 证券公司内部培训资料
- 小学英语时间介词专项练习
- 新世纪英语专业综合教程(第二版)第1册U
- 【新课标】浙教版最新2018年八年级数学
- 工程建设管理纲要
- 外研版 必修一Module 4 A Social Surve
- Adobe认证考试 AE复习资料
- 基于H.264AVC与AVS标准的帧内预测技术
- 《食品检验机构资质认定管理办法》(质
- ABB变频器培训课件
- (完整版)小学说明文阅读练习题及答案
- 深思洛克(SenseLock) 深思IV,深思4,深
- 弟子规全文带拼音




