A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 马金池 中级黑马   /  2013-1-23 23:15  /  1435 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文


  1. /*
  2. 有五个学生,每个学生有3门课的成绩,
  3. 从键盘输入以上数据(包括姓名,三门课成绩),
  4. 输入的格式:如:zhagnsan,30,40,60计算出总成绩,
  5. 并把学生的信息和计算出的总分数高低顺序存放在磁盘文件"stud.txt"中。

  6. 1,描述学生对象。
  7. 2,定义一个可操作学生对象的工具类。

  8. 思想:
  9. 1,通过获取键盘录入一行数据,并将该行中的信息取出封装成学生对象。
  10. 2,因为学生有很多,那么就需要存储,使用到集合。因为要对学生的总分排序。
  11.         所以可以使用TreeSet。
  12. 3,将集合的信息写入到一个文件中。


  13. */

  14. import java.io.*;
  15. import java.util.*;

  16. class Student implements Comparable<Student>
  17. {
  18.         private String name;
  19.         private int ma,cn,en;
  20.         private int sum;

  21.         Student(String name,int ma,int cn,int en)
  22.         {
  23.                 this.name = name;
  24.                 this.ma = ma;
  25.                 this.cn = cn;
  26.                 this.en = en;
  27.                 sum = ma + cn + en;
  28.         }


  29.         public int compareTo(Student s)
  30.         {
  31.                 int num = new Integer(this.sum).compareTo(new Integer

  32. (s.sum));
  33.                 if(num==0)
  34.                         return this.name.compareTo(s.name);
  35.                 return num;
  36.         }



  37.         public String getName()
  38.         {
  39.                 return name;
  40.         }
  41.         public int getSum()
  42.         {
  43.                 return sum;
  44.         }

  45.         public int hashCode()
  46.         {
  47.                 return name.hashCode()+sum*78;

  48.         }
  49.         public boolean equals(Object obj)
  50.         {
  51.                 if(!(obj instanceof Student))
  52.                         throw new ClassCastException("类型不匹配");
  53.                 Student s = (Student)obj;

  54.                 return this.name.equals(s.name) && this.sum==s.sum;
  55.         }

  56.         public String toString()
  57.         {
  58.                 return "student["+name+", "+ma+", "+cn+", "+en+"]";
  59.         }
  60. }

  61. class StudentInfoTool
  62. {
  63.         public static Set<Student> getStudents()throws IOException
  64.         {
  65.                 return getStudents(null);
  66.         }

  67.         public static Set<Student> getStudents(Comparator<Student> cmp)

  68. throws IOException
  69.         {
  70.                 BufferedReader bufr =
  71.                         new BufferedReader(new InputStreamReader

  72. (System.in));

  73.                 String line = null;
  74.                
  75.                 Set<Student> stus  = null;
  76.                 if(cmp==null)
  77.                         stus = new TreeSet<Student>();
  78.                 else
  79.                         stus = new TreeSet<Student>(cmp);
  80.                 while((line=bufr.readLine())!=null)
  81.                 {
  82.                         if("over".equals(line))
  83.                                 break;
  84.                        
  85.                         String[] info = line.split(",");
  86.                        
  87.                         Student stu = new Student(info[0],Integer.parseInt

  88. (info[1]),
  89.                                                                                

  90. Integer.parseInt(info[2]),
  91.                                                                                

  92. Integer.parseInt(info[3]));

  93.                        
  94.                         stus.add(stu);
  95.                 }

  96.                 bufr.close();

  97.                 return stus;
  98.         }

  99.         public static void write2File(Set<Student> stus)throws IOException
  100.         {
  101.                 BufferedWriter bufw = new BufferedWriter(new FileWriter

  102. ("stuinfo.txt"));

  103.                 for(Student stu : stus)
  104.                 {
  105.                         bufw.write(stu.toString()+"\t");
  106.                         bufw.write(stu.getSum()+"");
  107.                         bufw.newLine();
  108.                         bufw.flush();
  109.                 }

  110.                 bufw.close();

  111.         }
  112. }



  113. class StudentInfoTest
  114. {
  115.         public static void main(String[] args) throws IOException
  116.         {

  117.                 Comparator<Student> cmp = Collections.reverseOrder();

  118.                 Set<Student> stus = StudentInfoTool.getStudents(cmp);

  119.                 StudentInfoTool.write2File(stus);
  120.         }
  121. }
复制代码
其中public static Set<Student> getStudents()throws IOException
        {
                return getStudents(null);
        }
这个是做什么的

5 个回复

倒序浏览
返回一个set集合,存储学生类型的set集合
回复 使用道具 举报
删了试试。。。
回复 使用道具 举报
实现getStudents()方法的覆写,提高程序的健壮性和可用性
如果你不传入比较器,那么就是用默认的排序方法
如果不写这个的话,那么用户在使用getStudents()时就必须要传递参数
回复 使用道具 举报
楼上正解额.我想半天也说不出来.
回复 使用道具 举报
返回学生集合,方法定义是 public static Set<Student> getStudents(Comparator<Student> cmp),我传入null,就产生一个无比较器的treeSet
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马