Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.

Slides:



Advertisements
Similar presentations
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Four Defining Your Own Classes.
Advertisements

Chapter 2: Using Objects Part 1. To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about.
Lecture 2 Basics of C#. Members of a Class A field is a variable of any type that is declared directly in a class. Fields are members of their containing.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Road Map Introduction to object oriented programming. Classes
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
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.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Java Chapter 2 Creating Classes. Class Defines structure of an object or set of objects; Includes variables (data) and methods (actions) which determine.
1 Using Classes Object-Oriented Programming Using C++ Second Edition 5.
Using Classes Object-Oriented Programming Using C++ Second Edition 5.
Chapter 8 More Object Concepts
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
C++ for Engineers and Scientists Second Edition Chapter 6 Modularity Using Functions.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
The Java Programming Language
1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.
1 Introduction Modules  Most computer programs solve much larger problem than the examples in last sessions.  The problem is more manageable and easy.
Copyright © 2002, Systems and Computer Engineering, Carleton University a-JavaReview.ppt * Object-Oriented Software Development Unit.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Programming Logic and Design Using Methods. 2 Objectives Review how to use a simple method with local variables and constants Create a method that requires.
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.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Static Methods. 2 Objectives Look at how to build static (class) methods Study use of methods calling, parameters, returning values Contrast reference.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Chapter 11: Introduction to Classes. In this chapter you will learn about: – Classes – Basic class functions – Adding class functions – A case study involving.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Procedural programming in Java Methods, parameters and return values.
CSCI 1100/1202 April 1-3, Program Development The creation of software involves four basic activities: –establishing the requirements –creating.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Chapter 5 Introduction to Defining Classes
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
A First Book of ANSI C Fourth Edition
(C) 2010 Pearson Education, Inc. All rights reserved.  Best way to develop and maintain a large program is to construct it from small, simple pieces,
Programming Logic and Design Fifth Edition, Comprehensive Chapter 7 Using Methods.
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
Methods Chapter 6.
C++ for Engineers and Scientists Second Edition
Chapter 3 Introduction to Classes, Objects Methods and Strings
METHODS AND BEHAVIORS AKEEL AHMED.
Introduction to Classes
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 6 Methods: A Deeper Look
Static is one of the modifiers that determine variable and method characteristics. The static modifier associates a variable or method with its class.
Object Oriented Programming in java
Classes, Objects and Methods
SPL – PS3 C++ Classes.
Presentation transcript:

Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach

2 Method Development: Algorithms Algorithm  Step-by-step set of instructions  Describes how data are to be processed to produce desired result Must clearly understand difference between algorithmic and intuitive commands  Computers do not understand intuitive commands Coding the algorithm  Writing in programming language

3 Method Development: Algorithms (continued) Pseudocode  English-like phrases used to describe algorithm Formulas  Mathematical equations are used Flowcharts  Diagrams that employ symbols are used

4

5 Using Pseudocode Most commonly used method for developing algorithms Short English phrases Example:  Input three numbers into computer  Calculate average by adding numbers and dividing the sum by 3  Display average

6 Application: Swapping Values Swapping:  Exchanging data that are either stored or referenced by two variables  Commonly seen in programming Algorithm:  Store first variable’s value into temporary location  Store second variable’s value into first variable  Store temporary value into second variable

7 Application: Swapping Values (continued) How should two variables be made available to a method that will swap their values? Procedure:  Encapsulate two variables within a single object  Use class method to implement swap algorithm  Switch values in instance variables

8 static and final Variables So far, we’ve seen  local variable ( declared inside method)  instance variable ( declared inside class but outside method) Instance variable – term used because each instance of the class has its own version of the variable. Each object has its own memory place so it can have a distinct value for that class.

9 Static variable static variable (or class variable)  Created only once for each class  Shared by all objects created from class – only one copy of a static variable for all objects of the class  Changing the value of a static variable in one object changes it for all the other objects  Declared in same way as instance variables Except static reserved word is used in declaration Example static declaration:  private static int numEmployees = 125;

10

11 Static or Class Method Invoked through the class name – don’t have to instantiate an object of the class to invoke the method.  e.g. all method of the Math class are static methods so  Math.sqrt(16); or Math.pow(5,3); These methods perform basic calculations based on values passed as parameters. No object state to maintain in these situations.

12 Static Methods (cont.) Java main method must be static so that main can be executed without instantiating an object from the class containing main. Static methods do not operate in the context of a particular object. Can only reference static variables

13 Summary of static methods or class methods Provide means of accessing static variables in absence of any specific object Restricted to using:  Static variables  Other static methods  Additional values and objects that are passed as arguments into method Include static reserved word in method’s header

14 static Methods (continued) Can also be used for constructing general-purpose methods Perform functions using only data passed to them as arguments at time of call  i.e. main() method

15 Scope Section of program within which an identifier is valid Defines portion of class where variable can be used Scopes in Java:  Local  Class

16 Scope (continued) Local scope:  Variables declared within a method  All parameters of a method Class scope:  All instance and static variables declared in a class’s variable declaration section  All methods contained within a class’s method definition section

17 Visibility Determines whether a member can be accessed from outside a class in which it is declared Modifiers:  Public  Protected  Private Visibility rules

18

19 Values and Identities Identity  Every object has a unique identity Literal values do not have identities

20 final Variables final variables  Have initial value that cannot subsequently be changed  Also called: Named constants Symbolic constants Use final reserved word

21 final Variables (continued) Example:  public final static int NUMEMPS = 100; Blank final:  No initial value is provided  Value initialized later in program  final float DENSITY; Java convention:  Use uppercase letters for identifiers

22 Placement of Statements A variable must be declared before it can be used Proper placement: public class className { // declaration section final static variable declarations static variable declarations instance variable declarations continued …

23 Placement of Statements Proper placement: // methods section - example of one method method header line(parameter list) { final variable declarations variable declarations object declarations other Java statements }

24 Common Programming Errors Attempting to pass incorrect data types Declaring the same variable locally within both calling and called methods and assuming the change in one variable affects the other variable Forgetting to include the data type of a method’s parameters within the header line Attempting to alter an object’s private variables by passing an object into a non-class method as an argument

25 Summary A method is called by:  Giving its name  Passing any data to it in parentheses following the name A method’s return type declares the data type of the value returned by the method Methods can directly return at most a single value A called method cannot alter either a primitive data type argument’s value or a reference variable’s value

26 Summary (continued) Algorithm:  Step-by-step procedure  Describes how a single computation or task is to be performed Scope:  Section of a program within which an identifier is valid Static variables can be accessed without being referenced as a data member of a specific object