Name: ______________________________
Choose 5 out of the 7 questions to answer. Cross out the questions you don't want to have graded.
Here are some useful classes, objects, and methods:
Arrays. Declare an array like int[] x = new int[5]
. Access the elements
using indexing like x[0]
(for the first element), x[1]
, ... Get the
length of the array by using the length
member: x.length
.
String
class
toCharArray()
returns a char
array containing each letter of the string
(in order)length()
returns the number of letters in the stringcharAt(i)
returns the char
at index i
(starting from 0)System
class
out
is a static member representing the standard output stream
println()
prints its input followed by a newlinein
is a static member representing the standard input stream
Scanner
class. new Scanner(System.in)
returns a scanner that
reads from standard input and converts to useful types
nextInt
, nextDouble
, etc. return the next number typedArrayList
class. ArrayList<T>
is a dynamic array that holds type T
.
add(x)
appends the value x
to the end of the arrayget(i)
returns the element at index i
set(i, value)
sets the value stored at index i
to value
size()
returns the size of the listMap
interface. Map<KeyType, ValueType>
represents a dictionary-like
data structure that consists of key-value pairs. HashMap
is an
implementation of the interface.
put(k, v)
inserts an entry mapping k
to v
, or updates the
entry with key k
to have new value v
if it already exists.get(k)
returns the value associated with key k
containsKey(k)
tells if a given key is in the mapsize()
returns the number of key-value pairs in the map.
Consider the following class that keeps track of the sum of all its
inputs (by inputs, I mean all values passed to calls to update
):
public class Summer {
private int sum = 0;
public void update(int x) {
sum = sum + x;
}
public int getSum() {
return sum;
}
}
Define a new class that keeps track of the sum of the squares of all
its inputs (e.g. with inputs 2,3,4
it should track 4+9+16
) that uses
an instance member of type Summer
.
Working with the class in #1, define a new class that keeps track of
the sum of the cubes of all its inputs. Do this by making the
new class a subclass of Summer
and overriding the update
method.
Consider the following new definition for a more general summation-like class:
interface Transform {
int calculate(int x);
}
public class Accumulator {
private int total = 0;
private Transform trans;
public Accumulator(Transform t) {
trans = t;
}
public void update(int x) {
total = total + trans.calculate(x);
}
public int getTotal() {
return total;
}
}
Define a class Abs
that implements the Transform
interface so that when
used with Accumulator
, getTotal
would return the sum of the absolute
values of all inputs. An example of use is shown below:
Accumulator acc = new Accumulator(new Abs());
acc.update(3);
acc.update(-2);
System.out.println(acc.getTotal()); // prints 5
What does the following program print?
public class Program {
interface Predicate {
boolean test(int x);
}
public static class Mystery implements Predicate {
public boolean test(int x) {
return x % 2 == 0;
}
}
public static class AllTrue implements Predicate {
public boolean test(int x) {
return true;
}
}
public static void printMatching(int[] array, Predicate p) {
for (int x : array) {
if (p.test(x))
System.out.println(x);
}
}
public static void main(String[] args) {
int[] a = {2,5,4,3,1,10};
printMatching(a, new AllTrue());
printMatching(a, new Mystery());
}
}
Fill in the method below so that it returns a Map<String, Integer>
that
maps the element at index i
in the first array to the element at index i
in the second. For example, with input {"a", "b"}
and {3, 4}
, the output
should be a map of the form {"a" → 3, "b" → 4}
. You can assume that both
inputs are the same size.
public static Map<String, Integer>
zip(ArrayList<String> keys, ArrayList<Integer> values) {
}
Define a class called PrintCounter
that implements the following
interface:
interface PrintNum {
void print(String x);
int numberPrinted(char c);
}
It should keep track of the number of each character printed. For example,
after calling print("abc")
and print("a-b")
, numberPrinted('a')
should
return 2
, numberPrinted('-')
should return 1
, and numberPrinted('z')
should return 0.
The constructor should accept an instance of PrintStream
that is used
for printing (System.out
is an instance of PrintStream
).
What does the following program print?
public class Test {
public static class Printer {
public void print(String x) {
if (x.charAt(0) == 'B')
throw new IllegalArgumentException("I don't print B words!");
System.out.println(x);
}
}
public static void test(Printer p, String message) {
try {
p.print(message);
} catch (IllegalArgumentException e) {
System.out.println("Error");
}
}
public static void main(String[] args) {
Printer out = new Printer();
test(out, "Antonioni");
test(out, "Bergman");
test(out, "Cocteau");
test(out, "Disney");
}
}