1、计算机二级 JAVA-144及答案解析(总分:61.00,做题时间:90 分钟)一、1基本操作题(总题数:1,分数:30.00)1.10位同学参加某次团队测试,要求每位同学都必须及格、同时团队平均分不少于 80分,整个团队才能够通过。每位同学的成绩可以通过随机数产生(0100)。请在程序的每条横线处填写一条语句,是程序的功能完整。 注意:请勿改动 main()主方法和其他已有的语句内容,仅在横线处填入适当的语句。 源程序文件代码清单如下: _; public class While public static void main(String args) int fenshu=60; int
2、sumfenshu=0; int i=1; while(fenshu=60) System. out. print in (fenshu+“ “); sumfenshu+=fenshu; i+; System.out.println(); if(_) System. out. println ( “团队通过“) else System. out.println (“团队测试不通过“); (分数:30.00)_二、2简单应用题(总题数:1,分数:1.00)2.请完成下列 Java程序:假设某家银行,它可接受顾客的汇款,每做一次汇款,便可计算出汇款的总额。现有两个顾客,每人都分 3次,每次 50元
3、将钱汇入。编写一个程序,模拟实际作业。要求实现 2个类,一个是银行类,一个是顾客类。 注意:请勿改动 main()主方法和其他已有语句内容,仅在下划线处填入适当的语句。 程序运行结果如下: Customer2 :sum= 50 Customer1 :sum= 50 Customer1 :sum= 100 Customer2 :sum= 100 Customer1 :sum= 150 Customer2 :sum= 150 class bank private static int sum=0; public static void add(int.n,char c) int tmp=sum;
4、_; try Thread.sleep(int) (2000-500+1)*(Math.random()+500); catch(InterruptedException e) sum=tmp; System.out.println(“Customer“+c+“ :sum= “+sum); class customer extends Thread static char flag17_2 = “1“; public void run() char myflag17_2; synchronized(this) myflag17_2 = flag17_2+; for(int i=1;i=3;i+
5、) _; public class ex17_2 public static void main(String args) customer c1=new customer(); customer c2=new customer(); c1.start(); c2.start(); (分数:1.00)_三、3综合应用题(总题数:1,分数:30.00)3.本题程序的功能是用表格的形式列出当前目录所有的文件属性,包括是否是文件夹、文件名称、是否可读、是否可写、文件大小以及最后修改时间。程序中存在若干错误,请找出并改正(注意:不得改动程序的结构,不得增行或删行)。 import java.awt.*
6、; import javax.swing.*; import java.util.Date; import java.io.File; import javax.swing.table.*; public class advance extends Jframe public advance() super(“advance“); setSize(500,400); setDefaultCloseOperation(EXIT_ON_CLOSE); FileModel fm = new FileModel(); JTable jt = new JTable(fm); jt.setAutoResi
7、zeMode(JTable.AUTO_RESIZE_OFF); jt.setColumnSelectionAllowed(true); jt.setDefaultRenderer(Number.class,new BigRenderer(1000); JScrollPane jsp = new JScrollPane(jt); getContentPane().add(jsp,BorderLayout.CENTER); public static void main (String args) advance ft : new advance(); ft.setVisible(true); c
8、lass BigRenderer extends DefaultTableCellRenderer double threshold; public BigRenderer(double t) threshold = t; setHorizontalAlignment(JLabel.RIGHT); setHorizontalTextPosition(SwingConstants.RIGHT); public Component getTableCellRendererComponent(JTable table,Object value,boolean isSelected,boolean h
9、asFocus,int row,int col) return super.getTableCellRendererComponent(table,value,isSelected,hasFocus,row,col); class FileModel extends AbstractTableModel String titles = new String “目录“,“文件名“,“可读?“,“可写?“,“大小“,“最后修改时间“ ; Class types = new Class Boolean.class,String.class,Boolean.class,Boolean.class, N
10、umber.class,Date.class ; Object data; public FileModel() this(“.“); public FileModel(String dir) File pwd = new File(dir); setFileStats (pwd); public int getRowCount() return data.length; public int getColumnCount() return titles.length; public String getColumnName(int c) return titles c; public Cla
11、ss getColumnClass(int c) return typesc; public void getValueAt(int r,int c) return datar c; public void setFileStats(File dir) String files = dir.list; Data = new Objectfiles.length titles.length; for (int i = 0; ifiles.length; i+) File tmp = new File(filesi); datai 0 = new Boolean(tmp.isDirectory()
12、; datai 1 = tmp.getName(); datai 2 = new Boo lean(tmp.canRead(); datai 3 = new Boolean(tmp.canWrite(); datai 4 = new Long(tmp.length(); datai 5 = new Date(tmp.lastModified(); fireTableDataChanged (); (分数:30.00)_计算机二级 JAVA-144答案解析(总分:61.00,做题时间:90 分钟)一、1基本操作题(总题数:1,分数:30.00)1.10位同学参加某次团队测试,要求每位同学都必须及
13、格、同时团队平均分不少于 80分,整个团队才能够通过。每位同学的成绩可以通过随机数产生(0100)。请在程序的每条横线处填写一条语句,是程序的功能完整。 注意:请勿改动 main()主方法和其他已有的语句内容,仅在横线处填入适当的语句。 源程序文件代码清单如下: _; public class While public static void main(String args) int fenshu=60; int sumfenshu=0; int i=1; while(fenshu=60) System. out. print in (fenshu+“ “); sumfenshu+=fens
14、hu; i+; System.out.println(); if(_) System. out. println ( “团队通过“) else System. out.println (“团队测试不通过“); (分数:30.00)_正确答案:()解析:import javamath.*; Mathrandom()*100 (i=10) public static void add(int.n,char c) int tmp=sum; _; try Thread.sleep(int) (2000-500+1)*(Math.random()+500); catch(InterruptedExcep
15、tion e) sum=tmp; System.out.println(“Customer“+c+“ :sum= “+sum); class customer extends Thread static char flag17_2 = “1“; public void run() char myflag17_2; synchronized(this) myflag17_2 = flag17_2+; for(int i=1;i=3;i+) _; public class ex17_2 public static void main(String args) customer c1=new cus
16、tomer(); customer c2=new customer(); c1.start(); c2.start(); (分数:1.00)_正确答案:()解析:tmp=tmp+n bank.add(50,myflag17_2) 解析 本题主要考查线程的同步和设计简单的类来模拟现实问题的简单应用。解题关键是熟练掌握面向对象的编程思想,熟悉 Java 线程的同步编程,会使用 Math类的随机数方法。本题中,第 1个空,银行类对客户的存款进行累加;第 2个空,在客户类中,通过使用银行类的对象 bank调用 add()方法实现 3次汇款的操作,将钱数和客户标志作为参数传递给 add()方法。 三、3
17、综合应用题(总题数:1,分数:30.00)3.本题程序的功能是用表格的形式列出当前目录所有的文件属性,包括是否是文件夹、文件名称、是否可读、是否可写、文件大小以及最后修改时间。程序中存在若干错误,请找出并改正(注意:不得改动程序的结构,不得增行或删行)。 import java.awt.*; import javax.swing.*; import java.util.Date; import java.io.File; import javax.swing.table.*; public class advance extends Jframe public advance() super(
18、“advance“); setSize(500,400); setDefaultCloseOperation(EXIT_ON_CLOSE); FileModel fm = new FileModel(); JTable jt = new JTable(fm); jt.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); jt.setColumnSelectionAllowed(true); jt.setDefaultRenderer(Number.class,new BigRenderer(1000); JScrollPane jsp = new JScroll
19、Pane(jt); getContentPane().add(jsp,BorderLayout.CENTER); public static void main (String args) advance ft : new advance(); ft.setVisible(true); class BigRenderer extends DefaultTableCellRenderer double threshold; public BigRenderer(double t) threshold = t; setHorizontalAlignment(JLabel.RIGHT); setHo
20、rizontalTextPosition(SwingConstants.RIGHT); public Component getTableCellRendererComponent(JTable table,Object value,boolean isSelected,boolean hasFocus,int row,int col) return super.getTableCellRendererComponent(table,value,isSelected,hasFocus,row,col); class FileModel extends AbstractTableModel St
21、ring titles = new String “目录“,“文件名“,“可读?“,“可写?“,“大小“,“最后修改时间“ ; Class types = new Class Boolean.class,String.class,Boolean.class,Boolean.class, Number.class,Date.class ; Object data; public FileModel() this(“.“); public FileModel(String dir) File pwd = new File(dir); setFileStats (pwd); public int g
22、etRowCount() return data.length; public int getColumnCount() return titles.length; public String getColumnName(int c) return titles c; public Class getColumnClass(int c) return typesc; public void getValueAt(int r,int c) return datar c; public void setFileStats(File dir) String files = dir.list; Dat
23、a = new Objectfiles.length titles.length; for (int i = 0; ifiles.length; i+) File tmp = new File(filesi); datai 0 = new Boolean(tmp.isDirectory(); datai 1 = tmp.getName(); datai 2 = new Boo lean(tmp.canRead(); datai 3 = new Boolean(tmp.canWrite(); datai 4 = new Long(tmp.length(); datai 5 = new Date(tmp.lastModified(); fireTableDataChanged (); (分数:30.00)_正确答案:()解析:第 48行的“Class types=new Class“改为“Class types=new Class“。 第 79行的“public void getValueAt(int r,int c)“改为“public Object getValueAt(int r,int c)“。 第 85行的“String files=dir.list“改为“String files=dir.list()“。