编写一个程序,使用23种设计模式中的模板方法模式,完成能够计算三科平均成绩以及智育成绩的程序(Write a program that uses the template method mode in 23 design modes to complete the program that can calculate the average score of three subjects and the score of intellectual education)

题目:

Lisa正在编写一个能够计算三科——数学、英语、程序设计基础——平均成绩(平均成绩=三科总成绩/3)和智育成绩(智育成绩=三科总成绩*0.7)的程序。

由于两个成绩的获得都要先计算总成绩,因此她考虑使用23种设计模式中的模板方法模式完成。模板方法模式——定义一个操作方法中的算法的“骨架”,

而将一些步骤延迟到子类中。它通过把不变行为搬移到父类,去除子类中的重复代码来体现程序的优势。

为此,Lisa设计了一个抽象类和三个非抽象类:

  1. 抽象类——AbstractComputer.java  2. 计算平均成绩的类——ComputerAverage.java  3. 计算智育成绩的类——ComputerIntel.java  4. 测试类——Test.java

1AbstractComputer.java

表示两种计算的“骨架”,组成内容如下:

(1)、total:成员变量,double型,表示总成绩

(2)、public abstract double computer( ):表示计算平均成绩或智育成绩

(3)、public double templateComputer(double math,double english,double program):计算数学、英语、程序设计基础三科总成绩,为total赋值,并调用computer( )方法获得平均成绩或智育成绩

 1 package seven_topic;
 2 
 3 abstract class AbstractComputer {
 4     double total;
 5 
 6     public abstract double computer();
 7 
 8     public double templateComputer(double math, double english, double program) {
 9         total = math + english + program;
10         return computer();
11     }
12 }

2ComputerAverage.java

AbstractComputer类的非抽象子类,用于计算平均成绩,组成内容如下:

public double computer( ):重写的computer( )方法,计算total的平均值

1 class ComputerAverage extends AbstractComputer {
2 
3     public double computer() {
4 
5         return total / 3;
6     }
7 
8 }

3.ComputerIntel.java

AbstractComputer类的非抽象子类,用于计算智育成绩,组成内容如下:

public double computer( ):重写的computer( )方法,计算total折合的智育成绩

1 class ComputerIntel extends AbstractComputer {
2     public double computer() {
3         return total * 0.7;
4     }
5 
6 }

4.Test.java

在Test类中通过键盘输入三科成绩,如89,97.5,95,分别创建AbstractComputer类的上转型对象average和intel,

计算这三科成绩的平均成绩和智育成绩,并将结果输出。

import java.util.*;

public class Test {
    public static void main(String[] args) {
        System.out.println("请分别输入三科成绩:");
        @SuppressWarnings("resource")
        Scanner input = new Scanner(System.in);
        double math = input.nextDouble();
        double english = input.nextDouble();
        double program = input.nextDouble();
        AbstractComputer computerAverage = new ComputerAverage();
        System.out.println("三科平均成绩为:");
        System.out.println(computerAverage.templateComputer(math, english, program));

        AbstractComputer computerIntel = new ComputerIntel();
        System.out.println("三科的智育成绩为:");
        System.out.println(computerIntel.templateComputer(math, english, program));
    }

}

以上就是此次作业的全部内容了!

2022.5.14

————————

Title:

Lisa is writing a program that can calculate the average score (average score = total score of three subjects / 3) and intellectual education score (intellectual education score = total score of three subjects * 0.7) of three subjects – Mathematics, English and fundamentals of programming.

Since the total score must be calculated before obtaining the two grades, she considered using the template method of 23 design modes. Template method pattern – defines the “skeleton” of an algorithm in an operation method,

Instead, some steps are deferred to subclasses. It reflects the advantages of the program by moving the invariant behavior to the parent class and removing the duplicate code in the subclass.

To this end, Lisa designed an abstract class and three non abstract classes:

1. Abstract class abstractcomputer java  2. Computer average java  3. A class for calculating intellectual education achievements — computerintel java  4. Test class – test java

1AbstractComputer.java

It represents the “skeleton” of two calculations, which is composed of the following contents:

(1) . total: member variable, double type, indicating the total score

(2) . public abstract double computer(): it means to calculate the average score or intellectual education score

(3) Public double templatecomputer (double math, double English, double program): calculate the total scores of mathematics, English and basic programming, assign a value to the total, and call the computer () method to obtain the average score or intellectual education score

 1 package seven_topic;
 2 
 3 abstract class AbstractComputer {
 4     double total;
 5 
 6     public abstract double computer();
 7 
 8     public double templateComputer(double math, double english, double program) {
 9         total = math + english + program;
10         return computer();
11     }
12 }

2ComputerAverage.java

The non Abstract subclass of abstractcomputer class is used to calculate the average score. The composition is as follows:

Public double computer(): rewrite the computer() method to calculate the average value of total

1 class ComputerAverage extends AbstractComputer {
2 
3     public double computer() {
4 
5         return total / 3;
6     }
7 
8 }

3.ComputerIntel.java

Abstractcomputer is a non Abstract subclass of abstractcomputer, which is used to calculate intellectual education achievements. Its composition is as follows:

Public double computer(): rewrite the computer() method to calculate the intellectual education achievement converted by total

1 class ComputerIntel extends AbstractComputer {
2     public double computer() {
3         return total * 0.7;
4     }
5 
6 }

4.Test.java

In the test class, enter the scores of three subjects through the keyboard, such as 89, 97.5 and 95, and create the upper transformation objects average and Intel of abstractcomputer class respectively,

Calculate the average score and intellectual education score of the three subjects, and output the results.

import java.util.*;

public class Test {
    public static void main(String[] args) {
        System.out.println("请分别输入三科成绩:");
        @SuppressWarnings("resource")
        Scanner input = new Scanner(System.in);
        double math = input.nextDouble();
        double english = input.nextDouble();
        double program = input.nextDouble();
        AbstractComputer computerAverage = new ComputerAverage();
        System.out.println("三科平均成绩为:");
        System.out.println(computerAverage.templateComputer(math, english, program));

        AbstractComputer computerIntel = new ComputerIntel();
        System.out.println("三科的智育成绩为:");
        System.out.println(computerIntel.templateComputer(math, english, program));
    }

}

That’s all for this assignment!

2022.5.14