问答题

请完成下列Java程序:假设某家银行,它可接受顾客的汇款,每做一次汇款,便可计算出汇款的总额。现有两个顾客,每人都分3次,每次将50元钱汇入。编写一个程序,模拟实际作业。要求实现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. 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++)
______;


public class ex4_1
public static void main(String args[])
customer c1 = new customer( );
customer c2 = new customer( )
c1. start()
c2. start( )

【参考答案】

tmp=tmp+n
bank.add(50,myflag4_1)
热门 试题

问答题
下面是一个Applet程序,其功能是实现一个计数器,每隔0.15秒计数器数值加1,数值动态变化,并且能够控制计数器的暂停和继续。要求通过使用swing的构件建立图形用户界面,主要包括一个文本区域,用于显示计数器结果;两个按钮,一个使计数器暂停,一个使计数器继续工作。请改正程序中的错误(有下划线的语句),使程序能输出正确的结果。注意:不改动程序的结构,不得增行或删行。 import 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_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; 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) 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( 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> <HEAD> <TITLE>ex4_2< TITLE> < HEAD> <BODY> <applet code= ex4_2. class width=800 height=400 < applet> < BODY> < HTML>