CSc335 (OO Design and Programming) Assignment 4 Spring 2008:5 Handed out: Thursday, February 14, 2008 Due: Wednesday February 20, 2008, by 10PM using Web Turnin In this assignment you will be extending a given body of code to include exceptions and learn to use packages. Some concepts: Packages: The ICritters code you have received will all be in its own package (icritters.base); you will be adding additional packages as listed in the sections below. ICritters learn about Time: Add a timestamp instance variable of type java.util.Calendar (with an appropriate getter) to ICritterMemoryEvent. This should be be set to the current time in the constructor of the ICritterMemoryEvent. The book has an example of using the Calendar class. Check the API as well for details. Additionally, every time a new ICritterMemoryEvent is added to an ICritter's memories, the collection should be sorted in descending order by the derived moodModifierDelta of the event. In the case of events having identical moodModifierDeltas, you should then sort by the timestamps (most recent first) of the events. Hint: make ICritterMemoryEvent implement Comparable, for use with Collections.sort. ICritterTolerance: To add realism to the ICritters system, you will implement tolerances (also known as limits) as to how many Treats the ICritter can consume in a given amount of time. This will make the process of an Owner keeping their ICritter happy more complex. You will be adding a new class, ICritterTolerance, part of the icritters.core package. The new class will have a Treat and maxTreatsPerMinute. The class should of course have the usual getters and setters that make sense. You should add an instance variable called iCritterTolerances to ICritter which will be a collection of ICritterTolerance items. iCritterTolerances should be initialized upon construction of the ICritter so as to contain one ICritterTolerance per type of Treat. All of the ICritterTolerance objects should be initialized to 5 for now. CSc335 (OO Design and Programming) Assignment 4 Spring 2008:5.2 After "processing" a treat (getting the reaction, etc.), the ICritter will check to see how many of those types of treats it has consumed in the last sixty seconds (it should have all the data in its memories, remember those timestamps)? If this number exceeds its tolerance for that type of treat, it will throw an ICritterToleranceException. To compare the types of the treats, you can use getClass(). i.e. if(treatOne.getClass() == treatTwo.getClass()) { // ...they match... } Since you will be throwing an exception from your code, you will also have to handle it, propagating it up the list of calling methods, up to the ICrittersCaller main method (discussed below), where you will write code to catch the Exception. ICritterToleranceException: This class will be in a new package (icritter.exceptions), and will be a subclass of java.lang.Exception. See the constructors for java.lang.Exception to see what to provide as constructors for this class. The message for the ICritterToleranceException should be something like: "Tolerance exceeded with a treat of type: ..." Feel free to add toString methods to the Treat classes to make them "prettier" upon being printed. ICrittersCaller: The ICrittersCaller will act as a simple testing program for the ICritter system. It should be in its own package (icritters.testing) and will do the following. It will contain a main method which, when run, first creates an Owner (which will create a default ICritter), and then loops forever (or until we get an exception) doing the following: iterate over all the different types of treats, buying each one and giving it to the ICritter. Care must be taken to maintain enough credits. Eventually an should be thrown. ICrittersCaller should catch it and print out a message indicating what happened and exit. DiscountedTreat: We've added a new treat: DiscountedTreat. Its cost will always be less than 5 credits, but the amount of the discount is random. Also, the programmer who coded this up wrote buggy calculation code that occasionally throws a java.lang.ArithmeticException. You should handle this in ICrittersCaller as well.