Designing a class.

Slides:



Advertisements
Similar presentations
1 Objects, Classes, and Packages “Static” Classes Introduction to Classes Object Variables and Object References Instantiating Objects Using Methods in.
Advertisements

Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Object-Oriented PHP (1)
Road Map Introduction to object oriented programming. Classes
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
Object Based Programming. Summary Slide  Instantiating An Object  Encapsulation  Inheritance  Polymorphism –Overriding Methods –Overloading vs. Overriding.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Object Oriented Programming Philosophy. Part 1 -- Basic Understanding & Encapsulation.
Software Life Cycle Requirements and problem analysis. –What exactly is this system supposed to do? Design –How will the system solve the problem? Coding.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
Creating Classes and Objects Chapter Microsoft Visual Basic.NET: Reloaded 1.
10-Nov-15 Java Object Oriented Programming What is it?
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Classes Definition A class is a data type whose variables are objects Object – a variable that has member functions as well the ability to hold.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
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)
Designing Classes CS239 – Jan 26, Key points from yesterday’s lab  Enumerated types are abstract data types that define a set of values.  They.
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.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
 Write a description (or use one provided)  Create an initial encapsulation  Refine the encapsulation  Identify helpful constructors  Think about.
Monday, Jan 27, 2003Kate Gregory with material from Deitel and Deitel Week 4 Questions from Last Week Hand in Lab 2 Classes.
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.
Unified Modeling Language (UML)
© 2004 Pearson Addison-Wesley. All rights reserved January 23, 2006 Creating Objects & String Class ComS 207: Programming I (in Java) Iowa State University,
1 Chapter 9 Introduction to Classes and Objects Program Development and Design Using C++, Third Edition.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Constructors and Destructors
Copyright © Jim Fawcett Spring 2017
More About Objects and Methods
COMPSCI 107 Computer Science Fundamentals
Static data members Constructors and Destructors
Topic 7 Introduction to Classes and Objects
Writing Classes We have talked about classes and objects.
10.2 Classes Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
More About Objects and Methods
Creating Objects & String Class
Agenda Warmup Lesson 2.5 (Ascii, Method Overloading)
Inheritance Often, software encapsulates multiple concepts for which some attributes/behaviors overlap E.g. A computer (role-playing) game has: Monsters:
Creating and Using Classes
Classes In C#.
Java Enter your code from FRQ to Shell
Corresponds with Chapter 7
Lecture 22 Inheritance Richard Gesick.
Outline Writing Classes Copyright © 2012 Pearson Education, Inc.
Learning Objectives Classes Constructors Principles of OOP
Advanced Java Programming
We’re slowly working our way towards classes and objects
More on Classes and Objects
Constructors and Destructors
Arrays .
Static is one of the modifiers that determine variable and method characteristics. The static modifier associates a variable or method with its class.
Java Classes Aliases & Null & This
Tonga Institute of Higher Education
COP 3330 Object-oriented Programming in C++
JAVA CLASSES.
CMSC202 Computer Science II for Majors Lecture 07 – Classes and Objects (Continued) Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
Java Programming Language
Java 1/31/2017 Back to Objects.
Creating and Using Classes
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

Designing a class

Kinds of classes Utility classes – Math (Do something) Object classes – ExpressionParser, Scanner (Are something) Hybrid classes – Integer (Are something but contain helpful utilities)

Steps Write a description (or use one provided) Create an initial encapsulation Refine the encapsulation Identify helpful constructors Identify helpful overloaded constructors Think about the need for private methods Identify helpful overloaded methods Identify class level attributes Identify class level behaviors

Description Should include what we know about the object data Should include what the object should be able to do If the description is not complete, you can’t create the class.

Initial Encapsulation Identify nouns and verb phrases in the description. Nouns are data. What data types? Which are the important nouns. Which ones are pertinent to the problem? Verbs are the methods. What parameters do they need? What do they need to return?

Money The money class will represent US Money amounts. Once created a particular Money object cannot change. We need to be able to add two Money objects to get a third, subtract two Money objects, multiply a money object by a value (such as .05) to get a new Money amount and divide a Money object by a value (such as 2.5). We must also be able to compare Money objects to determine which is larger, small or if they are the same. We must be able to display a Money object as normal dollars and cents. Money may only be a whole number of cents. In other words, Money object may be 3 dollars and 12 cents, but not 3 dollars and 12.5 cents.

Initial encapsulation On your whiteboards, begin writing the class. You should carefully think about the instance variables that you need. How will you internally store money values? (What does a Money object look like?) What methods are directly suggested by the description? (Don’t worry about constructors at this point). (What should a Money object do?)

Refinement Do the attributes really reflect the state of the object? Are there any that would be used in only one method? Are there any that represent derivations? If any of the data changes, would we need another method. For example, rather than storing derived data, perhaps we need a method for that.

Identify constructors How will we want to build new Money objects? What makes sense for the user of this class to do?

Identify any helpful overloaded constructors Should we have a default constructor? Not automatic if any other constructor is defined Should we have multiple explicit value constructors? Are there multiple ways of initializing data Are there methods that can help the constructor do it’s job? “Check” methods to check for validity Constructors can call other constructors. Stock.java CopyConDemo.java

Do we need any helper methods? These can be methods that would be “utility” methods to users of the class or could be a helper method for other methods in the class. Private if only used inside of the class; public if a utility.

Identify overloaded methods Different parameter types may have subtle differences. We may want to provide different interfaces if, for example, we have a similar function but want to pass in different parameters. Or, if we want to allow the possibility of different numbers of parameters (min method).

Variable length parameter lists May obviate the need to have overloaded methods in the case of cardinality (handle 2, handle 3, handle more). See VarargsDemo1.java

Class attributes? Helpful constants, special values Common values used across all objects Object counters What might we want to include that are “constant” money amounts?