上机模拟试题(四)

上机模拟试题(四)

一、            基本操作题 (本大题共 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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值