上机模拟试题(二)
上机模拟试题(二)
1.基本操作题 (本大题共 1 小题,共 30 分)
下列程序中,要求按照从大到小的顺序输出0—100之间(包括0和100)的能被3整除的所有偶数,并输出符合上述要求的数的个数,请将程序补充完整。程序运行结果如下:
96,90,84,78,72,66,60,54,48,42,36,30,24,18,12,6,0,17
public class ex3_1{
??? public int cnt=0;
??? public static void main(String[] args){
??? ex3_1 obj3_1=new ex3_1();
??? obj3_1.method3_1();
}
public void method3_1(){
??? for(__________){
?????? if(___________){
??? System.out.print(i+”,”);
??? _______________;
}
}
System.out.println();
System.out.println(cnt);
}
}
2.简单的应用题 (本大题共 1 小题,共 30 分)
??? 请完成下列Java程序;首先建立一个类,包含2个私有成员变量,分别为int和double型,并提供公共方法seInt()和setDouble()分别给两个变量赋值和用toString()方法显示对象的私有成员变量的值,要求将这个类定义为Serializable,在创建这个类的持久对象,将对象写入名为ex3_1.data的文件中并显示对象的2个成员变量的值,最后从这个文件中读取这个对象并显示对象的2个成员变量。
??? 注意:请勿改动main()主方法和其他已有语句内容,仅在下划线处填入适当的语句。程序运行结果如下:
??? ex3_2:i=123 d=123.456
??? ex3_2:I=123 d=123.456
??? import java.io *;
??? public class ex3_2 implements Serializable{
?????? private int n3_2;
?????? private double d3_2;
?????? ex3_2(){
?????????? n3_2=25;
?????????? d3_2=12.23;
?????? }
?????? public void setDouble (double x){
?????????? d3_2=x;
?????? }
?????? public void setInt(int x){
?????????? n3_2=x;
?????? }
?????? public String toString(){
?????????? return(“ex3_2:i”+n3_2+”d=”+d3_2);
?????? }
?????? public static void main(String[] arg){
?????????? ex3_2 el=new ex3_2;
?????????? ex3_2 e2;
?????????? el.setInt(123);
el.setDouble(123.456);
?????????? System.out.println(el);
?????????? try{
????????????? FileOutputStream fos3_2=new FileOutputStream(“ex3_2.data”);
????????????? ObjectOutputStream oos3_2=new ObjectOutputStream(fos3_2);
????????????? __________________;
?????????? }catch(IOException ioe) {
????????????? System.out.println(ioe);
?????????? }
?????????? try{
????????????? FileOutputStream fis3_2=new FileOutputStream(“ex3_2.data”);
????????????? ObjectOutputStream ois3_2=new ObjectOutputStream(fis3_2);
????????????? System.out.println(e2);
????????????? }catch(IOException ioe){
????????????????? System.out.println(ioe);
????????????? }catch(ClassNotFoundException cnfe) {
????????????????? System.out.println(cnfe);
????????????? }
?????????? }
?????? }
??? 3.综合应用题
(本大题共 1 小题,共 40 分)
??? 下面是一个Applet程序,其功能是根据给出的小时,分钟和秒数计算相等的秒数,即将1分钟化为60秒,依次类推。要求建立一个时间类,时间参数均作为类的成员变量,并且给出换算时间的方法,也作为这个类的成员函数,可以供外部对象进行调用,同时还需要在输出窗口中显示换算结果,并且将结果写到out3_3.txt文件中,本题给出确定的时间为4小时23分47秒,要求换算成秒做单位的时间。
??? 请改正程序中的错误(有下画线的语句),使程序能输出正常的结果。
??? 注意:不改动程序的结构,不得增行或删行。
??? import java.io.*;
??? import.java.awt.*;
??? import java.applet.*;
/*
??? <applet code=ex3_3.class width=800 height=400>
??? </applet>
??? */
??? public class ex3_3 extends Applet{
?????? public void paint (Graphics g){
?????????? int nSum;
?????????? class myTime3_3{
????????????? public int h;
????????????? public int m;
????????????? public int s;
????????????? public int out;
????????????? public int caculateSecond(){
????????????????? out=h*60+m*60+s;
????????????????? return out;
????????????????? }
????????????? }
????????????? myTime3_3 objTime3_3=new myTime3_3();
????????????? objTime3_3.h=4;
????????????? objTime3_3.s=47;
????????????? nSum=objTime3_3.caculateSecond();
????????????? g.drawstring(“时:”+objectTime3_3.h,20,30);
????????????? g.drawstring(“分:”+objectTime3_3.m,20,30);
????????????? g.drawstring(“秒:”+objectTime3_3.s,20,30);
????????????? g.drawstring(“合计:”+objTime3_3.out+”秒”,20,90);
?????????? try{
????????????? FileOutputStram fos3_3=new FileOutputStream(“out3_3.txt”);
????????????? BufferedOutputStream bos3_3=new BufferedOutputStream(fos3_3,1024);
????????????? PrintStream ps3_3=new PrintStream(bos3_3,false);
????????????? System.setout(ps3_3);
????????????? System.out.println(“g.drawstring(“合计:”+objectTime3_3.out+”秒””);
????????????? ps3_3.close();
?????????? }catch(IOException ioe){
????????????? System.out.println(ioe);
?????????? }
????????????? }
?????????? }
?????????? ex3_3.html
?????????? <HTML>
?????????? <HEAD>
????????????? <TITLE>ex3_3</TITLE>
?????????? </HEAD>
?????????? <applet code=”ex3_3.class”width=800 height=400>
?????????? </applet>
??? </BODY>
??? <HTML/>