用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

对象群体的组织

2021-12-10 作者: GuanTou举报

[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;
	}
}







[代码运行效果截图]


对象群体的组织


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...