Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Arrays;
- class ScoreInfo {
- private int score;
- private int numStudents;
- public ScoreInfo(int aScore) {
- score = aScore;
- numStudents = 1;
- }
- public void increment() {
- numStudents++;
- }
- public int getScore() {
- return score;
- }
- public int getFrequency() {
- return numStudents;
- }
- public String toString() {
- return score + " " + numStudents;
- }
- }
- class Stats {
- private ArrayList<ScoreInfo> scoreList;
- public Stats() {
- scoreList = new ArrayList<ScoreInfo>();
- }
- public boolean record(int score) {
- for (int i = 0; i < scoreList.size(); i++) {
- if (scoreList.get(i).getScore() == score) {
- scoreList.get(i).increment();
- return false;
- }
- else if (score < scoreList.get(i).getScore()) {
- scoreList.add(i, new ScoreInfo(score));
- return true;
- }
- }
- scoreList.add(new ScoreInfo(score));
- return true;
- }
- public void recordScores(int[] stuScores) {
- for (int stuScore : stuScores) {
- record(stuScore);
- }
- }
- public void printList() {
- System.out.println(scoreList);
- }
- }
- public class ScoreInfoRunner {
- public static void main(String[] args) {
- int[] array = {40, 50, 40, 100, 30, 40, 20, 100, 50, 100};
- Stats test = new Stats();
- test.recordScores(array);
- test.printList();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement