Q2 [Early exam]
Q2.1.
Wahoo! (7, 10) (7, 10) (2, 5) (7, 10) (7, 10) (7, 10)Q2.2. 4
Q2 [Later exam]
Q2.1.
Penny! (8, 5) (8, 5) (5, 2) (11, 8) (11, 8) (5, 2)Q2.2. 4
Q3.1
Many correct solutions. Here's the most common one.
Description: A single-word sentence.
Test case 2:
Hello.
Expected result:
Hello
Q3.2 Output for Test case 1:
This is aOutput for Test case 2: (depends on answer given for Q3.1)
<empty>Q3.3 Changes shown in bold
Scanner in = new Scanner(System.in);
String word = in.next();
while (word.charAt(word.length()-1) != '.') {
System.out.println(word);
word = in.next();
}
System.out.println(word.substring(0, word.length() - 1));
Q4. [Early exam] only one correct answer
cp ../lab1/Hello.java .Q4. [Later exam] only one correct answer
cp ~/lab1/Hello.java .
Q5 Solution in bold.
This is one of two common solutions, the other one involves an array
smaller by two, and shifting the index (e.g., position 0 stores how
many times we rolled 2, i.e., a 1 and 1).
public class DiceSimulator {
private Die die; // one or two Die objects ok here
private int numRolls
private int[] results;
// constructs a dice simulator using dice with the given number of sides
// pre: numSides >= 1
public DiceSimulator(int numSides) {
die = new Die(numSides);
numRolls = 0;
results = new int[2*numSides + 1];
}
// run the simulation for the given number of rolls of the pair of dice
// pre: numRolls >= 0
public void run(int numRolls) {
this.numRolls += numRolls;
for (int i = 0; i < numRolls; i++) {
int sum = die.roll() + die.roll();
results[sum]++;
}
}
// prints out the results of the simulation:
// shows for each of the values from 2 to 2*numSides,
// how many times the roll of the pair summed up to that value
// [see sample output for details of output format]
public void printResults() {
// to save you some typing... here's *part* of the code for printing
// the headers for the table of results
System.out.print("Results of rolling the dice ");
System.out.println(numRolls + " times:");
System.out.println("sum number of rolls");
for (int sum = 2; sum < results.length; sum++) {
System.out.println(sum + " " + results[sum]);
}
}
}
Q6.1 instance variables
Q6.2 parameters (methods also accepted)
Q7
When running this program, when a user resizes the window the
bar graph and labels will keep changing as the user drags the mouse.
Q8 [Early exam]
// PRE: values.length > 1 and each values[i] is >= 0
public static double avgExcludeHighest(int[] values) {
int max = 0; // or values[0]
int sum = 0;
for (int i = 0; i < values.length; i++) {
if (values[i] > max) {
max = values[i];
}
sum += values[i];
}
sum -= max;
int count = values.length - 1;
return sum / (double) count;
}
// PRE: scores.length > 1 and each scores[i] is >= 0
public static double avgExcludeLowest(int[] scores) {
int min = scores[0]; // or Integer.MAX_VALUE
int sum = 0;
for (int i = 0; i < scores.length; i++) {
if (scores[i] < min) {
min = scores[i];
}
sum += scores[i];
}
sum -= min;
int count = scores.length - 1;
return sum / (double) count;
}