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

This page cannot be seen from the preview

Don't miss anything!

bg1
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 )
pf3
pf4

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

  1. 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.

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:

___hw5.zip Turn it in via web turnin (http://lectura.cs.arizona.edu/turnin) by 11:59 pm on the due date. **Failure to properly format the name of your assignment will merit a deduction of 3 points. Hints and Tips** To create a JLabel called myLabel with the image “my-icritter.jpg”, which is located in the top-level directory of your project, you should do this: JLabel myLabel = new JLabel(new ImageIcon(System.getProperty(“user.dir”)+“/my-icritter.jpg” )); Remember, keep any ActionListener implementations in ICritterFrame. We encourage you to be creative with any additional methods/members you will want to add to these implementations. After all, some of the ActionListeners somehow need to have access to the ICritterFrame's Owner instance. You can do this however you want, as long as it is in good form and it make sense. A good approach to assignments like this is to create the GUI first. Then, add functionality to the GUI components and the corresponding model classes one at a time until they all work correctly. If you get stuck on the GUI, have a look at the sample TicTacToe GUI provided in the notes as well as the section handouts.