Please download lab3.zip to get started.
Since the 1970s, computer users have been entertained by cookie-style fortunes. The original program can still be enjoyed today -- just type "fortune" on the command line.
This lab asks you to implement your own fortune-telling program. In the process you are introduced to linked lists.
The Nitty Gritty
The program consists of two classes:
- Fortune.java -- this is the class that models a single fortune
- FortuneCookie.java -- the main program.
- FCLinkedList.java -- A linked list class that will store the fortunes for the telling.
You'll need to complete the constructors, the getters, a toString(), an equals(), and a compareTo(). If you need a reminder about Comparables, please refer to the Java API documentation.
In order to offer the fortunes in a random order, you will generate an array of lucky numbers using the Random class. For example, if r is an instance of Random, r.nextInt(100), gives a random number between 0 and 99.
FCLinkedList is the meat of the assignment. We learned about LinkedLists this past week; now is the time to put all that knowledge into action. You should complete the constructor and size(), as well as add(), which adds a cookie at a specified position in the linked list
- peek(), lets the user "peek" at the fortune at the head of the list.
- poll(), does the same thing as peek(), but removes the cookie from the list as well
- getFortune(), "peeks" at a fortune at a specified position in the list (0th, 1st, 2nd, 3rd, ...)
- remove() "polls" a fortune at a specified position in the list
- toString() a canonical toString that returns a well-formatted String representation of the list
This class houses the main(). It basically handles all user interaction. It need not be elaborate. It should work from the command line and allow the user to
- add a fortune
- peek at a fortune
- look at a user-specified fortune in the list
- grab the first fortune from the top,
- remove a specific fortune at a user-specific index
- count the number of fortunes they have
- print out all the fortunes
- quit the fortune machine.
This is just a list of sample fortunes that you can use to create your fortune cookies with. Feel free to use different ones.
Plan of Attack