上机模拟试题(四)
上机模拟试题(四)
一、??????????? 基本操作题 (本大题共 1 小题,共 30 分)
下列程序用循环语句将数组a的所有元素分别赋给数组c。请将程序补充完整。
注意:请勿改动已有的语句内容,仅在下划线处填入适当的语句。
程序运行结果:
c[0]=1
c[1]=2
c[2]=3
c[3]=4
c[4]=5
源程序如下:
public classTest
{
? public static void main(String[ ] args)
? {
int a[ ]={1,2,3,4,5};
int c[ ]=new int[a.length];
[1]??? (int i=0;I<a.length; i + +)
{
??? c [ i ]=[2]?? ??;
??? System.out.println(“c[“+ i +”]=”+c[ i ]);
?}
}
}
二、??????????? 简单应用题 (本大题共 1 小题,共 30 分)
使用对象的串行化机制构造一个应用程序,使之能够存取文件的记录。
注意:请勿改动已有的语句内容,仅在下划线处填入适当的语句。
程序运行结果如下:
张三 600.0 1999
李四 1000.0 1962
王五 750.0 1988
源程序如下:
ObjectTest.java
Import java.io.*;
class MyDate implements Serializable
{
private int year,month,day;
?
public MyDate()
{
???? year = 2005
???? month = 2
???????? day = 27
}
?
public MyDate(int year, int month, int day)
{
this . year = year ;
this . month = month ;
this . day = day ;
?
}
?
public int getYear( )
{
return year ;
}
public int getMonth( )
{
return month ;
}
public int getDay( )
{
return day ;
}
?
public void print( )
{
System.out.println(“日期是: “+year+”年”+month+”月”+day+”日”);
}
}
?
class Employee implements Serializable
{
? public static final int NAMESIZE = 40 ;
? public static final int RECORDSIZE = 2*NAMESIZE+8+4*3;
? private String name ;
? private double salary ;
? private MyDate hireDay ;
?
? public Employee( ) {
}
?
public Employee (String name, double salary ,MyDate hireDay)
{
this . name = name ;
this . salary = salary ;
this . hireDay = hireDay ;
}
?
public void print ( )
{
System .out . println (name +” ” +salary +” ”+hireYear ( ) );
}
?
public void raiseSalary(double by Percent)
{
salary * =1 + byPercent/100 ;
}
?
public int hireYear( )
{
??? return hireDay.getYear( );
}
}
?
public class ObjectTest
?? public static viod main((String[ ] args )
throws IOException,ClassNotFoundException
?? {
Employee[ ] staff = new Employee[3]
staff [0] = new Employee (“张三”,600,new MyDate(1999,9,1));
?staff [1] = new Employee (“李四”,1000,new MyDate(1962,7,1));
?staff [2] = new Employee (“王五”,750,new MyDate(1988,3,1));
?
FileOutputStream fos = new FileOutputStream(“data.txt”);
ObjectOutputStream oos = new ObjectOutputStream(fos);
try
{
??? for(int I =0; i < 3 ; i + +)
?????? oos.writeObject(staff [ i ] );
??? oos . close ( );
}
catch(IOException e)
}
??? System . out . println(“保存文件错误:”+e);
??? System . exit(1);
}
?
for(int i = 0; i<3;i ++)
?? staff[ i ]=null;
?
FileInputStream fis = new FileInputStream(“date.txt”);
ObjectInputStream ois = new ObjectInputStream(fis);
try??
{
??? for(int i =0 ; i <3 ; i + +)
???????? staff[ i ] = ???????????;
????????? ois.close( );
}
catch(IOException e)
{
????? System.out.println(“读取文件错误:”+e);
????? System.exit(1)
}
?
for(int i =0; i <3; i + +)
????? ????????[ 2 ]?????
}
}
三、??????????? 综合应用题 ( 本大题共 1 小题,共 40 分)
下面是一个Appliation应用程序,其功能特点是点击框架上的“退出”按钮,弹出确认对话框,单击对话框中的“确定”按钮,退出程序。
请改正程序中的错误(由下划线的语句),使程序能够输出正确的结果。
源程序如下:
import java.awt.*
import java.awt.event.*
import javax.swing.*;
import javax.swing.event.*;
?
public class FrameTest3
{
public static void main(String[ ]args)
{
DialogFrame frame = new DialogFrame( );
frame.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);
frame.show( );
}
}
?
class DialogFrame extends JFrame
{
public DialogFrame( )
{
?? setTitle(“对话框测试”);
?? setSize(WIDTH,HETGHT);
??
?? Container contentPane = getContentPane
?
?? Jpanel buttonPanel = new Jpanel( )
?? Jbutton logoutButton = new Jbutton(“退出”);
?? LogoutButton.addActionListener(new LogoutButton( ) );//[1]
?? ButtonPanel.add(logoutButton);
?
??? ContentPane.add(buttonPanel,BorderLayout.SOUTH);
}
private class LogoutAction implements ActionListener
{
??? public void actionPerformed(ActionEvent event)
??? {
????? int selection = JoptionPane.showConfirmDialog(
????????? DialogFrame.this,
????????? “你确实要退出吗?”,
????????? “退出?”,
?????????? JOptionPane.OK_CANCEL_OPTION,
?????????? JoptionPane.WARNING_MESSAGE);
????? ???If(Logout = =JoptionPane.OK_OPTION) // [2 ]
System.exit ( 0 );
?????????? }
}
?
public static final int WIDTH = 200;
public static final int HEIGHT = 120;
}