我有一个非常简单的解决方案,但是我还没有学会如何做,也无法弄清楚如何解决问题。

我有一个接口类:

public interface Analyzable {
  double getAverage();
  GradedActivity getHighest();
  GradedActivity getLowest();
}


这是我教授给我的。我将他的代码从两个GradedActivity更改为两倍,并且在此代码中返回它们没有问题。但是我很难将它们作为GradedActivity的返回

public GradedActivity getHighest() {
    double highest = grades[0].getScore();
    for(int i = )......
    //Code works and returns correct number (tested)
    return highest;


这段代码给我这个错误
“无法将Double转换为GradedActivity”

这是我的GradedActivity代码

public class GradedActivity {
private double score;

public GradedActivity() {
}

public void setScore(double s) {
    score = s;
}

public double getScore() {
    return score;
}

public char getGrade() {
    char letterGrade;
    if(score >= 90)
        letterGrade = 'A';
    else if(score >= 80)
        letterGrade = 'B';
    else if(score >= 70)
        letterGrade = 'C';
    else if(score >= 60)
        letterGrade = 'D';
    else
        letterGrade = 'F';

    return letterGrade;
}


}

这是CourseGrades代码

enter code here


公共课程CourseGrades扩展GradedActivity实现Analyzable
{
    私人GradedActivity []等级=新的GradedActivity [4];

public CourseGrades()
{

}

public void setLab(GradedActivity lab)
{
    grades[0] = lab;
}

public void setPassFailExam(PassFailExam passFailExam)
{
    grades[1] = passFailExam;
}

public void setEssay(GradedActivity essay)
{
    grades[2] = essay;
}

public void setFinalExam(FinalExam finalExam)
{
    grades[3] = finalExam;
}



public String toString() {
    String str;
    str = "Lab Score: " + grades[0].getScore() +
                        "\t\t\tGrade: " + grades[0].getGrade() +
                "\nP/F Exam Score: " + grades[1].getScore() +
                        "\t\tGrade: " + grades[1].getGrade() +
                "\nEssay Score: " + grades[2].getScore() +
                        "\t\tGrade: " + grades[2].getGrade() +
                "\nFinal Exam Score: " + grades[3].getScore() +
                        "\t\tGrade: " + grades[3].getGrade();
    return str;
}

public double getAverage()
{
    double sum = 0;
    for(int i = 0; i < 4; i++) {
        sum += grades[i].getScore();
    }
    double ave = sum/4;
    return ave;
}

public GradedActivity getHighest() {

    double highest = grades[0].getScore();
    for(int i = 1; i < 4; i++) {
        if(grades[i].getScore() > highest)
            highest = grades[i].getScore();
    }
    return highest;
}

public GradedActivity getLowest() {
    double lowest = grades[0].getScore();
    for(int i = 1; i < 4; i++) {
        if(grades[i].getScore() < lowest)
            lowest = grades[i].getScore();
    }
    return setScore(lowest);
}


}

这是界面

公共课程CourseGrades扩展GradedActivity实现Analyzable
{
    私人GradedActivity []等级=新的GradedActivity [4];

public CourseGrades()
{

}

public void setLab(GradedActivity lab)
{
    grades[0] = lab;
}

public void setPassFailExam(PassFailExam passFailExam)
{
    grades[1] = passFailExam;
}

public void setEssay(GradedActivity essay)
{
    grades[2] = essay;
}

public void setFinalExam(FinalExam finalExam)
{
    grades[3] = finalExam;
}



public String toString() {
    String str;
    str = "Lab Score: " + grades[0].getScore() +
                        "\t\t\tGrade: " + grades[0].getGrade() +
                "\nP/F Exam Score: " + grades[1].getScore() +
                        "\t\tGrade: " + grades[1].getGrade() +
                "\nEssay Score: " + grades[2].getScore() +
                        "\t\tGrade: " + grades[2].getGrade() +
                "\nFinal Exam Score: " + grades[3].getScore() +
                        "\t\tGrade: " + grades[3].getGrade();
    return str;
}

public double getAverage()
{
    double sum = 0;
    for(int i = 0; i < 4; i++) {
        sum += grades[i].getScore();
    }
    double ave = sum/4;
    return ave;
}

public GradedActivity getHighest() {

    double highest = grades[0].getScore();
    for(int i = 1; i < 4; i++) {
        if(grades[i].getScore() > highest)
            highest = grades[i].getScore();
    }
    return highest;
}

public GradedActivity getLowest() {
    double lowest = grades[0].getScore();
    for(int i = 1; i < 4; i++) {
        if(grades[i].getScore() < lowest)
            lowest = grades[i].getScore();
    }
    return setScore(lowest);
}


}

最佳答案

由于不能将double强制转换为GradedActivity,因此需要首先创建一个double,设置值(我将创建一个构造函数),然后将其返回。

尝试

   GradedActivity highGA = new GradedActivity();
   highGA.setScore (highest);
   return highGA;

09-30 09:10