《Java程序设计》参考答案1(6)

2019-01-26 16:29

浙江工业大学软件学院《Java程序设计》练习题参考答案——赵小敏 自编

版权所有 不得转载 违者必究

exit.addActionListener(this); Panel p1=new Panel(); p1.add(save); p1.add(cancel); p1.add(exit);

add(text,BorderLayout.CENTER); add(p1,BorderLayout.SOUTH); setSize(400,200); setVisible(true); }

public void actionPerformed(ActionEvent e){ if(e.getSource()==save){ try{

PrintWriter out = new PrintWriter(new FileWriter(fileName)); out.println(text.getText()); out.close();

System.out.println(\文件保存成功\ }catch (Exception ex) { ex.printStackTrace(); } }

else if(e.getSource()==cancel){ text.setText(\ return; }

else if(e.getSource()==exit){ System.exit(0); } }

public String readFile(String fileName){ StringBuffer str=new StringBuffer(); try {

File file=new File(fileName);

if(!file.exists()) file.createNewFile();

BufferedReader br = new BufferedReader(new FileReader(fileName)); String line = br.readLine(); while(line!=null){

str.append(line+\ line = br.readLine(); }

br.close();

}catch (Exception e) { e.printStackTrace();

第 26 页 共 32 页

浙江工业大学软件学院《Java程序设计》练习题参考答案——赵小敏 自编

版权所有 不得转载 违者必究

}

return str.toString(); }

public static void main(String args[]){ new TextEdit(); } } 27、

(1)用户登陆界面的构造方法的参考代码如下: (10分) public UserLogin(){

super(\用户登录\

userType=new Label(\用户类型\ userSelect=new Choice(); userSelect.add(\学生用户\ userSelect.add(\教师用户\

userSelect.addItemListener(this);

userLabel=new Label(\用户名:\ pswLabel=new Label(\密 码:\ userName=new TextField(10); psw=new TextField(10);

yesBtn=new Button(\确 定 \ cancelBtn=new Button(\取 消 \ exitBtn=new Button(\退 出 \ yesBtn.addActionListener(this); cancelBtn.addActionListener(this); exitBtn.addActionListener(this);

panel1=new Panel();

panel1.setLayout(new GridLayout(3,2)); panel2=new Panel();

setLayout(new BorderLayout());

panel1.add(userType); panel1.add(userSelect); panel1.add(userLabel); panel1.add(userName); panel1.add(pswLabel); panel1.add(psw);

add(panel1,BorderLayout.CENTER);

panel2.add(yesBtn); panel2.add(cancelBtn); panel2.add(exitBtn);

add(panel2,BorderLayout.SOUTH);

第 27 页 共 32 页

浙江工业大学软件学院《Java程序设计》练习题参考答案——赵小敏 自编

版权所有 不得转载 违者必究

setSize(200,160); //或写成setBounds(300,300,200,160); setVisible(true); }

(2) 事件处理方法的参考代码如下: (10分)

public void itemStateChanged( ItemEvent e ){ i=userSelect.getSelectedIndex(); }

public void actionPerformed(ActionEvent e){ if(e.getSource()==cancelBtn){ userName.setText(\ psw.setText(\ return; }

if(e.getSource()==exitBtn){ System.exit(0); }

else {

if(userName.getText().trim().equals(\ System.out.println(\用户名不可为空!\ return; }

if(psw.getText().trim().equals(\ System.out.println(\密码不可为空!\ return; }

switch(i){ case 0:

if(userName.getText().trim().equals(\ System.out.println(\学生用户登陆成功!\ break; }

case 1:

if(userName.getText().trim().equals(\ System.out.println(\教师用户登陆成功!\ break; }

default: System.out.println(\用户名不存在或者密码不正确!\ } } }

第 28 页 共 32 页

浙江工业大学软件学院《Java程序设计》练习题参考答案——赵小敏 自编

版权所有 不得转载 违者必究

评分说明:

1) 写对public void itemStateChanged( ItemEvent e )方法头给2分, 2) 写对i=userSelect.getSelectedIndex();给2分

3) 写对public void actionPerformed(ActionEvent e)方法头给1分 4) 写对 对cancelBtn按钮的处理给1分 5) 写对 对exitBtn按钮的处理给1分 6) 写对对确定按钮中的判断处理给3分

7) 程序编写不规范或关键字写错,酌情扣1-5分

28参考代码如下:

public class ThreadTest {

public static void main(String[] args) { PrintThread pt1,pt2;

pt1 = new PrintThread(\线程1正在运行\ pt2 = new PrintThread(\线程2正在运行\

pt1.setPriority(10); pt2.setPriority(6);

pt1.start(); pt2.start(); } }

class PrintThread extends Thread { String name;

public PrintThread(String threadName) { name = threadName; }

public void run() {

for(int i=0; i<200; i++) System.out.println(name); } }

29、参考代码如下: class ATM {

private float accountBalance; public ATM(float money){ accountBalance=money; }

第 29 页 共 32 页

浙江工业大学软件学院《Java程序设计》练习题参考答案——赵小敏 自编

版权所有 不得转载 违者必究

public synchronized float getAccountBalance(){ try{

Thread.sleep(2000); }catch(Exception e){ }

return accountBalance; }

public synchronized void deposit(float money){ accountBalance=accountBalance+money; }

public synchronized void drawMoney(float money) throws Exception{ if(accountBalance

throw new Exception(\ }

accountBalance=accountBalance-money; } }

class UserA extends Thread{ private ATM atm;

private float money;

public UserA(ATM a,float m){ atm=a; money=m; }

public void run(){

System.out.println(atm.getAccountBalance()); System.out.println(\ try{

atm.deposit(money); }

catch(Exception e){ }

System.out.println(\ } }

class UserB extends Thread{ private ATM atm;

private float money;

public UserB(ATM a,float m){ atm=a; money=m; }

public void run(){

System.out.println(atm.getAccountBalance());

第 30 页 共 32 页


《Java程序设计》参考答案1(6).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:ARM培训班RealView实验手册 - old

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: