文档库 最新最全的文档下载
当前位置:文档库 › 编程练习代码

编程练习代码

1:[论述题]
阅读下面程序,并回答问题。
(1)try块中包含的哪些语句或表达式可能抛出异常?
(2)流DataOutputStream和DataInputStream常被用于何种操作?
(3)假定文件out.txt中原本没有任何数据,这段程序执行完成后,文件out.txt的内容是什么?程序在控制台窗口输出什么?
import java.io.*;
public class Test4 {
public static void main(String args[]) {
try {
DataOutputStream dout = new DataOutputStream(
new FileOutputStream("out.txt"));
for (int i = 0; i < 10; i++)
dout.writeInt('0' + i);
dout.close();
DataInputStream din = new DataInputStream(
new FileInputStream("out.txt"));
for (int i = 0; i < 10; i++)
System.out.print(din.readInt() - '0' + ", ");
din.close();
} catch (IOException e) {
System.err.println("发生异常:" + e);
e.printStackTrace();
}
}
}

参考答案:问题(1):try块中下面的语句可能抛出异常:
new FileOutputStream("out.txt");
dout.writeInt( '0' + i);
dout.close( );
new FileInputStream("out.txt");
din.readInt( )
din.close( );
问题(2):常被用于读取与存储(读写或输入/输出)基本数据类型的数据。
问题(3):
文件out.txt的内容是:0 1 2 3 4 5 6 7 8 9
程序在控制台窗口输出:0,1,2,3,4,5,6,7,8,9,
2:[论述题]
阅读下面程序,并回答问题。
(1)Java程序分为哪两种类型,这段程序是哪一类Java程序?
(2)这个图形用户界面上包含那几类组件?点击按钮后程序显示什么?
(3)ActionListener是什么?程序中哪个方法是ActionListener中的方法?其功能是什么?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Test4 extends JApplet implements ActionListener{
private Container cp = getContentPane();;
private JLabel prompt = new JLabel("请点击按钮");
private JButton start = new JButton("开始");
private JTextField output = new JTextField(20);
public void init( ){
cp.setLayout(new FlowLayout());
cp.add(start);
cp.add(prompt);
cp.add(output);
output.setEditable(false);
start.addActionListener(this);
}
public void actionPerform

ed(ActionEvent e){
if ((JButton)e.getSource( ) == start )
output.setText("好好学习,天天向上");
}
}


参考答案:问题(1):Java程序分为Java应用程序(或Java application)和Java小应用程序(或Java
applet);这段程序是Java小应用程序。
问题(2):界面上包含一个标签(JLabel),一个按钮(JButton)和一个文本框(JTextField);点击按钮后,文本框内显示"好好学习,天天向上”。

问题(3):ActionListener是动作事件监听器接口;方法actionPerformed()是ActionListener中的方法;其功能是处理applet界面里发生的动作事件。
3:[论述题]
阅读下面程序,并回答问题。
(1)类Child和类Parent之间是什么关系?
(2)关键字super和this分别是什么含义?
(3)这段程序的输出是什么?
class Parent{
public void printMe( ){
System.out.println("parent");
}
}
class Child extends Parent{
public void printMe( ){
System.out.println("child");
}
public void printAll( ){
super.printMe( );
this.printMe( );;
}
}
public class Test3{
public static void main(String args[ ]){
Child myC = new Child( );
myC.printAll( );
}
}


参考答案:
问题(1):Child是Parent的子类(或Parent是Child的父类,或继承关系)。
问题(2):super指对象的父类(或超类);this指使用它的对象本身(或对对象自己的引用)。
问题(3):程序的输出是:
parent
child


4:[论述题]
阅读下面程序,并回答问题。
(1)类Test3和类SuperTest之间是什么关系?
(2)关键字super和this分别是什么含义?
(3)这段程序的输出是什么?
class SuperTest {
public int age;
public SuperTest(String s) {
System.out.println("Hi, I am " + s);
age = 35;
}
}
public class Test3 extends SuperTest {
public int age;
public Test3(String s) {
super(s);
System.out.println("Nice to meet you!");
age = 7;
}
public void print() {
System.out.println("Age is " + age);
System.out.println("My age is " + this.age);
System.out.println("My parent's age is " + super.age);
}
public static void main(String args[]) {
Test3 test = new Test3("Olive");
test.print();
}
}


参考答案:
问题(1):Test3是SuperTest的子类(或SuperTest是Test3的父类,或继承关系)。
问题(2):super指对象的父类(或超类);this指使用它的对象本身(或对对象自己的引用)。
问题(3):程序的输出是:
Hi, I a

m Olive
Nice to meet you!
Age is 7
My age is 7
My parent's age is 35

相关文档