CIS 17: Java Programming
Project E
Objective:
To create static factory methods, use them in lieu of constructors and upcast to an interface for the access of multiple implementations.
- The program will toss coins and/or dice and report the results.
- Create a Die and a Coin class that can each receive a toss() message. A single argument will determine the number of tosses. The tosses will, of course, result in random outcomes.
- These classes will implement a Tosser interface that declares the toss() method.
- Also create a TossFactory class that contains static methods returning references to instantiated Die or Coin objects.
- Be careful; avoid attempting to simply modify an example from the text. Close the book if you feel the temptation!
- You may use the following as a starting point for your work:
interface Tosser {/*...*/}
class Die implements Tosser {/*...*/}
class Coin implements Tosser {/*...*/}
class TossFactory {/*...*/}
public class TossGames {
public static void main(String[] args) {
Tosser t = TossFactory.getDie();
t.toss(10);
t = TossFactory.getCoin();
t.toss(5); }
}
- Here is a sample of the output from one run of the completed code:
Three
Three
Three
Three
Four
One
Four
Two
One
Three
Heads
Tails
Tails
Tails
Heads
Submit your work via the CATE form for Project E.
Legend: method/function keyword literal
2007/03/28