问题 问答题

下面是一个Applet程序,其功能是实现一个计数器,每隔0.15秒计数器数值加1,数值动态变化,并且能够控制计数器的暂停和继续。要求通过使用Swing的构件建立图形用户界面。主要包括一个文本区域,用于显示计数器结果:两个按钮,一个使计数器暂停,一个使计数器继续工作。请改正程序中的错误(有下划线的语句),使程序能输出正确的结果。 注意:不改动程序的结构,不得增行或删行。 源程序文件代码清单如下。 import javax.swing.*; import java.awt.*; import java.awt.event.*; /* <applet code="ex19_3.class" width=800 height=400> </applet> */ public class ex19_3 extends JApplet { private JTextField jtf=new JTextField(15); private JButton Hold=new JButton("Hold"); private JButton resume=new JButton("Resume"); private ex19_3th obj19_3th=new ex19_3th(); class ex19_3th extends Thread { private int cnt=0; private boolean bIsHold=false; public ex19_3th() {start();} public void hold() { bIsHold=true; } public synchronized void fauxResume() { bIsHold=false; wait (); } public void run() { while (true) { try { sleep(150); synchronized(this){while(bIsHold)notify();} }catch(InterruptedException ie){System.err.println("Interrupted");} jtf.setText(cnt); } } } public void init() { Container cp=getContentPane(); cp.setLayout(new FlowLayout()); cp.add(jtf); Hold.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent ae) { obj19_3th.hold ( ); } }); cp.add(Hold); resume.addActionListener ( new ActionListener() { public void actionPerformed(ActionEvent e) { obj19_3th.fauxResume(); } }); cp.add(resume); } public static void main(String args[]) { ex19_3 obj19_3=new ex19_3(); String str= obj19_3.getClass().toString(); if(str.indexOf("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(obj19_3); frm.setSize(300, 200); obj19_3.init(); obj19_3.start{); frm.setVisible(true); } } ex19_3.html <HTML> <HEAD> <TITLE>ex19_3</TITLE> </HEAD> <BODY> <applet code="ex19_3.class" width=800 height=400> </applet> </BODY> </HTML>

答案

参考答案:

解析:notify()wait()jtf.setText.(Integer.toString(cnt++)) 本题主要考查图形用户界面,Swing以及线程同步、共享、死锁相结合的综合应用。解题关键是熟悉wait()方法和notify()方法的含义,wait()必须被声明为synchronized,这样它才能拥有对象锁。fauxResume()把bIsHold标志设为false,并调用notify(),为了唤醒 synchronized子句中的wait(),notify()也必须被声明为synchronized,这样才能在调用notify()之前获得对象锁,然后该对象锁才能在wait()执行时被运用。本题中,第1和第2处,应该在bIsHold为true时调用wait(),而在 fauxResume()中调用notify();第3处,需要对 int类型做转换才能够作为String类型输出,并且要对计数器变量cnt做累加。程序运行结果如下。

多项选择题
阅读理解

阅读理解

     Is there something that you've always wanted to try but just never had the time? Well, make plans to try it now since you are on summer vacation. Not all vacations call for taking a tour bus to take photos of

famous landmarks. Some vacations allow you plenty of opportunities to learn.

     The most difficult aspect of a learning vacation may be choosing one because the possibilities are

endless. If you enjoy cooking, various companies can take you to Italy, France, Spain, Mexico or even

Peru. Once there, you can learn to prepare the local cuisine(烹饪). Trips are often planned to fit in with

local food festivals or special events.

     The term "learning vacation" often brings language to mind.The best way to learn a language is in an

environment where it's spoken. Study Spanish, French or English. Or attempt a more unusual language like Polish, Estonian or Thai. You'll be able to learn about the country and absorb the culture at the same time.

If you are fond of sports, you can polish your skills or learn new ones. Golf and tennis schools welcome

players of all levels. If you want a bigger thrill, you can learn to surf, go climbing or race cars.

     It's even possible to learn the art and techniques of bull fighting while on vacation!

     You can also discover our inner artist. Many places offer painting classes in different mediums. The

scenic locations of the schools offer plenty of subjects that provide inspiration for practice.

     If you prefer capturing the world on film, take a photography vacation. Travel with a small group to

photograph beautiful animals or scenery. You can also practise your technique on people or at historical

sights.

     Once you decide on a vacation, choose a company carefully. Request names of recent customers you

can contact, and then ask them for an evaluation. The more you know before you go, the better prepared you'll be. Then go out and learn something!

1. Why is it hard for you to decide on a learning vacation?

A. Because it is hard for you to make plans.

B. Because the possibilities are unlimited.

C. Because there are many good foods from abroad.

D. Because there're too many food festivals or events

2. The advantage of learning a language in its native country is that ________.

A. the environment is fit for you to use the language

B. you are able to learn the original foreign language

C. native speakers offer you a lot of chances to practise

D. you can learn the language and experience its culture

3. Which of the following sports suits you if you don't like thrills?

A. Car racing.  

B. Playing tennis.

C. Rock climbing.  

D. Surfing.

4. The structure of the text would be________.