问答题

请完成以下程序,首先由一个类Example2_3实现Serializable接口,并有三个成员变量,分别为int型、double型和String型,可以用toString的方法显示这三个成员变量。在main方法中创建这个Example2_3的持久对象,根据用户在命令行输入的三个参数来设定其中成员变量的值。然后,将这个对象写入名为TheSerial.data的文件,并显示成员变量。最后从文件TheSerial.data中读出三个成员变量并显示出来。 注意:请勿改动main()主方法和其他已有语句内容,仅在横线处填入适当语句。 import java.io.*; class TheSerial implements Serializable { private int intValue; private double doubleValue; private String string; TheSerial() { intValue = 123; doubleValue = 12.34; string = "Serialize Test"; } public void setDouble(double d) { doubleValue = d; } public void setInt(int i) { intValue = i; } public void setString(String s) { string = s; } public String toString() { return("int="+intValue+"double="+doubleValue+" string="+string); } } public class Example2_3 { public static void main(String argv[]) { TheSerial e1 = new TheSerial(); TheSerial e2; try { e1.setInt(Integer.parseInt(argv[0])); e1.setDouble(Double.parseDouble(argv[1])); e1.setString[argv[2]); } catch(Exception e) { e1.setString(e.getMessage{)); } System.out.println(e1); try { FileOutputStream oS = new FileOutputStream("TheSerial.data"); ObjectOutputStream oIS = new ObjectOutputStream(oS); ______; } catch(IOException ioException) { System.out.println(ioException.getMessage()); } try { FileInputStream iS = new FileInputStream("TheSerial. data"); ObjectInputStream oIS = new ObjectInputStream(iS); ______ System.out.println(e2); } catch(IOException ioException) { System.out.println(ioException.getMessage()); } catch(ClassNotFoundException cnfException) { System.out.println(cnfException.getMessage()); } } }

【参考答案】

①oOS.writeObject(e1) ②e2=(TheSerial)oIS.readObject()
热门 试题

问答题
本程序的功能是监听对于菜单项和工具条按钮的操作。主窗口中有菜单、工具条和一个文字标签,菜单中有“文件”项,“文件”菜单中包括菜单项“新建”、“保存”、“打印”、“页面设置”和“退出”,工具条上包括按钮“新建”、“保存”、“打印”、“页面设置”和“退出”。单击任何一个菜单项或任何一个工具条按钮,文字标签都会显示哪个构件被选中。请更正题中带下划线的部分。运行结果如下图所示。注意:不改变程序的结构,不得增行或删行。import javax.swing.*;import java.awt.*;import java.awt.event.*;public class advance extends JFrame{private JLabel actionlnfo;public advance(String titleText){setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);super(titleText);actionInfo=new JLabel( 事件信息 );JToolBar tb=new JTooBar();Menu file=new Menu( 文件 );JMenuBar mb=new JMenuBar();mb.add(file);NewAction na=new NewAction(actionInfo);file.add(na);tb.add(na);SaveAction sa=new SaveAction(actionInfo);file.add(sa);tb.add(sa);PrintAction pta=new PrintAction(actionInfo);file.add(pra);tb.add(pra);PageSetAction psa=new PageSetAction(actionInfo);file.add(psa);tb.add(psa);ExitAction ea=new ExitAction(actionInfo);file.add(ea);tb.add(ea);setJMenuBar(mb);Container cp=getContentPane();cp.setLayout(new BorderLayout());cp.add(tb);cp.add(actionInfo, BorderLayout.CENTER );setSize(350, 200);setVisible(true);}public static void main(String[] args){new advance( advance );}}class NewAction extends AbstractAction{JLabel label;public NewAction(JLabel label){super( 新建 );this.label=label;}public void actionPerformed(ActionEvent e){label.setText( 您选择了新建 );}}class SaveAction extends AbstractAction{JLabel label;public SaveAction(JLabel label){super( 保存 );this.label=label;}public void actionPerformed(ActionEvent e){label.setText ( 您选择了保存 );}}class printAction extends AbstractAction{JLabel label;public PrintAction(JLabel label){super( 打印 );this.label=label;}public void actionPerformed(ActionEvent e){label.setText ( 您选择了打印 );}}class PageSetAction extends AbstractAction{JLabel label;public pageSetAction(JLabel label){super( 页面设置 );this.label=label;}public void actionPerformed(ActionEvent e){label.setText( 您选择了页面设置 );}}class ExitAction extends AbstractAction{JLabel label;public ExitAction(JLabel label){super( 退出 );this.label=label;}public void actionPerformed(ActionEvent e){label.setText ( 您选择了退出 );}}