package textstudent; |
import java.io.Serializable; |
public class Student implements Serializable { |
private String id; |
private String name; |
private int eng; |
private int math; |
private int comp; |
private int sum; |
public Student(String id,String name, int eng, int math, int comp){ |
this .id=id; |
this .name=name; |
this .eng=eng; |
this .math=math; |
this .comp=comp; |
sum(); |
} |
public Student(Student s){ |
this .id= s.id; |
this .name= s.name; |
this .eng=s.eng; |
this .math=s.math; |
this .comp=s.comp; |
sum(); |
} |
public void setId(String id){ |
this .id=id; |
} |
public void setName(String name){ |
this .name=name; |
} |
public void setEng( int eng) { |
this .eng = eng; |
} |
public void setMath( int math) { |
this .math = math; |
} |
public void setComp( int comp) { |
this .comp = comp; |
} |
public String getId() { |
return id; |
} |
public String getName() { |
return name; |
} |
public int getEng() { |
return eng; |
} |
public int getMath() { |
return math; |
} |
public int getComp() { |
return comp; |
} |
public int getSum() { |
return sum; |
} |
void sum(){ |
this .sum=eng+math+comp; |
} |
public String toString(){ |
return getId()+ "\t" +getName()+ "\t" +getEng()+ "\t" |
+getMath()+ "\t" +getComp()+ "\t" +getSum(); |
} |
public boolean equals(Object x){ |
if ( this .getClass()!=x.getClass()) |
return false ; |
Student b=(Student) x; |
return ( this .getId().equals(b.getId())); |
} |
public int compare(Student A){ |
if ( this .getSum()>A.getSum()) |
return 1 ; |
else if ( this .getSum()==A.getSum()) |
return 0 ; |
else |
return - 1 ; |
} |
} |
-------------------------------------------------------------------------- |
package textstudent; |
public class StudentClass { |
private String name; |
static int capacity= 40 ; |
private Student students[]; |
private int size; |
public StudentClass(String name, int size){ |
this .name=name; |
this .size=size; |
students= new Student[capacity]; |
} |
public String getName() { |
return name; |
} |
public static int getCapacity() { |
return capacity; |
} |
public Student[] getStudents() { |
return students; |
} |
public int getsize() { |
return size; |
} |
public void setName(String name) { |
this .name = name; |
} |
public static void setCapacity( int capacity) { |
StudentClass.capacity = capacity; |
} |
public void setSize( int size) { |
if (size>capacity){ |
System.out.println( "size为" +size+ ",不能超过" +capacity); |
return ; |
} |
this .size=size; |
} |
public void setStudents(Student... students) { |
for ( int i= 0 ;i<size;i++){ |
this .students[i]= new Student(students[i]); |
} |
this .size=students.length; |
} |
public String toString(){ |
String s; |
s= "班级" +name+ "\t" + "容量" +capacity+ "\t" + "实际人数" +size+ "\n\n" ; |
s=s+ "学号" + "\t" + "姓名" + "\t" + "英语" + "\t" + "数学" + "\t" + "计算机" + "\t" + "总成绩\n" ; |
for ( int i= 0 ;i<size;i++){ |
s=s+students[i].getId()+ "\t" +students[i].getName()+ "\t" +students[i].getEng()+ "\t" +students[i].getMath() |
+ "\t" +students[i].getComp()+ "\t" +students[i].getSum()+ "\n" ; |
} |
return s; |
} |
//查找 |
public int find(String id){ |
for ( int i= 0 ;i<size;i++) { |
if (students[i].getId().equals(id)) |
return i; |
} |
return - 1 ; |
} |
//增加 |
public boolean add(Student aStudent){ |
if (size==capacity) |
return false ; |
if (find(aStudent.getId())>= 0 ) |
return false ; |
this .students[size]= new Student( new String(aStudent.getId()), new String(aStudent.getName()), |
aStudent.getEng(),aStudent.getMath(),aStudent.getComp()); |
size++; |
return true ; |
} |
//删除 |
public boolean del(Student aStudent){ |
int pos=find(aStudent.getId()); |
if (pos==- 1 ) |
return false ; |
for ( int i=pos+ 1 ;i<size;i++){ |
students[i- 1 ]=students[i]; |
} |
size--; |
return true ; |
} |
public boolean del(String id){ |
int pos=find(id); |
if (pos==- 1 ) |
return false ; |
for ( int i=pos+ 1 ;i<size;i++){ |
students[i- 1 ]=students[i]; |
} |
size--; |
return true ; |
} |
// 选择排序 |
public void selectionSort(){ |
Student temp; |
for ( int i= 0 ;i<size- 1 ;i++){ |
for ( int j= 0 ;j<size- 1 -i;j++){ |
if (students[j].getSum()>students[j+ 1 ].getSum()){ |
temp = students[j]; |
students[j] = students[j+ 1 ]; |
students[j+ 1 ] = temp; |
} |
} |
} |
} |
} |
------------------------------------------------------------------- |
package textstudent; |
import java.io.*; |
public class Keyboard { |
static BufferedReader inputStream = new BufferedReader( new InputStreamReader(System.in)); |
public static int getInteger() { |
try { |
return (Integer.valueOf(inputStream.readLine().trim()).intValue()); |
} |
catch (Exception e) { |
e.printStackTrace(); |
return 0 ; |
} |
} |
public static String getString() { |
try { |
return (inputStream.readLine()); |
} |
catch (IOException e) { |
return "0" ; |
} |
} |
} |
----------------------------------------------------------------------------------- |
package textstudent; |
import java.io.FileInputStream; |
import java.io.ObjectInputStream; |
public class SortTester { |
public static void main(String[] args) { |
Student students[] = new Student[ 5 ]; |
//从文件stu.ser中读出学生的信息 |
try { |
FileInputStream fi = new FileInputStream( "stu.ser" ); |
ObjectInputStream si = new ObjectInputStream(fi); |
for ( int i = 0 ; i < 5 ; i++){ |
students[i] = (Student) si.readObject(); |
} |
si.close(); |
} catch (Exception e){ |
System.out.println(e); |
} |
StudentClass aClass = new StudentClass( "软件0201" , 5 ); |
aClass.setStudents(students); |
System.out.println(aClass); |
//选择排序 |
aClass.selectionSort(); |
System.out.println( "选择排序后的结果:\n" ); |
System.out.println(aClass); |
} |
} |