Student类:
public class Student {
public String name;
public double grade;
public Student(String name,double grade) {
this.name = name;
this.grade = grade;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getGrade() {
return grade;
}
public void setGrade(double grade) {
this.grade = grade;
}
@Override
public String toString() {
return "学生信息-- 名字:" + name + ", 成绩:" + grade + "。\n";
}
}
StudentManager类:
import java.util.ArrayList;
public class StudentManager {
ArrayList<Student> list = new ArrayList<Student>();
//添加学生
public void addStudent(Student stu) {
list.add(stu);
}
public Student queryStudent(String name) {
int len = list.size();
int i = 0;
while(i<len) {
Student stu = list.get(i);
if(stu.getName().equals(name)) {
return stu; //结果返回指定名字的Student学生类实例对象
}
i++;
}
return null; //表示没有查询到指定名字的学生
}
public boolean removeStudent(String name) {
int len = list.size();
int i = 0;
while(i<len) {
Student stu = list.get(i);
if(stu.getName().equals(name)) {
list.remove(i);
return true; //删除成功
}
i++;
}
return false; //表示删除失败
}
public void statisticsStudentsGrade() {
int len = list.size();
double avg=0;
double high=0;
double low=list.get(0).getGrade();
double total=0;
double grade=0;
int i = 0;
while(i<len) {
Student stu = list.get(i);
grade = stu.getGrade();
total += grade;
if(grade>=high) {
high = grade;
}else if(grade<=low) {
low = grade;
}
i++;
}
System.out.println("学生共"+len+"人,最高分:"+high
+",最低分:"+low+",平均分:"+total/len);
}
}
Test类:
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
StudentManager manager = new StudentManager();
Student s1 = new Student("张三",100);
Student s2 = new Student("李四",85);
Student s3 = new Student("王五",96);
manager.addStudent(s1);
manager.addStudent(s2);
manager.addStudent(s3);
manager.statisticsStudentsGrade();
}
}