关于《全国计算机等级考试二级教程(Java语言程序设计)(2019年版)》中的一些代码错误的纠正


       本人是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 }

 

由于我书还没看完,先找到这基础错误,待我发现新的错误后继续和大家分享讨论。

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM