java第八章课后习题解答
- 格式:doc
- 大小:101.50 KB
- 文档页数:15
第八章【练习8.1】1.A2.A3.B4. 组件是GUI程序的基本组成元素,任何一个GUI就是由若干组件对象构成的。
Swing是在AWT基础之上构建的一套新的Java图形界面库,其不再依赖操作系统的本地代码而是自己负责绘制组件的外观,因此也被称为轻量级(Light-weight)组件,这是它与AWT组件的最大区别。
Swing中几乎所有的类都直接或间接继承自AWT中的类,另一方面,Swing的事件模型也是完全基于AWT的,因此,AWT和Swing并非两套彼此独立的Java图形库。
5. 容器组件指那些能够“容纳”组件的特殊组件,如窗口、面板、对话框等。
容器可以嵌套,即容器中又包含容器。
Swing提供的常用容器组件有窗口、面板、可滚动面板、分割面板、分页面板等。
6.①顶层容器:指GUI程序中位于“最上层”的容器,其不能被包含到别的容器中,如窗口、对话框等。
②非顶层容器:位于顶层容器之下的容器,如面板、内部窗口等。
7. JLabel(标签)用于显示文字或图片,不能接受用户的输入。
JTextField (文本框) 可以接受用户输入或编辑单行文本。
JTextArea(文本区) 接受用户输入或编辑多行文本。
JPasswordField(密码输入框) 是JTextField 的子类,两者的主要区别是JPasswordField 不会显示出用户输入的内容,而只会显示出程序指定的一个固定字符,比如'*'。
8. 将多个单选按钮加入到同一个ButtonGroup群组对象中构成一组,保证该组按钮任一时刻只能有一个单选按钮被选中。
【练习8.2】1.C2.D3.D4.D5.//BasicSwingComponent.java1 import java.awt.FlowLayout;2 import java.awt.event.ItemEvent;3 import java.awt.event.ItemListener;45 import javax.swing.JButton;6 import javax.swing.JCheckBox;7 import javax.swing.JComboBox;8 import javax.swing.JFrame;9 import javax.swing.JLabel;10 import javax.swing.JList;11 import javax.swing.JPanel;12 import javax.swing.JPasswordField;13 import javax.swing.JRadioButton;14 import javax.swing.JScrollPane;15 import javax.swing.JTextArea;16 import javax.swing.JTextField;1718 public class BasicSwingComponent {1920 public static void main(String[] args) {21 JFrame win = new JFrame("JFrame");22 win.setSize(300, 300);23 win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);24 win.setLayout(new FlowLayout());2526 JPanel jp1 = new JPanel();27 JPanel jp4 = new JPanel();28 JLabel label1 = new JLabel("用户名:");29 jp1.add(label1);30 JTextField textField = new JTextField(10);31 jp1.add(textField);32 jp4.add(new JLabel("口令:"));33 JPasswordField pfd = new JPasswordField(10);34 pfd.setEchoChar('*');35 jp4.add(pfd);3637 JPanel panel2 = new JPanel();38 JLabel label12 = new JLabel("性别:");39 panel2.add(label12);40 JRadioButton radio = new JRadioButton("男");41 radio.setSelected(true);42 panel2.add(radio);43 JRadioButton radio2 = new JRadioButton("女");44 panel2.add(radio2);4546 JPanel panel3 = new JPanel();47 JLabel label3 = new JLabel("爱好:");48 panel3.add(label3);49 JCheckBox check = new JCheckBox("运行");50 panel3.add(check);51 JCheckBox check2 = new JCheckBox("音乐");52 check2.setSelected(true);53 panel3.add(check2);54 JCheckBox check3 = new JCheckBox("网络");55 panel3.add(check3);5657 JPanel panel4 = new JPanel();58 JLabel label4 = new JLabel("JComboBox:");59 panel4.add(label4);60 String[] majors = { "软件工程", "计算机", "物联网", "大数据" };61 JComboBox cbxMajor = new JComboBox(majors);62 cbxMajor.addItemListener(new ItemListener() {6364 @Override65 public void itemStateChanged(ItemEvent e) {66 String selection=(String)e.getItem();//获取选择项67 if(e.getStateChange()==ItemEvent.SELECTED )68 System.out.println(selection);6970 }});71 panel4.add(cbxMajor);7273 JPanel panel5 = new JPanel();74 panel5.add(new JLabel("JList:"));75 JList lst = new JList(majors);76 panel5.add(lst);7778 JTextArea ta = new JTextArea();79 ta.setText("此处为简介 \n第二行\n第三行\n第四行");80 ta.setRows(3);81 ta.setColumns(10);82 JScrollPane scp = new JScrollPane(ta);8384 JPanel panel6 = new JPanel();85 JButton button = new JButton("提交");86 panel6.add(button);8788 win.add(jp1);89 win.add(jp4);90 win.add(panel2);91 win.add(panel3);92 win.add(scp);93 win.add(panel6);9495 win.setVisible(true);96 }97 }【练习8.3】1. D2. A3.①事件源(Event Source):事件的产生者或来源。
Chapter 8 Objects and Classes1.See the section "Defining Classes for Objects."2.The syntax to define a classis public class ClassName {}3.The syntax to declare a reference variable foran object isClassName v;4.The syntax to create an object isnew ClassName();5.Constructors are special kinds of methods that arecalled when creating an object using the new operator.Constructors do not have a return type—not even void.6. A class has a default constructor only if theclass does not define any constructor.7.The member access operator is used to access adata field or invoke a method from an object.8.An anonymous object is the one that does not havea reference variable referencing it.9. A NullPointerException occurs when a null referencevariable is used to access the members of an object.10.An array is an object. The default value for theelements of an array is 0 for numeric, false for boolean,‘ u0000’ for char, null for object element type.11.(a) There is such constructor ShowErrors(int) in theShowErrors class.The ShowErrors class in the book has a defaultconstructor. It is actually same aspublic class ShowErrors {public static void main(String[] args) {ShowErrors t = new ShowErrors(5);}public ShowErrors () {}}On Line 3, new ShowErrors(5) attempts to create aninstance using a constructor ShowErrors(int), but theShowErrors class does not have such a constructor.That is an error.(b) x() is not a method in the ShowErrors class.The ShowErrors class in the book has a defaultconstructor. It is actually same aspublic class ShowErrors {public static void main(String[] args) {ShowErrors t = new ShowErrors();t.x();}public ShowErrors () {}}On Line 4, t.x() is invoked, but the ShowErrors classdoes not have the method named x(). That is an error.(c)The program compiles fine, but it has aruntime error because variable c is null when theprintln statement is executed.(d)new C(5.0) does not match any constructors in classC. The program has a compilation error because classC does not have a constructor with a double argument.12.The program does not compile because new A() is used inclass Test, but class A does not have a defaultconstructor. See the second NOTE in the Section,“Constructors.”13.falsee the Date’s no -arg constructor to create a Date forthe current time. Use the Date’s toString() method to display a string representation for the Date.15. Use the JFrame ’s no -arg constructor to create JFrame. Use thesetTitle(String) method a set a title and use the setVisible(true)method to display the frame.16.Date is in java.util. JFrame and JOptionPane are injavax.swing. System and Math are in ng.17.System.out.println(f.i);Answer: CorrectSystem.out.println(f.s);Answer: Correctf.imethod();Answer: Correctf.smethod();Answer: CorrectSystem.out.println(F.i);Answer: IncorrectSystem.out.println(F.s);Answer: CorrectF.imethod();Answer: IncorrectF.smethod();Answer: Correct18.Add static in the main method and in the factorialmethod because these two methods don ’t need referenceany instance objects or invoke any instance methods in theTest class.19. You cannot invoke an instance method or reference aninstance variable from a static method. You can invoke astatic method or reference a static variable from aninstance method? c is an instance variable, which cannot beaccessed from the static context in method2.20.Accessor method is for retrieving private data value andmutator method is for changing private data value. The namingconvention for accessor method is getDataFieldName() for non-boolean values and isDataFieldName() for boolean values. Thenaming convention for mutator method is setDataFieldName(value).21.Two benefits: (1) for protecting data and (2) for easyto maintain the class.22.Not a problem. Though radius is private,myCircle.radius is used inside the Circle class. Thus, itis fine.23.Java uses “pass by value ” to pass parameters to amethod. When passing a variable of a primitive type to amethod, the variable remains unchanged after themethod finishes. However, when passing a variable of areference type to a method, any changes to the objectreferenced by the variable inside the method arepermanent changes to the object referenced by thevariable outside of the method. Both the actualparameter and the formal parameter variables referenceto the same object.The output of the program is as follows:count 101times 024.Remark: The reference value of circle1 is passed to xand the reference value of circle2 is passed to y. The contents ofthe objects are not swapped in the swap1 method. circle1 andcircle2 are not swapped. To actually swap the contents of these objects, replace the following three linesCircle temp = x;x=y;y=temp;bydouble temp = x.radius;x.radius = y.radius;y.radius = temp;as in swap2.25. a. a[0] = 1 a[1] = 2 b.a[0] = 2 a[1] = 1c. e1 = 2 e2 = 1d. t1 ’s i = 2 t1t2 ’s i = 2 t2’s j = 1’s j = 126.(a) null(b)1234567(c)7654321(d)123456727.(Line 4 prints null since dates[0] is null. Line 5 causes a NullPointerException since it invokes toString() method from the null reference.)。
第8章习题参考答案一、简答题1.实现类的继承是通过哪个关键字实现的?使用extends 和implements 这两个关键字来实现继承,而且所有的类都是继承于ng.Object,当一个类没有继承的两个关键字,则默认继承object(这个类在ng 包中,所以不需要import祖先类。
在Java 中,类的继承是单一继承,也就是说,一个子类只能拥有一个父类,所以extends 只能继承一个类。
2.Java能实现多继承关系吗?如何解决这个问题?在Java 中,类的继承是单一继承,也就是说,一个子类只能拥有一个父类,所以extends 只能继承一个类。
使用implements 关键字可以变相的使java具有多继承的特性,使用范围为类继承接口的情况,可以同时继承多个接口,接口跟接口之间采用逗号分隔。
3.如果父类和子类同时提供了同名方法,在类实例化后,调用的是哪个类的方法?采用什么办法避免混淆?子类。
通过super 与this 关键字区别父类和子类。
super关键字:我们可以通过super关键字来实现对父类成员的访问,用来引用当前对象的父类。
this关键字:指向自己的引用,表示当前正在调用此方法的对象引用。
4.什么是抽象类?抽象类和普通类有什么不同?抽象类是指类中含有抽象方法的类,抽象类和普通类区别是:1、和普通类比较起来,抽象类它不可以被实例化,这个区别还是非常明显的。
2、抽象类能够有构造函数,被继承的时候,子类就一定要继承父类的一个构造方法,但是,抽象方法不可以被声明成静态。
3、在抽象类当中,可以允许普通方法有主体,抽象方法只需要申明,不需要实现。
4、含有抽象方法的类,必须要申明为抽象类。
5、抽象的子类必须要实现抽象类当中的所有抽象方法,否则的话,这个子类也是抽象类。
6、抽象类它一定要有abstract关键词修饰。
《JAVA大学实用教程》(第四版)课后习题答案第一章Java 语言概述2.“java编译器将源文件编译为的字节码文件是机器码”这句话正确吗?答:不正确3.j ava 应用程序的主类必须含有怎样的方法?答:含有main 方法4。
“java 应用程序必须有一个类是public 类”这句话正确吗?答;不正确,只能有一个public 类5。
“java Applet 程序的主类必须是public 类”这句话正确吗?答:正确,因为java Applet 主类必须是Applet 类的子类并且是public 的类6。
请叙述java 源程序的命名规则。
答:与public 的类同名。
7。
源文件生成的字节码文件在运行时都加载到内存中吗?答:非也,动态随需要运行才加载。
8.面向对象的程序设计语言有那些基本特征?答:封装;继承;多态性。
9.在Java 程序中有多个类文件时,用Java 命令应该运行那个类?答:具有main 方法的类第二章基本数据类型和数组4。
下列哪些语句是错的?Int x=120;Byte b=120;b=x;答:B=x;错应为b=(byte)x5。
下列哪些语句是错的?答:y=d;错,应y=(float)d6。
下列两个语句是等价的吗?Char x=97;Char x=‘a’;答:是等价的。
7。
下列system.out.printf 语句输出结果是什么?Int a=97;Byte b1=(byte)128;Byte b2=(byte)(-129);System.out.printf(“%c,%d,%d”,a,b1,b2);如果输出语句改为:System.out.printf(“%d,%d,%d”,a,b1,b2);输出什么?答:输出a ,-128,127修改后输出97,-128,1278.数组是基本数据类型吗?怎样获取数组的长度?答:不是基本数据类型,是复合数据类型。
可以通过:数组名.length 的方法获得数组长度9。
Java EE 开发技术智慧树知到课后章节答案2023年下武昌理工学院武昌理工学院第一章测试1.Apache Tomcat服务器默认使用的通信端口是()。
答案:80802.下面选项中,不是由包java.sql提供的接口是()。
答案:DriverManager3.定义一个Maven依赖坐标,通常需要定义()个值。
答案:34.Java应用于嵌入式开发,指的是()。
答案:Java ME5.Java程序或者Web程序以JDBC方式访问数据库时,可以不使用由数据库厂商提供的驱动包。
()答案:错6.使用Maven能方便地管理Java项目或Java Web项目的依赖包。
()答案:对7.在Java Web中,通常选用Tomcat作为Web服务器。
()答案:对8.使用JDBC提供的Statement接口能实现对数据库的参数式查询。
()答案:错9.IDEA内置了Maven。
()答案:对第二章测试1.page指令的()属性用于引入需要的包或类。
答案:import2.JSP内置对象(),提供了重定向方法sendRedirect()。
答案:response3.会话跟踪所使用的JSP内置对象是()。
答案:session4.JSP表达式用法<%=exp%>,可以通过使用内置对象()的方法println()实现。
答案:out5.JSP页面调试,必须有Web服务器环境。
()答案:对6.JSP页面不能包含HTML标签和JavaScript脚本。
()答案:错7.使用动作标签<jsp:forward>会产生新的请求对象。
()答案:错8.EL表达式简化了对JSP内置对象属性的访问,通常配合JSTL标签来使用。
()答案:对9.JSP文件包含指令标签必须使用file属性。
()答案:对10.若表单提交的数据含有中文,则在接收之前,应使用JSP内置对象request的方法setChraracterEncoding()设置字符编码,以避免显示或写入数据库时出现中文乱码。
Java语言程序设计第八章课后习题答案1.进程和线程有何区别,Java是如何实现多线程的。
答:区别:一个程序至少有一个进程,一个进程至少有一个线程;线程的划分尺度小于进程;进程在执行过程中拥有独立的内存单元,而多个线程共享内存,从而极大地提高了程序的运行效率。
Java程序一般是继承Thread 类或者实现 Runnable接口,从而实现多线程。
2.简述线程的生命周期,重点注意线程阻塞的几种情况,以及如何重回就绪状态。
答:线程的声明周期:新建-就绪-(阻塞)-运行--死亡线程阻塞的情况:休眠、进入对象wait池等待、进入对象lock池等待;休眠时间到回到就绪状态;在wait池中获得notify()进入lock池,然后获得锁棋标进入就绪状态。
3.随便选择两个城市作为预选旅游目标。
实现两个独立的线程分别显示10次城市名,每次显示后休眠一段随机时间(1000毫秒以内),哪个先显示完毕,就决定去哪个城市。
分别用Runnable接口和Thread类实现。
(注:两个类,相同一个测试类)//Runnable接口实现的线程runable类public class runnable implements Runnable {private String city;public runnable() {}public runnable(String city) {this.city = city;}public void run() {for (int i = 0; i < 10; i++) {System.out.println(city);try {//休眠1000毫秒。
Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}}}// Thread类实现的线程thread类public class runnable extends Thread {private String city;public runnable() {}public runnable(String city) {this.city = city;}public void run() {for (int i = 0; i < 10; i++) {System.out.println(city);try {//休眠1000毫秒。
Java程序设计(华东交通大学)智慧树知到课后章节答案2023年下华东交通大学华东交通大学第一章测试1.编译和运行以下代码的结果为:public class MyMain{public static void main(String argv){System.out.println("Hello cruel world");}}答案:编译无错,但运行时指示找不到main方法2.以下哪个是Java应用程序入口的main方法头?答案:public static void main(String a[])3.编译Java源程序文件将产生相应的字节码文件,字节码文件的扩展名为?class4.main方法是Java Application程序执行的入口点,关于main方法的方法头合法的有?答案:public static void main(String arg[ ]);public static void main(String[ ] args)5.每个源程序文件中只能定义一个类。
答案:错第二章测试1.在Java中,十进制数16的十六进制表示格式是?答案:0x102.要产生[10,100]之间的随机整数使用哪个表达式?10+(int)(Math.random()*91)3.下列符号中不能作为Java标识符的是?答案:45six4.下面各项中定义变量及赋值不正确的是?答案:float f = 45.0;5.执行以下代码段后, x, a,和 b的值为?int x, a = 6, b = 7;x = a++ + b++;答案:x= 13, a=7, b=86.下列哪个不是Java的保留字?答案:cin7.哪些赋值是合法的?答案:float f = -412;;long test = 012; ;double d = 0x12345678;8.下列代码中,将引入编译错误的行是1 public class Exercise{2 public static void main(String args[]){3 float f = 0.0 ;4 f = f + 1.0 ;5 }6 }答案:第3行;第4行9.下列哪些是合法标识符?答案:$persons ;TwoUsers10.下列哪些是java中有效的整数表示形式?答案:022;22;0x22第三章测试1.如何更改break语句使退出inner和middle循环,继续外循环的下一轮?outer: for (int x = 0; x < 3; x++) {middle: for (int y = 0; y < 3; y++) {inner: for (int z = 0; z < 3; z++) {if (arr(x, y, z) == targetValue)break;}}}答案:break middle;2.以下程序的输出结果为?public class Test {public static void main(String args[]) {for ( int k = 0; k < 3; k++)System.out.print("k");}}答案:kkk3.以下代码的调试结果为?1: public class Q102: {3: public static void main(String[] args) 4: {5: int i = 10;6: int j = 10;7: boolean b = false;8:9: if( b = i == j)10: System.out.println("True");11: else12: System.out.println("False");13: }14: }答案:输出:True4.以下代码的调试结果为?public class test {public static void main(String args[]) {int i = 1;do {i--;} while (i > 2);System.out.println(i);}}答案:5.下面的代码段执行之后count的值是什么?int count = 0;for (int i = 1; i < 4; i++) {count += i;}System.out.println(count);答案:66.以下程序的运行结果为:1. public class Conditional {2. public static void main(String args [] ) {3. int x = 4;4. System.out.println( "value is " +5. ((x > 4) ? 99.99 : 9));6. }7. }答案:输出: value is 9.07.下列程序的运行结果?public class Test {public static void main(String a[]) {int x=3,y=4,z=5;if (x>3) {if (y<2)System.out.println("show one");elseSystem.out.println("show two");}else {if (z>4)System.out.println("show three");elseSystem.out.println("show four");}}}答案:show three8.以下程序调试结果public class test {public static void main(String args[]) {int i=1, j=3;while (j>0) {j--;i++;}System.out.println(i);}}答案:49.在switch(expression)语句中,expression的数据类型不能是?答案:boolean;double10.假设a是int类型变量,并初始化为1,则下列哪个为合法的条件语句?答案:if (a<3) { } ; if (true) { }第四章测试1.以下程序运行时输入:java Cycle hello two me 2public class Cycle{public static void main(String args[]){System.out.println(args[1]);}}则运行结果为?答案:two2.public class test {public static void main(String args[]) {int m=0;for ( int k=0;k<2;k++)method(m++);System.out.println(m);}public static void method(int m) {System.out.print(m);}答案:0123.以下程序运行结果为:public class Q {public static void main(String argv[]) { int anar[]= new int[5];System.out.println(anar[0]);}}答案:4.下列程序的运行结果是:public class Test {public static void main(String args[]) {int m[]={1,2,3,4,5,6,7,8};int sum = 0;for (int i=0;i<8;i++){sum = sum + m[i];if (i==3) break;}System.out.println(sum);}}答案:105.下面定义和给数组初始化正确的是:答案:String temp [] = {''a'', ''b'', ''c''};6.在注释//Start For loop 处要插入哪段代码可以实现根据变量i的值定位访问数组ia[]的所有元素。