【计算机类职业资格】国家二级(JAVA)机试-试卷23及答案解析.doc
《【计算机类职业资格】国家二级(JAVA)机试-试卷23及答案解析.doc》由会员分享,可在线阅读,更多相关《【计算机类职业资格】国家二级(JAVA)机试-试卷23及答案解析.doc(4页珍藏版)》请在麦多课文档分享上搜索。
1、国家二级(JAVA)机试-试卷 23 及答案解析(总分:12.00,做题时间:90 分钟)一、基本操作题(总题数:2,分数:4.00)1.基本操作题()(分数:2.00)_2.本程序的功能是,从键盘输入一个整数,存入一个输入流中,然后输出它的两倍值。请将程序补充完整。注意:请勿修改 main()主方法和其他已有语句内容,仅在横线处填入适当语句。 import java.io.*; public class basic public static void main(String args) File dir=new File(“.“); Filter filter=new Filter(“ja
2、va“); System.out.println(“list java files in directory“ +dir); String files=dir.list(filter); /列出目录 dir 下,文件后缀名为 java 的所有文件 for(_;_;i+) File f=new File(dir,filesi); _ (f.isFile() /如果该对象为后缀为 java 的文件,则打印文件名 System.out.println(“file“ +f); else System.out.println(“sub directory“ +f); /如果是目录则打印目录名 class
3、 Filter implements FilenameFilter String extent; Filter(String extent) this.extent=extent; public boolean accept(File dir,String name) return name.endswith(“.“+extent);/返回文件的后缀名 (分数:2.00)_二、简单应用题(总题数:2,分数:4.00)3.简单应用题()(分数:2.00)_4.请完成程序,首先由一个类 simple 实现 Serializable 接口,并有三个成员变量,分别为 int 型、double 型和 S
4、tring 型,可以用 toString 的方法显示这三个成员变量。在 main 方法中创建这个 simple的持久对象,根据用户在命令行输入的三个参数来设定其中成员变量的值。然后,将这个对象写入名为TheSerial.data 的文件中,并显示成员变量。最后从文件 TheSerial.data 中读出三个成员变量并显示出来。 注意:请勿修改 main()主方法和其他已有语句内容,仅在横线处填入适当语句。 import java.io.*; class TheSerial implements Serializable private int intvalue; private double
5、doublevalue; private String string; The Serial () intvalue=123; doublevalue=12.34; string=“Serialize Test“; public void setDouble(double d) doublevalue=d; public void setInt(int i) intvalue=i; public void setString(String s) string=s; public String to String() return(“int=“+intvalue+“ double=“+doubl
6、evalue+“ string=“+string); public class simple public static void main(String args) The Serial e1=new TheSerial(); TheSerial e2; try e1.setInt(Integer.parseInt(args0); e1.setDouble(Double.parseDouble(args1); e1.setString(args2); catch(Exception e) e1.setString(e.getMessage(); System.out.println(e1);
7、 try FileOutputStream oS=new FileOutputStream(“TheSerial.data“); ObjectOutputStream oOS=new ObjectOutputStream(oS); _; catch(IOException ioException) System.out.println (ioException.getMessage (); try FileInputStream iS=new FileInputStream(“TheSerial.data“); ObjectInputStream oIS=new ObjectInputStre
8、am(iS); _; System.out.println(e2); catch(IOException ioException) System.out.println(ioException.getMessage(); catch(ClassNotFoundException cnfException) System.out.println(cnfException.getMessage(); (分数:2.00)_三、综合应用题(总题数:2,分数:4.00)5.综合应用题()(分数:2.00)_6.本题程序中实现了一个“生产者一消费者问题”。生产者产生一个随机数存入 DataPool 类中,
9、消费者从中取出数据。DataPool 类一次只能存放一个数据。请更正题中带下划线的部分。 注意:不改变程序的结构,不得增行或删行。 class DataPool private int data; private boolean isFull; public DataPool() isFull=false; public synchronized void putData(int d) if(isFull= =true) try this.notify(); catch(InterruptedException e) data=d; isFull=true; System.out.printl
10、n(“生产了一个数据:“+data); this.notify(); public synchronized int getData() if(isFull= =false) try this.wait(); catch(InterruptedException e) isFull=false; System.out.println(“消费了一个数据“+data); this.wait(); return this.data; boolean getIsFull() return isFull; class Producer extends Thread DataPool pool; publ
11、ic Producer(DataPool pool) this.pool=pool; public void run() for(int i=0; i10; i+) int data=(int) (Math.random()*1000); try /用于生产数据 sleep(data); catch(InterruptedException e) pool.putData(data); class Consumer implements Runnable DataPool pool; public Consumer(DataPool pool) this.pool=pool; public v
12、oid run() for(int i=0; i10; i+) int data=pool.getData(); try /用于处理数据 sleep(int) (Math.random()*1000); catch(InterruptedException e) public class advance public static void main(String args) Data Pool pool=new Data Pool(); Producer pro=new Producer(pool); Runnable con=new Consumer(pool); Thread conTh
13、=new Thread(con); pro.start(); conTh.start(); (分数:2.00)_国家二级(JAVA)机试-试卷 23 答案解析(总分:12.00,做题时间:90 分钟)一、基本操作题(总题数:2,分数:4.00)1.基本操作题()(分数:2.00)_解析:2.本程序的功能是,从键盘输入一个整数,存入一个输入流中,然后输出它的两倍值。请将程序补充完整。注意:请勿修改 main()主方法和其他已有语句内容,仅在横线处填入适当语句。 import java.io.*; public class basic public static void main(String
14、args) File dir=new File(“.“); Filter filter=new Filter(“java“); System.out.println(“list java files in directory“ +dir); String files=dir.list(filter); /列出目录 dir 下,文件后缀名为 java 的所有文件 for(_;_;i+) File f=new File(dir,filesi); _ (f.isFile() /如果该对象为后缀为 java 的文件,则打印文件名 System.out.println(“file“ +f); else
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
5000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 计算机 职业资格 国家 二级 JAVA 试试 23 答案 解析 DOC
