2、用java写策略模式:定义一系列算法,把他们封装起来,并且使它们可相互替换。要求使用策略模式解决以下问题: 高考入学考试课程有3门,分别是数学、语文、外语,每门150分。高考招生时,重点大学录取规则为录取最低分数线时350分。普通大学录取规则为300分。 具体要求: (1)说明策略模式中3个角色以及具体作用。(2)定义 Student 类,包含姓名和存放成绩的 double 数组,以及构造方法。 (3)定义 Strategy 接口,里面封装 enterRule 方法,要求参数是一个 double 数组。 (4)定义具体策略类 StrategyA 要求实现 Strategy 接口,用于实现重点大学录取。 (5)定义具体策略类 StrategyB 要求实现 Strategy 接口,用于实现普通大学录取。 (6)定义 Context 类,包含成员变量 Strategy 接口,以及相应 setStrategy 方法和用于判断是否录取的 public void getAccepet ( double a [])方法。 (7)定义应用程序 Application 类,使用策略模式中所涉及的类,判断高考数学分数为 120分,语文分数为116分,外语为130分的李婷同学是否被重点大学录取。判断高考数学分数为100分,语文分数为90分,外语为80分的张宇同学是否被普通大学录取。
时间: 2024-03-12 11:46:17 浏览: 27
(1) 策略模式中的三个角色分别是:Context(上下文类),Strategy(策略接口),ConcreteStrategy(具体策略类)。Context 类是使用某个策略的入口,它持有一个 Strategy 接口类型的成员变量,用于存放具体的策略类对象。Strategy 接口是所有具体策略类的统一接口,其中定义了一个 enterRule 方法,该方法接收一个 double 类型的数组参数,用于计算分数是否达到录取线。ConcreteStrategy 是具体实现 Strategy 接口的类,它们实现了 enterRule 方法,用于实现不同的录取规则。
(2) 声明 Student 类如下:
```java
public class Student {
private String name; // 学生姓名
private double[] scores; // 学生成绩
public Student(String name, double[] scores) {
this.name = name;
this.scores = scores;
}
public String getName() {
return name;
}
public double[] getScores() {
return scores;
}
}
```
(3) 声明 Strategy 接口如下:
```java
public interface Strategy {
boolean enterRule(double[] scores);
}
```
(4) 声明具体策略类 StrategyA 如下:
```java
public class StrategyA implements Strategy {
@Override
public boolean enterRule(double[] scores) {
int totalScore = 0;
for (double score : scores) {
totalScore += score;
}
return totalScore >= 350;
}
}
```
(5) 声明具体策略类 StrategyB 如下:
```java
public class StrategyB implements Strategy {
@Override
public boolean enterRule(double[] scores) {
int totalScore = 0;
for (double score : scores) {
totalScore += score;
}
return totalScore >= 300;
}
}
```
(6) 声明 Context 类如下:
```java
public class Context {
private Strategy strategy;
public void setStrategy(Strategy strategy) {
this.strategy = strategy;
}
public boolean getAccept(double[] scores) {
return strategy.enterRule(scores);
}
}
```
(7) 在 Application 类中,创建两个学生对象,分别是李婷和张宇,然后分别使用不同的策略类进行判断是否被录取,代码如下:
```java
public class Application {
public static void main(String[] args) {
// 创建两个学生对象
Student liTing = new Student("李婷", new double[]{120, 116, 130});
Student zhangYu = new Student("张宇", new double[]{100, 90, 80});
// 使用重点大学录取策略
Context context1 = new Context();
context1.setStrategy(new StrategyA());
boolean isAccepted1 = context1.getAccept(liTing.getScores());
System.out.println(liTing.getName() + "是否被重点大学录取:" + isAccepted1);
// 使用普通大学录取策略
Context context2 = new Context();
context2.setStrategy(new StrategyB());
boolean isAccepted2 = context2.getAccept(zhangYu.getScores());
System.out.println(zhangYu.getName() + "是否被普通大学录取:" + isAccepted2);
}
}
```
输出结果:
```
李婷是否被重点大学录取:true
张宇是否被普通大学录取:false
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)