五、程序分析题(本大题共5小题,每小题4分,共20分)
32.阅读下列程序,请写出该程序的运行结果。
Class Parent {
String one,two;
public Parent(String a,String b){
one=a;two=b;
}
public void print(){
System.out.println(two+"to"+one);
}
}
public class Child extends Parent{
public Child(String a,String b){
super(a,b);
}
public void print(){
System.out.println(one+"to"+two);
}
public static void main(String args[]){
parent p=new Parent("south","north");
parent t=new Child("east","west");
p.print();
t.print();
}
}
33.阅读下列程序,请写出该程序的功能。
import java.applet.Applet;import java.awt.*;
public class Test33 extends Applet{
public void paint(Graphics g){
for(int i=0;i<6;i++)
drawGraphics(I,g);
}
public void drawGraphics(int n,Graphics g){
g.clearRect(10,10,300,300);
switch(n){
case 0:case 3:
g.setColor(Color.red);
g.drawRect(50,50,150,150);
break;
case 1:case 4:
g.setColor(Color.blue);
g.drawOval(40,40,1 60,160);
break;
case 2:case5:
g.setColor(Color.green);
g.fillRect(50,50,200,200);
break;
}
try{
Thread.sleep(1500);
}
catch(ImerruptedException e){System.exit(0);}
}
}
34.阅读下列程序,请写出该程序的功能。
import javax.swing.*; import java.awt.*; import java.awt.event.*;
public class Test34 {
public static void main(String[]args){
ComboBoxDemo myComboBoxGUI = newComboBoxDemo();
}
}
Class ComboBoxDemo extends JFrame implemems ItemListener{
String CountryList[]={"美国","中国","英国","俄罗斯","韩国","德国"};
im GoldMedalNumber[]={46,38,29,24,13,11};
JLabel label;JComboBox Combobox;
Public ComboBoxDemo(){
setSize(350,150);
setTitle("第三十届伦敦奥运会金牌查询");
Comainer conPane = getContentPane();
conPane.setLayout(new FlowLayout());
combobox=new JComboBox(CountryList);
combobox.addItemListener(this);
conPane.add(combobox);
label=new JLabel(); conPane.add(1abel); this.setVisible(true);
}
public void itemStateChanged(ItemEvem e){
if(e.getS ource()==combobox)
label.setText(
String.valueOf(GoldMedalNumber[combobox.getSelectedIndex()]));
}
}
35.阅读下列程序,请回答下面的问题:
(1)两个文本区内分别每次追加何种字符?有多少个?
(2)两个文本区里面内容的显示速度有何区别?
import iava.awt.*;import java.applet.*;import javax.swing.*;
public class Test35 extends Applet{
JTextArea 1Text,rText;
public void init() {
setSize(600,500); setLayout(null);
1Text=new JTextArea(); rText=new JTextArea();
lText.setFont(new Font ("Times New Roman",1,40));
rText.setFont(new Font("Modern",1,40));
JScrollPane jsp1=new JScrollPane(1Text);
JScrollPane jsp2=new JScrollPane(rText);
add(jsp1); add(jsp2);
jspl.setBounds(20,20,250,450); jsp2.setBounds(310,20,200,450);
(new threadl(1Text,"1")).start(); (new threadl(rText,"2")).start();
}
}
class threadl extends Thread{
protected JTextArea t;
String text="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
public thread1(JTextArea text,String m){ super(m); t=text; }
public void run() {
while(true){
String S=" ";
for(int i=0;i<6;i++){
int k;
if(getName().equals("1"))
k=((int)(Math.random()*100)%26);
else
k=((int)(Math.random()*100)%10)+26;
s+=text.charAt(k); //获取k位置字符
}
t.append(s+"\n");
t.setCaretPosition(t.getText().1ength());//最近输出的内容在文本区中显示
try{sleep(getName().equals("1")?400:700);}
catch (InterruptedException e){}
}
}
}
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("文件读写出错! "+e2);
}
System.out.println("程序结束! "’);
}
}