COM1204 Summer Quiz #1 review- Prof. Futrelle
Quiz #1 to be given on Thursday 3 July - Closed book/notes
There are three possible questions, of which only two will appear on the quiz.
The questions will be quite similar to the ones below, with minor variations
only.
First question: This involves writing Java code and discussing a bit
just how it works.
- Define an interface AnyYear containing a method currentYear() returning an int.
- Define a class ThisYear implementing AnyYear in which the method currentYear()
returns 2003.
- Define a class TestYear containing a main() that tests the above by
creating and instance of ThisYear and printing the value returned
by currentYear().
- Extra credit: Create a constructor for ThisYear that can be passed
a year as an argument that would then be the return value of currentYear().
The code for the above, which would appear in three separate .java files:
/**
* Omnibus demo code for COM1204 Quiz #1, summer 2003.
* R. P. Futrelle, Northeastern U. CCIS.
*/
public class TestYear {
public static void main(String[] args) {
ThisYear tyear = new ThisYear();
System.out.println(tyear.currentYear());
}
} // class TestYear
public interface AnyYear {
int currentYear();
} // interface AnyYear
public class ThisYear implements AnyYear{
public int currentYear() {
return 2003;
}
} // class ThisYear
Second question: This involves writing Java code and discussing a bit
just how it works.
- Create a class Iter with a main() that shows the use of the Iterator pattern,
using the Java Iterator class.
- In main(), create a HashSet hash with the constructor HashSet(), found
in java.util.HashSet.
- Also create a Vector vec with the constructor Vector(), in
java.util.Vector.
- Add "h1" and "h2" to hash with its add() method
and add "v1" and "v2" to vec with its add() method.
- Create an Iterator for both hash and vec using the iterator() method.
- Using a while loop with hasNext() and next() print the contents
of the two collections, hash and vec.
- IMPORTANT: Now explain why this is an example of a Design Pattern
and not just a bunch of code. Hint: The iteration is proceeding over
distinct Collection types. There are two patterns here, Iterator
and Factory.
The code for the above only requires a single file:
import java.util.*;
/**
* Demo code for COM1204 Quiz #1, summer 2003.
* R. P. Futrelle, Northeastern U. CCIS.
*/
public class Iter {
public static void main(String[] args) {
HashSet hash = new HashSet();
Vector vec = new Vector();
hash.add("h1");
hash.add("h2");
vec.add("v1");
vec.add("v2");
Iterator hashIt = hash.iterator();
while(hashIt.hasNext()) {
System.out.println(hashIt.next());
}
Iterator vecIt = vec.iterator();
while(vecIt.hasNext()) {
System.out.println(vecIt.next());
}
} // main()
} // class Iter
Third question: Given an example such as one of the following,
explain in what way they are examples of the Observer Pattern.
This requires you to clearly explain what the basics of the Observer Pattern
are, as well as interpreting the example in terms of the Observer Pattern.
This is a technical question. You should try to give a technical and concise
answer, not just vague remarks.
- Registering a product you purchased and getting a letter suggesting
you get a maintenance agreement for it.
- A person trying to arrange a meeting who gets updates from various
people on their availability and tells the others about those times.
- Getting email from a professor about the details of an exam.
- Sending a query to a mailing list about some topic.
- [and some better examples, as I think of them!]
Go to COM1204 home page.
Return to Prof. Futrelle's home page