全國2012年1月自考《Java語言程序設(shè)計(一)》試題_第5頁
五、程序分析題(本大題共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)點擊每一個按鈕分別會顯示什么內(nèi)容?
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();//獲得內(nèi)容面板
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();}
}
責(zé)編: