📄 exam01_daan2.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("bbs.txt");<br> ir=new InputStreamReader(is);<br> in=new BufferedReader(ir);<br> while((s=in.readLine())!=null)<br> System.out.println("Read: "+s); <br> }<br> catch(FileNotFoundException e)<br> { System.out.println("File not found!");<br> System.exit(-2); <br> }<br> catch(IOException e)<br> { System.out.println("Error:"+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("The date is:"+year+"年"+month+"月"+day+"日");}<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+" "+salary+" "+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("Dong",600,new Day(1998,9,1));<br> staff[1] = new Employee("Wang",1000,new Day(1958,2,15));<br> staff[2] = new Employee("Meng",750,new Day(1995,3,15)); <br> FileOutputStream fo=new FileOutputStream("data.txt");<br> ObjectOutputStream so=new ObjectOutputStream(fo);<br> try<br> { for(i=0;i<3;i++) so.writeObject(staff[i]);<br> so.close(); <br> }<br> catch(IOException e)<br> { System.out.println("Saving Error:"+e);<br> System.exit(1); <br> }<br> for(i=0;i<3;i++) staff[i]=null;<br> FileInputStream fi=new FileInputStream("data.txt");<br> ObjectInputStream si= new ObjectInputStream(fi);<br> try<br> {<br> for(i=0;i<3;i++)<br> staff[i]=(Employee) si.readObject();<br> si.close();<br> } <br> catch(IOException e)<br> { System.out.print("Reading Error:"+e);<br> System.exit(1); <br> } <br> for(i=0;i<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 + -