Defining Classes and Methods

Slides:



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

Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Functions ROBERT REAVES. Functions  Interface – the formal description of what a subprogram does and how we communicate with it  Encapsulation – Hiding.
The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been.
Chapter 41 Defining Classes and Methods Chapter 4.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Information Hiding and Encapsulation
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter 11: Classes and Data Abstraction
COMP 110 Information Hiding and Encapsulation Tabitha Peck M.S. February 25, 2008 MWF 3-3:50 pm Philips
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Java Classes Appendix C © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 05: Classes and Data Abstraction.
Slide 1 Chapter 6 Structures and Classes. Slide 2 Learning Objectives  Structures  Structure types  Structures as function arguments  Initializing.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
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.
COMP Information Hiding and Encapsulation Yi Hong June 03, 2015.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Defining Classes and Methods
Structures and Classes
More About Objects and Methods
Programming Logic and Design Seventh Edition
3 Introduction to Classes and Objects.
Java Programming: Guided Learning with Early Objects
Chapter 10: Void Functions
Chapter 3: Using Methods, Classes, and Objects
About the Presentations
COS 260 DAY 10 Tony Gauvin.
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 3 Introduction to Classes, Objects Methods and Strings
COMP 110 Information hiding and encapsulation
Defining Classes and Methods
Inheritance, Polymorphism, and Interfaces. Oh My
Introduction to Classes and Objects
Dynamic Data Structures and Generics
Object-Oriented Programming
Object Oriented Programming in java
Defining Classes and Methods
Defining Classes and Methods
Information Hiding and Encapsulation Section 4.2
Review for Midterm 3.
Classes and Objects Object Creation
Classes and Objects Systems Programming.
CMSC 202 Constructors Version 9/10.
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

Defining Classes and Methods Chapter 5 Part 2 Modified by JJ

The Steps! Define the Class Create the Instance Variables (attributes) Make their scope private Create the Constructors Default Parameterized for each attribute Create the Accessors for every attribute Create the Mutators for every attribute Create the other Methods Equals() and toString() are both good ones to have Profit!

Information Hiding, Encapsulation: Outline Pre- and Postcondition Comments The public and private Modifiers Methods Calling Methods Encapsulation Automatic Documentation with javadoc UML Class Diagrams

Information Hiding Programmer using a class method need not know details of implementation Only needs to know what the method does Information hiding: Designing a method so it can be used without knowing details Also referred to as abstraction Method design should separate what from how

The public and private Modifiers Type specified as public Any other class can directly access that object by name Classes generally specified as public Instance variables usually not public Instead specify as private

Accessor and Mutator Methods When instance variables are private must provide methods to access values stored there Typically named getSomeValue Referred to as an accessor method Must also provide methods to change the values of the private instance variable Typically named setSomeValue Referred to as a mutator method

Encapsulation Consider example of driving a car We see and use break pedal, accelerator pedal, steering wheel – know what they do We do not see mechanical details of how they do their jobs Encapsulation divides class definition into Class interface Class implementation

Encapsulation A class interface A class implementation Tells what the class does Gives headings for public methods and comments about them A class implementation Contains private variables Includes definitions of public and private methods

Programmer who uses the class Encapsulation Figure 5.3 A well encapsulated class definition Programmer who uses the class

Encapsulation Preface class definition with comment on how to use class Declare all instance variables in the class as private. Provide public accessor methods to retrieve data Provide public methods manipulating data Such methods could include public mutator methods. Place a comment before each public method heading that fully specifies how to use method. Make any helping methods private. Write comments within class definition to describe implementation details.

Automatic Documentation javadoc Generates documentation for class interface Comments in source code must be enclosed in /** */ Utility javadoc will include These comments Headings of public methods Output of javadoc is HTML format

Pre- and Postcondition Comments Precondition comment States conditions that must be true before method is invoked Example

Pre- and Postcondition Comments Tells what will be true after method executed Example

UML Class Diagrams Recall Figure 5.2 A class outline as a UML class diagram

UML Class Diagrams Note Figure 5.4 for the Purchase class Minus signs imply private access Plus signs imply public access

UML Class Diagrams Contains more than interface, less than full implementation Usually written before class is defined Used by the programmer defining the class Contrast with the interface used by programmer who uses the class

Objects and References: Outline Variables of a Class Type Defining an equals Method for a Class Boolean-Valued Methods Parameters of a Class Type

Variables of a Class Type All variables are implemented as a memory location Data of primitive type stored in the memory location assigned to the variable Variable of class type contains memory address of object named by the variable

Variables of a Class Type Object itself not stored in the variable Stored elsewhere in memory Variable contains address of where it is stored Address called the reference to the variable A reference type variable holds references (memory addresses) This makes memory management of class types more efficient

Variables of a Class Type Figure 5.5a Behavior of class variables

Variables of a Class Type Figure 5.5b Behavior of class variables

Variables of a Class Type Figure 5.5c Behavior of class variables

Variables of a Class Type Figure 5.5d Behavior of class variables

Variables of a Class Type Figure 5.6a Dangers of using == with objects

Variables of a Class Type Figure 5.6b Dangers of using == with objects

Defining an equals Method As demonstrated by previous figures We cannot use == to compare two objects We must write a method for a given class which will make the comparison as needed

Boolean-Valued Methods Methods can return a value of type boolean Use a boolean value in the return statement Note method from listing 5.19

Unit Testing A methodology to test correctness of individual units of code Typically methods, classes Collection of unit tests is the test suite The process of running tests repeatedly after changes are make sure everything still works is regression testing

Parameters of a Class Type When assignment operator used with objects of class type Only memory address is copied Similar to use of parameter of class type Memory address of actual parameter passed to formal parameter Formal parameter may access public elements of the class Actual parameter thus can be changed by class methods

Summary Classes have Instance variables should be private Instance variables to store data Method definitions to perform actions Instance variables should be private Class needs accessor, mutator methods Methods may be Value returning methods Void methods that do not return a value

Summary Keyword this used within method definition represents invoking object Local variables defined within method definition Formal arguments must match actual parameters with respect to number, order, and data type Formal parameters act like local variables

Summary Parameter of primitive type initialized with value of actual parameter Value of actual parameter not altered by method Parameter of class type initialized with address of actual parameter object Value of actual parameter may be altered by method calls A method definition can include call to another method in same or different class

Summary Precondition comment states conditions that must be true before method invoked Postcondition comment describes resulting effects of method execution Utility program javadoc creates documentation Class designers use UML notation to describe classes Operators = and == behave differently with objects of class types (vs. primitive types)

Summary Designer of class should include an equals method