一案例要求:
二代码:(换方案只需要将操作类第二行的new新对象修改就能更改项目)
Ⅰ:(主函数)
package d1;
public class test {
public static void main(String[] args) {
operator a=new operator();
a.show();
a.average();
}
}
Ⅱ:(实体类)
package d1;
public class student {
private String name;
private String sex;
private double score;
public student() {
}
public student(String name, String 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 String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
}
Ⅲ:(操作类)
package d1;
import java.util.ArrayList;
public class operator {
private ArrayList<student> arrayList=new ArrayList<>();
private choice a=new oper2();
public operator() {
arrayList.add(new student("孙悟空","男",12));
arrayList.add(new student("紫霞","女",66));
arrayList.add(new student("牛魔王","男",6));
arrayList.add(new student("山神","男",99));
}
public void show(){
a.show(arrayList);
}
public void average(){
a.average(arrayList);
}
}
Ⅳ:(接口)
package d1;
import java.util.ArrayList;
public interface choice {
public void show(ArrayList<student> arrayList);
public void average(ArrayList<student> arrayList);
}
Ⅴ:(实现类1)
package d1;
import java.util.ArrayList;
public class oper1 implements choice{
@Override
public void show(ArrayList<student> arrayList) {
for (int i = 0; i < arrayList.size(); i++) {
System.out.println("姓名 "+arrayList.get(i).getName()+" 性别 "+arrayList.get(i).getSex()+" 成绩 "+arrayList.get(i).getScore());
}
}
@Override
public void average(ArrayList<student> arrayList) {
double sum=0;
for (int i = 0; i < arrayList.size(); i++) {
sum+=arrayList.get(i).getScore();
}
System.out.println("平均成绩为 "+sum/arrayList.size());
}
}
Ⅵ:(实现类2)
package d1;
import java.util.ArrayList;
public class oper2 implements choice {
@Override
public void show(ArrayList<student> arrayList) {
int a1=0,a2=0;
for (int i = 0; i < arrayList.size(); i++) {
System.out.println("姓名 "+arrayList.get(i).getName()+" 性别 "+arrayList.get(i).getSex()+" 成绩 "+arrayList.get(i).getScore());
if (arrayList.get(i).getSex().equals("男")){
a1++;
}else a2++;
}
System.out.println("男生人数 "+a1+" 女生人数 "+a2);
}
@Override
public void average(ArrayList<student> arrayList) {
double sum=0,max=arrayList.get(0).getScore(),min=arrayList.get(0).getScore();
for (int i = 0; i < arrayList.size(); i++) {
sum+=arrayList.get(i).getScore();
if (arrayList.get(i).getScore()>=max){
max=arrayList.get(i).getScore();
}
if(arrayList.get(i).getScore()<=min){
min=arrayList.get(i).getScore();
}
}
System.out.println("平均成绩为 "+(sum-max-min)/(arrayList.size()-2));
}
}