Handout June 26, 2003
Interfaces
143 Sections AB/AE
Name:
True or False:
1. The following statements are legal, given the following definition:
public interface ICityEmployee{…}
public class Mayor implements ICityEmployee, IElectedEmployee{…}
a) An interface cannot have member variables
b) Variables, arguments and results can have interfaces as their type
c) ICityEmployee myMayor=new Mayor();
d) IElectedEmployee myMayor=new Mayor();
e) Mayor myMayor=new ICityEmployee();
f) IElectedEmployee myMayor=new IElectedEmployee();
g) Class Mayor needs to implement all methods declared in ICityEmployee
h) Class Mayor needs to implement all methods declared in ICityEmployee, but not all the ones declared in IElectedEmployee
i) Class Mayor can only have methods declared in ICityEmployee and IElectedEmployee
j) An object of class Mayor is a valid argument to the following funcion:
public double getEmployeeSalary(ICityEmployee employee)
k) An object of class Mayor is a valid argument to the following funcion:
public int getTermLength(IElectedEmployee employee)
l) Interfaces can have private methods
2. Consider the following problem:
You and your neighbor (by seat) are asked to design a chess game. You have procrastinated until the last minute and now it is imperative that you split the coding burden in order to finish on time. You need to:
Suggested Design (note many others are possible and valid):
Notice the commonality between the game pieces and define a common interface for these.
Provide a class Board that abstracts the physical represenation of the board.
Sample JavaDoc comment for a method—let’s take move(..) in interface Piece:
/**
* This method moves the piece from its current position to
* The position specified by the method parameters
* The piece needs to notify the board of its new position
* by calling updatePiecePosition
* @param newXPos the new row position of the piece
* @param newYPos the new column position of the piece
* @return true if the move is legal and the piece moved, false otherwise
*/