


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Assignment; Class: Object-Oriented Programming and Design; Subject: COMPUTER SCIENCE; University: University of Arizona; Term: Spring 2009;
Typology: Assignments
1 / 4
This page cannot be seen from the preview
Don't miss anything!



C SC 335 (OO Design and Programming) Assignment 5 2009 Assignment 5: iCritter, part 3 Objective: Learning Swing Assigned: 2/17/2009 in class Due: 2/26/2009, 11:59 pm Overview In this phase of iCritter, we will be focusing on the iCritter GUI. For this assignment, you will be using a wide variety of basic Swing constructs to provide a user with ways of interacting with his/her iCritter, as well as ways for the iCritter to communicate its status back to the user. Additionally, you will keep track of relevant owner data, such as the number of credits and the types of treats the owner currently has available. Design This assignment will make use of the Model-View-Controller and the Observer/Observable design patterns. Recall that we attempted to make a package called “base” in the previous assignment. We will be making that package. Refer to the Final UML about which package each class belongs in. This code will serve as the Model part of the iCritter program. We have merged the last two iCritter coding assignments into a single codebase for you. However, you will need to make the following modifications to it: The Model: ProtoICritter You should modify ProtoICritter to extend Observable, and you should modify the following methods to notify any Observer instances of data changes: addFriend( ICritter other ) removeFriend( ICritter other ) addInterest( String interest ) removeInterest( String interest ) addMemoryEvent( Treat aTreat, ICritterReaction reaction ) For these methods, you should use the notifyObservers(Object o)method. You should pass a proper instance of view.ICritterUpdate as the argument (see below). Additionally, you should add getHappiness() to ICritterness. This method will return an integer determining the happiness of the iCritter. For now, “happiness” is determined by the quality of the treats you have given it. This is defined as (-4 + <the number of treats in the last 8 Treats which have been an instance of FancyTreat>). For example:
If the iCritter has not yet consumed eight treats, then this method still calculates the happiness based on how many fancy treats it has consumed so far. The return values are the same, except that the heuristic will consider every treat it has been given instead of only considering the last eight. Order does not matter for either evaluation. Owner You should modify Owner to extend Observable, and you should modify the following methods to notify any Observer instances of data changes: giveTreat( Treat aTreat ) adjustCredits( Integer amount ) addTreat( Treat aTreat )
As with ProtoICritter, you should use the notifyObservers(Object o)method, passing a proper instance of ICritterUpdate as the argument. Also, we want you to modify the Owner constructor to take a string as an argument. The string should be the owner's name (you will want to store this in the Owner class) Also modify the constructor to give an Owner instance 30 credits instead of
updateICritterComponents() and updateOwnerComponents() to the appropriate components' values. updateICritterComponents(ICritter theCritter)
iCritter. It should be called from the update() method inherited from Observer, and even then it should only be called if the argument passed to update() is IcritterUpdate.UPDATE_ICRITTER. updateOwnerComponents( Owner theOwner )
Owner instance. It should be called from the update() method inherited from Observer, and even then it should only be called if the argument passed to update() is ICritterUpdate.UPDATE_OWNER. The Controller: Create a class in the view package called ICritterFrame that extends JFrame. This will act as our Controller. It should implement the following methods: ICritterFrame( String OwnerName )
BorderLayout and should add a new instance of ICritterPanel to itself at the location BorderLayout.CENTER. You should pass the Owner instance to the new ICritterPanel instance.
this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ) somewhere in this method if you want your program to exit when the window is closed.
main( String[] args )
ICritterFrame should also contain all of the ActionListener implementations that need to access ICritterFrame's Owner instance and any data within it.
b. This JLabel should contain a string with the number of credits the Owner has. c. This should be a JList, and should list all of the treats the Owner currently has. The list should have an entry called “Fancy Treat” for each FancyTreat instance in the Owner's treat list, and it should have an entry called “Cheap Treat” for each CheapTreat instance in the Owner's treat list. d. This entry should be a JButton called with the label “Give Treat”. This button should have an ActionListener implementation added to it, so that when the button is clicked, the ActionListener should see which treat is highlighted in the JList, remove that treat entry from the JList, and call Owner.giveTreat() on the corresponding treat in the Owner's treat list. e. This should be a JButton with the label “Buy Fancy Treat.” This button should have an ActionListener added to it, so that when the button is clicked, the ActionListener calls Owner.buyFancyTreat(). f. This should be a JButton with the label “Buy Cheap Treat.” This button should have an ActionListener added to it, so that when the button is clicked, the ActionListener calls Owner.buyCheapTreat(). ICritterPanel should have a BorderLayout layout manager. It should have item 2 at BorderLayout.NORTH, item 3 at BorderLayout.CENTER, item 4 at BorderLayout.WEST, and item 5 at BorderLayout.SOUTH. ICritterPanel should be the JPanel contained by ICritterFrame. Turn-in Create a. zip archive of your project workspace and name it according to this format: