Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Note: These answers have not yet been updated to 5.0.Questions
Question 1: What methods would a class that implements thejava.util.Iterator
interface have to implement?
Answer 1:next
,hasNext
, andremove
Question 2: What is wrong with the following interface?
Answer 2: It has a method implementation in it. It should just have a declaration.public interface SomethingIsWrong { public void aMethod(int aValue) { System.out.println("Hi Mom"); } }Question 3: Fix the interface in Question 2.
Answer 3:public interface SomethingIsWrong { public void aMethod(int aValue); }Question 4: Is the following interface valid?
Answer 4: Yes. Methods are not required. Empty interfaces can be used as types and to mark classes without requiring any particular method implementations. For an example of a useful empty interface, seepublic interface Marker { }java.io.Serializable.
Exercises
Exercise 1: Write a class that implements the Iterator interface found in thejava.util
package. The ordered data for this exercise is the thirteen cards in a suit from a deck of cards. The first call to next returns2
, the subsequent call returns the next highest card,3
, and so on, up toAce
. Write a small main method to test your class.
Answer 1: SeeIteratorDemo.java
Exercise 2: Suppose that you have written a time server, which periodically notifies its clients of the current date and time. Write an interface that the server could use to enforce a particular protocol on its clients.
Answer 2: SeeTimeClient.java
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.