java第八章问答题
- 格式:docx
- 大小:14.08 KB
- 文档页数:1
习题八参考答案1.什么是组件?什么是容器?并说明各自的作用。
答:从实现角度来看,组件(Component)是构成GUI的基本要素,作用是通过对不同事件的响应来完成和用户的交互或组件之间的交互;容器是能容纳和排列组件的对象,如Applet> Panel (面板)、Frame (窗口)等,作用就是放置组件并控制组件位置。
2.简述Swing组件的优点。
答:Swing是在AWT基础上扩展而来的,提供了非常丰富的组件,远远多于AWT,并且引入了新的概念和性能,这使得基于Swing开发GUI应用程序比直接使用AWT开发更为灵活、方便、效率高,而且能设计出更优美的、感受更好的GUI。
3.简述容器的概念,结合8.4.7小节的内容,解释什么是应用程序的主框架?答:容器是用来容纳其他组件和容器的特殊组件,是由容器类(Container类)创建的对象。
在Java语言中,容器类是组件类(组件类Component类)的一个子类,具有组件的所有性质。
在AWT 技术中,容器类由java. awt包提供,主要包括面板类Panel、窗口类Window、结构类Frame、对话框类Dialog等。
在Swing技术中,容器类由javax. swing包提供,并可分为如下三类:>顶层容器:JFramc. JApplet. JDialog、JWindow;>中间容器:JPanel、JScrollPane^ JSplitPane、JDesktopPaneJToolBar;特殊容器:在GUI上起特殊作用的中间层,如J Interna IFrame、JLayeredPane、 JRootPaneo 应用程序的主框架由可以容纳应用程序各种组件的顶层容器创建,除了负责组件的管理外,通常还提供最大化、最小化、关闭按钮等,实现应用程序展现方式以及关闭等。
4.总结JFrame的使用要点,并说明内容面板的作用。
答:JFrame类包含很多设置窗体的方法,可以用setTitle(String tille)方法设置窗体标题,用setBounds(inl x,int y,int width,int height)方法设置窗体显示的位置及大小,用setVisable (Boolean b)方法设置可见与否(默认不可见)。
一、判断题1、(√)使用括号改善表达式的清晰性。
2、(×)对递归定义的数据结构不要使用递归定义的过程。
3、(×)尽可能对程序代码进行优化。
4、(√)不要修改不好的程序,要重新编写。
5、(√)不要进行浮点数的相等比较。
6、(×)应尽可能多地使用临时变量。
7、(√)利用数据类型对数据值进行防范。
8、(×)用计数方法而不是用文件结束符判别批量数据输入的结束。
9、(×)程序中的注释是可有可无的。
10、(√)使用有意义的标识符。
11、(×)应尽量把程序编写的短一些。
12、(×)应尽量使用goto语句。
二、选择题1、在编码中首先要考虑的是(B)。
A、程序的执行效率B、程序的可读性C、程序的一致性D、程序的安全性2、(D)。
不属于序言性注释内容的是A、模块设计者B、修改日期C、程序的整体说明D、语句功能3、序言性注释应置于文件或模块的(A)位置。
A、起始B、结束C、中间D、起始和中间4、如果编写系统软件,可选用的语言是(C)。
A、FoxProB、COBOLC、CD、FORTRAN5、选择程序设计语言不应该考虑的是(B)。
A、应用领域B、语言的功能C、用户的要求D、用户的使用水平6、与编程风格有关的因素不包括(D)。
A、源程序文档化B、语句构造C、输入输出D、程序的编译效率7、最早用于科学计算的程序设计语言是(C)。
A、PROLOGB、SmalltalkC、FORTRAND、COBOL8、功能性注释的主要内容不包括(B)。
A、程序段的功能B、模块的功能C、数据的状态D、语句的功能9、对建立良好的编程风格,下面描述正确的是(A)。
A、程序应简单、清晰、可读性好B、符号名的命名只要符合语法即可C、充分考虑程序的执行效率D、程序的注释可有可无10、源程序中应包含一些内部文档,以帮助阅读和理解程序,源程序的内部文档通常包括合适的标识符、注释和(A)。
第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关键词修饰。
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毫秒。
第八章考试题一、选择题:(每题3分,共45题)1.Math类中向下取整的方法是( B )。
A、Ceil()B、floor()C、round()D、abs()2.Java的集合框架中重要的接口java.util.Collection定义了许多方法。
选项中哪个方法不是Collection接口所定义的( C )。
A、int size()B、boolean containsAll(Collection c)C、compareTo(Object obj)D、boolean remove(Object obj)3.list是一个ArrayList的对象,哪个选项的代码填写到//todo delete处,可以在Iterator遍历的过程中正确并安全的删除一个list中保存的对象(D )Iterator it = list.iterator();int index = 0;while (it.hasNext()){Object obj = it.next();if (needDelete(obj)) {//needDelete返回boolean,决定是否要删除//todo delete}index ++;}A、list.remove(obj);B、list.remove(index);C、list.remove(it.next());D、list.remove();4.执行以下代码后,打印出来的y的值是( C )。
int x=8,y=2;if (Math.pow(x,y)= =64)y=x;if (Math.pow(x,y)<63)y=2*x;if (Math.pow(x,y)>63)y=x/2;System.out.println(y);A、2B、8C、4D、165.下列关于日期类说法正确的是( A )A、Calendar是一个抽象类,用来表示日历的概念。
B、Date类after()方法测试该日期是否比参数中指定的的日期迟。
《Java基础入门》课后习题答案第8章GUI(图形用户界面)一、填空题1、在Java 中,图形用户界面简称: GUI2、事件监听器: 负责监听事件源上发生的事件,并对各种事件做出响应处理。
3、GUI 设计提供了一些基本的图形用户接口开发工具,如AWT:Swing 和JavaFX。
4、.如果单击Frame窗口有上角的关闭按相能将其关闭:那么这个Frame窗口添加了:WindowListener 监听器,并实现了监听器:windowClosing(WindowEvent e) 的方法。
5、创建下拉式菜单需要使用三个组件,分别是:JMenuBar、JMenu、JMenuItem二、判断题1、容器(Container)是一个可以包含基本组件和其他容器的组件。
对2.可以通过实现AetionListener 接口或者继承AconAdpter 类来实现动作事件监听器。
错3.CardLayout 布局管理器将界面看做一系列卡片,在任何时候只有其中一张卡片是可见的。
对4.非模态对话框是指用户需要等到处理完对话框后才能继续与其他窗口进行交互。
错5.JFrame 的默认布局管理器是FlowLayout。
错三、选择题1.下面四个组件中哪一个不是JComponent 的子类? (B)A.JButtonB.JDialogC. JLabelD.JMenuBar2.下面四个选项中,哪些是事件处理机制中的角色?(多选) (ABD)A.事件B.事件源C.事件接口D.事件监听器3. ActionEvent 的对象会被传递给以下哪个事件处理器方法? (D)A.addChangeListener()B. addActionListener()C. stateChanged()D. actionPerformed()4.下面哪些是FlowLayout 类中表示对齐方式的常量?(多选) (ABD)A.FlowLayout.LEFTB.FlowLayout.CENTERC. FlowLayout. VERTICALD. FlowLayout. RIGHT5.下列选项中关于JavaFX,描述错误的是(C )A.JavaFX是一个强大的图形和多媒体处理工具包集合B. JavaFX 允许开发者来设计、创建、测试、调试和部署客户端程序C.JavaFX不能与Swing实现互操作D.JavaFX可以实现跨平台功能四、简答题1、请简述GUI 中实现事件监听的步骤:●通过实现XxxListener接口或者继承XxxAdapter类实现一个事件监听器类,并对处理监听动作的方法进行重写●创建事件源对象和事件监听器对象●调用事件源的addXxxLisntener()方法,为事件源注册事件监听器对象2、请简述Swing 中的布局管理器有哪些。
第一章测试1【单选题】(10分)Java属于以下哪种语言?「A.汇编语言「B.机器语言「C.其余选项都不对「D.高级语言参考答案D2【单选题】(10分)下面哪种类型的文件可以在Java虚拟机中运行?()「A..class「B..exe c C.. java r D.【多选题】(8分)Java中的数据类型可以分为两大类,分别是:()和()「A.引用型数据类型「B.整数类型「C.基本数据类型「D.浮点类型参考答案AC9【判断题】(8分)Java语言标识符名称不区分大小写。
「A.对「B.错参考答案B10【判断题】(8分)0xC5表示的是一个十六进制数。
「A.对「B.错参考答案A11【判断题】(10分)continue;语句只用于循环语句中,它的作用是跳出循环。
「A.对「B.错参考答案B12【判断题】(10分)-5%3的运算结果是20「A.B.参考答案A第三章测试1【单选题】(8分)在非静态成员方法中,可以使用关键字()访问类的其他非静态成员。
「A. this 「B. public 「c. super r D. static参考答案A2【单选题】(8分)下面关于类的定义,哪一个是正确的?()「A. publicelassvoidnumber{ //•••)「B.publicclassMOVE(){//•••)c c.publicclassCar{//•••)「D.publicvoidIIH{//…参考答案c3【单选题】(8分)在Java中,针对类和成员提供了4种访问级别,以下控制级别由小到大的顺序是:()O「A.private〈默认<protected<public「B.默认<private<protected<public「C.protected〈默认<private<public「D.protectecKpri vat e〈默认〈public参考答案A4【单选题】(8分)Test类的定义如下:publicclassTest{publicTest(){System. out. printin ("构造方法一被调用了。
Java题库——Chapter8对象和类1)________ represents an entity(实体) in the real world that can be distinctly identified. 1) _______ A)A data field B) An object C)A method D) A class2)________ is a construct that defines objects of the same type. 2) _______A)A method B) A data fieldC)A class D) An object3)An object is an instance of a ________. 3) _______A)data B) method C) class D) program对象是类的⼀个实例4)The keyword ________ is required to declare a class. 4) _______A)private B) class C)public D) All of the above.5)________ is invoked to create an object. 5) _______A)A constructor B)A method with the void return type C)The main method D)A method with a return type6)Which of the following statements are true? (Choose all that apply.) 6) _______A)Constructors must have the same name as the class itself. B)Constructors do not have a return type, not even void. C)Constructors are invoked using the new operator when an object is created. D)A default no-arg constructor is provided automatically if no constructors are explicitly declared in the class. E)At least one constructor must always be defined explicitly. 必须始终显式定义⾄少⼀个构造函数。
Java2实用教程(第四版)课后习题1-8章答案最终习题一(第1章)一、问答题1.James Gosling2.需3个步骤:1)用文本编辑器编写源文件。
2)使用javac编译源文件,得到字节码文件。
3)使用解释器运行程序。
3.由类所构成,应用程序必须有一个类含有public static void main(String args[])方法,含有该方法的类称为应用程序的主类。
不一定,但最多有一个public类。
4.Path设置为:以安装的版本为例。
)ClassPath设置为:set classpath=D:\jdk\jre\lib\;.;5..java和.class6. java Bird7.独行风格(大括号独占行)和行尾风格(左大扩号在上一行行尾,右大括号独占行)二、选择题1.B。
2.D。
三、阅读程序1.(a)。
(b)两个字节码,分别是和。
(c)得到“NoSuchMethodError”,得到“NoClassDefFo undError: Xiti/class”,得到“您好,很高兴认识您nice to meet you”习题二(第2章)一、问答题1.用来标识类名、变量名、方法名、类型名、数组名、文件名的有效字符序列称为标识符。
标识符由字母、下划线、美元符号和数字组成,第一个字符不能是数字。
false不是标识符。
2.关键字就是Java语言中已经被赋予特定意义的一些单词,不可以把关键字作为名字来用。
不是关键字。
class implements interface enum extends abstract。
3.boolean,char,byte,short,int,long,float,double。
4.float常量必须用F或f为后缀。
double常量用D或d为后缀,但允许省略后缀。
5.一维数组名.length。
二维数组名.length。
二、选择题1.C。
2.ADF。
3.B。
4.BE。
5.【代码2】【代码3】【代码4】【代码5】。
第八章第1题分四个部分分别建四个Java文本(1)public class Application {public static void main(String[] args) {new MyFrame("对话框实践");}}(2)import java.awt.*;import java.awt.event.*;public class ExceptionDialog extends Dialog implements ActionListener {Button btn;public ExceptionDialog(Frame f) {super(f,"Exception!",true);btn = new Button("close");Label label = new Label("输入格式有误!",Label.CENTER);add(label,BorderLayout.CENTER);add(btn,BorderLayout.SOUTH);btn.addActionListener(this);addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {setVisible(false);}});pack();setLocation(500,330);setResizable(false);}public void actionPerformed(ActionEvent e) {setVisible(false);}}(3)import java.awt.*;import java.awt.event.*;public class MyDialog extends Dialog {Button yes,no;Label label;String mess = null;public MyDialog(Frame f,boolean b) {super(f,"信息提示",b);label = new Label("您输入的数字> 1000!!!是否输入?");label.setAlignment(Label.CENTER);Container con = new Container();con.setLayout(new GridLayout(1,2));yes = new Button("OK");yes.setForeground(Color.red);no = new Button("Cancle");no.setForeground(Color.red);con.add(yes);con.add(no);add(label,BorderLayout.CENTER);add(con,BorderLayout.SOUTH);addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {setVisible(false);}});pack();setResizable(false);setLocation(510,330);}public void setMess(String mess) {this.mess = mess;bel.setText("您输入的数字" + this.mess + " > 1000!!!是否输入?");}public String getMess() {return this.mess;}public Button getYes() {return this.yes;}public Button getNo() {return this.no;}public void setHide() {yes.setVisible(false);no.setVisible(false);}}(4)import java.awt.*;import java.awt.event.*;public class MyFrame extends Frame implements ActionListener { MyDialog modelDialog;ExceptionDialog exception;TextField num;TextArea dis;public MyFrame(String title) {super(title);modelDialog = new MyDialog(this,true);exception = new ExceptionDialog(this);num = new TextField(20);dis = new TextArea(10,10);dis.setEnabled(false);add(num,BorderLayout.NORTH);add(dis,BorderLayout.CENTER);num.addActionListener(this);modelDialog.getY es().addActionListener(this);modelDialog.getNo().addActionListener(this);addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) { System.exit(1);}});setVisible(true);setLocation(500,300);setResizable(false);pack();}public void actionPerformed(ActionEvent e) { Object obj = e.getSource();if(obj == this.num) {String str = this.num.getText();try {if(Integer.parseInt(str) > 1000) {modelDialog.setMess(str);modelDialog.setVisible(true);}else {dis.append(str + "\n");}}catch (NumberFormatException e1) {exception.setVisible(true);}}if(obj == this.modelDialog.getY es()) {dis.append(this.modelDialog.getMess() + "\n");modelDialog.setVisible(false);}else if(obj == this.modelDialog.getNo()) {modelDialog.setVisible(false);}num.setText(null);}}第八章第2题分四个部分分别建5个Java文本(1)public class Application {public static void main(String[] args) {new MyFrame("Dialog");}}(2)import java.awt.*;import java.awt.event.*;public class MyFrame extends Frame implements Info ,ActionListener { TextField num;TextArea info;String mess;InfoDialog infoDialog;ExceptionDialog exceptionDialog;public MyFrame(String s) {super(s);infoDialog = new InfoDialog(this);exceptionDialog = new ExceptionDialog(this);num = new TextField(30);info = new TextArea(10,30);info.setEnabled(false);num.addActionListener(this);add(num,BorderLayout.NORTH);add(info,BorderLayout.CENTER);addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(1);}});pack();setLocation(480,300);setVisible(true);setResizable(false);}public void actionPerformed(ActionEvent e) { mess = e.getActionCommand();if(mess == null) {exceptionDialog.setVisible(true);}try {if(Long.parseLong(mess) > 1000) {infoDialog.notifyDialog();}else {.append(this.mess + "\n");}}catch(NumberFormatException e1) {exceptionDialog.setVisible(true);}this.num.setText(null);}public String getMess() {return this.mess;}public void setInfo() {.append(this.mess + "\n");}}(3)public interface DisDialog {public void notifyDialog();}(4)public interface Info {public String getMess();public void setInfo();}(5)import java.awt.*;import java.awt.event.*;public class InfoDialog extends Dialog implements ActionListener { Label label;Button yes,no;Info info;public InfoDialog(Frame f) {super(f,"info",true); = (Info)f;yes = new Button("ok");yes.addActionListener(this);no = new Button("no");no.addActionListener(this);Container con = new Container();con.setLayout(new FlowLayout());con.add(yes);con.add(no);add(con,BorderLayout.SOUTH);label = new Label("",Label.CENTER);add(label,BorderLayout.CENTER);addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { setVisible(false);}});setSize(340,90);setLocation(500,330);setResizable(false);}public void actionPerformed(ActionEvent e) { Object obj = e.getSource();String str = info.getMess();this.validate();add(label,BorderLayout.CENTER);if(obj == yes) {info.setInfo();}setVisible(false);}public void notifyDialog() {bel.setText("您输入的数字" + info.getMess() + " > 1000!!!是否确认输入?");this.setVisible(true);}}。
Java程序设计杭州医学院智慧树知到答案2024年第一章测试1.Java语言与其它语言相比,独有的特点是()A:面向对象B:多线程C:网络编程D:平台无关答案:D2.Android系统是电脑桌面操作系统。
()A:错 B:对答案:A3.机器语言是种高级语言()A:对 B:错答案:B4.开发Java可以不用搭建JDK环境()A:错 B:对答案:A5.Eclipse 是Java常用的IDE ()A:对 B:错答案:A第二章测试1.面有关java基本类型的默认值和取值范围,说法错误的是()。
A:字节型的类型默认值是0,取值范围是-27—27-1B:字符型类型默认是0,取值范围是-215 —215-1C:boolean类型默认值是false,取值范围是true\falseD:long类型默认是0,取值范围是-263—263-1答案:B2.IDE中嵌入的控制台能满足程序的输入输出功能()A:对 B:错答案:A3.Java 使用Scanner类进行控制台输入操作()A:对 B:错答案:A4.3days 是Java中合法标识符()A:对 B:错答案:B5.标识符可以为任意长度,但实际使用中应尽量避免过长的命名,以便于代码阅读()A:对 B:错答案:A第三章测试1.若 a 的值为 3 时,下列程序段被执行后, c 的值是多少?()A:2B:3C:4D:1答案:B2.if else语句嵌套不能超过3层()A:错 B:对答案:A3.if语句只作用于紧接着下去的第一句话,所以用大括号将属于某分支的所有语句扩起来是种良好的工程习惯()A:错 B:对答案:B4.假设表达式p为真,表达式q为假,那么p||q为真()A:对 B:错答案:A5.switch 语句适合离散数值的分支处理()A:错 B:对答案:B第四章测试1.以下代码的输出的正确结果是()A:15B:16C:8D:24答案:C2.Java中 Math.ceil(9.9)返回值为 9 ()A:对 B:错答案:B3.Java中 Math.floor(-3.4)返回值为 -3.0 ()A:错 B:对答案:A4.Java中,字符串是用单引号引起来的0个或者多个字符()A:对 B:错答案:B5.ASCII 已经满足了所有编码需求()A:对 B:错答案:B第五章测试1.下列循环语句序列执行完成后,i的值是()A:10B:2C:11D:不确定答案:C2.循环语句的目的之一是为了避免写重复代码()A:错 B:对答案:B3.While 语句, do while语句都可以用来实现循环目的()A:对 B:错答案:A4.break 可以用于if语句中()A:对 B:错答案:B5.continue语句只能用于循环语句中()A:对 B:错答案:A第六章测试1.下列方法定义中,正确的是()A:int x( ){ ...return true; }B:int x( int a, b){ return a+b; }C:int x( ){ char ch=’a’; return (int)ch; }D:void x( ){ ...return true; }答案:C2.函数调用时,提供的参数要和形参一一对应()A:错 B:对答案:B3.函数的实参可以是常量、变量或表达式()A:错 B:对答案:B4.从工程角度讲,不建议过多使用全局变量()A:对 B:错答案:A5.设计函数层次时,我们主要采用自顶向下的思维进行设计()A:对 B:错答案:A第七章测试1.以下说法错误的是()A:在Java中,数组存储在堆中连续内存空间里B:数组的大小可以任意改变C:数组不是一种原生类D:数组是一个对象答案:B2.数组的使用时,需要先定义,后使用()A:错 B:对答案:B3.假设一维数组的长度为10,那么在使用下标访问数组的时候,下标最大可以为10 ()A:对 B:错答案:B4.Java中,如果用=进行数组拷贝,这个过程是深拷贝,改变其中一个数组,不影响另一个数组()A:错 B:对答案:A5.Java中支持可变长数组形参()A:错 B:对答案:B第八章测试1.以下二维数组声明合法的是()A:char[2][3] ch = new char[][]B:char[2][] ch = new char[][3]C:char[][] ch = new char[2][3]D:char[][] ch = new [2]char[3]答案:C2.二维数组的元素按照行优先进行存储,第一行的元素先于第二行的元素存储,以此类推()A:错 B:对答案:B3.三维数组可以理解为以二维数组为元素的一维数组()A:对 B:错答案:A4.Java中,支持二维数组作为函数参数()A:对 B:错答案:A5.Java中,使用new创建二维数组的时候可省略列数,但不可省略行数()A:错 B:对答案:B第九章测试1.下列关于构造方法的叙述中,错误的是()A:Java语言规定构造方法只能通过new自动调用 B:Java语言规定构造方法名与类名必须相同 C:Java语言规定构造方法不可以重载 D:Java语言规定构造方法没有返回值,但不用void声明答案:C2.为了使包ch4在当前程序中可见, 可以使用的语句是()。
Java Web 程序设计教程(范立锋、林果园编著)第8章初识Hibernate框架习题答案1.什么是ORM?ORM就是对象关系映射。
其中的“O”代表的就是对象(object),“R”代表的是关系“relation”,M代表的是映射“mapping”。
其原理就是将对象与表、对象的属性与表的字段分别建立映射关系。
2.Hibernate有那些配置文件?这些配置文件都使用什么语法配置?HIbernate有两个配置文件。
一个配置文件是hibernate.cfg.xml,使用XML语法来配置数据库连接等信息,或者hibernate.properties,使用‘名称’=‘值’的形式配置。
HIbernate的另一个配置文件是映射文件,用来经数据表中的字段信息映射项目中创建的持久化的属性信息。
这样才能使用HIbernate的ORM机制,操作持久化类对象属性的同时就可以对数据中的数据进行更改。
3.简述在Hibernate中使用的映射关系类型。
映射关系类型如下表所示:Java Web程序设计4.Hibernate中用于开始使用Hibernate的入口配置类是什么?入口类是Configuration,该类用来读取HIbernate的配置文件并实例化SessionFactory对象等。
该类的实例化代码如下。
使用属性文件配置HIbernate时:Configuration config=new Configuration();使用配置文件配置HIbernate时:Configuration config=new Configuration().configrue();5.Hibernate中的关联关系都有哪些?实体之间通过关系来相互关联,关系之间有一对一(1:1)、一对多(1:n)和多对多(n:m)的关系。
J a v a语言程序设计第九版第八章答案(总4页)--本页仅作为文档封面,使用时请直接删除即可----内页可以根据需求调整合适字体及大小--Chapter 8 Objects and Classes1. See the section "Defining Classes for Objects."2. The syntax to define a class ispublic class ClassName {}3.The syntax to declare a reference variable for an object isClassName v;4.The syntax to create an object isnew ClassName();5. Constructors are special kinds of methods that are called whencreating an object using the new operator. Constructors do nothave a return type—not even void.6. A class has a default constructor only if the class does notdefine any constructor.7. The member access operator is used to access a data field orinvoke a method from an object.8.An anonymous object is the one that does not have a referencevariable referencing it.9. A NullPointerException occurs when a null reference variable is usedto access the members of an object.10.An array is an object. The default value for the elements of an arrayis 0 for numeric, false for boolean, ‘\u0000’ for char, null forobject element type.11.(a) There is such constructor ShowErrors(int) in the ShowErrors class.The ShowErrors class in the book has a default constructor. It isactually 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 an instance usinga constructor ShowErrors(int), but the ShowErrors class does nothave 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 default constructor. It is actually same aspublic class ShowErrors {public static void main(String[] args) {ShowErrors t = new ShowErrors();();}public ShowErrors () {}}On Line 4, () is invoked, but the ShowErrors class does not have the method named x(). That is an error.(c) The program compiles fine, but it has a runtime error becausevariable c is null when the println statement is executed.(d) new C does not match any constructors in class C. The programhas a compilation error because class C does not have aconstructor with a double argument.12.The program does not compile because new A() is used in class Test,but class A does not have a default constructor. See the second NOTEin the Section, “Constructors.”13.falsee the Date’s no-arg constructor to create a Date for the currenttime. Use the Date’s toString() method to display a stringrepresentation for the Date.e 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 . JFrame and JOptionPane are in . System and Math are in .17. Answer: CorrectAnswer: Correct();Answer: Correct();Answer: CorrectAnswer: IncorrectAnswer: Correct();Answer: Incorrect();Answer: Correct18. Add static in the main method and in the factorial method becausethese two methods don’t need reference any instance objects orinvoke any instance methods in the Test class.19. You cannot invoke an instance method or reference an instancevariable from a static method. You can invoke a static method orreference a static variable from an instance method c is an instancevariable, which cannot be accessed from the static context in method2.20. Accessor method is for retrieving private data value and mutator method is for changing private data value. The naming convention for accessor method is getDataFieldName() for non-boolean values and isDataFieldName() for boolean values. The naming convention for mutator method issetDataFieldName(value).21. Two benefits: (1) for protecting data and (2) for easy to maintainthe class.22. Not a problem. Though radius is private, is used inside the Circleclass. Thus, it is fine.23. Java uses “pass by value” to pass parameters to a method. Whenpassing a variable of a primitive type to a method, the variableremains unchanged after the method finishes. However, when passinga variable of a reference type to a method, any changes to theobject referenced by the variable inside the method are permanentchanges to the object referenced by the variable outside of themethod. Both the actual parameter and the formal parametervariables reference to the same object.The output of the program is as follows:count 101times 024.Remark: The reference value of circle1 is passed to x and the reference value of circle2 is passed to y. The contents of the objects are not swappedin the swap1 method. circle1 and circle2 are not swapped. To actually swap the contents of these objects, replace the following three linesCircle temp = x;x =y;y =temp;bydouble temp = ;= ;= temp;as in swap2.25. a. a[0] = 1 a[1] = 2b. a[0] = 2 a[1] = 1c. e1 = 2 e2 = 1d. t1’s i = 2 t1’s j = 1t2’s i = 2 t2’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.)。
1.何为线程和多线程?如何建立多线程?
答:线程:是一个程序内部的顺序控制流
两种方式,第一种继承Thread,第二种实现Runnable
2.线程的生命周期由哪几种状态?各状态之间分别用哪些方法切换?
答:新建、就绪、运行、阻塞、死亡五种状态。
1、线程通过new方法创建,调用start,线程进入就绪状态,等待系统的调度(时间
片轮转调度)。
当系统调度,进入运行状态。
正常结束或者异常退出,进程进入死亡状态。
2、处于运行状态的线程若遇到sleep,则线程进入睡眠状态,不会让出资源锁,sleep
结束,线程转为就绪状态,等待系统重新调度。
3、处于运行状态的线程可能在等待io,也可能进入挂起状态。
io完成,转为就绪状态。
4、处于运行状态的线程yield,线程转为就绪状态。
(yield只让给权限比自己高的)
5、处于运行状态的线程遇到wait,线程处于等待状态,需要notify()/notifyALL来
唤醒线程,唤醒后的线程处于锁定状态,获取了“同步锁”,之后,线程才转为就绪状态。
处于运行的线程synchronized,加上后变成同步操作。
处于锁定状态,获取了“同步锁”,之后,线程才转为就绪状态。
3.Java程序实现多线程有哪些途径?
•答:1.继承Thread类,重写run方法
• 2.实现Runnable接口,重写run方法,实现Runnable接口的实现类
• 3.通过Callable和FutureTask创建线程的实例对象作为Thread构造函数的taget
4.通过线程池创建线程。