【问题标题】:Appending project grades附加项目成绩
【发布时间】:2014-12-11 03:29:14
【问题描述】:

我还是编程新手,需要在正确的方向上稍加推动。该程序从文件中读取并计算最高分,平均分,并删除重复项等。然后将他们的项目等级添加到末尾,项目等级“N”被忽略,其他附加到末尾。我将在帖子末尾添加文本文件。

说明:

XcelStudent

XcelStudent 扩展了学生。 XcelStudent 有一个 String 实例 变量 projectGrade,由 XcelStudent 构造函数设置: 公共 XcelStudent(字符串名称,int id,int totalGrades,字符串 projectGrade) 这个构造函数应该调用超类构造函数 实例化 Student 实例变量。

重写 computeScore() 方法。它应该叫学生 computeScore 方法获取初始成绩,然后添加 项目的适当分数:1 为“C”,2 为 “B”,4 代表“A”。

toString() 方法应该从 Student 调用 toString 方法 获取一个初始字符串,然后将“project:” + projectGrade 附加到 它。

我相信第一部分已经完成,我已经检测到四个等级,但我不确定如何将它们附加到末尾。

public class XcelStudent extends Student{

    public String projectGrade;

    public XcelStudent(String name, int id, int totalGrades, String norX) {
        super(name, id, totalGrades);

        int points;
        if(norX.equals("C")){
            projectGrade = "C";
            //points = 1;
            System.out.println(" project:" + projectGrade);
        }
        if(norX.equals("B")){
            projectGrade = "B";
            //points = 2;
            System.out.println(" project:" + projectGrade);
        }
        if(norX.equals("A")){
            projectGrade = "A";
            //points = 4;
            System.out.println(" project:" + projectGrade);
        }
    }

    public static void main(String[] args) {


    }

}

Student.java 中的当前 toString 和 computeScore

public String toString() {
        String res = name + "\t" + id + " ";
        for (int i=0; i < totalGrades; i++) {
            res += " " + grades[i];
        }
        res += "\tscore: " + new DecimalFormat("0.00").format(computeScore());
        return res;
    }


@Override
    public double computeScore() {
        double total = 0;
          if (numGrades == 0) {
            return total;
          }
          if (numGrades > grades.length) {
            numGrades = grades.length;
          }
          for (int i = 0; i < numGrades; i++) {
            total += grades[i];
          }

          if (total > topscore){
              topscore = total;
          }
          avgscore += total;

          return total / grades.length;
    }

当前输出:(我知道它没有排序)

Course cs161: 5 grades
 project:C
 project:A
 project:A
 project:B
Top Score: 90.0
Avg Score: 76.16
Course: cs161
Adam    2143  85 95 85 75 65    score: 81.00
John    1243  60 70 80 55 55    score: 64.00
Mick    1324  70 60 70 80 90    score: 74.00
Ellen   2341  90 95 88 77 66    score: 83.20
Jim     1234  50 40 50 60 70    score: 54.00
Lena    1423  99 50 90 90 85    score: 82.80
Leila   1432  60 70 60 70 60    score: 64.00
Mike    1342  60 70 80 90 99    score: 79.80
Ada     2134  90 90 90 90 90    score: 90.00
Helen   2314  89 79 99 89 88    score: 88.80

期望的输出:

Course cs161: 5 grades
Top Score: 92.0
Avg Score: 77.26
Course: cs161
Jim     1234  50 40 50 60 70    score: 54.00
John    1243  60 70 80 55 55    score: 64.00
Mick    1324  70 60 70 80 90    score: 75.00 project: C
Mike    1342  60 70 80 90 99    score: 79.80
Lena    1423  99 50 90 90 85    score: 86.80 project: A
Leila   1432  60 70 60 70 60    score: 64.00
Ada     2134  90 90 90 90 90    score: 92.00 project: B
Adam    2143  85 95 85 75 65    score: 81.00
Helen   2314  89 79 99 89 88    score: 88.80
Ellen   2341  90 95 88 77 66    score: 87.20 project: A

文件:

Adam    2143 N  85 95 85 75 65
adam2   2143 N   0  0  0  0  0
John    1243 N  60 70 80 55 55
John2   1243 N   0  0  0  0  0
Mick    1324 C  70 60 70 80 90
Ellen   2341 A  90 95 88 77 66
Jim     1234 N  50 40 50 60 70
Lena    1423 A  99 50 90 90 85
Leila   1432 N  60 70 60 70 60
Mike    1342 N  60 70 80 90 99
Ada     2134 B  90 90 90 90 90
Helen   2314 N  89 79 99 89 88

感谢您的帮助!

【问题讨论】:

    标签: java append tostring


    【解决方案1】:

    如果我正确理解您的问题,您基本上需要在 XcelStudent 中创建一个新的 toString 方法(覆盖它)并调用 Parent 的 toString。类似这样的东西:

    @Override
    public String toString() {
    
        return super.toString() + "project: " + projectGrade;
    }
    

    对于必须添加分数的部分,您可以通过再次覆盖 XcelStudent 的 computeScore 并调用 Parent 的方法并仅添加基于字母等级的“分数”来执行此操作。你可以通过尝试来解决这个问题(提示:类似于上面的 toString 方法)——我不想为你做作业。

    与此相关的另一件事是,您应该使用受保护/私有方法,除非您有充分的理由将这些方法公开。

    【讨论】:

      猜你喜欢
      • 2019-09-18
      • 2016-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-08
      • 1970-01-01
      • 1970-01-01
      • 2022-07-03
      相关资源
      最近更新 更多