编写学生类,包含属性:姓名、学号,重写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 + """);
}
}