77范文网 - 专业文章范例文档资料分享平台

Java程序设计附录_实训参考答案(4)

来源:网络收集 时间:2018-11-21 下载这篇文档 手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:或QQ: 处理(尽可能给您提供完整文档),感谢您的支持与谅解。点击这里给我发消息

Java语言程序设计基础 public interface A

{ public void funa(); public int getFuna(); }

public interface B { public void funb(); public int getFunb(); }

public interface C extends A { }

public interface InterfaceExam1 extends A,B { } 2.

interface Volume {

float calculateVolume(); }

class Circle implements Volume { private float x; private float y; private float radius; Circle(float x ,float y,float radius) { this.x=x; this.y=y; this. radius=radius; } public float calculateVolume() { return((float)Math. PI*radius*radius); } }

class Rectangle implements Volume { private float width ; private float height; Rectangle(float width,float height)

274

附录 实训参考答案 { this.width=width; this.height=height; }

public float calculateVolume() {

return(width*height); } }

public class InterfaceExam2 {

public static void main(String[] args) { Circle c=new Circle(3,4,8); System.out.println(\ Rectangle r=new Rectangle(4,6); System.out.println(\olume()); } }

实训11 String类和StringBuffer类

实训内容

1.编写一个应用程序,判断两个字符串是否相同,并进一步按字典顺序比较字符串的大小。

2.编写一个应用程序,使用StringBuffer对象实现对字符串的编辑操作,包括:替换字符串中的某些字符,删除串中某些字符,在字符串中插入或尾部加入新的字符串,翻转字符串等。 参考答案

1.

class StringExample { public static void main(string arg[]) {

String s1=new String(\

String s2=new String(\ if(s1.equals(s2)) {

System.out.println(\与s2相同\;}

else {

System.out.println(\与s2不同\;}

275

Java语言程序设计基础

if(s1.compareTo(s2)) {

System.out.println(\按字典顺序,s1大于s2\;} else {

System.out.println(\按字典顺序,s1小于s2\; }

System.out.println(\

System.out.println(\System.out.println(\

}

} 2.

class StringBufferExample { public static void main(string arg[]) {

StringBuffer str=new StringBuffer(\

System.out.println(str); str.replace(3,5,\ //将str中的“DE”替换成“123”

}

}

System.out.println(str);

str.insert(2,\在str中的“C”前插入“game” System.out.println(str);

str.delete(1,3); //将str中的“BC” 删除掉 System.out.println(str);

str.append(\ //在str的尾部追加“456” System.out.println(str); str.reverse(); //将str翻转 System.out.println(str);

实训12 Math类

实训内容

编写一个应用程序,实现角度和弧度的转换、求常用函数的函数值等多种功能(利用Math类的方法) 参考答案

class Angles{

public static void main(String args[]) {

double pi=Math.PI;

276

附录 实训参考答案

}

}

double theta=120.0; double x,y; x=pi;

System.out.println(\

System.out.println(\System.out.println(\System.out.println(\

System.out.println(theta+\theta=1.312;

System.out.println(theta+\x=pi*120/180.0;

System.out.println(\System.out.println(\System.out.println(\System.out.println(\System.out.println(\System.out.println(\System.out.println(\System.out.println(\x=2.0;

System.out.println(\

System.out.println(\System.out.println(\System.out.println(\

实训13 异常处理

实验内容

1.异常类型不匹配的程序。编写一个catch 语句中声明的异常类型不匹配的程序。 2.包含多个catch 子句的异常处理程序。程序要求:在try 子句中设计通过两个catch 子句分别来捕获异常,在try块中同时设置两个异常的小程序。

3.程序要求:编写一个在try块或catch块中有return语句的程序,然后再在try块或catch块中加入System.exit()方法。

4.程序要求:编写一个描述异常层次结构由低到高的捕获顺序的小程序。

5.根据所给throws 和throw关键字的使用格式,编写一个具有声明抛弃异常的小程序。

277

Java语言程序设计基础 参考答案 1.

public class ExceptionExam2 { public static void main(String [] args) { try { int i=0; i=3/i; } catch(AarryIndexOutOfBoundsException e) { System.out.println(\异常是:\

}

finally {

System.out.println(\语句被执行\

} }

}

结论:该程序表明,如果试图捕获一个不同类型的异常,将会发生意想不到的情况。 2.

public class ExceptionExam3

{ public static void main(String [] args) { try { int i=0; i=3/i; int [] a=new int[3]; a[3]=4; } catch(ArithmeticException e) { System.out.println(\发生了异常1\

}

catch(ArithmeticException e) { System.out.println(\发生了异常2\

278

百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库Java程序设计附录_实训参考答案(4)在线全文阅读。

Java程序设计附录_实训参考答案(4).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印 下载失败或者文档不完整,请联系客服人员解决!
本文链接:https://www.77cn.com.cn/wenku/zonghe/294522.html(转载请注明文章来源)
Copyright © 2008-2022 免费范文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ: 邮箱:tiandhx2@hotmail.com
苏ICP备16052595号-18
× 注册会员免费下载(下载后可以自由复制和排版)
注册会员下载
全站内容免费自由复制
注册会员下载
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: