This is a practice test. The actual test may contain fewer or more questions. The actual test may or may not supply you with other materials (such as starter code).
Implement the utility class described by this API. You do not have to include javadoc comments.
package test1;
import java.util.ArrayList;
import java.util.List;
public class Pratice1B {
private Pratice1B() {
}
public static Integer last(List<Integer> t) {
return t.get(t.size() - 1);
}
public static int totalArea(List<Integer> widths, List<Integer> heights) {
if (widths.size() != heights.size()) {
throw new IllegalArgumentException();
}
int area = 0;
for (int i = 0; i < widths.size(); i++) {
if (widths.get(i) > 0 && heights.get(i) > 0) {
area += widths.get(i) * heights.get(i);
}
}
return area;
}
public static int alternatingSum(List<Integer> t) {
int sum = 0;
int multiple = 1;
for (Integer i : t) {
sum += multiple * i;
multiple *= -1;
}
return sum;
}
public static List<Integer> encode(List<Integer> t) {
List<Integer> rle = new ArrayList<Integer>();
int count = 1;
int prev = t.get(0);
if (t.size() > 1) {
for (int i = 1; i < t.size(); i++) {
int digit = t.get(i);
if (digit == prev) {
count++;
} else {
rle.add(count);
rle.add(prev);
count = 1;
prev = digit;
}
}
}
rle.add(count);
rle.add(prev);
return rle;
}
}
Create a text file named answers.txt (use File->New->
Untitled text file in eclipse). Type your answer to the following question in the text file.