本文主要是介绍J015_班级学生信息管理模块(接口应用案例),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Student
package com.itheima.d4_interfacecase1;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;}
}
StudentOperator
package com.itheima.d4_interfacecase1;import java.util.ArrayList;public interface StudentOperator {void printAllInfo(ArrayList<Student> students);void printAverageScore(ArrayList<Student> students);
}
StudentOperatorImpl1
package com.itheima.d4_interfacecase1;import java.util.ArrayList;public class StudentOperatorImpl1 implements StudentOperator{//输出全班学生信息@Overridepublic 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("-------------------------------------------");}//打印班级平均分@Overridepublic void printAverageScore(ArrayList<Student> students) {double averageSource = 0.0;for (int i = 0; i < students.size(); i++) {Student s = students.get(i);averageSource += s.getScore();}System.out.println("平均分:" + averageSource/students.size());}
}
StudentOperatorImpl2
package com.itheima.d4_interfacecase1;import java.util.ArrayList;public class StudentOperatorImpl2 implements StudentOperator{@Overridepublic void printAllInfo(ArrayList<Student> students) {//记录男生人数int count1 = 0;//记录女生人数int count2 = 0;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());if (s.getSex() == '男') {count1 ++;}else {count2 ++;}}System.out.println("男生人数为:" + count1 + ",女生人数为:" + count2);System.out.println("总人数为:" + students.size());System.out.println("-------------------------------------------");}@Overridepublic void printAverageScore(ArrayList<Student> students) {double averageSource = 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);averageSource += s.getScore();if (s.getScore() > max) {max = s.getScore();}if (s.getScore() < min) {min = s.getScore();}}System.out.println("最高分为:" + max + "最低分为:" + min);System.out.println("平均分:" + (averageSource - max - min) / (students.size() - 2));}
}
ClassManger
package com.itheima.d4_interfacecase1;import java.util.ArrayList;public class ClassManger {private ArrayList<Student> students = new ArrayList<>();private StudentOperator operator = new StudentOperatorImpl2();public ClassManger() {students.add(new Student("迪丽热巴",'女',99));students.add(new Student("古力娜扎",'女',99.9));students.add(new Student("黑八可",'男',80));students.add(new Student("小白白",'男',70));}//打印学生全部信息public void printInfo() {operator.printAllInfo(students);}//打印学生平均分public void printAveScore(){operator.printAverageScore(students);}
}
Test
package com.itheima.d4_interfacecase1;public class Test {public static void main(String[] args) {//完成班级学生信息管理模块案例ClassManger clas = new ClassManger();clas.printInfo();clas.printAveScore();}
}
这篇关于J015_班级学生信息管理模块(接口应用案例)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!