else
System.out.println(m); } }
public class Test {
public static void main(String agrs[]) { int m;
Student stu1=new Student(); m=987; try {
stu1.speak(m); m=1234; stu1.speak(m); }
catch(MyException e) {
e.showStr1(); } } }
18.编写一个类,该类有一个方法public int f(int a,int b),该方法返回a 和b 的最大公约数。然后再 编写一个该类的子类,要求子类重写方法f,而且重写的方法将返回a 和b 的最小公倍数。要求在重写 的方法的方法体中首先调用被隐藏的方法返回a 和b 的最大公约数m,然后将乘积(a*b)/ m 返回。要求 在应用程序的主类中分别使用父类和子类创建对象,并分别调用方法f 计算两个正整数的最大公约数 和最小公倍数。 答: class A {
public int f(int a,int b) { if(a
int temp=0; temp=a; a=b; b=temp;
}
int r=a%b; while(r!=0) { a=b; b=r; r=a%b; } return b; } }
class B extends A {
public int f(int a,int b) { int m;
m=super.f(a,b); return (a*b)/m; } }
public class Test {
public static void main(String args[]) {
A a=new A();
System.out.println(\和102 的最大公约数是:\B b=new B();
System.out.println(\和102 的最小公倍数是:\} }
第5 章字符串
1. 使用Strin g 类的public String toUpperCase ()方法可以将一个字符串中的小写字母变成大写字母, 使用public String toLowerCase ()方法可以将一个字符串中的大写字母变成小写字母。编写一个程 序,使用这两个方法实现大小写的转换。 答: class Test {
public static void main(String args[])
{
String str=\
System.out.println(\要转换的字符串是:\String s=str.toUpperCase();
System.out.println(\转换成大写字符串是:\s=str.toLowerCase();
System.out.println(\转换成小写字符串是:\} }
2. 使用Strin g类的public String concat(Stri ng str)方法可以把调用该方法的字符串与参数指定的字
符串连接,把st r 指定的串连接到当前串的尾部获得一个新的串。编写一个程序通过连接两个串得到 一个新串,并输出这个新串。 答: class Test {
public static void main(String args[]) {
String str1=\String str2=\String s=str1.concat(str2);
System.out.println(\将字符串\与字符串\连接后得到的新字符串是:\System.out.println(s); } }
3. Strin g 类的public char charAt(int index)方法可以得到当前字符串inde x 位置上的一个字符。说
出下列程序的输出结果。 public class E3 {
public static void main(Strin g args[]) {
String s=\中国科学技术大学\char a=s.charAt( 2),b=s.charAt(6); System.ou t.print(a); System.ou t.println(b); } } 答: 科大
4. 使用java.uti l 包中的Array s 类的静态方法public static void sort(double a[])可以把参数a 指
定的doubl e 型数组按升序排序,使用java.uti l 包中的Array s 类的静态方法public static void sort(doubl e a[],int start,int end)可以把参数a 指定的doubl e 型数组中从位置star t 到end- 1 位
置的数按升序排序。写出下列程序的输出结果。 import java.util. *; public class E4 {
public static void main(Strin g args[]) {
int a[]={23,67, 89,90,-987};
double b[]={12.89 ,90.87,34,678.987,-98. 78,0.89}; Arrays.sor t(a); Arrays.sor t(b,1,4); for(int i=0;i<=4;i+ +) {
System.ou t.print(a[i]+\}
for(int i=0;i System.ou t.print(b[i]+\} } } 答: -987,23,67,89,90,12.89,34.0,90.87,678.987,-98.78,0.89, 5. 使用java.lan g 包中Syste m 类的静态方法arraycop y 可以实现数组的快速复制,上机实习下列程序, 并总结出arraycop y 方法参数的使用规则。 public class ArrayCopy { public static void main(Strin g args[]) { char a1[]={'a',' b','c','d','e','f'},b 1[]={'1','2','3','4',' 5','6'}; System.arr aycopy(a1,0,b1,1,a1.le ngth-1); System.ou t.println(new String(a1)) ; System.ou t.println(new String(b1)) ; byte a2[]={97,98 ,99,100,101,102},b2[] ={65,67,68,69,70,71}; System.arr aycopy(b2,0,a2,3,b2.le ngth-3); System.ou t.println(new String(a2)) ; System.ou t.println(new String(b2)) ; } } 答:①运行结果:abcdef 1abcde abcACD ACDEFG ②arraycopy 的方法是public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)。其中五个参数分别表示: src - 被复制的数组 srcPos - 从第几个元素开始复制 dest - 要复制到的数组 destPos - 从第几个元素开始粘贴 length - 一共需要复制的元素个数 第6 章时间、日期和数字 1. 用Dat a 类不带参数的构造方法创建日期,要求日期的输出格式是:星期小时分秒。 答: import java.util.*; import java.text.*; class Test { public static void main(String args[]) { Date 时间=new Date(); SimpleDateFormat s=new SimpleDateFormat(\时mm 分ss 秒\System.out.println(s.format(时间)); } } 2. 输出200 6 年2 月的日历页,程序需处理闰年问题。 答: import java.util.*; class Test { public static void main(String args[]) { int year=2006,month=2; int allDay; 百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库《Java2实用教程》课后习题参考答案(4)在线全文阅读。
相关推荐: