- Here is one answer (boldface is what is needed for an answer).
// ScoresTest.java: Used for question 1 on Exam 2
public class ScoresTest {
public static void main (String[] args) {
final int SIZE = 50;
// Exam 2 part 1.a.
int[] scores = new int[SIZE];
// Exam 2 part 1.b.
int numScores = 0;
while(true) {
int newScore = Keyboard.readInt();
if (newScore < 0) break;
scores[numScores++] = newScore;
}
System.out.println("Part 1.b.: " + numScores);
printArray("Part 1.b.", scores, numScores);
// Exam 2 part 1.c.
int sumScores = 0;
for (int i = 0; i < numScores; i++)
sumScores = sumScores + scores[i];
System.out.println("Part 1.c.: sumScores: " + sumScores);
// Exam 2 part 1.d.
double averageScore =
(double)sumScores / (double)numScores;
System.out.println("Part 1.c.: averageScore: " + averageScore);
}
private static void printArray(String heading, int[] g, int limit) {
System.out.print(heading + ": Array nums: ");
for (int i = 0; i < limit; i++)
System.out.print(g[i] + " ");
System.out.println();
}
}
Finally, here is sample output, assuming that input is:
89 56 78 84 51 62 37 93 -1
Part 1.b.: 8
Part 1.b.: Array nums: 89 56 78 84 51 62 37 93
Part 1.c.: sumScores: 550
Part 1.c.: averageScore: 68.75
- Here is a sample program, with 2 answers to the second part:
// Exam2Question2: Question 2 of Exam 2
public class Exam2Question2 {
public static int linearSearch(int [] a, int value) {
for (int i = 0; i < a.length; i++)
if (a[i] == value) return i;
return -1;
}
public static boolean noZeros(int[] a) {
for (int i = 0; i < a.length; i++)
if (a[i] == 0) return false;
return true;
}
public static boolean noZeros2(int[] a) {
if (linearSearch(a, 0) == -1) return true;
else return false;
}
public static void main (String[] args) {
int[] nums = {89, 56, 78, 0, 84, 51, 62, 37, 93};
// Exam 2 part 2.a.
System.out.println("Part 2.a.: Search in nums for 37: " +
linearSearch(nums, 37));
// Exam 2 part 2.b.
System.out.println("Part 2.b.: Check for any zeros: " +
noZeros(nums));
// Exam 2 part 2.b., Second Version
System.out.println("Part 2.b.: Check for any zeros: " +
noZeros2(nums));
}
}
Here is the output:
Part 2.a.: Search in nums for 37: 7
Part 2.b.: Check for any zeros: false
Part 2.b.: Check for any zeros: false
Rerun without the 0 in the list of numbers above:
Part 2.a.: Search in nums for 37: 6
Part 2.b.: Check for any zeros: true
Part 2.b.: Check for any zeros: true
- Here is one answer (boldface is what is needed for an answer).
// StringTest.java: Used for Question 3 of Exam 2
public class StringTest {
public static void main (String[] args) {
// Exam 2 part 3.a.
String[] birds = {"rhea", "redbird", "robin",
"raven", "roadrunner", "raptor"};
printArray("Part 3.a.", birds);
// Exam 2 part 3.b.
System.out.print("Part 3.b.: Positions 0 and 3 are ");
if (birds[0].equals(birds[3]))
System.out.println("equal");
else
System.out.println("not equal");
// Exam 2 part 3.c.
String last = birds[0];
for (int i = 1; i < birds.length; i++)
if (last.compareTo(birds[i]) < 0) last = birds[i];
System.out.println("Part 3.c.: Last bird: " + last);
// Exam 2 part 3.c., Second Version
int index = 0;
for (int n = 1; n < birds.length; n++)
if (birds[n].compareTo(birds[index]) > 0) index = n;
System.out.println("Part 3.c.: Last bird: " + birds[index]);
}
private static void printArray(String heading, String[] b) {
System.out.print(heading + ": Array birds: ");
for (int i = 0; i < b.length; i++)
System.out.print(b[i] + " ");
System.out.println();
}
}
Here is sample output:
Part 3.a.: Array birds: rhea redbird robin raven roadrunner raptor
Part 3.b.: Positions 0 and 3 are not equal
Part 3.c.: Last bird: robin
Part 3.c.: Last bird: robin
- Here is one answer (boldface is what is needed for an answer).
(For a description of Chuck-a-Luck and calculation of odds,
see Chuck-a-Luck.)
// Dice.java: roll a single die
import java.util.*;
public class Dice {
private static Random ran = new Random();
public int roll() {
// Answer to Question 4.a. here
return ran.nextInt(6) + 1;
/* return (int)(6.0*ran.nextDouble() + 1.0); */
}
}
// ChuckaLuck.java: play the game Chuck-a-Luck
public class ChuckaLuck {
public static final int RUNS = 1000;
public static void main (String[] args) {
// create an instance of the Dice class
// Answer to Question 4.b. here
Dice dice = new Dice();
// create an array of four counters
// Answer to Question 4.c. here
int[] counters = new int[4];
// create an array to hold three rolls
// Answer to Question 4.c. here
int[] rolls = new int[3];
int numFives; // count the number of fives
// play Chuck-a-Luck RUNS many times
for (int i = 0; i < RUNS; i++) {
// Answer to Question 4.d. here
numFives = 0;
// roll the three dice
for (int j = 0; j < 3; j++) {
rolls[j] = dice.roll(); // don't really need to record rolls
if (rolls[j] == 5) numFives++;
}
// Answer to Question 4.e. here (given in code above)
// count the results
// Answer to Question 4.f. here
counters[numFives]++;
}
int totalMoney; // money won or lost
totalMoney = counters[0]*(-1) + counters[1]*1 +
counters[2]*2 + counters[3]*3;
for (int i = 0; i < 4; i++)
System.out.println(i + " Four(s) occured " + counters[i] + " times");
System.out.println("Total Money bet: $" + RUNS);
System.out.print("Total Money (won/lost): $" + Math.abs(totalMoney));
if (totalMoney < 0) System.out.println(" lost");
else System.out.println(" won");
System.out.println("Percent Loss: " + (double)totalMoney/RUNS*100 + "%");
}
}
Finally, here is sample output, for a number of runs, showing
one time when money was won:
0 Four(s) occured 589 times
1 Four(s) occured 330 times
2 Four(s) occured 73 times
3 Four(s) occured 8 times
Total Money bet: $1000
Total Money (won/lost): $89 lost
Percent Loss: -8.9%
=================================
0 Four(s) occured 583 times
1 Four(s) occured 348 times
2 Four(s) occured 63 times
3 Four(s) occured 6 times
Total Money bet: $1000
Total Money (won/lost): $91 lost
Percent Loss: -9.1%
=================================
0 Four(s) occured 576 times
1 Four(s) occured 343 times
2 Four(s) occured 77 times
3 Four(s) occured 4 times
Total Money bet: $1000
Total Money (won/lost): $67 lost
Percent Loss: -6.7%
=================================
0 Four(s) occured 543 times
1 Four(s) occured 372 times
2 Four(s) occured 81 times
3 Four(s) occured 4 times
Total Money bet: $1000
Total Money (won/lost): $3 won
Percent Loss: 0.3%
Here is a larger run with RUNS = 100000000
0 Four(s) occured 57859724 times
1 Four(s) occured 34726726 times
2 Four(s) occured 6949995 times
3 Four(s) occured 463555 times
Total Money bet: $100000000
Total Money (won/lost): $7842343 lost
Percent Loss: -7.842343%