1、二级 JAVA 机试 45 及答案解析(总分:100.00,做题时间:90 分钟)一、B1基本操作题/B(总题数:1,分数:30.00)1.下面的程序是用 do_while 语句计算 10 的阶乘。请在程序的每条横线处填写一个语句,使程序的功能完整。 注意:请勿改动 main()主方法和其他已有的语句内容,仅在横线处填入适当的语句。 public class DoWhileLoop public static void main(_) int n=10; long result=1 do _ _ System. out. println(“10 的阶乘为:“+result); (分数:30.0
2、0)_二、B2简单应用题/B(总题数:1,分数:40.00)2.请完成下列 Java 程序:假设某家银行,它可接受顾客的汇款,每做一次汇款,便可计算出汇款的总额。现有两个顾客,每人都分 3 次,每次将 50 元钱汇入。编写一个程序,模拟实际作业。要求实现 2 个类,一个是银行类,一个是顾客类。 注意:请勿改动 main()主方法和其他已有语句内容,仅在下划线处填入适当的语句。 程序运行结果如下: Customer2 : sum=50 Customer1 : sum=50 Customer1 : sum=100 Customer2 : sum=100 Customer1 : sum=150 Cu
3、stomer2 : sum=150 class bank private static int sum=0; public static void add(int n,char c) int tmp=sum; _; 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
4、 char flag4_1 =1 public void run() char myflag4_1, synchronized(this) myflag4_1= flag4_1+; for (int i=1;i=3;i+) _; public class ex4_1 public static void main(String args) customer c1 = new customer( ); customer c2 = new customer( ) c1. start() c2. start( ) (分数:40.00)_三、B3综合应用题/B(总题数:1,分数:30.00)3.下面是
5、一个 Applet 程序,其功能是实现一个计数器,每隔 0.15 秒计数器数值加 1,数值动态变化,并且能够控制计数器的暂停和继续。要求通过使用 swing 的构件建立图形用户界面,主要包括一个文本区域,用于显示计数器结果;两个按钮,一个使计数器暂停,一个使计数器继续工作。请改正程序中的错误(有下划线的语句),使程序能输出正确的结果。注意:不改动程序的结构,不得增行或删行。 import javax, swing. * import java. awt. * import java. awt. event. * /* applet code= “ex4_2. class“ width=800
6、height=400 /applet */ public class ex4_2 extends JApplet private JTextField jtf=new JTextField(15); private JButton Hold = new JButton (“Hold“), resume = new JButton ( “Resume“ ); private ex4_2th obj4_2th= new ex4_2th(); class ex4_2th extends Thread private int cnt=0; private boolean bIsHold=false;
7、public ex4_2th() start(); public void hold() bIsHold=true public synchronized void fauxResume() bIsHold=false; Uwait( )/U; public void run() while (true) try sleep(150) synchronized(this) while(bIsHold) Unotify( )/U; catch(InterruptedException ie) System. err. println(“Interrupted“); Ujtf. setText(c
8、nt)/U; public void init() Container cp = getContentPane( ) cp. setLayout(new FlowLayout( ) ); cp. add(jtf) Hold. addActionListener( new ActionListener() public void actionPerformed(ActionEvent ae) obj4_2th. hold() ); cp. add(Hold); resume, addActionListener ( new ActionListener() public void actionP
9、erformed(ActionEvent e) obj4_2th. fauxResume( ) ); cp. add(resume) public static void main(String args) ex4_2 obj4_2 =new ex4_2() String str= obj4_2. getClass(), toString(); if (str. indexOf(“class“)!=-1) str=str. substring (6) JFrame frm=new JFrame(str) frm. addWindowListener(new WindowAdapter() pu
10、blic void windowClosing(WindowEvent we) System. exit(0); ); frm. getContentPane(), add(obj4_2) frm. setSize(300,200) obj4_2. init() obj4_2. start( ); frm. setVisible(true) ex4_2. html HTML HEAD TITLEex4_2/TITLE /HEAD BODY applet code=“ex4_2. class“ width=800 height=400 /applet /BODY /HTML(分数:30.00)_
11、二级 JAVA 机试 45 答案解析(总分:100.00,做题时间:90 分钟)一、B1基本操作题/B(总题数:1,分数:30.00)1.下面的程序是用 do_while 语句计算 10 的阶乘。请在程序的每条横线处填写一个语句,使程序的功能完整。 注意:请勿改动 main()主方法和其他已有的语句内容,仅在横线处填入适当的语句。 public class DoWhileLoop public static void main(_) int n=10; long result=1 do _ _ System. out. println(“10 的阶乘为:“+result); (分数:30.00
12、)_正确答案:()解析:String args result*=n-; while(n=1); 解析 本题主要是要求熟练掌握 main()主方法的使用、while 循环语句的用法。在本题中, String args的作用是声明字符数组 args,result *=n-;语句的作用是获得 n 的阶乘并赋值给变量 result,while(n=1);语句是循环终止条件。二、B2简单应用题/B(总题数:1,分数:40.00)2.请完成下列 Java 程序:假设某家银行,它可接受顾客的汇款,每做一次汇款,便可计算出汇款的总额。现有两个顾客,每人都分 3 次,每次将 50 元钱汇入。编写一个程序,模拟实
13、际作业。要求实现 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; _; try Thread.
14、 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 flag4_1 =1 public void run() char myflag4_1, synchronized(this) myflag4_1= flag4_1+; for (int i=1;i=3;i+) _; publ
15、ic class ex4_1 public static void main(String args) customer c1 = new customer( ); customer c2 = new customer( ) c1. start() c2. start( ) (分数:40.00)_正确答案:()解析:tmp=tmp+n bank.add(50,myflag4_1) 解析 本题主要考查线程的同步和设计简单的类来模拟现实问题的简单应用。解题关键是熟练掌握面向对象的编程思想,熟悉 Java 线程的同步编程,会使用 Math类的随机数方法。本题中,第 1 个空,银行类对客户的存款进行累
16、加;第 2 个空,在客户类中,通过使用银行类的对象 bank 调用 add()方法实现 3 次汇款的操作,将钱数和客户标志作为参数传递给 add()方法。三、B3综合应用题/B(总题数:1,分数:30.00)3.下面是一个 Applet 程序,其功能是实现一个计数器,每隔 0.15 秒计数器数值加 1,数值动态变化,并且能够控制计数器的暂停和继续。要求通过使用 swing 的构件建立图形用户界面,主要包括一个文本区域,用于显示计数器结果;两个按钮,一个使计数器暂停,一个使计数器继续工作。请改正程序中的错误(有下划线的语句),使程序能输出正确的结果。注意:不改动程序的结构,不得增行或删行。 im
17、port javax, swing. * import java. awt. * import java. awt. event. * /* applet code= “ex4_2. class“ width=800 height=400 /applet */ public class ex4_2 extends JApplet private JTextField jtf=new JTextField(15); private JButton Hold = new JButton (“Hold“), resume = new JButton ( “Resume“ ); private ex4
18、_2th obj4_2th= new ex4_2th(); class ex4_2th extends Thread private int cnt=0; private boolean bIsHold=false; public ex4_2th() start(); public void hold() bIsHold=true public synchronized void fauxResume() bIsHold=false; Uwait( )/U; public void run() while (true) try sleep(150) synchronized(this) whi
19、le(bIsHold) Unotify( )/U; catch(InterruptedException ie) System. err. println(“Interrupted“); Ujtf. setText(cnt)/U; public void init() Container cp = getContentPane( ) cp. setLayout(new FlowLayout( ) ); cp. add(jtf) Hold. addActionListener( new ActionListener() public void actionPerformed(ActionEven
20、t ae) obj4_2th. hold() ); cp. add(Hold); resume, addActionListener ( new ActionListener() public void actionPerformed(ActionEvent e) obj4_2th. fauxResume( ) ); cp. add(resume) public static void main(String args) ex4_2 obj4_2 =new ex4_2() String str= obj4_2. getClass(), toString(); if (str. indexOf(
21、“class“)!=-1) str=str. substring (6) JFrame frm=new JFrame(str) frm. addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent we) System. exit(0); ); frm. getContentPane(), add(obj4_2) frm. setSize(300,200) obj4_2. init() obj4_2. start( ); frm. setVisible(true) ex4_2. html HTML HE
22、AD TITLEex4_2/TITLE /HEAD BODY applet code=“ex4_2. class“ width=800 height=400 /applet /BODY /HTML(分数:30.00)_正确答案:()解析:notify() wait() jtfsetText(IntegertoString(cnt+) 解析 本题主要考查图形用户界面,swing 以及线程同步、共享、死锁相结合的综合应用。解题关键是熟悉 wait()方法和 notify()方法的含义。 wait()必须被声明为 synchronized,这样它才能拥有对象锁。fauxResume()把 bIsHold 标志设为false,并调用 notify(),为了唤醒 synchronized 子句中的 wait(),所以 notify()也必须被声明为 synchronized,这样才能在调用 notify()之前获得对象锁(然后该对象锁才能在 wait()执行时被运用)。本题中,第一和第二处,应该在 bIsHold 为 true 时调用 wait(),而在 fauxResume()中调用 notify();第三处,需要对 int 类型作转换才能够作为 String 类型输出,并且要对计数器变量 cnt 做累加。