DOC PREVIEW
UNI CS 4550 - Objects and Responsibilities

This preview shows page 1-2-19-20 out of 20 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Computer Science II 810:062 Section 01 Session 2 - Objects and ResponsibilitiesThe anatomy of an OO programLast class we considered a MemoPad exampleObject-Oriented ProgramsMemoPad’s collaborating objectsMemoPad UML DiagramMemoPad’s ConstructorObjects interact by sending each other messagesFocus on MemoDatabase interfaceInterface for the memo databaseDefaultMemoDatabase classSlide 12private vs. public?Access ModifiersSo when do we use what?Slide 16Homework #1Lunar Lander ExampleSlide 19For next classComputer Science II810:062 Section 01Session 2 - Objects and ResponsibilitiesThe anatomy of an OO programThe world consists of objects that interact to solve a problem.Last class we considered a MemoPad exampleAllows for the storage of a collection of memos that are associated with keysObject-Oriented Programs•MemoPad models the piece of the world of interest. •main() is the “big bang” that creates one or more objects for this MemoPad “world”.public class MemoPadApp { public static void main( String [] args ) { MemoPad p = new MemoPad(); p.show(); } // end main} // end class MemoPadAppMemoPad’s collaborating objectsField PanelButton PanelMessage PanelMemoDatabasekey valueMemoAssociationMemoPad UML DiagramMemoPad’s Constructor public class MemoPad extends CloseableFrame { private MemoDatabase database; private FieldPanel fieldPanel; private ButtonPanel buttonPanel; private MessagePanel messagePanel; public MemoPad() { database = new DefaultMemoDatabase(); ... fieldPanel = new FieldPanel(); ... buttonPanel = new ButtonPanel( this ); ... messagePanel = new MessagePanel( "..No message..”); ... } // end MemoPad constructor ...Objects interact by sending each other messages// in MemoPad classpublic void find() { String value = database.find(fieldPanel.currentKey()); if ( value == null ) messagePanel.setMessage( "Key not found." ); else { fieldPanel.setValue( value ); messagePanel.setMessage( fieldPanel.currentKey() + " : " + value ); } // end if} // end findFocus on MemoDatabase interface•its interface is the set of messages to which it should respond•but to make an database object we need a class definition that adheres to the MemoDatabase interface, i.e., that implements the specified methods•here the DefaultMemoDatabase class is usedInterface for the memo database public interface MemoDatabase { public String find ( String key ); public boolean remove ( String key ); public boolean insert (MemoAssociation m); public boolean containsKey( String key );} // end interface MemoDatabaseDefaultMemoDatabase class import java.util.Hashtable;public class DefaultMemoDatabase implements MemoDatabase{ private Hashtable associations; public DefaultMemoDatabase() { associations = new Hashtable(); } // end DefaultMemoDatabase constructor public boolean insert( MemoAssociation newEntry ) { if ( containsKey( newEntry.key() ) ) return false; associations.put( newEntry.key(), newEntry.value() ); return true; } // end insert ...Group #1“Shawn” BancroftLucas BergmannPaul ChenZach ClarkDustin CollinsGroup #2Mathew DahmsTerrence GloverAlex HabegerMichael JaramilloGroup #3Tressa McCabeAngela McCauleyPaul NadlerTony SyndergaardBryan Zubrodprivate vs. public?•What things are declared private?•What things are declared public? •Why are they declared private?•Why are they declared public?Access Modifiers•A Java programmer can control access to a class and its parts (both data and actions) through the use of Access Modifiers.–Any item that is declared public can be accessed by any piece of code that knows the name of the item.–Any item that is declared private can be accessed only be code within the same class.So when do we use what?Because of this, we normally followed the following conventions --•Classes are usually declared public, because we want to create instances of the class that can help us solve a problem.•Methods are sometimes declared public, because they correspond to messages that other objects can send to the instance.•Instance variables are private, because we want the instance to have exclusive control over changes to their values.Access ModifiersBut what if want to do things a little differently? Could we have-- •a private class? –Sure. You might want to declare a private class because you don’t want just anyone to create instances of it. •a private method? –You might want to declare a private method, because it is not a part of the object’s public interface. Perhaps it is simply a helper to one of the public methods. •a public instance variable? –You might want to declare a public instance variable simply for your convenience as a programmer. BUT..Homework #1•Write a different class that implements the MemoDatabase using a different representation (e.g., array, ArrayList, vector, two arrays, ...)•Modify the MemoPad class to use your class.•Submission details to follow...Lunar Lander Example•Lander starts some initial distance above the surface of the moon with a limited amount of fuel•As time ticks off, the lander falls toward the moon, gaining speed according to the gravity of the moon.•The user can request to thrust the lander's engines which counteract the force of gravity and slow down the vehicle. However, this also burns fuel.•The user can request multiple burns in any time interval.•If the vehicle hits the surface traveling slow enough, then the user wins!Lunar Lander Example•What objects can you identify?•What behaviors must these objects perform?For next class•Read chapter 3. •Read over all the code for the MemoPadApp. Much of the GUI (buttons, etc.) stuff will not make sense, but it might give you a feel for the


View Full Document

UNI CS 4550 - Objects and Responsibilities

Download Objects and Responsibilities
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Objects and Responsibilities and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Objects and Responsibilities 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?