我有一个非常简单的解决方案,但我还没有学会如何做到这一点,也无法弄清楚如何解决问题。
我有一个Interface类:
public interface Analyzable {
double getAverage();
GradedActivity getHighest();
GradedActivity getLowest();
}
这是我的教授给我的。我将他的代码从两个GradedActivity更改为双精度,并且在此代码中返回它们没有任何问题。但是我无法将它们作为GradedActivity&#39>返回
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 [] grade = new 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 [] grade = new 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);
}
}
答案 0 :(得分:2)
由于double不能转换为GradedActivity
,您需要先创建一个,设置值(我会创建一个构造函数)然后返回它。
试
GradedActivity highGA = new GradedActivity();
highGA.setScore (highest);
return highGA;
答案 1 :(得分:0)
您需要获取值并包装在GradedActivity
实例中并返回它,例如...
public GradedActivity getHighest() {
double highest = grades[0].getScore();
for(int i = )......
//Code works and returns correct number (tested)
GradedActivity ga = new GradedActivity();
ga.setScore(highest);
return ga;
答案 2 :(得分:0)
Java不会自动从double
转换为GradedActivity
您必须创建一个新的Graded Activity实例并设置其值。
GradedActivity ga = new GradedActivity(); // create instance
ga.setScore(value); // set value
return ga; // return GradedActivity instead of double