《Java程序设计精编教程(第3版)》之课后习题 - 个人作

    科技2025-10-26  2

    文章目录

    第1章 Java入门第2章 Java应用程序的基本结构第3章 标识符与简单数据类型第4章 运算符、表达式与语句第5章 类与对象第6章 子类与继承第7章 接口与实现第8章 内部类与异常类第9章 常用实用类


    第1章 Java入门

    Java语言的主要贡献者是谁? 答: Java之父——James Gosling编写、运行Java程序需要经过哪些主要步骤? 答: ①使用文本编写器编写源文件 ②使用编译器(javac.exe)编译源文件,得到字节码文件 ③使用解释器(java.exe)执行字节码文件如果JDK的安装目录为D:\jdk,应当怎样设置path的值? 答: D:\jdk\bin;下列哪个是JDK提供的编译器?(B) A. java.exe B. javac.exe C. javap.exe D. javaw.exeJava源文件的扩展名是什么?Java字节码的扩展名是什么? 答: 源文件扩展名:.java 字节码扩展名:.class下列那个是Java应用程序主类中正确的main方法声明?(D) A. public void main (String args[ ]) B. static void main (String args[ ]) C. public static void Main (String args[]) D. public static void main (String args[ ]) 2020.09.24

    第2章 Java应用程序的基本结构

    模仿图2.4简单绘制出例2.2中rectangle和lader对象的内存示意图。 答: 如下图所示:

    模仿例子2.2编写含有3个类的Java应用程序,要求3个类分别在3个源文件中,其中一个源文件包含有名字为Teacher的类,该类创建的对象调用add(double a,double b)的方法可以得到2个数的和、调用sub(double a,double b)的方法可以得到2个数的差;一个源文件包含有名字为Student的类,该类创建的对象调用speak()的方法可以在命令行窗口输出“老师好”;一个源文件包含名字为MainClass的主类。要求在主类的main方法中分别用Teacher和Student类创建对象,使得程序能输出12与236的和以及234与120的差,并输出“老师好”。 答: 代码如下:

    //Teacher.java public class Teacher { double add(double a, double b) { return a + b; } double sub(double a, double b) { return a - b; } } //Student.java public class Student { void speak() { System.out.println("老师好"); } } //MainClass.java public class MainClass { public static void main(String args[]) { Teacher sun = new Teacher(); double sum; double diff; sum = sun.add(12, 236); diff = sun.sub(234, 120); System.out.println("\n12与236的和:"+sum+"\n\n234与120的差:"+diff+"\n"); Student shi = new Student(); shi.speak(); } }

    程序运行结果见下图:

    当源文件中有多个类时,请简述源文件的命名规则。 答: 有多个类时,存在以下两种情况: ①有public类时,源文件的名字须和public类名完全相同,且扩展名为.java ②无public类时,源文件的名字和某个类名相同便可,且扩展名为.java当代码量较大时应当使用哪种编程风格? 答: Kernighthan风格(也称行尾风格),即左大括号在上一行的行尾,右大括号独占一行。 2020.10.08

    第3章 标识符与简单数据类型

    什么叫标识符?标识符的规则是什么?true是否可以作为标识符? 答: 标识符:用来标识符名、变量名、方法名、类型名、数组名、文件名的有效字符序列。简单地的说,标识符就是一个名字。标识符的规则见下面。ture不能作为标识符。

    标识符的语法规则:

    标识符由字母、下划线、美元符号和数字组成,长度不受限制。标识符的第一个字符不能是数字字符。标识符不能是关键字。标识符不能是true、false和null 什么叫关键字?true和false是否是关键字?请说出6个关键字。 答: 关键字:关键字就是Java语言中已经被赋予特定意义的一些单词,另外不可以把关键字作为标识符来用。true和false不是关键字。 byte、short、char、int、long和float。Java 的基本数据类型都是什么? 答: boolean、byte、short、int、long、float、double、char上机运行下列程序,注意观察输出的结果。 public class E { public static void main (String args[ ]) { for(int i=20302; i<=20322; i++) { System.out.println((char)i); } } }

    答: 运行结果见下图:

    上机调试下列程序,注意System.out.print()和System.out.println()的区别。 public class OutputData { public static void main(String args[]) { int x=234,y=432; System.out.println(x+"<"+(2*x)); System.out.print("我输出结果后不回车"); System.out.println("我输出结果后自动回车到下一行"); System.out.println("x+y= "+(x+y)); } }

    答: System.out.print():输出结果后不回车

    System.out.println():输出结果后自动回车到下一行

    代码运行结果见下图:

    编写一个Java应用程序,输出全部的大写英文字母。 答: 代码如下: public class Exercise3_6 { public static void main (String args[ ]) { char star = 'A'; char end = 'Z'; for(char i = star; i <= end; i++) { System.out.print(i); } } }

    代码运行效果见下图:

    是否可以将例子3.4 //例子3.4 import java.util.Scanner; public class Example3_4 { public static void main(String args[]) { People zhangSan = new People(); Scanner reader = new Scanner(System.in); System.out.println("输入年龄,回车确认"); zhangSan.age = reader.nextInt(); System.out.println("输入体重,回车确认"); zhangSan.weight = reader.nextFloat(); zhangSan.speak(); } }

    中的

    zhangSan.weight = reader.nextFloat();

    更改为:

    zhangSan.weight = reader.nexDouble();

    答: 不可以,因为zhangSan.weight是float类型,而nexDouble()语句接受到的double类型,从double转换到float可能会有损失,程序也会报错。

    2020.10.09

    第4章 运算符、表达式与语句

    1.下列程序的输出结果是什么?if-else语句的书写是否规范?

    public class E { public static void main (String args[]) { int x=10,y=5,z=100,result=0; if(x>y) x=z; else y=x; z=y; result=x+y+z; System.out.println(result); } }

    答: 输出结果:110

    此处的if-else语句的书写并不规范,if-else语句后的复合语句应用大括号“{}”括起来。

    2.下列程序的输出结果是什么?

    public class E { public static void main (String args[ ]) { char c='\0'; for(int i=1; i<=4; i++) { switch(i) { case 1: c='新'; System.out.print(c); case 2: c='亲'; System.out.print(c); break; case 3: c='斤'; System.out.print(c); default: System.out.print("!"); } } } }

    答: 输出结果:新亲亲斤!!

    3.参考例子4.2,在应用程序中使用if-else if-else多条件分支语句代替switch语句来判断整数的中奖情况。

    //【例4.2】 //JudgeAward.java public class JudgeAward { void giveMess(int number) { switch(number) { case 9 : case 131 : case 12 : System.out.println(number+"是三等奖"); break; case 209 : case 596 : case 27 : System.out.println(number+"是二等奖"); break; case 175 : case 316 : case 59 : System.out.println(number+"是一等奖"); break; default: System.out.println("未中奖"); } } } //Example4_2.java import java.util.Scanner; public class Example4_2 { public static void main(String args[]) { Scanner reader = new Scanner(System.in); System.out.println("输入正整数回车确认"); int number = reader.nextInt(); JudgeAward judge = new JudgeAward(); judge.giveMess(number); } }

    答: 用if-else if-else多条件分支语句代替switch语句的代码如下:

    public class JudgeAward { void giveMess(int number) { if (number == 9 || number == 12 || number == 131) { System.out.println(number+"是三等奖"); } else if (number == 27 || number == 209 || number == 596) { System.out.println(number+"是二等奖"); } else if (number == 59 || number == 316 || number == 875) { System.out.println(number+"是一等奖"); } else { System.out.println("未中奖"); } } }

    4.为了节约用电,将用户的用电量分成3个区间,针对不同的区间给出不同的收费标准。对于1至90千瓦(度)的电量,每千瓦0.6元;对于91至150千瓦的电量,每千瓦1.1元;对于用大于151千瓦的电量,每千瓦1.7元。编写一个Java应用程序程序。在主类的main方法中,输入用户的用电量,程序输出电费。 答: 代码如下:

    import java.util.Scanner; public class Exercise4_4 { public static void main(String args[]) { Scanner reader = new Scanner(System.in); double amount = 0; double price = 0; System.out.print("用电量: "); amount = reader.nextDouble(); if (amount >= 1 && amount <= 90) { price = amount * 0.6; } else if (amount >= 91 && amount <= 150) { price = (90 * 0.6) + (90 - amount) * 1.1; } else if (amount >= 151) { price = (90 * 0.6) + (60 * 1.1) + (amount - 150) * 1.7; } else { System.out.println("Error!用电量不能为负"); } System.out.printf("电费: %5.2f", price); } }

    代码运行效果:

    5.编写一个应用程序,用两个for循环语句分别输出大写和小写的“字母表”。 答: 代码如下:

    public class Exercise4_5 { public static void main(String args[]) { char star = 'a'; char end = 'z'; for (char i = star; i <= end; i++) { System.out.print(i + " "); } System.out.println(); for (char i = star; i <= end; i++) { System.out.print((char)(i-32) + " "); } } }

    代码运行效果:

    6.一个数如果恰好等于它的因子之和,这个数就称为“完数”。编写一个应用程序求1000之内的所有完数。 答: 代码如下:

    public class Exercise4_6 { public static void main(String args[]) { System.out.print("1000之内的完数有: "); for (int i = 1; i <= 1000; i++) { int sum = 0; for (int j = 1; j < i; j++) { if (i % j == 0) { sum += j; } } if (sum == i) { System.out.print(i + " "); } } } }

    代码运行结果:

    7.编写一个应用程序求满足1+2!+3!…+n!<=9876的最大整数n。 答: 代码如下:

    public class Exercise4_7 { public static void main(String args[]) { int n = 1, sum =0; while(true) { int temp = 1; for (int i = 1; i <= n; i++) { temp *= i; } sum += temp; if (sum > 9876) { break; } n++; } System.out.print("1 + 2! + 3! …+ n! <= 9876的最大整数n: " + (n - 1)); } }

    代码运行结果:

    2020.10.16

    第5章 类与对象

    类中的实例变量在什么时候会被分配内存空间? 答: 使用new运算符和类的构造方法为声明的对象分配变量时,即创建对象时。

    什么叫方法的重载?构造方法可以重载吗? 答: 一个类中可以有多个方法具有相同的名字,但这些方法的参数必须不同,即或者是参数的个数不同,或者是参数的类型不同。 可以,Java允许一个类中编写若干个构造方法,但必须保证他们的参数不同,即参数的个数不同,或者是参数的类型不同。

    类中的实例方法可以操作类变量(static变量)吗?类方法(static方法)可以操作实例变量吗? 答: 实例方法可以操作类变量。但类方法不可以操作实例变量,因为在类创建对象之前,实例成员变量还没分配内存。

    类中的实例方法可以用类名直接调用吗? 答: 实例方法不能通过类名调用,只能由对象来调用。

    简述类变量和实例变量的区别。 答: ①不同对象的实例变量互不相同 ②所有对象共享类变量 ③通过类名直接访问类变量

    下列哪些类声明是错误的?(C,D) A. class A B. public class A C. protected class A D. private class A 不能用protected和private修饰类

    下列A类的类体中【代码1】~【代码5】哪些是错误的?

    class Tom { private int x = 120; protected int y = 20; int z = 11; private void f() { x = 200; System.out.println(x); } void g() { x = 200; System.out.println(x); } } public class A { public static void main(String args[]) { Tom tom = new Tom(); tom.x=22; //【代码1】 tom.y=33; //【代码2】 tom.z=55; //【代码3】 tom.f(); //【代码4】 tom.g(); //【代码5】 } }

    答: 【代码1】和【代码4】,成员变量x和方法f()均被关键字private修饰,它们的访问权限都是私有的,在类A中用类Tom创建的对象tom不能访问自己的私有变量和私有方法。

    请说出A类中System.out.println的输出结果。 class B { int x = 100, y = 200; public void setX(int x) { x = x; } public void setY(int y) { this.y = y; } public int getXYSum() { return x + y; } } public class A { public static void main(String args[]) { B b = new B(); b.setX(-100); b.setY(-200); System.out.println("sum = " + b.getXYSum()); } }

    答: 输出结果:

    sum = -100

    语句b.setX(-100);执行后不改变对象b的实例变量x的值,因为在类B中存在成员变量与局部变量同名的情况,方法setX()没有使用this关键字,所以在setX()方法中成员变量x暂时失效,而方法setY()使用了this.y,所以对象b的变量y值为-200,当对象b调用getXYSum()方法时,返回的值为x(100) + y(-200) = -100,即得到-100的输出结果。

    运行验证如下图所示:

    请说出A类中System.out.println的输出结果。 class B { int n; static int sum = 0; void setN(int n) { this.n = n; } int getSum() { for(int i = 1; i <= n; i++) sum = sum + i; return sum; } } public class A { public static void main(String args[]) { B b1 = new B(), b2 = new B(); b1.setN(3); b2.setN(5); int s1 = b1.getSum(); int s2 = b2.getSum(); System.out.println(s1 + s2); } }

    答: 输出结果:

    27

    类B中的成员变量sum被关键字static修饰,是类变量,被对象b1和b2所共享。在对象b1调用setN()方法时会改变sum的值,在语句int s1 = b1.getSum();执行后,sum值由0变为6,然后执行下一语句int s2 = b2.getSum();时,会得到21的结果,最终语句System.out.println(s1 + s2);执行完后得到27的输出结果。

    运行验证结果如下:

    请说出E类中System.out.println的输出结果。 class A { double f(int x, double y) { return x + y; } int f(int x, int y) { return x * y; } } public class E { public static void main(String args[]) { A a = new A(); System.out.println(a.f(10, 10)); System.out.println(a.f(10, 10.0)); } }

    答: 输出结果:

    100 20.0

    运行验证如下:

    2020.11.07

    第6章 子类与继承

    子类将继承父类的那些成员变量和方法?子类在什么情况下隐藏父类的成员变量和方法? 答: 如果子类和父类在同一个包中,那么,子类自然地继承了其父类中不是private的成员变量作为自己的成员变量,并且也自然地继承了父类中不是private的方法作为自己的方法, 继承的成员或方法的访问权限保持不变。当子类和父类不在同一个包中时,父类中的private和友好访问权限的成员变量不会被子类继承,也就是说,子类只继承父类中的protected和public访问权限的成员变量作为子类的成员变量;同样,子类只继承父类中的protected和public访问权限的方法作为子类的方法。如果所声明的成员的变量的名字和从父类继承来的成员变量的名字相同(声明的类型可以不同),在这种情况下,子类就会隐藏掉所继承的成员变量。

    父类的final方法可以被子类重写吗? 答: 不可以。

    什么类中可以有abstract方法? 答: abstract类。

    什么叫对象的上转型对象? 答: 假设,A类是B类的父类,当用子类创建一个对象b,并把这个对象b的引用放到父类的对象a中时,称对象a是对象b的上转型对象。

    下列叙述哪些是正确的?(AD) A. final 类不可以有子类。 B. abstract类中只可以有abstract方法。 C. abstract类中可以有非abstract方法,但该方法不可以用final修饰。 D. 不可以同时用final和abstract修饰一个方法。

    6.请说出E类中System.out.println的输出结果。

    class A { double f(double x, double y) { return x + y; } } class B extends A { double f(int x, int y) { return x * y; } } public class E { public static void main(String args[]) { B b = new B(); System.out.println(b.f(3, 5)); System.out.println(b.f(3.0, 5.0)); } }

    答: 输出结果如下:

    15.0 8.0

    7.请说出E类中System.out.println的输出结果。

    class A { double f(double x, double y) { return x + y; } static int g(int n) { return n * n; } } class B extends A { double f(double x, double y) { double m = super.f(x, y); return m + x * y; } static int g(int n) { int m = A.g(n); return m + n; } } public class E { public static void main(String args[]) { B b = new B(); System.out.println(b.f(10.0, 8.0)); System.out.println(b.g(3)); } }

    答: 输出结果如下:

    98.0 12 2020.11.20

    第7章 接口与实现

    接口中能声明变量吗? 答: 不能。

    接口中能定义非抽象方法吗? 答: 不能。

    什么叫接口的回调? 答: 可以把实现某一接口的类创建的对象的引用赋给该接口声明的接口变量中。那么该接口变量就可以调用被类实现的接口中的方法。

    请说出E类中System.out.println的输出结果。

    interface A { double f(double x, double y); } class B implements A { public double f(double x, double y) { return x * y; } int g(int a, int b) { return a + b; } } public class E { public static void main(String args[]) { A a = new B(); System.out.println(a.f(3, 5)); B b = (B) a; System.out.println(b.g(3, 5)); } }

    答:

    15.0 8 请说出E类中System.out.println的输出结果。 interface Com { int add(int a, int b); } abstract class A { abstract int add(int a, int b); } class B extends A implements Com { public int add(int a, int b) { return a + b; } } public class E { public static void main(String args[]) { B b = new B(); Com com = b; System.out.println(com.add(12, 6)); A a = b; System.out.println(a.add(10, 5)); } }

    答:

    18 15 2020.11.29

    第8章 内部类与异常类

    内部类的外嵌类的成员变量在内部类中仍然有效吗? 答: 有效。

    内部类中的方法也可以调用外嵌类中的方法吗? 答: 可以。

    内部类的类体中可以声明类变量和类方法吗? 答: 不可以。

    请说出下列程序的输出结果。

    class Cry { public void cry() { System.out.println("大家好"); } } public class E { public static void main(String args[]) { Cry hello = new Cry() { public void cry() { System.out.println("大家好,祝工作顺利!"); } }; hello.cry(); } }

    答:

    大家好,祝工作顺利! 2020.12.13

    第9章 常用实用类

    下列叙述哪些是正确的?(ABD) A. String 类是final 类,不可以有子类。 B. String 类在java.lang包中。 C. “abc”=="abc"的值是false . D. “abc”.equals(“abc”)的值是true

    请说出E类中System.out.println的输出结果。

    import java.util.*; class GetToken { String s[]; public String getToken(int index, String str) { StringTokenizer fenxi = new StringTokenizer(str); int number = fenxi.countTokens(); s = new String[number + 1]; int k = 1; while (fenxi.hasMoreTokens()) { String temp = fenxi.nextToken(); s[k] = temp; k++; } if (index <= number) return s[index]; else return null; } } class E { public static void main(String args[]) { String str = "We Love This Game"; GetToken token = new GetToken(); String s1 = token.getToken(2, str), s2 = token.getToken(4, str); System.out.println(s1 + ":" + s2); } }

    答: 输出结果:

    Love:Game 请说出E类中System.out.println的输出结果。 public class E { public static void main(String args[]) { byte d[] = "abc我们喜欢篮球".getBytes(); System.out.println(d.length); String s = new String(d, 0, 7); System.out.println(s); } }

    答: 输出结果:

    15 abc我们 请说出E类中System.out.println的输出结果。 class MyString { public String getString(String s) { StringBuffer str = new StringBuffer(); for (int i = 0; i < s.length(); i++) { if (i % 2 == 0) { char c = s.charAt(i); str.append(c); } } return new String(str); } } public class E { public static void main(String args[]) { String s = "1234567890"; MyString ms = new MyString(); System.out.println(ms.getString(s)); } }

    答: 输出结果:

    13579 请说出E类中System.out.println的输出结果。 public class E { public static void main(String args[]) { String regex = "\\djava\\w{1,}"; String str1 = "88javaookk"; String str2 = "9javaHello"; if (str1.matches(regex)) { System.out.println(str1); } if (str2.matches(regex)) { System.out.println(str2); } } }

    答: 输出结果:

    9javaHello 字符串调用public String toUpperCase()方法返回一个字符串,该字符串把当前字符串中的小写字母变成大写字母;.字符串调用public String toLowerCase()方法返回一个字符串,该字符串把当前字符串中的大写字母变成小写字母。String类的public String concat(String str)方法返回一个字符串,该字符串是把调用该方法的字符串与参数指定的字符串连接。编写一个程序,练习使用这3个方法。 答: public class E { public static void main (String args[ ]) { String s1,s2,t1="ABCDabcd"; s1=t1.toUpperCase(); s2=t1.toLowerCase(); System.out.println(s1); System.out.println(s2); String s3=s1.concat(s2); System.out.println(s3); } } String类的public char charAt(int index)方法可以得到当前字符串index位置上的一个字符。编写程序使用该方法得到一个字符串中的第一个和最后一个字符。 答: public class E { public static void main (String args[ ]) { String s="ABCDabcd"; char cStart=s.charAt(0); char cEnd = s.charAt(s.length()-1); System.out.println(cStart); System.out.println(cEnd); } } 通过键盘输入年份和月份。程序输出相应的日历牌。 答: import java.util.*; public class E { public static void main(String args[]) { Scanner read=new Scanner(System.in); CalendarBean cb=new CalendarBean(); int year=2000,month=1; System.out.println("输入年:"); year=read.nextInt(); System.out.println("输入月:"); month=read.nextInt(); cb.setYear(year); cb.setMonth(month); String [] a= cb.getCalendar();//返回号码的一维数组 char [] str="日一二三四五六".toCharArray(); for(char c:str) { System.out.printf("%3c",c); } for(int i=0;i<a.length;i++) { //输出数组a if(i%7==0) System.out.println(""); //换行 System.out.printf("%4s",a[i]); } } } class CalendarBean { String [] day; int year=0,month=0; public void setYear(int year) { this.year=year; } public void setMonth(int month) { this.month=month; } public String [] getCalendar() { String [] a=new String[42]; Calendar rili=Calendar.getInstance(); rili.set(year,month-1,1); int weekDay=rili.get(Calendar.DAY_OF_WEEK)-1; //计算出1号的星期 int day=0; if(month==1||month==3||month==5||month==7||month==8||month==10||month==12) day=31; if(month==4||month==6||month==9||month==11) day=30; if(month==2) { if(((year%4==0)&&(year%100!=0))||(year%400==0)) day=29; else day=28; } for(int i=0;i<weekDay;i++) a[i]=" "; for(int i=weekDay,n=1;i<weekDay+day;i++) { a[i]=String.valueOf(n) ; n++; } for(int i=weekDay+day;i<a.length;i++) a[i]=" "; return a; } } 计算某年、某月、某日和某年、某月、某日之间的天数间隔。要求年、月、日通过通过键盘输入到程序中。 答: import java.util.*; public class E { public static void main (String args[ ]) { Scanner read=new Scanner(System.in); int year1,month1,day1,year2,month2,day2; System.out.println("输入第一个日期的年、月、日(用空格或回车分隔):"); year1=read.nextInt(); month1=read.nextInt(); day1=read.nextInt(); System.out.println("输入第二个日期的年月日(用空格或回车分隔):"); year2=read.nextInt(); month2=read.nextInt(); day2=read.nextInt(); Calendar calendar=Calendar.getInstance(); calendar.set(year1,month1-1,day1); long timeYear1=calendar.getTimeInMillis(); calendar.set(year2,month2-1,day2); long timeYear2=calendar.getTimeInMillis(); long 相隔天数=Math.abs((timeYear1-timeYear2)/(1000*60*60*24)); System.out.println(""+year1+"年"+month1+"月"+day1+"日和"+ year2+"年"+month2+"月"+day2+"日相隔"+相隔天数+"天"); } } 编程练习Math类的常用方法。 答: import java.util.*; public class E { public static void main (String args[ ]) { double a=0,b=0,c=0; a=12; b=24; c=Math.asin(0.56); System.out.println(c); c=Math.cos(3.14); System.out.println(c); c=Math.exp(1); System.out.println(c); c=Math.log(8); System.out.println(c); } } 参看例子19,编写程序剔除一个字符串中的全部非数字字符,例如,将形如“ab123you”的非数字字符全部剔除,得到字符串“123”。 答: public class E { public static void main (String args[ ]) { String str = "ab123you你是谁?"; String regex = "\\D+"; str = str.replaceAll(regex,""); System.out.println(str); } } 参看例子21,使用Scanner类的实例解析“数学87分,物理76分,英语96分”中的考试成绩,并计算出总成绩以及平均分数。 答: import java.util.*; public class E { public static void main(String args[]) { String cost = "数学87分,物理76分,英语96分"; Scanner scanner = new Scanner(cost); scanner.useDelimiter("[^0123456789.]+"); double sum=0; int count =0; while(scanner.hasNext()){ try{ double score = scanner.nextDouble(); count++; sum = sum+score; System.out.println(score); } catch(InputMismatchException exp){ String t = scanner.next(); } } System.out.println("总分:"+sum+"分"); System.out.println("平均分:"+sum/count+"分"); } } 2020.12.20
    Processed: 0.013, SQL: 8