Designing Classes CS239 – Jan 26, 2006. Key points from yesterday’s lab  Enumerated types are abstract data types that define a set of values.  They.

Slides:



Advertisements
Similar presentations
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Advertisements

1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
CSCI 1100/1202 April 3, Testing A program should be executed multiple times with various input in an attempt to find errors Debugging is the process.
Road Map Introduction to object oriented programming. Classes
Object Oriented Design and UML
VBA Modules, Functions, Variables, and Constants
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
ECE122 L17: Method Development and Testing April 5, 2007 ECE 122 Engineering Problem Solving with Java Lecture 17 Method Development and Testing.
Classes in C++ Bryce Boe 2012/08/15 CS32, Summer 2012 B.
1 Object Oriented Design and UML Software Development Activities Object Oriented Design Unified Modeling Language (UML) Reading for this Lecture: L&L 6.1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Writing Classes (Chapter 4)
Chapter 5 - Writing a Problem Domain Class Definition1 Chapter 5 Writing a Problem Domain Class Definition.
An Object-Oriented Approach to Programming Logic and Design
CSCI-383 Object-Oriented Programming & Design Lecture 13.
APCS Java AB 2004 Review of CS1 and CS2 Review for AP test #1 Sources: 2003 Workshop notes from Chris Nevison (Colgate University) AP Study Guide to go.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
Object-Oriented Programming. An object is anything that can be represented by data in a computer’s memory and manipulated by a computer program.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
CSSE501 Object-Oriented Development. Chapter 4: Classes and Methods  Chapters 4 and 5 present two sides of OOP: Chapter 4 discusses the static, compile.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
© 2004 Pearson Addison-Wesley. All rights reserved September 12, 2007 Encapsulation ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
CSCI 1100/1202 April 1-3, Program Development The creation of software involves four basic activities: –establishing the requirements –creating.
CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) * Acknowledgement:
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
1 OOP - An Introduction ISQS 6337 John R. Durrett.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
Final Review. From ArrayLists to Arrays The ArrayList : used to organize a list of objects –It is a class in the Java API –the ArrayList class uses an.
1 Object-Oriented Design Now we can extend our discussion of the design of classes and objects Chapter 6 focuses on: software development activities determining.
SEEM Java – Basic Introduction, Classes and Objects.
OOP Review CS 124.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
 Write a description (or use one provided)  Create an initial encapsulation  Refine the encapsulation  Identify helpful constructors  Think about.
© 2004 Pearson Addison-Wesley. All rights reserved October 31, 2007 Static Class Members ComS 207: Programming I (in Java) Iowa State University, FALL.
A High Flying Overview CS139 – Fall 2006 How far we have come.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
A High Flying Overview CS139 – Fall 2010 How far we have come.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
CSE 501N Fall ‘09 06: Data Abstraction & Design
More About Objects and Methods
Object-Oriented Programming & Design Lecture 14 Martin van Bommel
About the Presentations
CS139 – Fall 2010 How far we have come
Object Based Programming
Chapter 5 – Writing Classes
Static Class Members March 29, 2006 ComS 207: Programming I (in Java)
Defining Classes and Methods
Java Inheritance.
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Designing a class.
Presentation transcript:

Designing Classes CS239 – Jan 26, 2006

Key points from yesterday’s lab  Enumerated types are abstract data types that define a set of values.  They form a java “collection”, so a for each loop can be used if we want to run through all of the values.  They can be used to declare new variables, which can only take on the values defined by the enum type.

Questions from the worksheet

Designing Classes  Context  Class Design process  Practice with a small class

Identifying Classes and Objects  The core activity of object-oriented design is determining the classes and objects that will make up the solution  The classes may be part of a class library, reused from a previous project, or newly written  One way to identify potential classes is to identify the objects discussed in the requirements  Objects are generally nouns, and the services that an object provides are generally verbs

Identifying Classes and Objects  A partial requirements document: The user must be allowed to specify each product by its primary characteristics, including its name and product number. If the bar code does not match the product, then an error should be generated to the message window and entered into the error log. The summary report of all transactions must be structured as specified in section 7.A. Of course, not all nouns will correspond to a class or object in the final solution

Identifying Classes and Objects  Remember that a class represents a group (classification) of objects with the same behaviors  Generally, classes that represent objects should be given names that are singular nouns  Examples: Coin, Student, Message  A class represents the concept of one such object  We are free to instantiate as many of each object as needed

Identifying Classes and Objects  Sometimes it is challenging to decide whether something should be represented as a class  For example, should an employee's address be represented as a set of instance variables or as an Address object  The more you examine the problem and its details the more clear these issues become  When a class becomes too complex, it often should be decomposed into multiple smaller classes to distribute the responsibilities

Identifying Classes and Objects  We want to define classes with the proper amount of detail  For example, it may be unnecessary to create separate classes for each type of appliance in a house  It may be sufficient to define a more general Appliance class with appropriate instance data  It all depends on the details of the problem being solved

Identifying Classes and Objects  Part of identifying the classes we need is the process of assigning responsibilities to each class  Every activity that a program must accomplish must be represented by one or more methods in one or more classes  We generally use verbs for the names of methods  In early stages it is not necessary to determine every method of every class – begin with primary responsibilities and evolve the design

Defining the class  Write an abstract (textual) description  Create an initial encapsulation  Refine the encapsulation  Identify constructors  Think about the need for private methods  Identify helpful overloaded methods  Identify helpful overloaded constructors  Identify class attributes  Identify class behaviors

Example You are working on a banking system. You have been given the following description of Money objects for the new system. Money will be represented as dollars and cents with cents never exceeding 99. Money can be increased or decreased by other Money amounts. Money can be negative or positive values. When displayed, money will be displayed in the usual way ex. $ and if negative $ Money amounts can be compared.

Initial Encapsulation – Specs  Nouns-attributes  Verbs-behaviors/methods Money will be represented as dollars and cents with cents never exceeding 99. Money can be increased or decreased by other Money amounts. Money can have negative or positive values. When displayed, money will be displayed in the usual way ex. $ and if negative $ Money amounts can be compared.

Initial Encapsulation  Attributes should be private  Accessor (get) methods  Mutator (set) methods  Other methods to take care of behaviors.

Initial encapsulation  In your group:  Determine your attributes and the type of each – How will you represent money?  Determine how you will deal with the negative amounts.  Then list the method headers which will represent the listed behaviors.

How many attributes do you have? Refine the attributes  Attributes should to define essential characteristics of the objects.  Local variables should be used for other types of data.

You should have 4 methods defined  What are they?  What is passed to the method?  What is returned from the method?

Refining the methods  Look for similar behaviors.  Are you increasing and decreasing as completely separate tasks. What is similar about them. Can you write one as a variant of another?  Did you build multiple methods for compare?  Is your display written as toString? Most classes should have one providing a “view” of the object.

Refining the methods  Look for missing behavior  Is a method complex and can/should be broken down into more than one method?  Is there something else within the scope of the definition that may be needed?

Constructors  What would a basic constructor look like?  What will it do to insure that all values are set correctly?

Private – helper methods  Is there anything that might help make the process of building and manipulating Money objects easier?  Is there any process that we don’t want to directly provide to users?

Are there methods that we might overload for different variants  What if we want to increase by a Money amount or simply by a multiplier?  Can these overloaded methods use one another?  (Java will use automatic type conversion if it cannot find an exact match but a similar match – think Math.pow(3,2);) You may not have to build every variant.

Overloaded constructors  Often use a default and an explicit value.  What kind of constructors would be appropriate to Money?

Any class attributes?  Typically, constants, special values.  Object counters.  Are there any appropriate to this class?

And are there any class behaviors?  Services that don’t work with a specific object.  Functions that return class attribute values or methods that manipulate class attribute values.  Utilities (think PA1).  Conversions  Factory methods (build objects of that type) – static but return an object of the class type.