要求:
结果:
测试类:
package Z;
public class Test {public static void main(String[] args) {ClassMnger p = new ClassMnger();p.Students();p.Studentall();p.studentavg();}
}
实体数据类
public class ClassAll {//存入班级全部学生类private String className;private char sex;private double score;public ClassAll() {}public ClassAll(String className, char sex, double score) {this.className = className;this.sex = sex;this.score = score;}public String getClassName() {return className;}public void setClassName(String className) {this.className = className;}public char getSex() {return sex;}public void setSex(char sex) {this.sex = sex;}public double getScore() {return score;}public void setScore(double score) {this.score = score;}
}
操作类
import java.util.ArrayList;public class ClassMnger {//班级操作类private ArrayList<ClassAll> student = new ArrayList<ClassAll>();//创建ClassAll学生信息类的集合对象//创建学生信息public void Students (){//给集合对象添加进去信息,利用有参构造函数加入student.add(new ClassAll("迪丽热巴",'女',85));student.add(new ClassAll("古力娜扎",'女',95));student.add(new ClassAll(" 单依纯",'女',100));student.add(new ClassAll("付 航",'男',99));}//以接口名命名,方便调用实现方法private operatr1 op = new f1();//创建实现接口类的对象(f1改为f2的话就执行方案2,便利程序员的切换)//打印全班全部学生的信息public void Studentall(){System.out.println("================================");//调用实现接口方法的方法op.printStudentall(student);//传入学生类对象集合}//打印全班全部学生的平均分public void studentavg (){System.out.println("==========================");//调用实现接口方法的方法op.printAverageScore(student);//传入学生类对象集合}
}
接口
import java.util.ArrayList;public interface operatr1 {//方案一的接口void printAverageScore(ArrayList<ClassAll> studentscore);//设置平均值方法,类型是ClassAll集合对象类void printStudentall(ArrayList<ClassAll> studentall);//设置学生全部信息方法,类型是ClassAll集合对象类
}
接口实现类方案1
import java.util.ArrayList;//方案1 ,实现接口类
public class f1 implements operatr1 {@Overridepublic void printAverageScore(ArrayList<ClassAll> studentscore) {double score = 0.0;//给分数赋初值for(int i=0; i<studentscore.size(); i++) {double Studentscore = studentscore.get(i).getScore();//找集合对象中的第几个值score += Studentscore;}//求平均值System.out.println("本班学生成绩的平均值是"+score/studentscore.size());}@Overridepublic void printStudentall(ArrayList<ClassAll> studentall) {for(int i=0; i<studentall.size(); i++) {System.out.println("姓名:"+studentall.get(i).getClassName()+" 性别:"+studentall.get(i).getSex()+"\t分数"+studentall.get(i).getScore());}}
}
接口实现类方案2
import java.util.ArrayList;public class f2 implements operatr1 {@Overridepublic void printAverageScore(ArrayList<ClassAll> studentscore) {double score = 0.0;//给分数赋初值double min =studentscore.get(1).getScore() ;//初始最小值double max =studentscore.get(1).getScore() ;//初始最大值for(int i=0; i<studentscore.size(); i++) {double Studentscore = studentscore.get(i).getScore();//找集合对象中的第几个值if(Studentscore<min) {min = Studentscore;};if(Studentscore>max) {max = Studentscore;};score += Studentscore;}//求去掉最大值去掉最小值的平均值System.out.println("本班学生成绩的平均值是"+(score-max-min)/studentscore.size());}@Overridepublic void printStudentall(ArrayList<ClassAll> studentall) {int count1 = 0;//初始化女生人数int count2 = 0;//初始化男生人数for(int i=0; i<studentall.size(); i++) {System.out.println("姓名:"+studentall.get(i).getClassName()+" 性别:"+studentall.get(i).getSex()+"\t分数"+studentall.get(i).getScore());if(studentall.get(i).getSex()=='男') {count2++;}else {count1++;}}System.out.println("男生人数有:"+count2+"女生人数有:"+count1);}
}