浙 江 工 业 大 学 考 试 命 题 纸
} }
2、写出下列程序代码的运行结果: public class Test extends TT{
public static void main(String args[]){ Test t = new Test(\ }
public Test(String s){ super(s);
System.out.println(\ }
public Test(){
this(\ } }
class TT{
public TT(){
System.out.println(\ }
public TT(String s){ this();
System.out.println(\ } }
3、仔细阅读下面的程序代码,若经编译和运行后,请写出打印结果。
class Man{
void drink(){
System.out.println(\ }
}
class OldMan extends Man{ void drink(){
System.out.println(\ }
}
class YongMan extends Man{ void drink(){
System.out.println(\
第 5 页
浙 江 工 业 大 学 考 试 命 题 纸
}
void dance(){
System.out.println(\
}
public static void main(String[] args) { Man tom = new Man();
Man yangguo = new YongMan(); Man hongqi = new OldMan();
tom.drink(); yangguo.drink(); hongqi.drink(); if(yangguo instanceof YongMan) ((YongMan)yangguo).dance(); }
}
4、class A_Class extends B_Class{ B_Class{ int x=98, y; int x; int f(){ } int x=45;
y = x; (1) }
}
public static void main(String[] args){
A_Class a = new A_Class(); a.f();
System.out.println(a.y); }
程序运行结果为
如果语句1用y = this.x; 代替,程序运行结果为 如果语句1用y = super.x; 代替,程序运行结果为
5、仔细阅读下列程序代码。 class Complex{ double x,y;
public Complex(double x,double y) { this.x=x;
this.y=y;
}
public static Complex add(Complex a,Complex b) { return new Complex(a.x+b.x,a.y+b.y); } }
第 6 页
浙 江 工 业 大 学 考 试 命 题 纸
public class Complexadd {
public static void main(String args[]) { String s=args[0]; double x,y; int p1,p2,len; try{
p1=s.indexOf(\查找\号位置 if (p1!=-1) {
String one=s.substring(0,p1); String two=s.substring(p1+1); p2=one.indexOf(\ len=one.length();
x=Double.parseDouble(one.substring(1,p2));
y=Double.parseDouble(one.substring(p2+1,len-2)); Complex c1=new Complex(x,y); p2=two.indexOf(\ len=two.length();
x=Double.parseDouble(two.substring(1,p2));
y=Double.parseDouble(two.substring(p2+1,len-2)); Complex c2=new Complex(x,y); Complex c3=Complex.add(c1,c2);
System.out.println(\结果为(\ }
}catch(NumberFormatException e) {
System.out.println(\数据格式错!\ } } }
问:(1)以上代码经编译后,在命令行下输入:
java Complexadd (3,4i)+(5,8i),则输出结果是什么?
(2)以上代码经编译后,在命令行下输入:
java Complexadd (3,4i)+(5,8ai),则输出结果是什么?
6、仔细阅读下面的程序代码,请写出该程序的功能。
import java.awt.*;
import java.awt.event.*;
public class myFrame extends JFrame implements ActionListener {
Label res; TextField my;
第 7 页
浙 江 工 业 大 学 考 试 命 题 纸
String
word[]={\,\
public myFrame (){ my =new TextField(20);
res=new Label(\英文单词 \ setLayout(new FlowLayout()); add(my);
add(res);
my.addActionListener(this); }
public void actionPerformed(ActionEvent e) { String s=my.getText(); int n=Integer.parseInt(s); res.setText( word[n] ); }
public static void main(String args[]) { Frame my= new myFrame(); my.setSize(300,300); my.setVisible(true); }
}
四、程序填空题(共2小题,每空1分,共10分)
1、仔细阅读下面的程序代码,请将划线上(1)~(5)的语句补充完整。 public class Point {
int x,y;//x表示点的横坐标,y表示点的纵坐标 public Point() { }
public Point(int x,int y){ (1) } public Point(Point p){ (2) } public Point getLocation(){
Point p= (3) ; //实例化一个Point对象p,其坐标是(x,y) return p; //返回对象p }
public int getX(){return x;} public int getY(){return y;}
public void move(int x,int y){this.x = x;this.y = y;} public String toString(){return \
第 8 页
浙 江 工 业 大 学 考 试 命 题 纸
public void translate(int x,int y){this.x += x;this.y += y;} public static void main(String args[]){
Point p= (4) ; //生成一个点p的对象(5,5) System.out.println(\
System.out.println(\ (5) ; //将p点在原有位置上增加(3,4) System.out.println(\
System.out.println(\ } }
2、下面的程序是要从标准输入设备(即键盘)读入10个整数,并存入整型数组a中,然后逆序输出这10个整数。注:在通过控制台由键盘输入10个整数时,每输入完一个整数按下回车键换行,再输入下一个。
import java.io.* ;
public class Reverse {
public static void main(String args[ ]) {
int i , n =10 ;
int[] a = (6) for ( i = 0 ; i < n ; i ++ ) try {
Reader r = (7) //定义字符流从键盘接收数据
BufferedReader br = (8) //定义缓冲流,从而按行读取数据
a[i] = (9)
// 读取一行数据并将其转换为一个整型数
} catch ( IOException e ) { } ; for (10)
//逆序输出数组a中的元素
System.out.print(a(i)); System.out.println( ); } }
第 9 页