《“JAVA语言程序设计”.doc》由会员分享,可在线阅读,更多相关《“JAVA语言程序设计”.doc(105页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、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;
2、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类型
3、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 设有变量说
4、明语句:int a = 7;boolean b,e;char c, d;float x = 5.2f, y, z;求出下列表达式的值:(1) b = 514|x25;(2) e = !(a 14|x25;e = !(a =a)?hi-a+10:hi-0;int lo = System.in.read();lo = (lo=a)?lo-a+10:lo-0;char c = (char)(hijava AS4_1请输入一个字符a输入为ac:java AS4_1请输入一个字符b输入为bc:java AS4_1请输入一个字符c输入为cc:java AS4_1请输入一个字符h输入为其他字符import
5、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.p
6、rintln(输入为其他字符);习题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.IO
7、Exception;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位数:135
8、6和为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 = dif
9、Year(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.
10、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) lon
11、g t = 0;int start = 1;int numDays = 0;for (int i = start; i 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
12、(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 写
13、出完成下列任务的语句或程序段。(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 conf
14、ront 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 clas
15、s 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 1;int midVal = amid;if (midVal key)high = mid - 1;elsereturn mi
16、d; / 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
17、 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;Syst
18、em.out.println(i + “ “ + ai);i +;n=0;for (k = 0; k a.length; k +) System.out.print(“a“ + k +”=” + ak + “t”);n+;if(njavac 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.lengt
19、h 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,
20、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
21、 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(到分的人数:+s
22、tat1);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 下列程序的输出
23、结果是什么?(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.
24、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.printl
25、n(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
26、 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-
27、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.
28、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
29、字符串拷贝一共有哪几种基本方法?怎样实现将一个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_6abc
30、dabcd习题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
31、(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
限制150内