教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 精品文档 > 实用模板 >

java上机实验答案与解析(2)

来源:网络收集 时间:2026-08-23
导读: Animal类的子类,统计鱼的数量 count,获得鱼数量的方法 getCount()。定义Tiger类,是Animal类的子类,统计老虎的数量 count,获得老虎数量的方法 getCount()。定义SouthEastTiger类,是Tiger类的子类,统计老虎的数

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字,全部文档内容请下载后查看。喜欢就下载吧 ……
java上机实验答案与解析(2).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wendang/454636.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)