一、程序填空题(每空3分,共30分)
1. 以下程序要求从键盘输入一个整数,判别该整数为几位数,并输出结果,请将下面的程序填写完整。
public class Blank1 { }
答案:(1)System.in (2)t=t/10
2. 在下面的程序中使用方法重载分别实现了两个和三个整数的相加,请将下面的程序填写完整。
class AddOver{ }
public class Blank2{ }
答案:(3)int add(int a,int b) (4)new AddOver()
3. 构造一个类来描述一个点,该类的构成包括点的x和y两个坐标,以及一些对点进行的操作,包括:取得点的坐标值,利用另一个点对当前点的坐标进行赋值,请将下面的程序填写完整。
class Point{
int x,y;
public (5) (int x, int y){
this.x=x; this.y=y;
public static void main(String[] args){ }
AddOver a= (4) ; System.out.println(a.add(1,2)); System.out.println(a.add(1,2,3)); public (3) { }
public int add( int a, int b, int c ){ }
return a+b+c ; return a+b;
public static void main(String[] args) throws IOException { }
Scanner sc=new Scanner( (1) ); int count=0,t; int x=sc.nextInt(); sc.close(); t=x;
while(t!=0){ }
System.out.println(x+\是\+count+\位数。\);
count++;
(2) ;
}
public Point getPoint(){ }
public void setPoint( (7) ){ } }
public static void main(String[] args){ }
Point a = new Point(3,4); Point b = new Point(0,0); b=a.getPoint();
Point c = new Point(0,0); c.setPoint(b); }
this.x=s.x; this.y=s.y; Point temp = new Point(0,0); temp.x=x; temp.y=y;
return (6) ;
public class Blank3 {
答案:(5)Point (6)temp
(7)Point s
4. 下面的程序完成从D:\\Hello.txt中读取文本并显示在屏幕上,请将下面的程序填写完整。
class Blank4 {
public static void main ( String[] args ) { String fileName = \, line; try {
BufferedReader in = new BufferedReader( (8) ); line = in.readLine(); while ( (9) ) {
System.out.println( line );
}
line = (10) ;
}
in.close();
catch (IOException e) {
System.out.println(\ + fileName ); } } }
答案:(8)new FileReader(fileName) (9)line!=null
(10)in.readLine()
5.下面的程序通过方法调用从包含7个学号的数组中随机抽取一个学号并输出显示,请将下面的程序填写完整。
public class Ex1 {
public (11) String getXh(){ } } }
答案:(11)static
(12)(int)(Math.random()*7)
(13)getXh()
6. 下面的程序定义了一个线程TimeThread,该线程每隔1秒钟输出显示一次当前系统时间,在main方法中使用TimeThread类创建3个新线程,并启动这些线程,请将下面的程序填写完整。 import java.util.*;
class TimeThread implements (14) { public void run() { while (true) {
Date currentTime = new Date(); try {
(15) ; // 休眠1秒钟 }
catch (Exception e) {System.out.println(e.toString());}
System.out.println(Thread.currentThread().getName()+\
} } }
public class Ex2 {
public static void main(String[] args){ }
答案:(14)Runnable (15)Thread.sleep(1000) String[] names={\ TimeThread myThread = new TimeThread(); for(int i=0;i<3;i++){
Thread threadObj= new Thread(myThread,names[i]); (16) ; //启动线程 } }
String[] xhs = {\int index = (12) ; //生成0~6之间的随机数。 return xhs[index];
System.out.println(\随机抽取的学号为:\ (13) );
public static void main(String[] args){
(16)threadObj.start()
7.下面的程序对“百鸡百钱”问题进行了求解,公鸡每只3元,母鸡每只5元,小鸡3只1元,用100元钱买100只鸡,公鸡、母鸡、小鸡应各买多少?请将程序填写完整。 public class Ex3 { } }
答案:(17)a<=33 (18)b<=20
(19)c%3==0
8. 下面的程序使用BufferedWriter类在D:\\Hello.txt文件中写入10万个数并输出所用的时间,请将程序填写完整。
import java.io.*; public class Ex4 {
public static void main(String[] args) throws IOException{ } }
答案:(20)new FileWriter(“D:\\\\Hello.txt”) (21)fw.write
9.根据程序注释提示将下面的程序填写完整。
class StringExample
{ public static void main(String args[]) { String s1=new String(\ String s2=new String(\
int x = (22) ; // 将s1转换为int类型 double y = (23) ; // 将s2转换为double类型
long t = System.currentTimeMillis(); for (int i=1;i<=100000;i++){ } fw.close();
t=System.currentTimeMillis()-t;
System.out.println(\
(21) ( i +\
BufferedWriter fw =new BufferedWriter( (20) ); public static void main(String[] args){
int a,b,c;
for ( a=0; (17) ; a++ ){
for ( b=0; (18) ; b++ ) {
c=100-a-b;
if ( (3*a+5*b+c/3==100) && ( (19) ) )
System.out.println(\公鸡:\ 母鸡:\ 小鸡:\ } }
double z = x + y;
String s3 = (24) ; //将z转换为字符串 StringBuffer sbr = new StringBuffer(\ String s4 = new String(\
(25) ; //将s4连接在sbr的后面
System.out.println(sbr.toString()); //显示为 Thingking in Java
} }
答案:(22)Integer.parseInt(s1) (23)Double.parseDouble(s2)
(24)new String(z); 或 String.valueOf(z); 或 z+”” (25)sbr.append(s4)
10. 下面的程序是采用冒泡法对数组元素按小到大的顺序排序,请将程序填写完整。 public class ArraySort {
public static void main(String[] args){ } }
答案:(26)a.length -1-i或 9-i (27)a[j] = a[j+1]
(28)a[j+1] = temp
11. “同构数”是指这样的整数:它恰好出现在其平方数的右端,例如5和6就是同构数。请编写一程序找出10~999之间的同构数,并输出显示。
public class TGS {
public static void main(String[] args) {
} }
答案:(29)i*i % 100 == i (30)i*i % 1000==i
for(int i=10;i<=999;i++){
if ( (29) || (30) ) }
System.out.println(i);
int[] a = new int[]{21,34,211,15,92,68,89,794,11,863}; int temp;
for(int i=0;i<10;i++)
for(int j=0;j< (26) ;j++)
if(a[j]>a[j+1]){ }
temp = a[j];
(27) ; (28) ;
for(int i=0;i