java面向对象编程求帮忙

编写类ScoreArray,成员变量int类型数组scores及数组有效记录数scoreCount。
编写构造函数,形式参数为int类型数组。
类的成员方法有:求最值、求均值、排序。静态方法:类功能简介
编写子类StudentScoreArray,增加求分数分布的统计功能。
编写入口函数main,生成实例试验上述功能。

(顺便问下这个需要写几个文件(类)?)

一共三个类:ScoreArray.java、StudentScoreArray.java和Test1.java,具体为:


public class ScoreArray {
private int[] scores;
private int scoreCount;

public int[] getScores() {
return scores;
}

public int getScoreCount() {
return scoreCount;
}
//构造函数
public ScoreArray(int[] scores) {
this.scores = scores;
for (int score : scores) {
if (score >= 0 && score <= 100) {
this.scoreCount++;
}
}
}
//求最大值
public int getMax() {
int[] scores = this.scores;
int temp;
for (int i = 0; i < scores.length; i++) {
for (int j = 0; j < scores.length - 1 - i; j++) {
if (scores[j] > scores[j + 1]) {
temp = scores[j];
scores[j] = scores[j + 1];
scores[j + 1] = temp;
}
}
}
return scores[scores.length - 1];
}
//求最小值
public int getMin() {
int[] scores = this.scores;
int temp;
for (int i = 0; i < scores.length; i++) {
for (int j = 0; j < scores.length - 1 - i; j++) {
if (scores[j] > scores[j + 1]) {
temp = scores[j];
scores[j] = scores[j + 1];
scores[j + 1] = temp;
}
}
}
return scores[0];
}
//求均值
public double getAvg() {
int sum = 0;
for (int score : this.scores) {
sum += score;
}
return new BigDecimal(sum).divide(
new BigDecimal(this.scores.length),
2, BigDecimal.ROUND_HALF_UP).doubleValue();
}
//排序
public void sort() {
int temp;
for (int i = 0; i < this.scores.length; i++) {
for (int j = 0; j < this.scores.length - 1 - i; j++) {
if (this.scores[j] > this.scores[j + 1]) {
temp = this.scores[j];
this.scores[j] = this.scores[j + 1];
this.scores[j + 1] = temp;
}
}
}
}
//静态说明类
public static void explain() {
System.out.println("本类[ScoreArray]实现了数组的:求最值[getMax()]、求均值[getAvg()]和排序[sort()]方法");
}
}


public class StudentScoreArray extends ScoreArray {
public StudentScoreArray(int[] scores) {
super(scores);
}
//统计
public void statistic() {
super.sort();
Map<Integer, Integer> map = new LinkedHashMap<>();
for (int i : super.getScores()) {
if (map.containsKey(i)) {
map.put(i, map.get(i) + 1);
} else {
map.put(i, 1);
}
}
map.forEach((k, v) -> System.out.println("分数为[" + k + "]的人数为:[" + v + "]"));
}
//静态说明类
public static void explain() {
System.out.println("本类[StudentScoreArray]实现了数组的:求最值[getMax()]、求均值[getAvg()]、排序[sort()]和分布统计[statistic()]方法");
}
}


public class Test1 {
public static void main(String[] args) {
int[] scores = {59, 60, 82, 58, 71, 99, 0, 59, 65};

ScoreArray scoreArray = new ScoreArray(scores);
ScoreArray.explain();
System.out.print("数组内容:[");
for (int i : scoreArray.getScores()) {
System.out.print(i + " ");
}
System.out.println("]");
System.out.println("有效值个数:" + scoreArray.getScoreCount());
System.out.println("最大值:" + scoreArray.getMax());
System.out.println("最小值:" + scoreArray.getMin());
System.out.println("平均值:" + scoreArray.getAvg());
scoreArray.sort();
System.out.print("排序后数组内容:[");
for (int i : scoreArray.getScores()) {
System.out.print(i + " ");
}
System.out.println("]");

System.out.println("");
System.out.println("========华丽的分割线========");
System.out.println("");

StudentScoreArray studentScoreArray = new StudentScoreArray(scores);
StudentScoreArray.explain();
System.out.print("数组内容:[");
for (int i : studentScoreArray.getScores()) {
System.out.print(i + ",");
}
System.out.println("]");
System.out.println("有效值个数:" + studentScoreArray.getScoreCount());
System.out.println("最大值:" + studentScoreArray.getMax());
System.out.println("最小值:" + studentScoreArray.getMin());
System.out.println("平均值:" + studentScoreArray.getAvg());
studentScoreArray.sort();
System.out.print("排序后数组内容:[");
for (int i : studentScoreArray.getScores()) {
System.out.print(i + " ");
}
System.out.println("]");
System.out.println("分数分布统计:");
studentScoreArray.statistic();
}
}


其中对StudentScoreArray类我要特别说明一下:

统计分布情况时,使用了Map,map是一种key-value的数据结构,其有个特点被我所利用:一个map中只能同时存在一个key,所以我以分数为key,以数量为value,遍历分数数组时,如果是第一次遇到这个key(分数),则将其value(数量)置为1;如果已经不是第一次遇见了,则将其value(数量)置为value + 1(数量 + 1)。另外需要遍历这个map实现统计结果的打印,我这里使用了java8以后才支持的Lambda表达式,所以你要运行这个程序必须要使用jdk1.8以上的版本。如果你觉得这样不妥,可以网上再搜一下map的遍历方式。


运行结果:



啊 代码又一坨的的挤在一起了,百度知道这个真是无语,我把几个关键的地方截个图给你康康:

ScoreArray.java

成员变量和构造函数

忘了说了,构造的同时还统计了有效分数(0~100)的数量


求最大值

冒泡排序后取最大值


求最小值

冒泡排序后取最小值


求均值

利用java 的BigDecimal类解决除法运算精度的问题,四舍五入并保留了两位小数


排序

就是冒泡排序,从小到大


静态的文本说明


StudentScoreArray.java:

继承


分数分布统计

注意我说的map那里


静态说明文本


Test1.java:

测试用例都使用的数组:int[] scores = {59, 60, 82, 58, 71, 99, 0, 59, 65};

温馨提示:答案为网友推荐,仅供参考
第1个回答  2019-10-20

一共三个类:ScoreArray.java、StudentScoreArray.java和Test1.java,具体为:

public class ScoreArray {private int[] scores;private int scoreCount;public int[] getScores() {return scores;}public int getScoreCount() {return scoreCount;}//构造函数public ScoreArray(int[] scores) {this.scores = scores;for (int score : scores) {if (score >= 0 && score <= 100) {this.scoreCount++;}}}//求最大值public int getMax() {int[] scores = this.scores;int temp;for (int i = 0; i < scores.length; i++) {for (int j = 0; j < scores.length - 1 - i; j++) {if (scores[j] > scores[j + 1]) {temp = scores[j];scores[j] = scores[j + 1];scores[j + 1] = temp;}}}return scores[scores.length - 1];}//求最小值public int getMin() {int[] scores = this.scores;int temp;for (int i = 0; i < scores.length; i++) {for (int j = 0; j < scores.length - 1 - i; j++) {if (scores[j] > scores[j + 1]) {temp = scores[j];scores[j] = scores[j + 1];scores[j + 1] = temp;}}}return scores[0];}//求均值public double getAvg() {int sum = 0;for (int score : this.scores) {sum += score;}return new BigDecimal(sum).divide(new BigDecimal(this.scores.length),2, BigDecimal.ROUND_HALF_UP).doubleValue();}//排序public void sort() {int temp;for (int i = 0; i < this.scores.length; i++) {for (int j = 0; j < this.scores.length - 1 - i; j++) {if (this.scores[j] > this.scores[j + 1]) {temp = this.scores[j];this.scores[j] = this.scores[j + 1];this.scores[j + 1] = temp;}}}}//静态说明类public static void explain() {System.out.println("本类[ScoreArray]实现了数组的:求最值[getMax()]、求均值[getAvg()]和排序[sort()]方法");}}

public class StudentScoreArray extends ScoreArray {public StudentScoreArray(int[] scores) {super(scores);}//统计public void statistic() {super.sort();Map<Integer, Integer> map = new LinkedHashMap<>();for (int i : super.getScores()) {if (map.containsKey(i)) {map.put(i, map.get(i) + 1);} else {map.put(i, 1);}}map.forEach((k, v) -> System.out.println("分数为[" + k + "]的人数为:[" + v + "]"));}//静态说明类public static void explain() {System.out.println("本类[StudentScoreArray]实现了数组的:求最值[getMax()]、求均值[getAvg()]、排序[sort()]和分布统计[statistic()]方法");}}

public class Test1 {public static void main(String[] args) {int[] scores = {59, 60, 82, 58, 71, 99, 0, 59, 65};ScoreArray scoreArray = new ScoreArray(scores);ScoreArray.explain();System.out.print("数组内容:[");for (int i : scoreArray.getScores()) {System.out.print(i + " ");}System.out.println("]");System.out.println("有效值个数:" + scoreArray.getScoreCount());System.out.println("最大值:" + scoreArray.getMax());System.out.println("最小值:" + scoreArray.getMin());System.out.println("平均值:" + scoreArray.getAvg());scoreArray.sort();System.out.print("排序后数组内容:[");for (int i : scoreArray.getScores()) {System.out.print(i + " ");}System.out.println("]");System.out.println("");System.out.println("========华丽的分割线========");System.out.println("");StudentScoreArray studentScoreArray = new StudentScoreArray(scores);StudentScoreArray.explain();System.out.print("数组内容:[");for (int i : studentScoreArray.getScores()) {System.out.print(i + ",");}System.out.println("]");System.out.println("有效值个数:" + studentScoreArray.getScoreCount());System.out.println("最大值:" + studentScoreArray.getMax());System.out.println("最小值:" + studentScoreArray.getMin());System.out.println("平均值:" + studentScoreArray.getAvg());studentScoreArray.sort();System.out.print("排序后数组内容:[");for (int i : studentScoreArray.getScores()) {System.out.print(i + " ");}System.out.println("]");System.out.println("分数分布统计:");studentScoreArray.statistic();}}

其中对StudentScoreArray类我要特别说明一下:

统计分布情况时,使用了Map,map是一种key-value的数据结构,其有个特点被我所利用:一个map中只能同时存在一个key,所以我以分数为key,以数量为value,遍历分数数组时,如果是第一次遇到这个key(分数),则将其value(数量)置为1;如果已经不是第一次遇见了,则将其value(数量)置为value + 1(数量 + 1)。另外需要遍历这个map实现统计结果的打印,我这里使用了java8以后才支持的Lambda表达式,所以你要运行这个程序必须要使用jdk1.8以上的版本。如果你觉得这样不妥,可以网上再搜一下map的遍历方式。

运行结果:


啊 代码又一坨的的挤在一起了,百度知道这个真是无语,我把几个关键的地方截个图给你康康:

ScoreArray.java

忘了说了,构造的同时还统计了有效分数(0~100)的数量

冒泡排序后取最大值

冒泡排序后取最小值

利用java 的BigDecimal类解决除法运算精度的问题,四舍五入并保留了两位小数

就是冒泡排序,从小到大

StudentScoreArray.java:

注意我说的map那里

Test1.java:

测试用例都使用的数组:int[] scores = {59, 60, 82, 58, 71, 99, 0, 59, 65};

相似回答