Amazon Interview Question
Software Engineer / DevelopersWe can have a an interface called ICard
public interface ICard extends Comparable
{
public static final int SPADES = 0;
public static final int HEARTS = 1;
public static final int DIAMONDS = 2;
public static final int CLUBS = 3;
public int getSuit();
public int getRank();
}
also we can have a deck class
public class Deck implements Iterator
{
private ArrayList myCardList;
private int myIndex;
public Deck()
{
myCardList = new ArrayList();
for(int suit = ICard.SPADES; suit <= ICard.CLUBS; suit++)
{
for (int rank = 1; rank <= 13; rank++)
{
myCardList.add(new Card(suit,rank));
}
}
shuffle();
}
private void shuffle()
{
Collections.shuffle(myCardList);
myIndex = 0;
}
public boolean hasNext()
{
return myIndex < myCardList.size();
}
public Object next()
{
ICard card = (ICard) myCardList.get(myIndex);
myIndex++;
return card;
}
public void remove()
{
throw new UnsupportedOperationException();
}
}
it looks like we need to use inheritance to acheive this, where in you have a superclass with generic functionality and a long list of subclassess overriding the generic methods of super class to provide specific behaviour.
- Raghu December 14, 2010