Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Assignment 5 - Object-Oriented Programming and Design | C SC 335, Assignments of Computer Science

Material Type: Assignment; Class: Object-Oriented Programming and Design; Subject: COMPUTER SCIENCE; University: University of Arizona; Term: Spring 2009;

Typology: Assignments

Pre 2010

Uploaded on 08/31/2009

koofers-user-fzl
koofers-user-fzl 🇺🇸

10 documents

1 / 4

Toggle sidebar

Related documents


Partial preview of the text

Download Assignment 5 - Object-Oriented Programming and Design | C SC 335 and more Assignments Computer Science in PDF only on Docsity! 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 none of the last eight Treats have been instances of FancyTreat, this method returns -4.  If four of the last eight Treats have been instances of FancyTreat, this method returns 0.  If all of the last eight Treats have been instances of FancyTreat, this method returns 4. 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 10. You should also add a getName()method to Owner. The Owner, as well as the iCritter that Owner has, should be initialized when ICritterFrame starts. ICritterFrame should open a JOptionPane or JDialogBox which will be responsible for setting Owner name, iCritter name, and iCritter type. The exact design is up to you. The View: Create a separate package in your workspace called view, and within that package, create an enum called ICritterUpdate. This enum defines what type of update an Observer should perform (provided that the Observer is aware of the ICritterUpdate class). It is very simple, and contains the following members/methods:  UPDATE_ICRITTER  UPDATE_OWNER  UPDATE_NONE If ProtoICritter calls notifyObservers(Object o), you should pass UPDATE_ICRITTER. If Owner calls notifyObservers(Object o), you should UPDATE_OWNER. Additionally, you should create a class in the view package called ICritterPanel. This class should be an extension of JPanel, should implement Observer, and should implement the following methods:  ICritterPanel( Owner theOwner )  This method creates all of the Swing components needed and combines them into the GUI. It should call updateICritterComponents() and updateOwnerComponents() to the appropriate components' values.  updateICritterComponents(ICritter theCritter)  This method updates all of the iCritter-related components in the GUI to reflect the data within the given 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 )  This method updates all of the owner-related components of the GUI to reflect the data within the given 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 )  The constructor should create an Owner instance from a string with your name. It should also set its layout to 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.  Remember, since ICritterFrame is a JFrame, you will need to call this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ) somewhere in this method if you want your program to exit when the window is closed.  Also, make sure to call this.pack() when you have finished adding all of your components.  main( String[] args )  Create a new ICritterFrame and make it visible in this method. ICritterFrame should also contain all of the ActionListener implementations that need to access ICritterFrame's Owner instance and any data within it.