初分析:
父类(课程信息类):三个变量(编号、名称、以及学生成绩)、构造方法默认以及全选的、方法get个set类和display。
子类(对父类的统计及分析):构造方法(关键字super()),方法(平均值、最大最小值以及分布律)
父类(课程信息类):
package HOME8; public class Course_infor { private String number; private String name; public int[] grades; public Course_infor() { } public Course_infor(String number, String name, int[] grades ) { this.number = number; this.name = name; this.grades = grades; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int[] getGrades() { return grades; } public void setGrades(int[] grades) { this.grades = grades; } public void display() { System.out.println("课程编号:"+number+"\n"+ "课程名称:"+name+"\n"); } }
子类(对父类的统计及分析)
package HOME8; public class Statistics extends Course_infor { int sum = 0,Max,Min; int[] DL = new int[11]; public Statistics() { } public Statistics(String number, String name, int[] grades ) { super(number,name,grades); Min = grades[0]; Max = grades[0]; } public void average() { for(int i = 0;i<grades.length;i++) { sum += grades[i]; } System.out.println("平均值:"+(double)sum/grades.length+"\n"); } public void Maxgrades() { for(int i = 0;i<grades.length;i++) { if(Max<grades[i]) { Max = grades[i]; } } System.out.println("最大值:"+Max+"\n"); } public void Mingrades() { for(int i = 0;i<grades.length;i++) { if(Min>grades[i]) { Min = grades[i]; } } System.out.println("最小值:"+Min+"\n"); } public void DL() { for(int i = 0;i<grades.length;i++) { DL[grades[i]/10]++; } for(int i=0;i<11;i++) { System.out.printf("%d~%d:",10*i,10*(i+1)); for(int j = 0;j<DL[i];j++) { System.out.printf("*"); } System.out.println(); } } }
测试
package HOME8; public class test { public static void main(String[] args) { Course_infor a = new Course_infor("a","软件工程实践",new int[]{12,14,1,2,21,22,33,44,55,66,77,88,99}); a.display(); Statistics b = new Statistics("a","软件工程实践",new int[]{12,14,1,2,21,22,33,44,55,66,77,88,99}); b.average(); b.Maxgrades(); b.Mingrades(); b.DL(); } }