本人是Java語言的初學者,並且買了《全國計算機等級考試二級教程(Java語言程序設計)》這本教材,想通過自己的努力自學Java語言並通過國二考試,但是這本教材中的程序案例屢屢出現錯誤,讓我這個初學者非常痛苦,這也說明了這些教材編寫者的疏忽大意。我通過上網大量的搜索和學習,糾正了教材中的一些錯誤代碼段,在這里分享給那些要考國二java考試並且買了這本教材的考生們。還有國二用的是jdk6,如果用別的版本運行,可能會出現一些錯誤,建議大家不要用別版本去學國二教材。
1.
80頁
由於未導入相關方法包使程序運行錯誤。
書中原代碼:例5.6 ObjectTest.java
1 public class ObjectTest 2 { 3 public static void main(String[] args) 4 { 5 Employee zhang1=new Employee("張浩",75000,1987,12,15); 6 Employee zhang2=zhang1; 7 Employee zhang3=new Employee("張浩",75000,1987,12,15); 8 Employee bob=new Employee("Bob Brandson",50000,1989,10,1); 9 System.out.println("zhang1==zhang2:"+(zhang1==zhang2)); 10 System.out.println("zhang1==zhang3:"+(zhang1==zhang3)); 11 System.out.println("zhang1.equals(zhang3):"+zhang1.equals(zhang3)); 12 System.out.println("zhang1.equals(bob):"+zhang1.equals(bob)); 13 System.out.println("bob.toString():"+bob); 14 Manager carl=new Manager("Carl Cracker",80000,1987,12,15); 15 Manager boss=new Manager("Carl Cracker",80000,1987,12,15); 16 boss.setBonus(5000); 17 System.out.println("boss.toString():"+boss); 18 System.out.println("carl.equals(boss):"+carl.equals(boss)); 19 System.out.println("zhang1.hashCode():"+zhang1.hashCode()); 20 System.out.println("zhang3.hashCode():"+zhang3.hashCode()); 21 System.out.println("bob.hashCode():"+bob.hashCode()); 22 System.out.println("carl.hashCode():"+carl.hashCode()); 23 System.out.println("boss.hashCode():"+boss.hashCode()); 24 } 25 } 26 class Employee 27 { 28
29 public Employee(String n,double s,int year,int month,int day) 30 { 31 name=n; 32 salary=s; 33 GregorianCalendar calendar=new GregorianCalendar(year,month-1,day); 34 hireDay=calendar.getTime(); 35 } 36 public boolean equals(Object otherObject) 37 { 38 if(this==otherObject)return true; 39 if(otherObject==null)return false; 40 if(getClass()!=otherObject.getClass())return false; 41 Employee other=(Employee)otherObject; 42 return name.equals(other.name)&&salary==other.salary&&hireDay.equals(other.hireDay); 43 } 44 public int hashCode() 45 { 46 return 7*name.hashCode()+11*new Double(salary).hashCode()+13*hireDay.hashCode(); 47 } 48 public String toString() 49 { 50 return getClass().getName()+"[name="+name+",salary="+salary+",hireDay="+hireDay+"]"; 51 } 52 private String name; 53 private double salary; 54 private Date hireDay; 55 } 56 class Manager extends Employee 57 { 58 public Manager(String n,double s,int year,int month,int day) 59 { 60 super(n,s,year,month,day); 61 bonus=0; 62 } 63 public void setBonus(double b) 64 { 65 bonus=b; 66 } 67 public boolean equals(Object otherObject) 68 { 69 if(!super.equals(otherObject)) 70 return false; 71 Manager other=(Manager)otherObject; 72 return bonus==other.bonus; 73 } 74 public int hashCode() 75 { 76 return super.hashCode()+17*new Double(bonus).hashCode(); 77 } 78 public String toString() 79 { 80 return super.toString()+"[bonus="+bonus+"]"; 81 } 82 private double bonus; 83 }
本人糾正后代碼:
只是在代碼開頭導入包,加入兩行代碼即可,
1 import java.util.GregorianCalendar; 2 import java.util.Date; 3 public class ObjectTest 4 { 5 public static void main(String[] args) 6 { 7 Employee zhang1=new Employee("張浩",75000,1987,12,15); 8 Employee zhang2=zhang1; 9 Employee zhang3=new Employee("張浩",75000,1987,12,15); 10 Employee bob=new Employee("Bob Brandson",50000,1989,10,1); 11 System.out.println("zhang1==zhang2:"+(zhang1==zhang2)); 12 System.out.println("zhang1==zhang3:"+(zhang1==zhang3)); 13 System.out.println("zhang1.equals(zhang3):"+zhang1.equals(zhang3)); 14 System.out.println("zhang1.equals(bob):"+zhang1.equals(bob)); 15 System.out.println("bob.toString():"+bob); 16 Manager carl=new Manager("Carl Cracker",80000,1987,12,15); 17 Manager boss=new Manager("Carl Cracker",80000,1987,12,15); 18 boss.setBonus(5000); 19 System.out.println("boss.toString():"+boss); 20 System.out.println("carl.equals(boss):"+carl.equals(boss)); 21 System.out.println("zhang1.hashCode():"+zhang1.hashCode()); 22 System.out.println("zhang3.hashCode():"+zhang3.hashCode()); 23 System.out.println("bob.hashCode():"+bob.hashCode()); 24 System.out.println("carl.hashCode():"+carl.hashCode()); 25 System.out.println("boss.hashCode():"+boss.hashCode()); 26 } 27 } 28 class Employee 29 { 30
31 public Employee(String n,double s,int year,int month,int day) 32 { 33 name=n; 34 salary=s; 35 GregorianCalendar calendar=new GregorianCalendar(year,month-1,day); 36 hireDay=calendar.getTime(); 37 } 38 public boolean equals(Object otherObject) 39 { 40 if(this==otherObject)return true; 41 if(otherObject==null)return false; 42 if(getClass()!=otherObject.getClass())return false; 43 Employee other=(Employee)otherObject; 44 return name.equals(other.name)&&salary==other.salary&&hireDay.equals(other.hireDay); 45 } 46 public int hashCode() 47 { 48 return 7*name.hashCode()+11*new Double(salary).hashCode()+13*hireDay.hashCode(); 49 } 50 public String toString() 51 { 52 return getClass().getName()+"[name="+name+",salary="+salary+",hireDay="+hireDay+"]"; 53 } 54 private String name; 55 private double salary; 56 private Date hireDay; 57 } 58 class Manager extends Employee 59 { 60 public Manager(String n,double s,int year,int month,int day) 61 { 62 super(n,s,year,month,day); 63 bonus=0; 64 } 65 public void setBonus(double b) 66 { 67 bonus=b; 68 } 69 public boolean equals(Object otherObject) 70 { 71 if(!super.equals(otherObject)) 72 return false; 73 Manager other=(Manager)otherObject; 74 return bonus==other.bonus; 75 } 76 public int hashCode() 77 { 78 return super.hashCode()+17*new Double(bonus).hashCode(); 79 } 80 public String toString() 81 { 82 return super.toString()+"[bonus="+bonus+"]"; 83 } 84 private double bonus; 85 }
2.
86頁例5.9 EnumTest.java這個程序是有問題的,但是我還沒改過來,有改過來的希望給留言一下,教教我啊。
書中原碼:
EnumTest.java
1 import java.util.*; 2 public class EnumTest 3 { 4 public static void main(String[] args) 5 { 6 Scanner in=new Scanner(System.in); 7 System.out.print("請輸入服裝號:(SMALL,MEDIUM,LARGE,EXTRA_LARGE,EXTRA_EXTRA_LARGE)"); 8 String input=in.next().toUpperCase(); 9 Size size=Enum.valueOf(Size.class,input); 10 System.out.println("size="+size); 11 System.out.println("abbreviation="+Size.getAbbreviation()); 12 if(size==Size.EXTRA_LARGE^size==Size.EXTRA_EXTRA_LARGE) 13 { 14 System.out.println("很好!你注意到了下划線“_”。"); 15 } 16 } 17 enum Size{SMALL("S"),MEDIUM("M"),LARGE("L"),EXTRA_LARGE("XL"),EXTRA_EXTRA_LARGE("XXL")}; 18 private Size(String abbreviation) 19 { 20 this.abbreviation=abbreviation; 21 } 22 public String getAbbreviation() 23 { 24 return abbreviation; 25 } 26 private String abbreviation; 27 }
3.
129頁,例7.3 ZipTest.java
其中兩行代碼可能是書寫有誤,我也沒看懂啥意思,最后在網上查資料改了過來。
本人修正后代碼:
1 import java.awt.*; 2 import java.awt.event.*; 3 import java.io.*; 4 import java.util.*; 5 import java.util.List; 6 import java.util.zip.*; 7 import javax.swing.*; 8 public class ZipTest { 9
10 public static void main(String[] args) { 11 // TODO Auto-generated method stub
12 EventQueue.invokeLater(new Runnable() { 13 public void run() { 14 ZipTestFrame frame=new ZipTestFrame(); 15 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 16 frame.setVisible(true); 17 } 18 }); 19 } 20
21 } 22 class ZipTestFrame extends JFrame{ 23 /**
24 * 25 */
26 private static final long serialVersionUID = 1L; 27 public ZipTestFrame() { 28 setTitle("ZipTest"); 29 setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT); 30 JMenuBar menuBar=new JMenuBar(); 31 JMenu menu=new JMenu("File"); 32 JMenuItem openItem=new JMenuItem("Open"); 33 menu.add(openItem); 34 openItem.addActionListener(new ActionListener(){ 35 public void actionPerformed(ActionEvent event) { 36 JFileChooser chooser=new JFileChooser(); 37 chooser.setCurrentDirectory(new File(".")); 38 int r=chooser.showOpenDialog(ZipTestFrame.this); 39 if(r==JFileChooser.APPROVE_OPTION) { 40 zipname=chooser.getSelectedFile().getPath(); 41 fileCombo.removeAllItems(); 42 scanZipFile(); 43 } 44 } 45 }); 46 JMenuItem exitItem=new JMenuItem("Exit"); 47 menu.add(exitItem); 48 exitItem.addActionListener(new ActionListener() { 49 public void actionPerformed(ActionEvent event) { 50 System.exit(0); 51 } 52 }); 53 menuBar.add(menu); 54 setJMenuBar(menuBar); 55 fileText=new JTextArea(); 56 fileCombo=new JComboBox(); 57 fileCombo.addActionListener(new ActionListener() { 58 public void actionPerformed(ActionEvent event) { 59 loadZipFile((String)fileCombo.getSelectedItem()); 60 } 61 }); 62 add(fileCombo,BorderLayout.SOUTH); 63 add(new JScrollPane(fileText),BorderLayout.CENTER); 64 } 65 public void scanZipFile() { 66 new SwingWorker<Void,String>(){ 67 protected Void doInBackground()throws Exception{ 68 ZipInputStream zin=new ZipInputStream(new FileInputStream(zipname)); 69 ZipEntry entry; 70 while((entry=zin.getNextEntry())!=null) { 71 publish(entry.getName()); 72 zin.closeEntry(); 73 } 74 zin.close(); 75 return null; 76 } 77 protected void process(List<String> names) { 78 for(String name:names)fileCombo.addItem(name); 79 } 80 }.execute(); 81 } 82 public void loadZipFile(final String name) { 83 fileCombo.setEnabled(false); 84 fileText.setText(""); 85 new SwingWorker<Void,Void>(){ 86 protected Void doInBackground()throws Exception{ 87 try { 88 ZipInputStream zin = new ZipInputStream(new FileInputStream(zipname)); 89 ZipEntry entry; 90 while((entry=zin.getNextEntry())!=null) { 91 if(entry.getName().equals(name)) { 92 Scanner in=new Scanner(zin); 93 while(in.hasNextLine()) { 94 fileText.append(in.nextLine()); 95 fileText.append("\n"); 96 } 97 } 98 zin.closeEntry(); 99 } 100 zin.close(); 101 } 102 catch(IOException e) { 103 e.printStackTrace(); 104 } 105 return null; 106 } 107 protected void done() { 108 fileCombo.setEnabled(true); 109 } 110 }.execute(); 111 } 112 protected void ZipInputStream(FileInputStream fileInputStream) { 113 // TODO Auto-generated method stub
114
115 } 116 public static final int DEFAULT_WIDTH=400; 117 public static final int DEFAULT_HEIGHT=300; 118 private JComboBox fileCombo; 119 private JTextArea fileText; 120 private String zipname; 121 }
由於我書還沒看完,先找到這基礎錯誤,待我發現新的錯誤后繼續和大家分享討論。