Java面向对象基本代码 java面向对象基本代码是什么( 三 )


sum = sum + i;
}
}
System.out.println(sum);
}
java面向对象编程求帮忙一共三个类Java面向对象基本代码:ScoreArray.java、StudentScoreArray.java和Test1.javaJava面向对象基本代码,具体为:
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 = 0score = 100) {
this.scoreCount++;
}
}
}
//求最大值
public int getMax() {
int[] scores = this.scores;
int temp;
for (int i = 0; iscores.length; i++) {
for (int j = 0; jscores.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; iscores.length; i++) {
for (int j = 0; jscores.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; ithis.scores.length; i++) {
for (int j = 0; jthis.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();
MapInteger, 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("========华丽的分割线========");

推荐阅读