全國2013年10月自考《Java語言程序設(shè)計(jì)(一)》真題_第6頁
36.閱讀下列程序,請寫出該程序的功能。
import java.io.*;
class Test36{
public static void main(Stringarg[]){
int ch;
try{
FileReader in=new FileReader(new File("file 1.txt"));
FileWriter out=new FileWriter(new File("file2.txt"));
while((ch=in.read())!=-1){
if(ch>=(int)′A′&&ch<=(int) ′Z′)
ch+=32;
else if(ch>=(int) ′a′&& ch<=(ira) ′z′)
ch-=32;
out.write(ch);
}
in.close(); out.close();
}catch (FileNotFoundException e1){
System.out.println("文件沒有找到! "+e1);
}catch(IOException e2){
System.out.println("文件讀寫出錯(cuò)! "+e2);
}
System.out.println("程序結(jié)束! "’);
}
}
六、程序設(shè)計(jì)題(本大題共2小題,每小題6分,共12分)
37.編寫方法int[][]transpose(int[][]a),方法將生成并返回一個(gè)新數(shù)組b,該數(shù)組為a的轉(zhuǎn)置數(shù)組。
注:數(shù)組轉(zhuǎn)置指的是將數(shù)組的行、列內(nèi)容互換。
38.以下程序界面有一個(gè)標(biāo)以“OK”的按鈕bt、一個(gè)文本區(qū)ta和一個(gè)文本框tf。當(dāng)在文本框中輸入內(nèi)容完畢后,點(diǎn)擊“OK”按鈕可以使文本框內(nèi)的字符串添加到文本區(qū)中,并輸出文字“因點(diǎn)擊按鈕而引發(fā)添加”;或者按回車鍵使得文本框內(nèi)的字符串添加到文本區(qū)中,并輸出文字“因在文本框里回車而引發(fā)添加”。
注:這里是給定程序的部分代碼,你要編寫的是actionPerformed(ActionEvent e)方法。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Test38 extends JFrame implements ActionListener{
JButton bt=new JButton("OK");
JTeXtField tf=new JTextField(20);
JTextArea ta=new JTextArea(10,20);
public Test38() {
Container c=getContentPane();
c.setLayout(new FlowLayout()); //指定布局方式為順序布局
c.add(ta);
c.add(bt);
c.add(tf);
bt.addActionListener(this);
tf.addActionListener(this);
setSize(400,300);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
//請?jiān)凇按痤}紙’’上編寫以下代碼
}
public static void main(String args[]) {
new Test38();
}
}
責(zé)編:abcwuli1234