Advertisement
apad464

ScoreInfoRunner (2014 FRQ 4)

Mar 10th, 2023
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.58 KB | Source Code | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3.  
  4. class ScoreInfo {
  5.     private int score;
  6.     private int numStudents;
  7.  
  8.     public ScoreInfo(int aScore) {
  9.         score = aScore;
  10.         numStudents = 1;
  11.     }
  12.  
  13.     public void increment() {
  14.         numStudents++;
  15.     }
  16.  
  17.     public int getScore() {
  18.         return score;
  19.     }
  20.  
  21.     public int getFrequency() {
  22.         return numStudents;
  23.     }
  24.  
  25.     public String toString() {
  26.         return score + " " + numStudents;
  27.     }
  28. }
  29.  
  30. class Stats {
  31.     private ArrayList<ScoreInfo> scoreList;
  32.  
  33.     public Stats() {
  34.         scoreList = new ArrayList<ScoreInfo>();
  35.     }
  36.     public boolean record(int score) {
  37.         for (int i = 0; i < scoreList.size(); i++) {
  38.             if (scoreList.get(i).getScore() == score) {
  39.                 scoreList.get(i).increment();
  40.                 return false;
  41.             }
  42.             else if (score < scoreList.get(i).getScore()) {
  43.                 scoreList.add(i, new ScoreInfo(score));
  44.                 return true;
  45.             }
  46.         }
  47.         scoreList.add(new ScoreInfo(score));
  48.         return true;
  49.     }
  50.  
  51.     public void recordScores(int[] stuScores) {
  52.         for (int stuScore : stuScores) {
  53.             record(stuScore);
  54.         }
  55.     }
  56.  
  57.     public void printList() {
  58.         System.out.println(scoreList);
  59.     }
  60. }
  61.  
  62. public class ScoreInfoRunner {
  63.     public static void main(String[] args) {
  64.         int[] array = {40, 50, 40, 100, 30, 40, 20, 100, 50, 100};
  65.         Stats test = new Stats();
  66.         test.recordScores(array);
  67.         test.printList();
  68.     }
  69. }
Tags: Java
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement