一、选择题(每题4分,共12分)
1.给出下面代码:
class Aclass{
void go(){
System.out.print(" Aclass");
}
}
public class Bclass extends Aclass{
void go(){
System.out.print(" Bclass");
}
public static void main(String args[]){
Aclass a=new Aclass();
Aclass a1=new Bclass();
a.go();
a1.go();
}
}
以上程序运行结果是:( )
A、 Aclass Aclass B、 Bclass Bclass
C、 Aclass Bclass D、 Bclass Aclass
2.编译并运行以下程序段的结果是:( )
import javax.swing.*;
import java.awt.*;
public class A{
public static void main(String args[ ]){
JFrame f=new JFrame ("Visual");
f.setSize(300,300);
f.setVisible(true);
Point p=f.getLocation();
System.out.println("x is "+ p.x+",y is "+ p.y);
}
}
A、x is 300, y is 300 B、x is 0, y is 0 C、x is 0,y is 300 D、x is 300, y is 0
3.给出下面代码段
1) public class Test {
2) int m, n;
3) public Test() {}
4) public Test(int a) { m=a; }
5) public static void main(String arg[]) {
6) Test t1,t2;
7) int j,k;
8) j=0; k=0;
9) t1=new Test();
10) t2=new Test(j,k);
11) }
12) }
哪行将引起一个编译时错误?( )
A、 line 3 B、 line 5 C、 line 6 D、 line 10
二、阅读程序(每题5分,共20分)
1.下列程序的运行结果是 ________________________________
class A{
class Dog{
private String name;
private int age;
public int step;
Dog(String s,int a){
name=s;
age=a;
step=0;
}
public void run(Dog fast){
fast.step++;
}
}
public static void main (String args[]){
A a=new A();
Dog d=a.new Dog("Tom",3);
d.step=25;
d.run(d);
System.out.println(d.step);
}
}
2.阅读以下程序,输出结果为____________________________。
abstract class AbstractClass{
int a;
void setA(int a){
this.a=a;
}
abstract void out(int a);
}
public class Instance extends AbstractClass {
int getA(){
return a;
}
void out(int a){
System.out.println("output: "+a);
}
public static void main(String[] args){
Instance i=new Instance();
i.setA(20);
i.out(i.a);
}
}
3.下列程序的运行结果是______________。
class MyClass {
int a[]={1,2,3,4,5};
void out(){
for(int j=0;j<a.length;j++)
System.out.print(a[j]+" ");
}
public static void main(String[] args){
MyClass my=new MyClass();
my.out();
}
}
4.阅读以下程序,输出结果为_________。
public class ClassA {
public int a;
}
class ClassB extends ClassA {
public String b="hello";
}
class ClassC extends ClassB {
public int c;
public static void main(String[] args) {
ClassC cc=new ClassC();
cc.a=10;
cc.c=20;
System.out.println(cc.a+cc.b+cc.c);
}
}
三、程序填空(每题8分,共16分)
1.JFrame程序:有2个复选框,其名称为“玩耍”和“学习”,根据选择情况,分别显示“我就喜欢玩耍”,“我只喜欢学习”,“两个我都喜欢”。
(1) ,(2)
(3) ,(4)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class testCheck extends JFrame __________(1)_____________ {
//实现监听器接口
JCheckBox c1,c2;
JPanel panel;
JLabel label;
String s="";
public testCheck(){
panel = new JPanel();
c1=new JCheckBox("玩耍");
c2=new JCheckBox("学习");
c1.addActionListener(this);
c2.addActionListener(this);
panel.add(c1);
panel.add(c2);
label = new JLabel("scds");
getContentPane().add(label, BorderLayout.NORTH);
getContentPane().add(panel, BorderLayout.CENTER);
setSize(200,100);
setVisible(true);
}
______________(2)_______________________(ActionEvent e) {
//JCheckBox选择事件响应处理方法
if(______________(3)_______________________) //如果两个都选择
s="玩耍和学习我都喜欢";
else if(c1.isSelected())
s="我就喜欢玩耍";
else if(c2.isSelected())
s="我只喜欢学习";
else
s="";
___________(4)_____________________;//在标签上显示字符串
}
}
class TestJCheckBoxClass {
public static void main(String[] args) {
testCheck frame = new testCheck();
}
}
2.下列程序的功能是:输入一个姓名,程序运行后,输出“姓名Welcome you!”.例如,输入“张三Welcome you !”.
请在下面横线处填入正确的内容,使程序可以正确运行.
(1) ,(2)
(3) ,(4)
import java.awt.*;
import java.awt.event.*;
import __________(1)_________________
public class welcomenYou{
public static void main(String args[]){
new FrameInOut();
}
}
class FrameInOut extends JFrame implements ActionListener{
JLabel prompt;
JTextField input,output;
JButton btnn;
public FrameInOut(){
super("Welcome you");
prompt=__________________(2)_______________________
input=new JTextField(10);
output=new JTextField(25);
btnn=new JButton("Class");
____________(3)_____________________(new FlowLayout());
getContentPane().add(prompt);
getContentPane().add(input);
getContentPane().add(output);
getContentPane().add(btnn);
input.addActionListener(this);
btnn.addActionListener(this);
setSize(300,200);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == input)
output.setText(____________(4)_______________________);
else{
dispose();
System.exit(0);
}
}
}
四、编程(1-4题每题10分、5题12分,共52分)
1.请编写一个实现如下功能的Application:从命令行依次取三个参数 x, y, op 做算术运算,其中 x, y为 int 型数据,op 为运算符(+、*之一),请显示 x op y 的结果。
2.按以下要求编程
(1)编写Animal接口,接口中声明run() 方法
(2)定义Bird类和Fish类实现Animal接口
(3)定义Test类测试上面的类
3.定义一个类,类中有两个整型变量x和y,并定义构造函数初始化这两个变量。类中还定义以下方法:求两个数的最大值并返回结果,求两个数的最小值并返回结果。
编写应用程序,测试上面定义的类,使用类中定义的各个方法并将其结果输出。
4.利用面板(JPanel)设计图7.4所示的版面的容器结构,在窗口中放置五个标签,五个标签的摆放位置要求如图。
5.编写JFrame, 其中包含两个按钮一个标签:按钮上的文本分别为"确定"和"取消"。当按下"确定"键时,在标签上显示你按下"确定"按钮的次数;当按下的键是"取消"时,在标签上显示你按下"取消"按钮的次数。