⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 exam01_daan2.htm

📁 清华大学JAVA教程
💻 HTM
字号:
<html><head><title>Untitled Document</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><link rel="stylesheet" href="../../css/text.css" type="text/css"></head><body bgcolor="#FFFFFF" text="#000000"><center><table width="95%" border="0" cellspacing="0" cellpadding="0">  <tr>      <td class=text>         <p><b>三、 编程题答案</b></p>        <p>编程第<font color="#0000FF">1</font>题<br>            import java.io.*;<br>            public class ReadFile<br>            {  public static void main(String args[])<br>              {  String s;<br>                FileInputStream is;<br>                InputStreamReader ir;<br>                BufferedReader in;<br>                try<br>                { is=new FileInputStream(&quot;bbs.txt&quot;);<br>                  ir=new InputStreamReader(is);<br>                  in=new BufferedReader(ir);<br>                  while((s=in.readLine())!=null)<br>                  System.out.println(&quot;Read: &quot;+s); <br>                 }<br>                catch(FileNotFoundException e)<br>                { System.out.println(&quot;File not found!&quot;);<br>                  System.exit(-2); <br>                }<br>                catch(IOException e)<br>                { System.out.println(&quot;Error:&quot;+e);<br>                  System.exit(-3); <br>                } <br>              }<br>            }<br>          <br>          编程第<font color="#0000FF">2</font>题<br>            import java.io.*;<br>            class Day implements Serializable<br>            {  private int year,month,day;<br>              public Day()<br>              {  year=2000;<br>                month=9;<br>                day=21;<br>              } <br>              public Day(int y,int m,int d)<br>              {  year=y; <br>                month=m;<br>                day=d;<br>              }<br>              public int getYear() { return year;}<br>              public int getMonth(){ return month;}<br>              public int getDay() { return day;}<br>              public void outPut() { System.out.println(&quot;The date is:&quot;+year+&quot;年&quot;+month+&quot;月&quot;+day+&quot;日&quot;);}<br>            }<br>            class Employee implements Serializable<br>            {  public static final int NAMESIZE=40;<br>              public static final int RECORDSIZE=2*NAMESIZE+8+4*3;<br>              private String name;<br>              private double salary;<br>              private Day hireDay;<br>              public Employee(){}<br>              public Employee(String n,double s,Day d)<br>              {  name=n;<br>                salary=s;<br>                hireDay=d;<br>              } <br>              public void print()<br>              {  System.out.println(name+&quot; &quot;+salary+&quot; &quot;+hireYear());}<br>              public void raiseSalary(double byPercent)<br>              {  salary*=1+byPercent/100; }<br>              public int hireYear()<br>              {  return hireDay.getYear(); }<br>              }<br>              public class ObjectSer<br>              {  public static void main(String args[])<br>                throws IOException,ClassNotFoundException<br>                { int i;<br>                 Employee[] staff = new Employee[3];<br>                 staff[0] = new Employee(&quot;Dong&quot;,600,new Day(1998,9,1));<br>                 staff[1] = new Employee(&quot;Wang&quot;,1000,new Day(1958,2,15));<br>                 staff[2] = new Employee(&quot;Meng&quot;,750,new Day(1995,3,15));           <br>                 FileOutputStream fo=new FileOutputStream(&quot;data.txt&quot;);<br>                 ObjectOutputStream so=new ObjectOutputStream(fo);<br>                 try<br>                 { for(i=0;i&lt;3;i++) so.writeObject(staff[i]);<br>                  so.close(); <br>                 }<br>                 catch(IOException e)<br>                 { System.out.println(&quot;Saving Error:&quot;+e);<br>                  System.exit(1); <br>                 }<br>                 for(i=0;i&lt;3;i++) staff[i]=null;<br>                 FileInputStream fi=new FileInputStream(&quot;data.txt&quot;);<br>                 ObjectInputStream si= new ObjectInputStream(fi);<br>                 try<br>                 {<br>                  for(i=0;i&lt;3;i++)<br>                  staff[i]=(Employee) si.readObject();<br>                  si.close();<br>                 } <br>                 catch(IOException e)<br>                 { System.out.print(&quot;Reading Error:&quot;+e);<br>                  System.exit(1); <br>                 } <br>                 for(i=0;i&lt;3;i++) staff[i].print(); <br>               }<br>            }          <!--czp-wenda-daan-->                    </p>      </td>  </tr></table>  </center></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -