编写学生类,包含属性:姓名、学号,重写ToString方法,返回学生姓名,重写Equals方法 - 爱问答

(爱问答)

编写学生类,包含属性:姓名、学号,重写ToString方法,返回学生姓名,重写Equals方法

编写学生类,包含属性:姓名、学号,重写ToString方法,返回学生姓名,重写Equals方法,如果两个学生对象的学号相等,则返回真,否则返回假。在Main函数中测试自己重写的两个方法。


package zhidao;

import java.util.Scanner;

public class Student implements Compare

{

   private String no;

   private String name;

   private double score;

   public Student ( String no, String name, double score )

   {

       this.no = no;

       this.name = name;

       this.score = score;

   }

   public String displays ()

   {

       StringBuilder builder = new StringBuilder ();

       builder.append ("Student [no=");

       builder.append (no);

       builder.append (", name=");

       builder.append (name);

       builder.append (", score=");

       builder.append (score);

       builder.append ("]");

       return builder.toString ();

   }

   public void setScore ( double score )

   {

       this.score = score;

   }

   public double getScore ()

   {

       return score;

   }

   @Override

   public boolean compare ( Object o )

   {

       return ( this == o || null != o && o.getClass ().equals (this.getClass ()) )

               && ( (Student) o ).score > this.score;

   }

   public static void sort ( Student[] students )

   {

       for ( int i = 0; i < students.length; i++ )

       {

           for ( int j = i + 1; j < students.length; j++ )

           {

               if (students[i].compare (students[j]))

               {

                   Student temp = students[i];

                   students[i] = students[j];

                   students[j] = temp;

               }

           }

       }

   }

   public static void main ( String[] args )

   {

       System.out.print ("建立一个含有n个学生的数组, 从键盘输入n: ");

       Scanner scanner = new Scanner (System.in);

       int count = 0;

       double scores = 0;

       int n = scanner.nextInt ();

       Student[] students = new Student[n];

       System.out.println ("从键盘输入每个学生的信息: 学号、姓名、数学成绩 用英文逗号分隔.");

       String line = null;

       try

       {

           while (count < n && null != ( line = scanner.next () ))

           {

               if (line.matches ("^w+,w+,d+(.d*)?$"))

               {

                   String no = line.split (",")[0].trim ();

                   String name = line.split (",")[1].trim ();

                   double score = Double.parseDouble (line.split (",")[2].trim ());

                   students[count] = new Student (no, name, score);

                   scores += score;

                   count++;

               }

           }

       }

       catch (ScoreException e)

       {

           e.printStackTrace ();

       }

       System.out.println ("排序:");

       sort (students);

       for ( int i = 0; i < students.length; i++ )

       {

           System.out.println (students[i].displays ());

       }

       System.out.println ("学生的数学成绩的平均分: " + ( scores / n ));

       scanner.close ();

       Student collegeStudent = new CollegeStudent ("001", "yugi111", 120, "1999-01-01", "man");

       System.out.println (collegeStudent.displays ());

   }

}

class CollegeStudent extends Student

{

   private String Date;

   private String sex;

   public CollegeStudent ( String no, String name, double score, String Date, String sex )

   {

       super (no, name, score);

       this.Date = Date;

       this.sex = sex;

   }

   @Override

   public String displays ()

   {

       StringBuilder builder = new StringBuilder ();

       builder.append ("CollegeStudent [Date=").append (Date).append (", sex=").append (sex).append ("]");

       return builder.toString ();

   }

}

interface Compare

{

   boolean compare ( Object o );

}

class ScoreException extends IllegalArgumentException

{

   private static final long serialversionUID = 1L;

   public ScoreException ()

   {

       super ();

   }

   public ScoreException ( String s )

   {

       super (s);

   }

   static ScoreException forInputString ( String s )

   {

       return new ScoreException ("For input string: "" + s + """);

   }

}


下一篇:c语言的数据类型是什么,能说得通俗一点吗

上一篇:安装EpicGames不行怎么办?急急急

热门标签:
excel 网盘 破解 word dll
最新更新:
微软重新评估新的Outlook的使用时机 联想推出搭载联发科Helio G80芯片组的Tab M9平板 英特尔创新大赛时间确定! 微软Edge浏览器在稳定渠道中推出Workspaces功能 英伟达RTX4060TiGPU推出MaxSun动漫主题! 谷歌地图为用户提供了街景服务! GameSir 在T4 Kaleid中推出了一款出色的控制器! 微软开始在Windows 11 中测试其画图应用程序的新深色模式! LG电子推出全球首款无线OLED电视 英伟达人工智能芯片崭露头角! Steam Deck可以玩什么游戏-Steam Deck价格限时优惠 雷蛇推出CobraPro鼠标 Kindle电子阅读器可以访问谷歌商店吗 Windows10如何加入组策略 window10图片查看器怎么没有了?