Key to CSE142 Final Exam, Winter 2018 1. Solution: 44,22 [44, 77] 0 44,22 [44, 77] 55 22,22 77 2. Original Array Final Array ----------------------------------------------------------------- {1, 1, 3} [1, 2, 3] {2, 1, 2, 4} [2, 2, 3, 4] {6, 13, 0, 3, 7} [6, 3, 3, 5, 7] {-1, 6, 3, 5, -3} [-1, 1, 3, 0, -3] {7, 2, 3, 1, -3, 12} [7, 5, 3, 0, 6, 2] 3. Solution: Diamond Ruby 1 Emerald 2 Emerald Ruby 1 Emerald 2 Emerald Emerald 1 Emerald 2 Garnet Garnet 1 Emerald 2 4. Two possible solutions appear below. public static int censorNames(Scanner fileScan) { int names = 0; while (fileScan.hasNext()) { names++; System.out.print(fileScan.next() + " "); String lname = fileScan.next(); for (int i = 0; i < lname.length(); i++) { System.out.print("X"); } System.out.println(); } return names; } public static int censorNames(Scanner fileScan) { int names = 0; boolean first = true; while (fileScan.hasNext()) { String token = fileScan.next(); if (first) { System.out.print(token + " "); names++; } else { for (int i = 0; i < token.length(); i++) { System.out.print("X"); } System.out.println(); } first = !first; } return names; } 5. One possible solution appears below. public static int analyzeParagraphs(Scanner input) { int max = 0; while (input.hasNextLine()) { String line = input.nextLine(); int count = 0; while (!line.equals("
")) {
count++;
line = input.nextLine();
}
System.out.println(count + "-line paragraph");
if (count > max) {
max = count;
}
}
return max;
}
6. Two possible solutions appear below.
public static double[] getTotalValues(String sequence, double[] weights) {
String nucs = "ACGT";
double[] result = new double[4];
int currWeight = 0;
for (int i = 0; i < sequence.length(); i++) {
int index = nucs.indexOf(sequence.charAt(i));
if (index >= 0) {
result[index] += weights[currWeight];
currWeight++;
}
}
return result;
}
public static double[] getTotalValues(String sequence, double[] weights) {
double[] result = new double[4];
String noJunk = sequence.replace("-", "");
for (int i = 0; i < noJunk.length(); i++) {
int index = -1;
if (noJunk.charAt(i) == 'A') {
index = 0;
} else if (noJunk.charAt(i) == 'C') {
index = 1;
} else if (noJunk.charAt(i) == 'G') {
index = 2;
} else if (noJunk.charAt(i) == 'T') {
index = 3;
}
if (index >= 0) {
result[index] += weights[i];
}
}
return result;
}
7. Three possible solutions appear below.
public static void split(ArrayList