APCS Lessons 9, 15-16 Test Review Part 1
ArrayList <Integer>
a = new ArrayList<Integer>();
What are the contents of the ArrayList after the following lines have been executed:
a.add(new
Integer(7));
a.add(new
Integer(5));
a.add(1,
new Integer(9));
a.get(a.size()-1);
a.add(new
Integer(2));
a.set(2,
new Integer(3));
a.remove(a.size()-1);
public class Score {
private int score;
private String name;
public Score(int scre, String nme) {…}
public
int getScore() {…} //returns score
public String getName() {…} //returns name
public int compareTo(Score compareMe) {…}
//returns < 0 if this comes before compareMe
//returns 0 if this equals compareMe
//returns > 0 if this comes after compareMe
}
public class
VideoGameScores {
private Score[] highScores;
//top
scores in descending order
public boolean areAllHeldBySamePerson() {…}
//returns true if all top
scores are held by the same //person
public void
syncHighScores(Score[] source1, Score[] source2) {…}
//will take high scores
from two different sources and
//update highScores with
the overall high scores
}
Example:
Score
s1 = new Score(10000, “abc”);
Score
s2 = new Score(8000, “mbs”);
Score
s3 = new Score(10000, “cs!”);
s1.compareTo(s2);
//returns -1
s1.compareTo(s3);
//returns 0
s2.compareTo(s3);
//returns 1
public
int compareTo(Score compareMe) {
}
public
Boolean areAllHeldBySamePerson() {
}
public void
syncHighScores(Score[] source1, Score[] source2) {
}
public boolean
doSomething(int[] a) {
boolean flag = true;
for (int j = 0; j < a.length - 1; j++)
flag = flag &&
(a[j] < a[j+1]);
return flag;
}