package com.Demo4; public class Test { public static void main(String[] args) { //目标:完成学生信息管理的实例 ClassManage classManage=new ClassManage(); classManage.printInfo(); classManage.printScore(); } }
package com.Demo4; public class Student { private String name; private char sex; private double score; public Student() { } public Student(String name, char sex, double score) { this.name = name; this.sex = sex; this.score = score; } public String getName() { return name; } public void setName(String name) { this.name = name; } 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; } }
package com.Demo4; import java.util.ArrayList; public interface StuentOperator { void printAllInfo(ArrayList<Student> students); void printAverageScore(ArrayList<Student> students); }
package com.Demo4; import java.util.ArrayList; public class ClassManage { private ArrayList<Student> students=new ArrayList<>(); private StuentOperator stuentOperator=new StuentOperatorlmp1(); //切换第二套方案 改成 StydentOperatorImpl2 public ClassManage(){ students.add(new Student("迪丽热巴",'女',99)); students.add(new Student("古力娜扎",'女',100)); students.add(new Student("马儿扎哈",'男',89)); students.add(new Student("卡尔扎巴",'男',80)); students.add(new Student("德莱文",'男',60)); } //打印全部学生信息 public void printInfo(){ stuentOperator.printAllInfo(students); } //打印全班全部学生的平均分 public void printScore(){ stuentOperator.printAverageScore(students); } }
package com.Demo4; import java.util.ArrayList; public class StuentOperatorlmp1 implements StuentOperator { @Override public void printAllInfo(ArrayList<Student> students) { System.out.println("-----------------全班全部学生信息如下--------------"); for (int i = 0; i < students.size(); i++) { Student s=students.get(i); System.out.println("姓名:"+ s.getName()+",性别:"+s.getSex()+",成绩:"+s.getScore()); } System.out.println("------------------------------------------------"); } @Override public void printAverageScore(ArrayList<Student> students) { double allScore=0.0; for (int i = 0; i < students.size(); i++) { Student s=students.get(i); allScore+=s.getScore(); } System.out.println("平均分:"+(allScore)/students.size()); } }
package com.Demo4; import java.util.ArrayList; public class StydentOperatorImpl2 implements StuentOperator{ @Override public void printAllInfo(ArrayList<Student> students) { System.out.println("-----------------全班全部学生信息如下--------------"); int count1=0; int count2=0; for (int i = 0; i < students.size(); i++) { Student s=students.get(i); System.out.println("姓名:"+ s.getName()+",性别:"+s.getSex()+",成绩:"+s.getScore()); if (s.getSex()=='男'){ count1++; }else { count2++; } } System.out.println("男生人数为:"+count1+",女生人数为:"+count2); System.out.println("全班人数为:"+students.size()); System.out.println("------------------------------------------------"); } @Override public void printAverageScore(ArrayList<Student> students) { double allScore=0.0; double max=students.get(0).getScore(); double min=students.get(0).getScore(); for (int i = 0; i < students.size(); i++) { Student s=students.get(i); if (s.getScore()>max ) max=s.getScore(); if (s.getScore()<min ) min=s.getScore(); allScore+=s.getScore(); } System.out.println("学生的最高分是:"+max); System.out.println("学生的最低分是:"+min); System.out.println("平均分:"+(allScore-max-min) / (students.size()-2)); } }
标签:getScore,students,void,接口,案例,ArrayList,Student,public,综合 From: https://www.cnblogs.com/Karl-hut/p/17440700.html