五、程序分析题(本大题共5小题,每小题4分,共20分) 32.阅读下列程序,请写出该程序的功能。 public class Test32 { public static void main(String args[]) { double sum=0.0; For (int i=1; i<=500;i++) sum+=1.0/(double)i; System.out.println( "sum="+sum); } } 33.阅读下列程序,请回答以下问题: (1)界面中有哪些组件? (2)点击每一个按钮分别会显示什么内容? import java.applet.*; import java.awt.*; import java.awt,event.*;import javax.swing.*; public class Test33 extends Applet implements ActionListener{ String msg=""; String buttonCom[]={"Yes","No","Undecided"}; JButton bList[]=new JButton[buttonCom.length]; JTeXtField t; public void init(){ setLayout(new GridLayout(4,1)); for(int i=0;i<buttonCom.1ength;i++){ bList[i]=new JButton("按钮"+(i+1)); add(bList[i]); bList[i].addActionListener(this); } t=new JTeXtField(); add(t); } public void actionPerformed(ActionEvent e){ for(int i=0;i<3;i++){ if(e.getSource()一bList[i]){ t.setTeXt("You pressed"+buttonCom[i]); break; } } } } 34.阅读下列程序,请写出该程序的功能。 import java.io.*; import java.awt.*; import javax.swing.*; import java.awt.event.*; class MyWindow extends JFrame implements ActionListener{ JTextArea text;BufieredReader in;JTextField fileName; FileReader file; MyWindow(){ Contaiher con=this.getContentPane();//获得内容面板 con.setLayout(new BorderLayout()); fileName=new JTextField("输入文件名"); fileName.addActionListener(this); text=new JTextArea(20,30); JScrollPane jsp:new JScrollPane(text); con.add(jsp,BorderLayout.CENTER); con.add(fileName,"South");setVisible(true); } public void actionPerformed(ActionEvent e) { String s; try{File f=new File(fileName.getText()); file=new FileReader(f); in=new BufferedReader(file); } catch(FileNotFoundException el){} try{ while((s=in.readLine())!=null) text.append(s+’\n’); }catch(IOException exp){} } } public class Test34{ public static void main(String args[ ]) {new MyWindow();} }
|