鱼C论坛

 找回密码
 立即注册
查看: 1930|回复: 1

5.这个题的类图怎么画?

[复制链接]
发表于 2023-10-29 17:56:27 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
5.我国机关事业单位以及国企工作人员招考的面试成绩在计算时是去掉一个最高分和最低分后再计算平均分(由7名考官打分),而某个科目的考试成绩则是计算全班同学(由20人组成一个班)的平均分。请定义一个工作人员招聘类StaffRecruit和期末考试类FinalExam,它们都实现了IAverage接口,但实现方式不同。IAverage中有一个抽象方法average(double[] x),两个默认方法max(double[] x)和min(double[] x)。请编写程序完成上述类和接口的设计,并编写测试应用程序测试。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-10-29 17:56:41 | 显示全部楼层
根据题目描述,可以设计以下类图:

  1. +-------------------+
  2. |     StaffRecruit  |
  3. +-------------------+
  4. |                   |
  5. | - scores: double[] |
  6. |                   |
  7. | + average(): double|
  8. | + max(): double    |
  9. | + min(): double    |
  10. +-------------------+
  11.        ^
  12.        |
  13.        |
  14. +-------------------+
  15. |    FinalExam      |
  16. +-------------------+
  17. |                   |
  18. | - scores: double[] |
  19. |                   |
  20. | + average(): double|
  21. | + max(): double    |
  22. | + min(): double    |
  23. +-------------------+
复制代码


在类图中, StaffRecruit 和 FinalExam 都实现了 IAverage 接口,该接口定义了 average(double[] x) 抽象方法和 max(double[] x) 、 min(double[] x) 默认方法。

下面是相应的代码实现:

  1. interface IAverage {
  2.     double average(double[] x);

  3.     default double max(double[] x) {
  4.         double max = Double.MIN_VALUE;
  5.         for (double score : x) {
  6.             if (score > max) {
  7.                 max = score;
  8.             }
  9.         }
  10.         return max;
  11.     }

  12.     default double min(double[] x) {
  13.         double min = Double.MAX_VALUE;
  14.         for (double score : x) {
  15.             if (score < min) {
  16.                 min = score;
  17.             }
  18.         }
  19.         return min;
  20.     }
  21. }

  22. class StaffRecruit implements IAverage {
  23.     private double[] scores;

  24.     public StaffRecruit(double[] scores) {
  25.         this.scores = scores;
  26.     }

  27.     @Override
  28.     public double average(double[] x) {
  29.         // 去掉一个最高分和最低分
  30.         double sum = 0;
  31.         double min = min(x);
  32.         double max = max(x);
  33.         for (double score : x) {
  34.             if (score != min && score != max) {
  35.                 sum += score;
  36.             }
  37.         }
  38.         return sum / (x.length - 2);
  39.     }
  40. }

  41. class FinalExam implements IAverage {
  42.     private double[] scores;

  43.     public FinalExam(double[] scores) {
  44.         this.scores = scores;
  45.     }

  46.     @Override
  47.     public double average(double[] x) {
  48.         double sum = 0;
  49.         for (double score : x) {
  50.             sum += score;
  51.         }
  52.         return sum / x.length;
  53.     }
  54. }

  55. public class Main {
  56.     public static void main(String[] args) {
  57.         double[] staffRecruitScores = { 80, 90, 95, 85, 88, 92, 87 };
  58.         double[] finalExamScores = { 75, 85, 90, 92, 88, 82, 80, 85, 90, 95, 85, 88, 92, 87, 85, 90, 95, 85, 88, 92 };

  59.         StaffRecruit staffRecruit = new StaffRecruit(staffRecruitScores);
  60.         FinalExam finalExam = new FinalExam(finalExamScores);

  61.         System.out.println("Staff Recruit Average: " + staffRecruit.average(staffRecruitScores));
  62.         System.out.println("Final Exam Average: " + finalExam.average(finalExamScores));
  63.     }
  64. }
复制代码


在测试应用程序中,我们创建了一个 StaffRecruit 对象和一个 FinalExam 对象,并传入相应的成绩数组。然后,调用 average() 方法分别计算了工作人员招聘面试成绩的平均分和期末考试成绩的平均分,并将结果打印出来。

注意:为了简化代码,我们直接将成绩数组作为参数传递给 average() 方法,实际应用中可能需要通过类的成员变量来传递。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-9 09:42

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表