简体   繁体   中英

Finding the highest and the lowest value

I have this assignment I gotta do. It looks good, except for the getHighest output and the getLowest result. Here's my code:

KNW_CourseGradesV2 Class:

public class KNW_CourseGradesV2 implements KNW_Analyzable
{
  //Declare array variable
  private KNW_GradedActivity[] grades = new KNW_GradedActivity[4];
  String results;

  /**
   * Constructor
   * */
  public KNW_CourseGradesV2()
  {

  }

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

  public void setPassFailExam(KNW_GradedActivity pfe)
  {
    grades[1] = pfe;
  }

  public void setEssay(KNW_GradedActivity essay)
  {
    grades[2] = essay;
  }
  public void setFinalExam(KNW_FinalExam fe)
  {
    grades[3] = fe;
  }
  public void setTotalGrade(KNW_GradedActivity tg)
  {
    grades[4] = tg;
  }

  public String toString()
  {
    //Lay out all of the scores
    return "Lab Score: " + grades[0].getScore() +
      "\tGrade: " + grades[0].getGrade() + 
      "\nPass/Fail Exam: " + grades[1].getScore() +
      "\tGrade: " + grades[1].getGrade() +
      "\nEssay Score: " + grades[2].getScore() + 
      "\tGrade: " + grades[2].getGrade() + 
      "\nFinal Exam Score: " + grades[3].getScore() + 
      "\tGrade: " + grades[3].getGrade();
  }

  //Find the average of the grades
  public double getAverage()
  {
    double sum = 0.0;
    double average = 0.0;
    double total = 0.0;

    //For loop to calc. the average
    for(int i =0; i<grades.length; i++)
    {
      total =+ grades[i].getScore();
      average = sum/grades.length;
    }
    return average;
  }

  public KNW_GradedActivity getHighest()
  {
    double highest = grades[0].getScore();

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

    //Created KNW_GradedActivity object to bring the highest number
    //into the setScore method so it can be a double
    KNW_GradedActivity gaH =  new KNW_GradedActivity();
    gaH.setScore(highest);
    return gaH;
  }

  public KNW_GradedActivity getLowest()
  {
    double lowest = grades[0].getScore();

    for(int i = 0; i < grades.length; i++)
    {
      if(grades[i].getScore() > lowest)
      {
        lowest = grades[i].getScore();
      }
    }
    //Samething here, just doing it for the lowest number
    KNW_GradedActivity gaL =  new KNW_GradedActivity();
    gaL.setScore(lowest);
    return gaL;
  }
}

KNW_Analyzable Class:

public interface KNW_Analyzable
{
  double getAverage();
  KNW_GradedActivity getHighest();
  KNW_GradedActivity getLowest();
}

KNW_GradedActivity Class:

public class KNW_GradedActivity
{
   private double score;  // Numeric score

   /**
      The setScore method sets the score field.
      @param s The value to store in score.
   */

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

   /**
      The getScore method returns the score.
      @return The value stored in the score field.
   */

   public double getScore()
   {
      return score;
   }

   /**
      The getGrade method returns a letter grade
      determined from the score field.
      @return The letter grade.
   */

   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;
   }
}

And here is the output I get. I will just show the problem I am facing, won't show all of the output.

Output:

Average: 0.0
Highest: KNW_GradedActivity@565d3fa7
Lowest: KNW_GradedActivity@655c4395> 

I have been trying for about a week to figure this out and find what is happening, and I can't seem to find it.

Notice that in the return of your methods, you are actually returning gaH and gaL , both of which are KNW_GradedActivity Objects. If you want to see the score, you should probably return gaH.getScore() and gaL.getScore() .

Why not to use Stream and DoubleSummaryStatistics to get the stats? Let the api do all the calculations:

List<KNW_GradedActivity> grades = Arrays.asList(new KNW_GradedActivity(), new KNW_GradedActivity(), new KNW_GradedActivity());
grades.get(0).setScore(91);
grades.get(1).setScore(81);
grades.get(2).setScore(71);

DoubleSummaryStatistics statistics = grades.stream().
        collect(Collectors.summarizingDouble(KNW_GradedActivity::getScore));
System.out.println(statistics);
// DoubleSummaryStatistics{count=3, sum=243.000000, min=71.000000, average=81.000000, max=91.000000}

Another alternative is to override toString method in your KNW_GradedActivity class:

@Override
public String toString() {
    return "{" +
            "score=" + score +
            ", grade='" + getGrade() + '\'' +
            '}';
}

This way you dont have to change your actual print statement and will have a nice output combining your score and grade. It will look like:

{score=90.0, grade='A'}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM