[java]代码库
package textstudent;
import java.io.*;
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=new String(s.name);
this.eng= s.eng;
this.math=s.math;
this.comp=comp;
sum();
}
public void setId(String id) {//set方法
this.id=id;
}
public void setName(String name) {
this.name=name;
}
public void setEng(int eng) {
this.eng=eng;
sum();
}
public void setMath(int math) {
this.math=math;
sum();
}
public void setComp(int comp) {
this.comp=comp;
sum();
}
public String getId() {//get方法
return id;
}
public String getName() {
return name;
}
public double getEng() {
return eng;
}
public double getMath() {
return math;
}
public double getComp() {
return comp;
}
public double getSum() {
return sum;
}
//计算总成绩
void sum() {
this.sum=eng+comp+math;
}
public String toString() {//toString方法
return (getId()+"\t"+getName()+"\t"+getEng()+"\t"+getMath()+"\t"+getComp()+"\t"+getSum());
}
public boolean equals(Object x) {//equal方法
if(this.getClass()!=x.getClass()) {
return false;
}
Student b=(Student)x;
return(this.getId().equals(b.getId()));
}
public int compare(Student A) {//compare方法
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=2;
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 int getCapacity() {
return capacity;
}
public Student[] getStudents() {
return students;
}
public int getsize() {
return size;
}
public void setName() {
this.name=name;
}
public void setCapavity() {
this.capacity=capacity;
}
public void setSize() {
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;
}
}
------------------------------------------------------------------
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.*;
public class Tester {
public static void main(String[] args) {
Student students[];
StudentClass aClass=new StudentClass("软件 0201",5);
students=new Student[aClass.getsize()];
for(int i=0;i<aClass.getsize();i++){
students[i]=new Student(getAStudent(i+1));
}
aClass.setStudents(students);
String sss=aClass.toString();
System.out.println(aClass);
try {
FileOutputStream fo=new FileOutputStream("stu.ser");
ObjectOutputStream so=new ObjectOutputStream(fo);
for(Student s:students) {
so.writeObject(s);
}
so.close();
}
catch(Exception e) {
System.out.println(e);
}
}
public static Student getAStudent(int i) {
Student studenti;
System.out.println("输入第"+i+"个学生的信息:");
System.out.println("学号");
String id=Keyboard.getString();
System.out.println("姓名");
String name=Keyboard.getString();
System.out.println("英语成绩");
int eng=Keyboard.getInteger();
System.out.println("数学成绩");
int math=Keyboard.getInteger();
System.out.println("计算机成绩");
int comp=Keyboard.getInteger();
studenti=new Student(id,name,eng,math,comp);
return studenti;
}
}
[代码运行效果截图]