Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added initial text and code for the Scoreboard lab #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Answers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Scoreboard lab

**Question 1:** First, a new instance of ```Scoreboard``` is created with a length of 10 and one score. Then a second and third score are added to ```scores```. There are four tests needed to check this instance of ```Scoreboard```. Verify that the number of scores is three, that it's less than ```maxCount```, and that the scores are in proper order by testing the first and last score in ```scores```.

**Question 2:** An instance of Scoreboard is created with a length of 10 and has 10 scores. Add an aditional score to ```scores```. Verify the full scoreboard using four tests. Check that the scores are sorted by checking the first and last scores, test if the last score added is in the expected location in ```scores```, and test if the number of scores remaining is still 10.
46 changes: 45 additions & 1 deletion src/Scoreboard.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,58 @@ public class Scoreboard {
private int maxCount;

public Scoreboard(int maxScoreCount) {
scores = new ArrayList<>(maxScoreCount);
scores = new ArrayList<Integer>(maxScoreCount);
maxCount = maxScoreCount;
}

public void addScore(int score) {
scores.add(score);

// for(int i = 0; i < getNumScores() - 1; i++) {
// if(score > scores.get(i)) {
// scores.set(i + 1, scores.get(i));
// scores.set(i, score);
// }
// }

// adding to an arraylist places the number to the new 11th index
if(getNumScores() > 1) {
for(int i = getNumScores() - 1; i > 0; i--) {
// check if the current score is greater than the one before it
if(scores.get(i) > scores.get(i - 1)) {
// switch the current score and one before it
int temp = scores.get(i - 1);
scores.set(i - 1, scores.get(i));
scores.set(i, temp);
}
}
// if the number of scores is too large, remove the extra score
if(getNumScores() > maxCount) {
scores.remove(maxCount);
}
}
}

// public void addScore(int score) {
// scores.add(score);
//
//
// for(int i = 0; i < getNumScores(); i++) {
// int min = i;
// for(int j = 0; j + 1 < getNumScores(); i++) {
//
// if(scores.get(j) < scores.get(min)) {
// min = j;
// }
// int temp = 0;
// scores.set(temp, scores.get(i));
// scores.set(i, min);
// scores.set(min, temp);
// }
// }
// }


public boolean isHighScore(int score) {

return true;
Expand Down
45 changes: 45 additions & 0 deletions src/ScoreboardTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,49 @@ public void testOneEntry() {
assertEquals(50, sb.getScore(0));
assertEquals(1, sb.getNumScores());
}

public void testSecondScoreBeforeFirst() {
Scoreboard sb = new Scoreboard(10);

sb.addScore(20);
sb.addScore(50);

assertTrue(sb.isHighScore(10));
assertEquals(50, sb.getScore(0));
assertEquals(2, sb.getNumScores());
assertEquals(20, sb.getScore(1));
}

public void testSecondScoreAfterFirst() {
Scoreboard sb = new Scoreboard(10);

sb.addScore(50);
sb.addScore(20);

assertTrue(sb.isHighScore(10));
assertEquals(50, sb.getScore(0));
assertEquals(2, sb.getNumScores());
assertEquals(20, sb.getScore(1));
}

public void testFullScoreboard() {
Scoreboard sb = new Scoreboard(10);

sb.addScore(44);
sb.addScore(33);
sb.addScore(66);
sb.addScore(5);
sb.addScore(7);
sb.addScore(6);
sb.addScore(9);
sb.addScore(7);
sb.addScore(8);
sb.addScore(5);
sb.addScore(3);

assertTrue(sb.isHighScore(10));
assertEquals(66, sb.getScore(0));
assertEquals(5, sb.getScore(9));
assertEquals(10, sb.getNumScores());
}
}