java第八章答案
java习题及答案第8章 习题参考答案

exitItem.setText("退出");
}
});
…
8.6简述使用面板的原因,编写一个继承自JPanel的面板类MyPanel。
答:用面板可以实现对所有组件进行分层管理,即对不同关系的组件采用不同的布局管理方式,使组件的布局更加合理和程序的界面更加美观。
//创建文件下拉式菜单,并添加到菜单栏
JMenufilemenu= new JMenu("文件");
mbar.add(filemenu);
//创建菜单项,并添加到文件菜单下
JMenuItemopenfileItem= new JMenuItem("打开文件");
JMenuItem closefileItem= new JMenuItem("关闭文件");
编程提示:编写继承自JPanel的面板类MyPanel时,可在MyPanel类中直接放置上面板要放置的组件,在使用该面板时就可以直接放置在窗体上了,例如可创建一个LoginPanel,上面放上用户名和密码输入框及其文本标签,并添加登录和退出按钮和相关代码。
8.7对比各种布局管理方式,指出各自的应用场合。
答:常见的布局管理器有边界布局(BorderLayout)、顺序布局(FlowLayout)、网格布局(Gri不用布局管理器)。其中,各种布局管理方式与适合应用的场合如下:
空布局管理是直接定位的方式排列容器中的组件,适合只在某一平台运行的程序采用;
//JMenuItemexitItem=new JMenuItem("系统退出");
filemenu.add(openfileItem);
Java面向对象程序设计智慧树知到答案章节测试2023年

第一章测试1.下列选项中,可以正确配置classpath的命令是() A:set classpath=C:Files.0_15 B:classpath set : C:Files.0_15 C:classpath set =C:Files.0_15 D:set classpath : C:Files.0_15 答案:A2.在JDK安装目录下,用于存放可执行程序的文件夹是() A:bin B:db C:jreD:lib 答案:A3.下面关于Java程序的描述中,错误的是() A:不同操作系统上的Java虚拟机是相同的 B:Java程序运行时,必须经过编译和运行两个步骤。
C:.class 格式的文件是用来运行的 D:.java格式的源文件是用来编译的答案:A4.下列选项中,哪些选项用于构成JDK开发环境()。
A:Java运行工具B:Java编译工具 C:Java文档生成工具 D:Java打包工具答案:ABCD5.下列关于使用Javac命令编译后生成文件的说法中,正确的是() A:编译后生成文件的后缀名为.class B:编译后生成的文件可以在Java虚拟机中运行C:编译后生成的文件为二进制文件 D:编译后生成文件的后缀名为.java 答案:ABC6.下列目录中,哪些属于JDK安装后的子目录() A:jre目录 B:lib目录C:include目录 D:demo目录答案:ABC7.lib是library的缩写,用于存放Java类库或库文件。
( ) A:对 B:错答案:A8.如果classpath环境变量没有进行设置,Java虚拟机会自动将其设置为“.”,也就是当前目录。
( ) A:错 B:对答案:B9.在任意命令行窗口中执行javac命令,如果都能正常地显示帮助信息,说明系统path环境变量配置成功。
( ) A:对 B:错答案:A10.Eclipse只是一个框架和一组服务,所有功能都是将插件组件加入到Eclipse框架中来实现的。
Java程序设计知到章节答案智慧树2023年信阳农林学院

Java程序设计知到章节测试答案智慧树2023年最新信阳农林学院第一章测试1.Java程序中的注释有哪几种?参考答案:/* 多行注释 */;//单行注释;/** 文档注释 */2.以下选项中,哪些属于JDK工具?参考答案:Java运行工具;Java编译器;Java打包工具;Java文档生成工具3.以下关于支持Java运行平台的叙述,哪项错误?参考答案:Java语言与平台无关。
Java程序的运行结果依赖于操作系统4.下面哪种类型的文件可以在Java虚拟机中运行?参考答案:.class5.JDK、JRE和JVM之间的关系是()参考答案:JDK包括JRE,JRE包括JVM第二章测试1.下列代码执行后,输出到控制台的内容是?public class Test{public static void main(String args[]){int m=6789;int[] a=new int[4];for(int i=0;i<a.length;i++){a[i]=m%10;m=m/10;}System.out.printf("%d%d%d%d",a[0],a[1],a[2],a[3]);}}参考答案:98762.下列代码执行后,输出到控制台的内容是?public class Test{public static void main(String args[]){int m=100,n=10;if (m>n){n=100;m=10;}elsen=-100;m=-99;System.out.printf("%d:%d",m,n);}}参考答案:-99:1003.下列ABCD注释标注的哪行代码有错误?public class Test{public static void main(String args[]){int a[] ={1,2,3,4}; //Afor(int n=0;n<a.length;n++){ //BSystem.out.println(a[n]);}for(int i:a){ //CSystem.out.println(i);}int m=0;for(m:a){ //DSystem.out.println(m);}}}参考答案:D4.下列ABCD注释标注的哪行代码有错误?public class Test{public static void main(String args[]){int m=12;int n=12;int number=100+'w'; //Aswitch(number){case 12:n++; //Bcase 100:n--; //Cbreak;case n: m++; //D}}}参考答案:D5.下列语句序列执行后,k 的值是()。
Java语言程序设计第九版第八章答案

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.)。
Java程序设计 第8章习题参考答案[2页]
![Java程序设计 第8章习题参考答案[2页]](https://img.taocdn.com/s3/m/279f7cf1f605cc1755270722192e453610665b0d.png)
第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语言程序设计-基础篇》答案-第08章

8.19 答: (a) 输出结果如下:
j is 3 i is 1 (b) 输出结果如下: i is 0 j is 4 i is 5 j is 4
8.20 答:(略)
8.21 答:结果是
z对 z错 z对 z对 z错 z错 z对 z错 z错
网 案 答 后 课
a 改为:return super.findArea() * length; hd 8.4 答:(略),自行查阅课本。 .k 8.5 答:每个类均有。
w 从公共父类 ng.Object 继承而来。 ww 创建对象后可以通过对象引用直接调用它们。 网 8.6 答:输出结果如下:
B’s constructor is invoked
1
7
网
案 8.12 答:无修饰符(包作用域)。说明:该题应为“应该在一个类的成员前面……”。
答 8.13 答:protected 修饰符。说明:该题应为“应该在一个类的成员前面……”。
后 8.14 答:类 B 能正常编译。 课 8.15 答:?处应该 protected,当然 public 是一定可以的。
案 A’s constructor is invoked 答 调用 new A(3)时,会调用 Object 的无参构造方法。 后 8.7 答:结果如下: 课 对(a)中的类 Circle,Test 类主方法输出 false;
对(b)中的类 Circle,Test 类主方法输出 false;
原因:
(a)中的 equals 方法,由于参数类型与其父类 Object 的 equals 方法 不同,所以没有形成覆盖,则在 Test 类方法中调用的是父类 Object 的 equals 方法,因此不能正确比较 2 个圆对象是否相等地。 (b)中的 equals 方法,形成了对父类 Object 的 equals 方法的覆盖, 则在 Test 类方法中调用的是子类 Circle 的 equals 方法,因此能正 确比较。
Java程序设计第八章作业

一、选择题1.下列选项中不属于多线程作用的是_______C____。
A)使多CPU系统更加有效 B)改善程序结构C)提高内存存储空间 D)提高应用程序响应能力2.在多线程中,当某种资源发生什么状况时被称为临界资源____C_______。
A)被系统占用C)多个线程共享B)多个进程共享D)达到最大或最小值3.利用继承Thread类实现多线程,在Thread子类中,下列方法中必须被实现的是_____A______。
A)run() B)start() C)Thread() D)sleep()4.线程生命周期中正确的状态是____C_____。
A)新建状态、运行状态和终止状态B)新建状态、运行状态、阻塞状态和终止状态C)新建状态、可运行状态、运行状态、阻塞状态和终止状态D)新建状态、可运行状态、运行状态、恢复状态和终止状态5.Thread类中能运行线程体的方法是___A________。
A)run() B)start() C)init() D)resume()6.下列的方法中可以用来创建一个新线程是____C_______。
A)实现ng.Runnable 接口并重写start()方法B)实现ng.Runnable 接口并重写run()方法C)继承ng.Thread 类并重写run()方法D)实现ng.Thread 类并实现start()方法7.下列关于线程优先级的说法中,正确的是_____C______。
A)线程的优先级是不能改变的B)线程的优先级是在创建时指定的C)在创建线程后的任何时候都可以设置D) B和C8.下列关于线程调度的说法中,不正确的是____D_______。
A)调用线程sleep()方法,可以使比当前线程优先级低的线程获得运行机会B)调用线程yield()方法,只会使与当前线程相同优先级低的线程获得运行机会C)当有比当前线程优先级高的线程出现时,高优先级的线程将抢占CPU并运行D)具有相同优先级的多个线程的调度一定是分时的9.调用线程的下列方法,不会改变该线程在生命周期中状态的方法是_B____。
自考Java语言程序设计(一)第八章Java异常处理及输入输出流简介课后习题

自考Java语言程序设计(一)第八章Java异常处理及输入输出流简介课后习题
八、Java异常处理及输入输出流简介
17.编写一个程序,在当前目录下创建一个子目录test,在这个新创建的子目录下创建一个文件,并把这个文件设置成只读。
18.位置指针的作用是什么?RandomAccessFile类提供了哪些方法实现对指针的控制?
19.编写一个程序,从键盘输入一串字符,统计这串字符中英文字母、数字、其他符号的字符数。
throw new Exception();
}
catch(Exception e)
{
System.out.println("catch3");
}
finally
{
System.out.println("final
说明:自定义异常类,关键是选择继承的超类——必须是Exception或者其子类。用异常代表错误,而不要再使用方法返回值。
}
finally
{ System.out.println("执行d Finally"); }
}
}
7.答:无论是出于何种原因,只要执行离开try/catch代码块,就会执行finally代码块。即无论try是否正常结束,都会执行 finally定义的最后的代码。如果try代码块中的任何代码或它的任何catch语句从方法返回,也会执行finally代码块。但本题中在try代 码块中执行了“System.exit(0);”语句,执行了这一语句后,Java虚拟机(JVM)将被终止。那么finally语句块一定不会被执行。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
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毫秒。
Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}}}//test8_3public class test8_3 {public static void main(String[] args) {// 将创建一个线程对象,这个对象接受一个实现了Runnable接口。
实际上这里也就是使用run()方法runnable r1=new runnable("广州");runnable r2=new runnable("乌鲁木齐");Thread t1 = new Thread(r1);Thread t2 = new Thread(r2);// 启动线程t1.start();t2.start();}}运行结果分别为:4.编写一个多线程程序实现如下功能:线程A和线程B分别在屏幕上显示信息“…start”后,调用wait等待;线程C开始后调用sleep休眠一段时间,然后调用notifyall,使线程A和线程B继续运行。
线程A和线程B恢复运行后输出信息“…end”后结束,线程C在判断线程B和线程A结束后自己结束运行。
//test8_4public class test8_4 {Thread A = new Thread("A") {public void run() {Wait("A");}};Thread B = new Thread("B") {public void run() {Wait("B");}};Thread C = new Thread("C") {public void run() {while (true) {if (!A.isAlive() && !B.isAlive())return;try {Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}notifyall();}}};public synchronized void Wait(String name) {System.out.println(name + "..start");try {wait();} catch (InterruptedException e) {e.printStackTrace();}System.out.println(name + "..end");}public synchronized void notifyall() {notifyAll();}public static void main(String args[]) {test8_4 test = new test8_4();//A、B两线程一起输入start和输出end,不过中间有C让线程休眠2秒,没法全部一次性输出,//之后再唤醒,让AB继续输出下半部分endtest.A.start();test.B.start();test.C.start();}}运行结果:4.lass test implements Runnable{public test(){ }public void run(){if(Thread.currentThread().getName().equals("A")){System.out.println("A...start");synchronized(this){try{wait();System.out.println("A...end");notify();}catch(Exception e){}}}else if(Thread.currentThread().getName().equals("B")){ System.out.println("B...start");synchronized(this){try{wait();System.out.println("B...end");}catch(Exception e){}}}else if(Thread.currentThread().getName().equals("C")){ try{Thread.sleep(10);synchronized(this){notify();}}catch(Exception e){}}}}public class d {public static void main(String args[]) {test show=new test();Thread A=new Thread(show,"A");Thread B=new Thread(show,"B");Thread C=new Thread(show,"C");A.start();B.start();C.start();}}5.实现一个数据单元,包括学号和姓名两部分。
编写两个线程,一个线程往数据单元中写,另一个线程往外读。
要求没写一次就往外读一次。
//Data类import java.util.Scanner;public class Data{String studentId;String name;boolean available = false;// 判断是读是写Scanner in = new Scanner(System.in);// 定义一个输入对象public synchronized void read(){if(available)try{wait();}catch(Exception e){}System.out.printf("请输入学号:");try{studentId=in.next();}catch(Exception e){System.out.println("输入学号出错!");}System.out.printf("请输入姓名:");try{name=in.next();}catch(Exception e){System.out.println("输入姓名出错!");}System.out.println();available=true;notify();}public synchronized void write(){if(!available)try{wait();}catch(Exception e){}System.out.println("输出学生学号:"+studentId+" 姓名:"+name+"\n"); available=false;notify();}}//Read类public class Read extends Thread{Data d1 = null;public Read(Data d){this.d1=d;}public void run(){while(true){d1.read();}}}//Write类class Write extends Thread{Data d2=null;public Write(Data d){this.d2=d;}public void run(){while(true){d2.write();}}}//test8_5类public class test8_5 {public static void main(String[] args){Data data=new Data();new Read(data).start();new Write(data).start();}}运行结果:6.创建两个不同优先级的线程,都从1数到10000,看看哪个数得快。