实验3 (学生成绩管理系统)持有对象
实验目的
掌握Java中数组的基本概念。掌握Java的对象容器类库,能够运用对象容器类持有对象,从而表达实体的复杂属性。
实验任务
1、开发一个学生成绩管理系统,首先定义一个学生信息类,用来表示单个学生的姓名和成绩,然后利用数组管理一个班级的所有学生的信息,并实现相应的输入、输出、查找、排序等功能。
import java.io.*;
import java.util.Scanner;
public class studentmanage {
public static class StudentInfo{
private String stdName;//学生姓名
private double score; //学生成绩
public StudentInfo(){}
public StudentInfo(String name, double s){
this.stdName = name;
this.score = s;
}
public String getStdName() {
return stdName;
}
public void setStdName(String stdName){
this.stdName = stdName;
}
public double getScore() {
return score;
}
public void setScore(double score){
this.score = score;
}
public void inputStdInfo(){
System.out.println("请输入学生信息:");
Scanner scanner = new Scanner(System.in);
System.out.print("姓名:");
stdName = scanner.nextLine();
System.out.print("成绩:");
score = scanner.nextDouble();
}
public void outputStdInfo(){
System.out.print(this.stdName + ":" + this.score + "\n");
}
}
// 一个班级的学生成绩类
public static class ScoreManager{
// 全班同学的成绩
private StudentInfo[] stdScores;
// 构造方法,参数s中记录了全班同学的成绩
public ScoreManager(int stdnum){
// 将参数s中的成绩清单复制到属性scores
stdScores = new StudentInfo[stdnum];
for (int index = 0; index < stdScores.length; index++){
stdScores[index] = new StudentInfo();
}
}
//输入全班学生的信息
public void inputClassInfo(){
int index = 0;
while (index < stdScores.length) {
stdScores[index].inputStdInfo();
if (stdScores[index].getScore()<0|| stdScores[index].getScore()>100) {
System.out.println("输入的成绩不在合理范围之内!");
continue;
}
index = index + 1;
}
}
// 返回全班的学生信息清单
public StudentInfo[] getClassInfo(){
return stdScores;
}
// 取第sno位学生的信息,sno超出范围则返回负数
public StudentInfo getScore(int sno){
if (sno < 1 || sno > stdScores.length)
return null;
return stdScores[sno-1];
}
// 查找有成绩为score的学生则返回true,否则返回false
public StudentInfo findByScore(double score){
for (int index = 0; index < stdScores.length; index++) {
if (stdScores[index].getScore() == score)
return stdScores[index];
}
return null;
}
// 统计low至high之间成绩段的学生人数,分数low和high包括在内
public int count(double low, double high){
//保证low<high
if(low > high){
double temp = low;
low = high;
high = temp;
}
int result = 0;
for (int index = 0; index < stdScores.length; index++) {
if (stdScores[index].getScore() >= low
&& stdScores[index].getScore() <= high)
result++;
}
return result;
}
// 返回全班的学生人数
public int countAll(){
return stdScores.length;
}
// 将学生成绩从高分到低分排序(采用简单的冒泡排序算法)
public void sort(){
// 从头到尾扫描整个数组,逐段将数据冒泡排序
for (int index = 0; index < stdScores.length - 1; index++) {
// 将index到scores.length - 1中的最大数冒泡到最上面(即最左)
for (int ptr = stdScores.length - 1; ptr > index; ptr--) {
// 如果位置ptr的下一元素比ptr位置的元素更大则交换两个元素
if (stdScores[ptr].getScore() > stdScores[ptr - 1].getScore()) {
StudentInfo temp = stdScores[ptr];
stdScores[ptr] = stdScores[ptr-1];
stdScores[ptr-1] = temp;
}
}
}
}
// 输入全班学生的人数,不合法则返回0
public static int inputNumber(){
int num;
System.out.print("请输入全班学生的人数:");
try {
BufferedReader in = new BufferedReader( new InputStreamReader(System.in));
String inputLine = in.readLine();
num = Integer.valueOf(inputLine).intValue();
} catch (Exception exc) {
System.out.println("学生人数输入错误!");
return 0;
}
if (num <= 0 || num > 1000) {
System.out.println("输入的学生人数不合理!");
return 0;
}
return num;
}
// 输出全班学生的成绩清单
public void output(){
System.out.println("学生成绩信息为:");
for (int index = 0; index < stdScores.length; index++) {
stdScores[index].outputStdInfo();
}
}
// 主程序
public static void main(String[] args){
// 提示用户输入全班学生人数
int stdNum = ScoreManager.inputNumber();
// 创建学生成绩清单数组并输入全班学生的成绩
ScoreManager sManager = new ScoreManager(stdNum);
sManager.inputClassInfo();
// 显示第5位同学的成绩
System.out.println("第5位同学的成绩为:" + sManager.getScore(5));
// 查找是否有75分的成绩
System.out.println("75分的成绩:" + sManager.findByScore(75));
// 统计不合格的人数与优秀的人数
System.out.println("不合格" + sManager.count(0, 59) + "人");
System.out.println("优秀" + sManager.count(90, 100) + "人");
// 将成绩排序后输出
System.out.println("排序后的学生成绩清单为:");
sManager.sort();
sManager.output();
}
}
}