Stats

<< Grub | APQuestionsTrailIndex | 2009NumberCube >>

StatsQuestion.pdf

StarterCode

ScoreInfo.java

public class ScoreInfo 
{ 
  private int score; 
  private int numStudents; 

  public ScoreInfo(int aScore) 
  { 
    score = aScore; 
    numStudents = 1; 
  } 

  /** adds 1 to the number of students who earned this score  
   */ 
  public void increment() 
  {  numStudents++;  } 

  /** @return this score 
   */ 
  public int getScore() 
  {  return score;  } 

  /** @return the number of students who earned this score  
   */ 
  public int getFrequency() 
  {  return numStudents;  } } 

Stats.java

import java.util.ArrayList;
public class Stats 
{ 
  private ArrayList<ScoreInfo> scoreList; 
    // listed in increasing score order; no two ScoreInfo objects contain the same score 
  public Stats()
  {
      scoreList = new ArrayList<ScoreInfo>();
  }
  /** Records a score in the database, keeping the database in increasing score order. If no other  
   *  ScoreInfo object represents score, a new ScoreInfo object representing score  
   *  is added to the database; otherwise, the frequency in the ScoreInfo object representing  
   *  score is incremented.  
   *  @param score a score to be recorded in the list 
   *  @return true if a new ScoreInfo object representing score was added to the list;  
   *          false otherwise 
   */ 
  public boolean record(int score) 
  { /*  to be implemented in part (a)  
     */        



  } 

  /** Records all scores in stuScores in the database, keeping the database in increasing score order 
   *  @param stuScores an array of student test scores 
   */ 
  public void recordScores(int[] stuScores) 
  {  /*  to be implemented in part (b)  */ 


  } 

  public String toString()
  {
      String result="";
      for (ScoreInfo s:scoreList) result+="Score of "+s.getScore()+" - "+s.getFrequency()+" students\n";
      return result;
  }
  // There may be instance variables, constructors, and methods that are not shown. 
} 

StatsTester.java

import java.util.ArrayList;
public class StatsTester
{

	public static void main(String[] args)
	{
		Stats s=new Stats();
		System.out.println("Testing part (a)" );
		System.out.println("Added 17 (should be true)"+s.record(17) );
		System.out.println("Added 17 (should be false)"+s.record(17) );
		System.out.println("Added 17 (should be false)"+s.record(17) );
		System.out.println("Added 15 (should be true)"+s.record(15) );
		System.out.println("Added 15 (should be false)"+s.record(15) );
		System.out.println("Added 21 (should be true)"+s.record(21) );
		System.out.println("Added 19 (should be true)"+s.record(19) );
		System.out.println(s);

		System.out.println("Testing part (b)" );
		int[] a={ 12,12, 18, 18, 18, 16, 18, 20, 10 };
		s.recordScores(a);
		System.out.print("Array=");
		for (int i:a) System.out.print(i+" ");

		System.out.println("\nStats:\n"+s);

	}


}

Solutions

Part (a)

public boolean record(int score) 
  {  
      //Find where in the scorelist my score belongs, as long as the score is larger, move up the scoreList
      int insertHere=0;  
      boolean isNewScore=true;
      while (insertHere < scoreList.size()  && score > scoreList.get(insertHere).getScore() )
      {          
          insertHere++;
       }
      //check to see if the score is already there
      if (insertHere < scoreList.size()  && score==scoreList.get(insertHere).getScore())
             isNewScore=false;

      if (isNewScore) 
          scoreList.add(insertHere, new ScoreInfo(score));
      else 
          scoreList.get(insertHere).increment();

      return isNewScore;


  } 

or

public boolean record(int score) 
  { 
    //Look for score in the scoreList
    boolean found = false;
    for (ScoreInfo s: scoreList)
             if(s.getScore()==score)
                          found=true;

    if (found){
            for (ScoreInfo s: scoreList)
             if(s.getScore()==score)
                          s.increment();
            return false;
    }
   //this must be a new score if we get this far

   ScoreInfo myNewScore=new ScoreInfo(score);
   //now we need to find its place in the score List, as in an insertion sort
  //start at the end....
   int myPlace=scoreList.size()-1;

   while (myPlace>0 && scoreList.get(myPlace).getScore() >score)
        myPlace--;
   if (myPlace<0) myPlace=0;
  //now we know the correct location, we add it to the list
   scoreList.add(myPlace, myNewScore);
   return true;

  } 

Part (b)

 public void recordScores(int[] stuScores) 
  {  
     for (int score : stuScores)
           record(score); 

  }