Computer Science II 810:062 Section 01 Session 2 - Objects and Responsibilities.

Slides:



Advertisements
Similar presentations
IMPLEMENTING CLASSES Chapter 3. Black Box  Something that magically does its thing!  You know what it does but not how.  You really don’t care how.
Advertisements

Software Engineering and Design Principles Chapter 1.
Introduction To System Analysis and Design
Copyright W. Howden1 Lecture 7: Functional and OO Design Descriptions.
1 Chapter 1 Object-Oriented Programming. 2 OO programming and design Object-oriented programming and design can be contrasted with alternative programming.
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 1 Introduction to Object-Oriented Programming and Software Development.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 1 Introduction to Object-Oriented Programming and Software Development.
Options for User Input Options for getting information from the user –Write event-driven code Con: requires a significant amount of new code to set-up.
Domain Modeling (with Objects). Motivation Programming classes teach – What an object is – How to create objects What is missing – Finding/determining.
Unified Modeling Language
16-Aug-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming in Java Topic : Interfaces, Copying/Cloning,
The chapter will address the following questions:
Object Oriented Software Development
1 Some Patterns of Novice Programs Author : Eugene Wallingford ; Dan Steinberg ; Robert Duvall ; Ralph Johnson Source : PLoP 2004 Advisor : Ku-Yaw Chang.
Object Oriented Analysis By: Don Villanueva CS 524 Software Engineering I Fall I 2007 – Sheldon X. Liang, Ph. D.
Practical Object-Oriented Design with UML 2e Slide 1/1 ©The McGraw-Hill Companies, 2004 PRACTICAL OBJECT-ORIENTED DESIGN WITH UML 2e Chapter 2: Modelling.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 12 Object-Oriented.
Chapter 3 Introduction to Collections – Stacks Modified
CSC 142 B 1 CSC 142 Java objects: a first view [Reading: chapters 1 & 2]
Java Classes Using Java Classes Introduction to UML.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Introduction To System Analysis and Design
Object-Oriented Analysis and Design An Introduction.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Computer Science II 810:062 Section 01. How is CS I different from CS II? When you teach Java there are a series of decisions that have to be made…
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
CPSC 102: Computer Science II Dr. Roy P. Pargas 408 Edwards Hall Office Hours 10:00-11:00 am MWF 2:00-3:00 pm TTh.
CET203 SOFTWARE DEVELOPMENT Session 1A Revision of Classes.
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.
P Chapter 2 introduces Object Oriented Programming. p OOP is a relatively new approach to programming which supports the creation of new data types and.
 A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
CS 4850: Senior Project Fall 2014 Object-Oriented Design.
Chapter 1 Data Structures and Algorithms. Primary Goals Present commonly used data structures Present commonly used data structures Introduce the idea.
CSC1401 Classes - 1. Learning Goals Computing concepts Identifying objects and classes Declaring a class Declaring fields Default field values.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Lecture 6: Structural Modeling
CSSE501 Object-Oriented Development. Chapter 4: Classes and Methods  Chapters 4 and 5 present two sides of OOP: Chapter 4 discusses the static, compile.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
Session 16 Pinball Game Construction Kit:. Pinball Version 1 Replaced the fire button with a mouse event. Multiple balls can be in the air at once. –Uses.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
HashCode() 1  if you override equals() you must override hashCode()  otherwise, the hashed containers won't work properly  recall that we did not override.
Chapter 17 – Object- Oriented Design. Chapter Goals To learn about the software life cycle To learn about the software life cycle To learn how to discover.
Designing Classes CS239 – Jan 26, Key points from yesterday’s lab  Enumerated types are abstract data types that define a set of values.  They.
Object-Oriented Principles Applications to Programming.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Why Use Classes - Anatomy of a Class.
© 2006 Pearson Addison-Wesley. All rights reserved 2-1 Chapter 2 Principles of Programming & Software Engineering.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP.
Session 6 Comments on Lab 3 & Implications of Inheritance.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Thomas Kuehne.
Inheritance Type/Subtype Relationship. Inheritance Idea: An object B of one type, termed child class, inherits from another object A of another type,
Data Design and Implementation. Definitions Atomic or primitive type A data type whose elements are single, non-decomposable data items Composite type.
Written by: Dr. JJ Shepherd
Session 7 Introduction to Inheritance. Accumulator Example a simple calculator app classes needed: –AdderApp - contains main –AddingFrame - GUI –CloseableFrame.
Computer Science II 810:062 Section 01 Session 2 - Objects and Responsibilities.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
OOP Basics Classes & Methods (c) IDMS/SQL News
Maitrayee Mukerji. INPUT MEMORY PROCESS OUTPUT DATA INFO.
Learning to Design Programs
More Sophisticated Behavior
Unified Modeling Language
Chapter 11 Object-Oriented Design
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Introduction to Data Structure
Presentation transcript:

Computer Science II 810:062 Section 01 Session 2 - Objects and Responsibilities

The anatomy of an OO program The world consists of objects that interact to solve a problem.

Last class we considered a MemoPad example Allows for the storage of a collection of memos that are associated with keys

Object-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 MemoPadApp

MemoPad’s collaborating objects Field Panel Button Panel Message Panel MemoDatabase keyvalue MemoAssociation

MemoPad UML Diagram

MemoPad’s Constructor public class MemoPad extends CloseableFrame { private MemoDatabase database; //Used interface type! private FieldPanel fieldPanel; private ButtonPanel buttonPanel; private MessagePanel messagePanel; public MemoPad() { // can assign any object that satisfies the interface 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 // later in MemoPad class public 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 find

Focus 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 used which stores MemoAssociations in a hashTable

Interface 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 MemoDatabase

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

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 at

How your understanding of private 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 Modifiers But 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..

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?

Learning to Design Programs You can learn to design programs by studying good and bad designs, and coming to understand why they are good and bad. But that isn’t enough: Do you think a violinist listens to music and reads a book and then steps onto stage to perform? Professional writers are always writing, so it's no wonder they are good. -- Richard Gabriel

Learning to Design Programs You also learn to design programs by designing programs and studying the results. –Good design comes from experience. –Experience comes from bad design.

Programming in the small and in the large What are some of the distinctions between these two concepts?

Object-Oriented Design Designing any system requires: 1.identifying the key components, or objects, in the system 2.identifying the main responsibilities of each object 3.identifying the main communication paths among them

Object-Oriented Design We focus on responsibility and behavior first because: we can understand the behavior of a system by looking at interactions among parts and users behavior makes no commitments to implementation designing to behavior promotes maximum reuse

Object-Oriented Design Two tools capture the three main features of a design: CRC cards Interaction diagrams

Object-Oriented Design CRC cards focus us on the who and the what and discourage the how. We design by decomposing responsibilities into small, cohesive, and well- defined sets. –When the responsibilities won’t fit on a CRC card, the set probably isn’t small enough. –When the responsibilities are not cohesive, we split off new objects. –When the responsibilities are not well-defined, we consider whether some other object should have some of the responsibilities. –Some advantages of CRC cards: low tech, right size, shuffleable, easy to throw away, etc.

A Blank CRC Card Class Name:Superclass:Subclasses: ResponsibilitiesCollaborations

Object-Oriented Design Interaction diagrams show us how different object collaborate to solve the problem.

Summary Designing a system requires: identifying the key components, or objects, in the system identifying the main responsibilities of each object drawing the main communication paths among them Together, CRC cards, class diagrams, and interaction diagrams document a design by recording these features. CRC document the who and the what and discourage the how. Class diagrams document communication paths. Interaction diagrams document the how.

For next class Read chapter 3.