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