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

Slides:



Advertisements
Similar presentations
Sequence of characters Generalized form Expresses Pattern of strings in a Generalized notation.
Advertisements

Based on Java Software Development, 5th Ed. By Lewis &Loftus
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
Written by: Dr. JJ Shepherd
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.
What is an object? Your dog, your desk, your television set, your bicycle. Real-world objects share two characteristics: They all have state and behavior;
ECE122 L6: Problem Definition and Implementation February 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 6 Problem Definition and Implementation.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
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.
Unified Modeling Language
16-Aug-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming in Java Topic : Interfaces, Copying/Cloning,
Object Oriented Software Development
CSC 212 – Data Structures Lecture 3: Fields, Methods, & Constructors.
Programming Languages and Paradigms Object-Oriented Programming.
From Problem Statement to Design
Computer Science II 810:062 Section 01 Session 2 - Objects and Responsibilities.
Writing Classes (Chapter 4)
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
CSC 212 – Data Structures Lecture 12: Java Review.
Object Oriented Programming CSI 1101 Nour El Kadri.
CSC 142 B 1 CSC 142 Java objects: a first view [Reading: chapters 1 & 2]
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Session 9 CannonWorld, Event-driven Programming, and Border Layout.
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.
CS 11 java track: lecture 4 This week: arrays interfaces listener classes inner classes GUI callbacks.
Copyright © 2002, Systems and Computer Engineering, Carleton University Hashtable.ppt * Object-Oriented Software Development Unit 8.
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.
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.
Chapter 6 Object-Oriented Design. © 2004 Pearson Addison-Wesley. All rights reserved6-2 Object-Oriented Design Now we can extend our discussion of the.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
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.
Object-oriented Design and Programming CS 2210: SW Development Methods Reading: Chapter 2 of MSD text – Section on UML: look at class diagrams but.
Introduction to Generics
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
CSC 142 B 1 CSC 142 Java objects: a first view [Reading: chapters 1 & 2]
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP.
Session 13 Pinball Game Construction Kit (Version 3):
Computer Science 111 Fundamentals of Computer Programming I Working with our own classes.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Why Use Classes - Anatomy of a Class.
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.
Inheritance Type/Subtype Relationship. Inheritance Idea: An object B of one type, termed child class, inherits from another object A of another type,
Copyright (c) Systems and Computer Engineering, Carleton University * Object-Oriented Software Development Unit 13 The Collections Framework.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Written by: Dr. JJ Shepherd
Session 7 Introduction to Inheritance. Accumulator Example a simple calculator app classes needed: –AdderApp - contains main –AddingFrame - GUI –CloseableFrame.
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
David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 5: Implementing Data Abstractions.
Comp1004: Object Oriented Design I Abstract Classes and Interfaces.
Maitrayee Mukerji. INPUT MEMORY PROCESS OUTPUT DATA INFO.
Classes and Objects: Encapsulation
More Sophisticated Behavior
Unified Modeling Language
Chapter 3: Using Methods, Classes, and Objects
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
Outline Anatomy of a Class Encapsulation Anatomy of a Method
JAVA CLASSES.
CS2013 Lecture 7 John Hurley Cal State LA.
Dr. R Z Khan Handout-3 Classes
Corresponds with Chapter 5
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; 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 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

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

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

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