“JAVA语言程序设计”.doc
Four short words sum up what has lifted most successful individuals above the crowd: a little bit more.-author-date“JAVA语言程序设计”“JAVA语言程序设计”“JAVA程序设计”作业答案l 第1章概述,习题1-4,并会编译和运行第1讲课件中的程序;习题1-4将本章例题中的程序输入到源程序文件Jex1_1.java,在命令行方式下编译运行。答案:public class Jex1_1 public static void main(String args) int m= 25;System.out.println(m + “平方的输出是” + m*m);编译: c:javac Jex1_1.java运行: c:java Jex1_125平方的输出是625l 第2章基本语法,习题2-1、2-2、2-3、2-5、2-11、2-12,并会编译和运行第2讲课件中的程序;习题2-1 下列常量的表示在Java中是否合法?若不合法,指出原因;若合法,则指出常量的数据类型。3276735u1.25e3f3l0.0086e-3287“Computer System”“a”au6118-0e23.5-.567答案:不合法35u,87,9645,e23int类型32767,float类型1.25e3f,long类型3l,double类型0.0086e-32,String类型“Computer System”、“a”char类型a和u6118int类型-0,double类型.5和-.567习题2-2 叙述标识符的定义。指出下列用户自定义的标识符中哪些是合法的,那些是非法的?为什么?abcEnglish_2xyx$yif$Elseb(3)defChine_bb3byAbsFloatfloat答案:合法的标识符abc,English,_2xy,x$y,if$,Else,Chine_bb,b3y,AbsFloat非法的标识符b(3),def, 3by,float习题2-3 设有变量说明语句:int a = 7;boolean b,e;char c, d;float x = 5.2f, y, z;求出下列表达式的值:(1) b = 5>14|x>25;(2) e = !(a < x);(3) c = a + 5;(4) y = x + a % 3 + x/2;(5) d = u0030 + 7;(6) z = a/2*2;答案:public class AS2_3 public static void main(String args) int a = 7;boolean b,e;char c, d;float x = 5.2f, y, z;b = 5>14|x>25;e = !(a < x);c = a + 5;y = x + a % 3 + x/2;d = u0030 + 7;z = a/2*2;System.out.println(“b=” + b);System.out.println(“e=” + e);System.out.println(“c=” + c);System.out.println(“y=” + y);System.out.println(“d=” + d);System.out.println(“z=” + z);编译:c:javac AS2_3.java运行:c:java AS2_3b=falsee=truec=fy=8.799999d=7z=6.0习题2-5 单精度浮点数的表示范围是多少?双精度浮点数的表示范围是多少?整型变量的表示范围呢?长整型的表示范围是否少?字符型变量的长度是多少?答案:整数的表示范围:-2147483648,2147483647长整数的表示范围:-9223372036854775808,9223372036854775807单精度浮点数的表示范围:1.4E-45,3.4028235E38双精度浮点数的表示范围:4.9E-324,1.7976931348623157E308字符型变量的长度是2个字节/16位习题2-11 设初始值a=5, b=3, c=0, d=2指出运算下列表达式后a,b,c,d的值。a *= a%= bc = b/= ad += b -= aa += b += a*= bc = a += b+= ad = (c = a/b + 15)答案:class Jex2_11 public static void main(String args)int a = 5, b = 3, c = 0, d = 2;a *= a%= b;c = b/= a;d += b -= a; a += b += a*= b;c = a += b+= a;d = (c = a/b + 15);System.out.println(a+" "+b+" "+c+" "+d);编译:c:javac Jex2_11.java运行:c:java Jex2_11习题2_12 写出程序运行结果。class jex2_12 public static void main(String args) int a, b, c, d, m =10, n =8;a = m+ + n+;System.out.println("a=" + a);b = m+ + +n;System.out.println("b=" + b);c = +m + +n;System.out.println("c=" + c);d = m- + n+;System.out.println("d=" + d);编译:c:javac jex2_12.java运行:c:java jex2_12a=18b=21c=24d=24l 第3章流程控制,习题3-2、3-3、4-1、4-3、4-4、4-9、4-10,并会编译和运行第3讲课件中的程序;习题3-2 简述print方法和println方法的区别。这两种方法有何共同之处?答案:print方法无“回车/换行”,而println方法则产生“回车/换行”,下一个输出则另起一行。print和println方法可以打印boolean、char、double、float、byte、short、int、long、String等数据类型。习题3-3 编写一个程序,从键盘接收一个十六进制的两位数,并将其转换为对应的ASCII字符在显示器上输出。提示:字符a的十六进制的ASCII码为0x61,ASCII码字符表参见;利用System.in.read()方法从键盘接收两位数。答案:public class Jex3_3 public static void main(String argsp)throws ExceptionSystem.out.println("请输入ASCII码的16进制表示(如'a'的十六进制表示为61):");int hi = System.in.read();hi = (hi>='a')?hi-'a'+10:hi-'0'int lo = System.in.read();lo = (lo>='a')?lo-'a'+10:lo-'0'char c = (char)(hi<<4|lo);System.out.println("该ASCII码对应的字符是:"+c);编译:c:javac Jex3_3.java运行:c:java Jex3_3请输入ASCII码的16进制表示(如'a'的十六进制表示为61):77该ASCII码对应的字符是:w习题4-1 用if-else语句判断从键盘输入字符,要求能够区分字符a,b,c和其他字符,再用switch语句重写上述程序。答案:import java.io.IOException;public class AS4_1 public static void main(String args) throws IOException System.out.println("请输入一个字符");char c = (char)System.in.read();if (c = 'a')System.out.println("输入为a");else if (c = 'b')System.out.println("输入为b");else if (c = 'c')System.out.println("输入为c");else System.out.println("输入为其他字符");编译:c:javac AS4_1.java运行:c:>java AS4_1请输入一个字符a输入为ac:>java AS4_1请输入一个字符b输入为bc:>java AS4_1请输入一个字符c输入为cc:>java AS4_1请输入一个字符h输入为其他字符import java.io.IOException;public class AS4_1 public static void main(String args) throws IOException System.out.println("请输入一个字符");char c = (char)System.in.read();switch (c) case 'a':System.out.println("输入为a");break;case 'b':System.out.println("输入为b");break;case 'c':System.out.println("输入为c");break;default:System.out.println("输入为其他字符");习题4-3 编写一个程序,输出三个数中最大的数。答案:public class AS4_3 public static void main(String args) int a = 789;int b = 34;int c = 78;int max;if (a >= b) max = a;else max = b;if(max < c )max = c;System.out.println(max);编译:c:javac AS4_3.java运行:c:java AS4_3789习题4-4 从键盘输入一个4位数,求出各位数字之和。答案:import java.io.IOException;public class AS4_4 public static void main(String args) throws IOException System.out.println("请输入一个4位数:");byte a = new byte10;int total = 0;int len = System.in.read(a);for(int i = 0; i < len - 2; i+)total +=ai - '0'System.out.println("和为" + total);编译:c:javac AS4_4.java运行:c:java AS4_4请输入一个4位数:1356和为15习题4-9 编程:给定出生日,计算与此对应的是星期几。答案:public class Weekly public static void main(String args) if (args.length != 3) System.out.println("Usage: java Weekly 2004 11 14");System.exit(-1);int year = Integer.parseInt(args0);int month = Integer.parseInt(args1);int day = Integer.parseInt(args2);long difY = difYear(year);long difM = difMonth(year, month);long difD = day - 1;long total = difY + difM + difD;int w = (int)(total%7 + 1);switch(w) case 1:System.out.println("星期一");break;case 2:System.out.println("星期二");break;case 3:System.out.println("星期三");break;case 4:System.out.println("星期四");break;case 5:System.out.println("星期五");break;case 6:System.out.println("星期六");break;case 7:System.out.println("星期日");break;public static long difYear(int y) long t = 0;int start = 1900;for (int i = start; i < y; i+) if(i %4 =0 && i%100!=0) | i%400=0) t = t + 366;elset = t + 365;return t;public static long difMonth(int y, int m) long t = 0;int start = 1;int numDays = 0;for (int i = start; i < m; i+) switch (i) case 1:case 3:case 5:case 7:case 8:case 10:case 12:numDays = 31; break;case 4: case 6: case 9:case 11:numDays = 30; break;case 2:if(y % 4 = 0) && !(y % 100 = 0) | (y % 400 = 0) )numDays = 29;elsenumDays = 28;break;t = t + numDays;return t;编译:c:>javac Weekly.java运行:c:>java Weekly 2004 11 15星期一习题4-10 按字母表顺序和逆序每隔一个字母打印,即打印出:a c e g i k m o q s u w y z x v t r p n l j h f d b答案:public class AS4_10 public static void main(String args) char c1 = 'a'char c2 = 'z'System.out.print(c1 + " ");for (int i = 0; i < 12; i+) c1 = (char)(c1 + 2);System.out.print(c1 + " ");System.out.print(c2 + " ");for (int j = 0; j < 12; j+) c2 = (char)(c2 - 2);System.out.print(c2 + " ");编译:c:javac AS4_10.java运行:c:java AS4_10a c e g i k m o q s u w y z x v t r p n l j h f d bl 第4章数组,习题5-1、5-5、5-8、5-12,并会编译和运行第4讲课件中的程序;习题5-1 写出完成下列任务的语句或程序段。(1) 声明并创建具有10个字符型元素的数组;(2) 声明并创建具有7个整型元素的数据;(3) 声明并创建具有5个整型元素的数组,用于统计一英文句子中出现小写字母a、b、c、d、e出现的次数。例如英文句子:Barack Hussein Obama became the 44th president of the United States on Tuesday before a massive crowd reveling in a moment of historical significance, and called on Americans to confront together an economic crisis that he said was caused by “our collective failure to make hard choices.”答案:(1) char c = new char10;(2) int i new int7;(3) int i = new int5;习题5-5 二分检索,已知数组a中的n个整数,按从小到大顺序排列,检索一个数是否在a中,若在,则指出其下标值,若不在,则返回值-1。例如数组1,2,7,64,165,271,若输入64,则返回其下标2;若输入70,则返回-1。答案:public class AS5_5 public static void main(String args) int i = 12, 67, 78, 234, 543, 670, 900, 1000;int idx = binarySearch(i, 543);System.out.println(idx);public static int binarySearch(int a, int key) int low = 0;int high = a.length-1;while (low <= high) int mid = (low + high) >> 1;int midVal = amid;if (midVal < key)low = mid + 1;else if (midVal > key)high = mid - 1;elsereturn mid; / key foundreturn -1; / key not found.编译:c:javac AS5_5.java运行:c:java AS5_54习题5-8 阅读下面的程序,指出运行结果。(1) public class ex5_1 public static void main(String args) boolean pins = new boolean4;for (int i = 0; i < pins.length; i +) pinsi = new booleani + 1;for (int i = 0; i < pins.length; i+) for (int k = 0; k < pinsi.length; k +) System.out.print(pinsik + ” ”);System.out.println(”);答:D:>javac ex5_1.javaD:>java ex5_1falsefalse falsefalse false falsefalse false false false(2)public class ex5_2 public static void main(String args) int i, n, k, j;int a = new int9;a0 = n = 2;i = 1;while(i < a.length) n += 1;j = (int)Math.sqrt(n);for (k = 2; k <=j; k+)if (n%k =0)break;if (i = j) ai = n;System.out.println(i + “ “ + ai);i +;n=0;for (k = 0; k <a.length; k +) System.out.print(“a“ + k +”=” + ak + “t”);n+;if(n<3)continue;System.out.println();n=0;答:D:>javac ex5_2.javaD:>java ex5_21 32 43 94 165 256 367 498 64a0=2 a1=3 a2=4a3=9 a4=16 a5=25a6=36 a7=49 a8=64(3)class ex5_3 public static void main(String args) int number = 0;if (args.length > 0)number = Integer.parseInt(args0);System.out.println(“the square root of ” + number + “ is “ + Math.sqrt(number);答:D:>javac ex5_3.javaD:>java ex5_3the square root of 0 is 0.0答案:习题5-12 设计一个程序,从文本文件中读入90个百分制的分数,按如下分段:039、4049、5059、6069、7079、8089、90100统计,并输出各分数段分数的个数以及各段分数个数占总个数的百分比。文件中的分数之间用逗号隔开如:78,90,92,85,88,87,87,73,75,81,86, 最后一个数字后面也有逗号答案:public class AS5_12 public static void main(String args)throws ExceptionInputStream fis = new FileInputStream(args0);StringBuffer sb = new StringBuffer();int stat=0,0,0,0,0,0,0;doint ch = fis.read();if(ch=-1)break;else if(ch!=',')sb.append(char)ch);elseint score = Integer.parseInt(sb.toString();if(score>=90)stat6+;else if(score>=80)stat5+;else if(score>=70)stat4+;else if(score>=60)stat3+;else if(score>=50)stat2+;else if(score>=40)stat1+;else stat0+;sb.delete(0, sb.length();while(true);fis.close();System.out.println("到分的人数:"+stat0);System.out.println("到分的人数:"+stat1);System.out.println("到分的人数:"+stat2);System.out.println("到分的人数:"+stat3);System.out.println("到分的人数:"+stat4);System.out.println("到分的人数:"+stat5);System.out.println("分以上的人数:"+stat6);编译:c:javac AS5_11.java运行:c:>java AS5_11 scorefilel 第5/6/7/8章类和对象,习题6-14、6-15、7-13、7-14、7-15,并会编译和运行第5/6/7/8讲课件中的程序;习题6-14 下列程序的输出结果是什么?(1)class ex6_1 static int x = 4, y =5;static void p(int a, int b) int x = 6;a = x + y + b;b = x + y + a;System.out.println("tx=" + x + " y=" + y + "ta=" + a + " b=" + b);public static void main(String args) int u = 2, v = 3;p(u, v);System.out.println("tu=" + u + " v=" + v + "tx=" +x + " y=" + y);编译:c:>javac ex6_1.java运行:c:>java ex6_1x=6 y=5a=14 b=25u=2 v=3x=4 y=5(2)class ex6_2 static int x=3, y =4;static int p(int a, int b) int w = a+b;a = x + y + b;b = x+y+w;System.out.println("w=" + w + " x="+x + " y=" +y + " a=" + a + " b="+ b);return b;public static void main(String args)int u = 5, v = 6;u=p(u, v) + v;System.out.println("u=" + u + " v=" + v + " x=" + x + " y=" +y);编译:c:>javac ex6_2.java运行:c:>java ex6_2w=11 x=3 y=4 a=13 b=18u=24 v=6 x=3 y=4习题6-15 设计方法sAbsMin,该方法具有3个int形参,返回3个数中绝对值最小数。答案:public static int absMin(int x, int y, int z)int min = x;min = (abs(min)>abs(y)?y:min;min = (abs(min)>abs(z)?z:min;return min;static int abs(int x)return (x>=0)?x:-x;习题7-13 定义一个接口,使它包含两个方法协议,第一个方法协议实现在两个数中求最小值,第二个方法协议实现在三个数中求绝对值最小值。答案:public interface AS7_13public int min(int x, int y);public int absMin(int x, int y, int z);习题7-14 实现上述两个方法协议的方法体。答案:public int min(int x, int y) return x > y? y:x;public int absMin(int x, int y, int z) ./参见上题习题7-15 用Java程序实现对上述两个方法协议的调用,并输出最大值和最小值。答案:public class AS7_15 implements AS7_13 public AS7_15() public int min(int x, int y) return x > y? y:x;public int absMin(int x, int y, int z) /参见上题public static void main(String args) AS7_15 as = new AS7_15();System.out.println(as.min(13, 56);System.out.println(as.absMin (34, 12, 3);编译:c:>javac AS7_15.java运行:c:>java AS7_15133l 第9章Java规范,习题8-4、8-6、8-7、8-11,并会编译和运行第8讲课件中的程序;习题8-4 怎样获得字符串的长度?请举一例来说明获取字符串长度的方法。答案:length()方法public class AS8_4 public static void main(String args)String s = "abcd"System.out.println(s.length();编译:c:>javac AS8_4.java运行:c:>java AS8_44习题8-6 字符串拷贝一共有哪几种基本方法?怎样实现将一个String对象拷贝到另一个String对象中?答案:copyValueOf()方法、getChars()方法、toCharArray()方法、substring()方法。直接赋值,程序如下:public class AS8_6 public static void main(String args)String s1 = "abcd"String s2 = "xyz"s2 = s1;System.out.println(s1);System.out.println(s2);编译:c:>javac AS8_6.java运行:c:>java AS8_6abcdabcd习题8-7 字符串比较共有几种基本方法?答案:equals()方法、equalsIgnoreCase()方法、regionMatches()方法、compareTo()方法。习题8-11 编程序实现把str串中的所有“dcba”串替换为“4321”串。答案:public class AS8_11 public static void main(String args)String s = "abcd1234dcba34bc_dcba"System.out.println(s);int i = s.indexOf("dcba");while (i != -1) s = s.substring(0, i) + "4321" + s.substring(i + 4);i = s.indexOf("dcba");System.out.println(s);编译:c:>javac AS8_11.java运行:c:>java AS8_11abcd1234dcba34bc_dcbaabcd1234432134bc_4321习题8-12 编写程序从一个文本文件(包含中文)中读取文件的内容,要求:1)用字符流和字节流两种方式读取2)统计字符和字节的个数。答案:import java.io.*;public class AS8_12 public static void main(String args)throws IOExceptionint cByte=0,cChar=0;/字节流方式InputStream is = new FileInputStream(args0);while(true)int ch = is.read();if(ch!=-1)System.out.print(char)ch);elsebreak;cByte+;is.close();System.out.println("字节数:"+cByte);/字符流方式Reader rdr = new FileReader