《Java语言程序设计(一)》课后习题答案全集 下载本文

内容发布更新时间 : 2024/4/28 13:15:23星期一 下面是文章的全部内容请认真阅读。

}

}

String out = \;

for (int i = 0; i < row; i++) { }

for (int j = 0; j < row; j++) { }

System.out.println();

out = \ + a[i][j]; if (out.length() == 1)

out = \ + out;

System.out.print(out + \);

4.5 java中的字符数组与字符串有什么区别?

答:字符数组是由类型为char的字符元素组成的,每个元素位置存储一个字符元素,对于数组名为s的数组,用char s[]表示。字符串是由0个或多个字符组成的序列,是一个对象,对于对象名为s用String s表示。

4.6 确定一个字符数组长度与确定一个String对象的长度有什么不同?

答:确定一个字符数组的长度用数组名.length,而确定一个String对象的长度用对象名.length()

4.7用toUpperCase()和toLowerCase()方法实现大小写转换

答:

public class UpperAndLowerTest {

public static void main(String args[]) {

String s1=(\

System.out.println(\未转换时的字符串s1=\

byte t1[]=s1.getBytes();//将字符串s1转换成字节数组t1

s2=s1.toUpperCase();//将字符串s1的字母全部转换成大写并保存到s2 s3=s1.toLowerCase();//将字符串s1的字母全部转换成小写并保存到s3 byte t2[]=s2.getBytes();//将字符串s2转换成字节数组t2 byte t3[]=s3.getBytes();//将字符串s1转换成字节数组t3 for(int i=0;i

if(t1[i]!=t2[i])//如果t1[i]不是大写字母而是小写字母

t1[i]=t2[i];//将小写字母转换成大写字母 else //如果是大写字母

t1[i]=t3[i];//将大写字母转换成小写字母

}

s1=new String(t1);//将字节数组t1转化成字符串保存到s1中 System.out.println(\进行字母大小写转换后的s1=\ }

26

}

4.8编写一个程序通过连接两个字符串得到一个新字符串并输出。

答:

public class ConcatTest {

public static void main(String args[]) {

String str1=\ String str2=\

String str3=str1.concat(str2); System.out.println(\ } }

4.9 声明MyDate类,功能:增加1天,增减1月和增加1年的方法;输出MyDate对象日期的方法;求两个MyDate对象日期差的方法。并提供能用当前日期初始化MyDate类对象的构造方法。

程序运行结果:

源文件:MyDate.java

import java.util.Calendar; /**

* 日历类

* @author 段智敏 */

public class MyDate { public static long YEAR_MSEL = 1000*60*60*24*365L; public static long MONTH_MSEL = 1000*60*60*24*30L; public static long DATE_MSEL = 1000*60*60*24L; public static long HOUR_MSEL = 1000*60*60L; public static long MINUTE_MSEL = 1000*60L; private int year; private int month; private int date; private int hour; private int minute; private int second; private Calendar calendar; /** * 构造方法 使用当前时间日期初始化对象 */ public MyDate() { calendar = Calendar.getInstance(); year = calendar.get(Calendar.YEAR); month = calendar.get(Calendar.MONTH) + 1; date = calendar.get(Calendar.DAY_OF_MONTH);

27

hour = calendar.get(Calendar.HOUR_OF_DAY); minute = calendar.get(Calendar.MINUTE); second = calendar.get(Calendar.SECOND); } /** * 构造方法 使用指定年、月、日初始化对象 */ public MyDate(int year, int month, int date) { set(year, month, date); } /** * 构造方法 使用指定年、月、日、时、分、秒初始化对象 */ public MyDate(int year, int month, int date, int hourOfDay, int minute, int second) { set(year, month, date, hourOfDay, minute, second); } /** * 设置日历字段 year、month 和 date 的值。 * @param year - 用来设置 YEAR 日历字段的值。 * @param month - 用来设置 month 日历字段的值。 * @param date - 用来设置 date 日历字段的值。 */ public void set(int year, int month, int date) { set(year, month, date,0,0,0); } /** * 设置日历字段 year、month 和 date 的值。 * @param year - 用来设置 YEAR 日历字段的值。 * @param month - 用来设置 month 日历字段的值。 * @param date - 用来设置 date 日历字段的值。 * @param hourOfDay - 用来设置 hour 日历字段的值。 * @param minute - 用来设置 minute 日历字段的值。 * @param second - 用来设置 second 日历字段的值。 */ public void set(int year, int month, int date, int hourOfDay, int minute, int second) { calendar = Calendar.getInstance(); calendar.set(year, month-1, date,hourOfDay,minute,second); this.year = calendar.get(Calendar.YEAR); this.month = calendar.get(Calendar.MONTH) + 1; this.date = calendar.get(Calendar.DAY_OF_MONTH); this.hour = calendar.get(Calendar.HOUR_OF_DAY); this.minute = calendar.get(Calendar.MINUTE); this.second = calendar.get(Calendar.SECOND); } /** * 增加一天 */ public void addOneDay()

28

{ date++; calendar.set(year, month, date); } /** * 增减一月 */ public void addOneMonth() { month++; calendar.set(year, month, date); } /** * 增加一年 */ public void addOneYear() { year++; calendar.set(year, month, date); } /** * 返回此时间值,以毫秒为单位。 * @return - 当前时间,以从历元至现在所经过的 UTC 毫秒数形式。 */ public long getTimeInMillis() { return calendar.getTimeInMillis(); } /** * 两个日期相差的时间 * @param md - 另一个日期对象 * @return - 相差的时间 */ public String apart(MyDate md) { long msel = this.getTimeInMillis() - md.getTimeInMillis(); msel = Math.abs(msel); boolean boo = msel>0?true:false; long year = msel/MyDate.YEAR_MSEL; long date = msel%MyDate.YEAR_MSEL/MyDate.DATE_MSEL; long hour = msel%MyDate.DATE_MSEL/MyDate.HOUR_MSEL; long minute = msel%MyDate.HOUR_MSEL/MyDate.MINUTE_MSEL; long second = msel%MyDate.MINUTE_MSEL/1000; String result = \; if( boo ) result = \已过去\; else result = \还有\; result += (year + \年\ + date+\天\ + hour + \小时\ + minute + \分钟\ + second + \秒\); return result; } /** * 返回此日历的字符串表示形式。

29

* @return - 此日历的字符串表示形式。 */ public String toString() { return year + \年\ + month + \月\ + date + \日,\ + hour + \ + minute + \ + second; } }

测试类源文件:TestMyDate.java /**

* MyDate的测试类 * @author 段智敏 */

public class TestMyDate { public static void main(String[] args) { MyDate md1 = new MyDate(); MyDate md2 = new MyDate(2014,1,30); System.out.println(\当前时间:\ + md1.toString()); System.out.println(\年除夕:\ + md2.toString()); md2.addOneDay(); System.out.println(\增加一天后,大年初一:\ + md2.toString()); } }

4.10 求前n个质数。要求确定m是否是质数,用早先求出的质数对m的整除性来确定。

/**

* 求前n个质数。

* 确定m是否是质数,用早先求出的质数对m的整除性来确定。
* @author 段智敏 */

public class Work4_10 { /** 用来存质数的数组 */ private int arrayInt[]; public Work4_10(int n) { arrayInt = new int[n]; arrayInt[0] = 2; int index = 1;//保存数组的有效长度 boolean boo = true; for (int i = 2; i < arrayInt.length; i++) { boo = true; for (int j = 0; j < index; j++) { if (i % arrayInt[j] == 0)//用已存入数组中的质数判断 boo = false; } if (boo)//如果是true 则是指数,存入数组,数组有效长度加1. {

30