Quiz J3-2
Submit your answers via this Google Form
Question 1
Say we have the class Shelf
below.
class Shelf<T> {
List<T> shelfItems;
public Shelf() {
shelfItems = new ArrayList<T>();
}
int addItem(T item){
shelfItems.add(item);
return shelfItems.size()-1;
}
void printShelf(){
for(T item: shelfItems)
System.out.println(item.toString()+" ");
}
}
What is the output of this program?
public static void main(final String args[]) {
Shelf<int> favorite_numbers = new Shelf();
favorite_numbers.addItem(5);
favorite_numbers.addItem(10);
favorite_numbers.printShelf();
}
Question 2
Now we add a function to build shelves to Shelf
:
static Shelf<T> shelfBuilder(){
return new Shelf<T>();
}
What’s the output of this new program?
public static void main(final String args[]) {
Shelf<String> favorite_words = Shelf.shelfBuilder();
favorite_words.addItem("Zoetrope");
favorite_words.addItem("Succinct");
favorite_words.printShelf();
}
Question 3
Suppose we add a new method to Shelf
to retrieve an item by the index in which it was added:
class Shelf<T> {
List<T> shelfItems;
//... same as Question 1
T getItem(int idx){return shelfItems.get(i);}
}
Given the program below:
```java
public static void main(final String args[]) {
Shelf<String> favorite_words = shelfBuilder();
favorite_words.addItem("Zoetrope");
favorite_words.addItem("Succinct");
//...
String s = favorite_words.getItem(1);
System.out.println(s);
}
What does this method erase to when the program compiles and the generics is removed?
//Option one
public static void main(final String args[]) {
Shelf<String> favorite_words = shelfBuilder();
favorite_words.addItem((Object) "Zoetrope");
favorite_words.addItem((Object) "Succinct");
//...
String s = favorite_words.getItem(1);
System.out.println(s);
...
}
// Option two
public static void main(final String args[]) {
Shelf<Object> favorite_words = shelfBuilder();
favorite_words.addItem((Object) "Zoetrope");
favorite_words.addItem((Object) "Succinct");
//...
String s = favorite_words.getItem(1);
System.out.println(s);
}
// Option three
public static void main(final String args[]) {
Shelf favorite_words = shelfBuilder();
...
favorite_words.addItem( "Zoetrope");
favorite_words.addItem( "Succinct");
//...
String s = (String) favorite_words.getItem(1);
System.out.println(s);
}
// Option four
public static void main(final String args[]) {
Shelf favorite_words = shelfBuilder();
...
favorite_words.addItem((Object) "Zoetrope");
favorite_words.addItem((Object) "Succinct");
//...
String s = (String) favorite_words.getItem(1);
System.out.println(s);
}
Question 4
Finally consider this Fibonacci generator class:
class Fibonacci implements Iterable<Integer> {
int max;
public Fibonacci(int max) {
this.max = max;
}
public Iterator<Integer> iterator() {
return new FibonacciIter();
}
class FibonacciIter implements Iterator<Integer> {
int current = 1;
int previous = 0;
@Override
public boolean hasNext() {
return current+previous <= max;
}
@Override
public Integer next() {
int tmp = current;
current += previous;
previous = tmp;
return current;
}
}
}
What is the output of the below?
public static void main(final String args[]) {
Fibonacci fibonacci = new Fibonacci(13);
for (Integer i : fibonacci)
System.out.print(i+" ");
fibonacci.max = 21;
for (Integer i : fibonacci)
System.out.print(i + " ");
}