教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 文库大全 > 资格考试 >

Java2实用教程课后答案 第三版 耿祥义 张跃平版(3)

来源:网络收集 时间:2026-08-26
导读: { MyException exception=new MyException(m); throw exception; } else System.out.println(m); } } public class Test { public static void main(String agrs[]) { int m; Student stu1=new Student(); m=987; t

{

MyException exception=new MyException(m);

throw exception;

}

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<b)

{

int temp=0;

temp=a;

java2 耿祥义 张跃平版 课后答案

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("18和102的最大公约数是:"+a.f(18,102));

B b=new B();

System.out.println("18和102的最小公倍数是:"+b.f(18,102));

}

}

第5章 字符串

1. 使用String类的public String toUpperCase()方法可以将一个字符串中的小写字母变成大写字母,使用public String toLowerCase()方法可以将一个字符串中的大写字母变成小写字母。编写一个程序,使用这两个方法实现大小写的转换。

答: class Test

{

public static void main(String args[])

{

java2 耿祥义 张跃平版 课后答案

String str="I can use Java";

System.out.println("要转换的字符串是:"+str);

String s=str.toUpperCase();

System.out.println("转换成大写字符串是:"+s);

s=str.toLowerCase();

System.out.println("转换成小写字符串是:"+s);

}

}

2. 使用String类的public String concat(String str)方法可以把调用该方法的字符串与参数指定的字符串连接,把str指定的串连接到当前串的尾部获得一个新的串。编写一个程序通过连接两个串得到一个新串,并输出这个新串。

答: class Test

{

public static void main(String args[])

{

String str1="I can u";

String str2="se Java";

String s=str1.concat(str2);

System.out.println("将字符串"+str1+"与字符串"+str2+"连接后得到的新字符串是:");

System.out.println(s);

}

}

3. String类的public char charAt(int index)方法可以得到当前字符串index位置上的一个字符。说出下列程序的输出结果。

public class E3

{

public static void main(String args[])

{

String s="中国科学技术大学";

char a=s.charAt(2),b=s.charAt(6);

System.out.print(a);

System.out.println(b);

}

}

答: 科大

4. 使用java.util包中的Arrays类的静态方法public static void sort(double a[])可以把参数a指定的double型数组按升序排序,使用java.util包中的Arrays类的静态方法public static void sort(double a[],int start,int end)可以把参数a指定的double型数组中从位置start到end-1位置的数按升序排序。写出下列程序的输出结果。

import java.util.*;

public class E4

java2 耿祥义 张跃平版 课后答案

{

public static void main(String args[])

{

int a[]={23,67,89,90,-987};

double b[]={12.89,90.87,34,678.987,-98.78,0.89};

Arrays.sort(a);

Arrays.sort(b,1,4);

for(int i=0;i<=4;i++)

{

System.out.print(a[i]+",");

}

for(int i=0;i<b.length;i++)

{

System.out.print(b[i]+",");

}

}

}

答: -987,23,67,89,90,12.89,34.0,90.87,678.987,-98.78,0.89,

5. 使用ng包中System类的静态方法arraycopy可以实现数组的快速复制,上机实习下列程序,并总结出arraycopy方法参数的使用规则。

public class ArrayCopy

{

public static void main(String args[])

{

char a1[]={'a','b','c','d','e','f'},b1[]={'1','2','3','4','5','6'};

System.arraycopy(a1,0,b1,1,a1.length-1);

System.out.println(new String(a1));

System.out.println(new String(b1));

byte a2[]={97,98,99,100,101,102},b2[]={65,67,68,69,70,71};

System.arraycopy(b2,0,a2,3,b2.length-3);

System.out.println(new String(a2));

System.out.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 - 从第几个元素开始粘贴

java2 耿祥义 张跃平版 课后答案

length - 一共需要复制的元素个数

第6章 时间、日期和数字

1. 用Data类不带参数的构造方法创建日期,要求日期的输出格式是:星期 小时 分 秒。

答: import java.util.*;

import java.text.*;

class Test

{

public static void main(String args[])

{

Date 时间=new Date();

SimpleDateFormat s=new SimpleDateFormat("E HH时 mm分 ss秒");

System.out.println(s.format(时间));

}

}

2. 输出2006年2月的日历页,程序需处理闰年问题。

答: import java.util.*;

class Test

{

public static void main(String args[])

{

int year=2006,month=2;

int allDay;

if((year%4==0&&year%100!=0)||(year%400==0))

allDay=29;

else

allDay=28;

Calendar feb=Calendar.getInstance();

feb.set(2006,1,1);

int dat=feb.get(Calendar.DAY_OF_WEEK)-1;

String a[]=new String[dat+allDay];

for(int i=0; …… 此处隐藏:3039字,全部文档内容请下载后查看。喜欢就下载吧 ……

Java2实用教程课后答案 第三版 耿祥义 张跃平版(3).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wenku/104999.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)