简答题) 一、程序填空题:搭建流水线 1.相关知识点: 如果对象a含有对象b的引用,对象b含有对象c的引用,那么就可以使用a、b、c搭建流水线。流水线的作用是,用只需将要处理的数据交给流水线,流水线会依次让流水线上的对象来处理数据,即流水线上首先由对象a处理数据,a处理数据后,自动将处理的数据交给b,b处理数据后,自动将处理的数据交给c。 2.实验目的 让学生掌握怎样搭建符合特殊用途的链式结构数据。 3.实验要求 程序有时候需要将任务按流水式进行,例如评判体操选手的任务按流水式为依次的三个步骤:录入裁判给选手的分数,去掉一个最高分和一个最低分,计算出平均成绩。 编写程序,搭建流水线,只需将评判体操选手的任务交给流水线即可。 源程序见附件,测试类为GymnasticGame,请补充各个类中缺失的代码。
时间: 2023-07-16 22:14:54 浏览: 185
1. Score类
```java
public class Score {
private double score;
public Score(double score) {
this.score = score;
}
public double getScore() {
return score;
}
}
```
2. Handler类
```java
public abstract class Handler {
protected Handler successor;
public void setSuccessor(Handler successor) {
this.successor = successor;
}
public abstract void handleRequest(Score score);
}
```
3. RemoveHighestScore类
```java
public class RemoveHighestScore extends Handler {
@Override
public void handleRequest(Score score) {
double[] scores = {score.getScore(), successor.getNextScore().getScore(), successor.getNextScore().getNextScore().getScore()};
Arrays.sort(scores);
score = new Score(scores[1]);
successor.handleRequest(score);
}
public Score getNextScore() {
return successor.getNextScore();
}
}
```
4. RemoveLowestScore类
```java
public class RemoveLowestScore extends Handler {
@Override
public void handleRequest(Score score) {
double[] scores = {score.getScore(), successor.getNextScore().getScore(), successor.getNextScore().getNextScore().getScore()};
Arrays.sort(scores);
score = new Score(scores[1]);
successor.handleRequest(score);
}
public Score getNextScore() {
return successor.getNextScore();
}
}
```
5. CalculateAverageScore类
```java
public class CalculateAverageScore extends Handler {
@Override
public void handleRequest(Score score) {
double sum = score.getScore() + successor.getNextScore().getScore() + successor.getNextScore().getNextScore().getScore();
double average = sum / 3;
System.out.println("选手的平均成绩为:" + average);
}
}
```
6. GymnasticGame类
```java
public class GymnasticGame {
private Handler handler;
public void setHandler(Handler handler) {
this.handler = handler;
}
public void handleRequest(Score score) {
handler.handleRequest(score);
}
public static void main(String[] args) {
Score score1 = new Score(9.5);
Score score2 = new Score(9.8);
Score score3 = new Score(9.3);
RemoveHighestScore removeHighestScore = new RemoveHighestScore();
RemoveLowestScore removeLowestScore = new RemoveLowestScore();
CalculateAverageScore calculateAverageScore = new CalculateAverageScore();
removeHighestScore.setSuccessor(removeLowestScore);
removeLowestScore.setSuccessor(calculateAverageScore);
GymnasticGame game = new GymnasticGame();
game.setHandler(removeHighestScore);
game.handleRequest(score1);
game.handleRequest(score2);
game.handleRequest(score3);
}
}
```
阅读全文