Object Oriented Methods : Deeper Look Lecture Three

Similar documents
CS110D: PROGRAMMING LANGUAGE I

IT 374 C# and Applications/ IT695 C# Data Structures

Methods: A Deeper Look

Java Methods. Lecture 8 COP 3252 Summer May 23, 2017

Methods (Deitel chapter 6)

Methods (Deitel chapter 6)

CS110: PROGRAMMING LANGUAGE I

Lecture 04 FUNCTIONS AND ARRAYS

Lecture 5: Methods CS2301

Chapter 5 - Methods Prentice Hall, Inc. All rights reserved.

CS-201 Introduction to Programming with Java

CS-201 Introduction to Programming with Java

Methods: A Deeper Look

Function Call Stack and Activation Records

C Functions. 5.2 Program Modules in C

C++ How to Program, 9/e by Pearson Education, Inc. All Rights Reserved.

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

C++ Programming Lecture 11 Functions Part I

Chapter 5 Methods. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Functions. Systems Programming Concepts

Introduction to Programming Using Java (98-388)

Module 10A Lecture - 20 What is a function? Why use functions Example: power (base, n)

To define methods, invoke methods, and pass arguments to a method ( ). To develop reusable code that is modular, easy-toread, easy-to-debug,

C Programming for Engineers Functions

Zheng-Liang Lu Java Programming 45 / 79

Functions and an Introduction to Recursion Pearson Education, Inc. All rights reserved.

COP3502 Programming Fundamentals for CIS Majors 1. Instructor: Parisa Rashidi

Chapter 5 Methods. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG

More About Objects and Methods. Objectives. Outline. Chapter 6

More About Objects and Methods

Chapter 5 Methods. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Chapter 6: Methods. Objectives 9/21/18. Opening Problem. Problem. Problem. Solution. CS1: Java Programming Colorado State University

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

Special Section: Building Your Own Compiler

Full file at

More About Objects and Methods

In this chapter you ll learn:

Lecture 7: Classes and Objects CS2301

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Programming in C++ Indian Institute of Technology, Kharagpur

Announcements. PS 3 is due Thursday, 10/6. Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am

PRIMITIVE VARIABLES. CS302 Introduction to Programming University of Wisconsin Madison Lecture 3. By Matthew Bernstein

Chapter 6 Methods. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. Pearson Education Limited

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

Lecture 04 FUNCTIONS AND ARRAYS

A variable is a name for a location in memory A variable must be declared

Functions and Recursion

CS1150 Principles of Computer Science Methods

CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II

Opening Problem. Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 6: User-Defined Functions I

CS 302: Introduction to Programming

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 4

Lecture #6-7 Methods

A Foundation for Programming

Opening Problem. Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.

Methods. CSE 114, Computer Science 1 Stony Brook University

19 Much that I bound, I could not free; Much that I freed returned to me. Lee Wilson Dodd

6.096 Introduction to C++

Chapter 3 Function Overloading

2/3/2018 CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II. Lecture Contents. C# basics. Methods Arrays. Dr. Amal Khalifa, Spr17

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

CS 106 Introduction to Computer Science I

Chapter 6 Methods. Dr. Hikmat Jaber

Annotation Annotation or block comments Provide high-level description and documentation of section of code More detail than simple comments

EECS168 Exam 3 Review

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Introduction to Programming

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Chapter 4 Defining Classes I

Functions. Computer System and programming in C Prentice Hall, Inc. All rights reserved.

Programming Exercise 7: Static Methods

All classes in a package can be imported by using only one import statement. If the postcondition of a method is not met, blame its implementer

Introduction to Computer Science Unit 2. Notes

Dr M Kasim A Jalil. Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson)

Full file at

2/6/2018. ECE 220: Computer Systems & Programming. Function Signature Needed to Call Function. Signature Include Name and Types for Inputs and Outputs

Method Invocation. Zheng-Liang Lu Java Programming 189 / 226

4. Java language basics: Function. Minhaeng Lee

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

Computer Science & Engineering 150A Problem Solving Using Computers

Discover how to get up and running with the Java Development Environment and with the Eclipse IDE to create Java programs.

Operators. Java Primer Operators-1 Scott MacKenzie = 2. (b) (a)

Define a method vs. calling a method. Chapter Goals. Contents 1/21/13

Chapter 3 - Functions

Compiler Theory. (Semantic Analysis and Run-Time Environments)

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 3. Existing Information. Notes. Notes. Notes. Lecture 03 - Functions

Chapter 5 Methods / Functions

C++, How to Program. Spring 2016 CISC1600 Yanjun Li 1

CS 106 Introduction to Computer Science I

Cpt S 122 Data Structures. Introduction to C++ Part II

Functions. Lecture 6 COP 3014 Spring February 11, 2018

1 class Lecture2 { 2 3 "Elementray Programming" / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch.

Chapter 3. Computer Science & Engineering 155E Computer Science I: Systems Engineering Focus. Existing Information.

C++ PROGRAMMING. For Industrial And Electrical Engineering Instructor: Ruba A. Salamh

CSc 10200! Introduction to Computing. Lecture 4-5 Edgardo Molina Fall 2013 City College of New York

OBJECT ORIENTED SIMULATION LANGUAGE. OOSimL Reference Manual - Part 1

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

CS1150 Principles of Computer Science Methods

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

Transcription:

University of Babylon Collage of Computer Assistant Lecturer : Wadhah R. Baiee Experience has shown that the best way to develop and maintain a large program is to construct it from small, simple pieces, or modules. This technique is called divide and conquer. Methods, which we introduced in Last lectures, help you modularize programs. Here, we study methods in more depth. We emphasize how to declare and use methods to facilitate the design, implementation, operation and maintenance of large programs. Methods (called functions or procedures in some languages) help you modularize a program by separating its tasks into self-contained units. You ve declared methods in every program you ve written. The statements in the method bodies are written only once, are hidden from other methods and can be reused from several locations in a program. One motivation for modularizing a program into methods is the divide-and-conquer approach, which makes program development more manageable by constructing programs from small, simple pieces. Another is software reusability sing existing methods as building blocks to create new programs. Often, you can create programs mostly from standardized methods rather than by building customized code. For example, in earlier programs, we did not define how to read data from the keyboard Java provides these capabilities in the methods of class Scanner. A third motivation is to avoid repeating code. Dividing a program into meaningful methods makes the program easier to debug and maintain. Static methods and Math Class: We use various Math class methods here to present the concept of static methods. Class Math provides a collection of methods that enable you to perform common mathematical calculations. For example, you can calculate the square root of 900.0 with the static method call. Math.sqrt( 900.0 ) Exercise : Build your own MyMath class with many common mathematical methods without using default Java Math class and its methods. 1

Object Oriented Methods : Deeper Look Lecture Three Declaring Methods with Multiple Parameterss Methods often requiree more than one piece of information too perform their tasks. We now consider how to write your own methods with multiplee parameters. Figure 6.3 uses a method called maximum to determine and return the largest of three double values. In main, lines l 14 18 prompt the user to enter three double values, then read them from the user. Line 21 calls method maximum (declared in lines 28 41) to determine the largest of the three values it receives r as arguments. When method maximum returns the result to line 21, the program assigns maximum s return value to local variable result.. Then line 24 outputs the maximum value. 2

Object Oriented Methods : Deeper Look Lecture Three Notes : Methods can return at most one value, but the returned value could be a reference to an object that contains many values. Variables should be declared as fields only if they ree required for use in more than one method of the class or if the program should save their values between calls to the class s methods. Declaring method parameters of the same type as float x, y instead of float x, float y is a syntax error a type is required for each parameter in the parameter list. Implementing Method maximum by Reusing Methodd Math.max The entire body of our maximum method could also a be implemented with two calls to Math.max, as follows: return Math.max( x, Math.max( y, z ) ); 3

Notes on Declaring and Using Methods There are three ways to call a method: 1. Using a method name by itself to call another method of the same class such as maximum(number1, number2, number3) in line 21 of Fig. 6.3. 2. Using a variable that contains a reference to an object, followed by a dot (.) and the method name to call a non-static method of the referenced object such as, mygradebook.displaymessage(), which calls a method of class GradeBook from the main method of GradeBookTest. 3. Using the class name and a dot (.) to call a static method of a class such as Math.sqrt(900.0). A static method can call only other static methods of the same class directly (i.e., using the method name by itself) and can manipulate only static variables in the same class directly. To access the class s non-static members, a static method must use a reference to an object of the class. Recall that static methods relate to a class as a whole, whereas non-static methods are associated with a specific instance (object) of the class and may manipulate the instance variables of that object. Many objects of a class, each with its own copies of the instance variables, may exist at the same time. Suppose a static method were to invoke a non-static method directly. How would the method know which object s instance variables to manipulate? What would happen if no objects of the class existed at the time the non-static method was invoked? Thus, Java does not allow a static method to access non-static members of the same class directly. Method-Call Stack and Activation Records To understand how Java performs method calls, we first need to consider a data structure (i.e., collection of related data items) known as a stack. You can think of a stack as analogous to a pile of dishes. When a dish is placed on the pile, it s normally placed at the top (referred to as pushing the dish onto the stack). Similarly, when a dish is removed from the pile, it s always removed from the top (referred to as popping the dish off the stack). Stacks are known as last-in, first-out (LIFO) data 4

structures the last item pushed (inserted) on the stack is the first item popped (removed) from the stack. When a program calls a method, the called method must know how to return to its caller, so the return address of the calling method is pushed onto the programexecution stack (sometimes referred to as the method-call stack). If a series of method calls occurs, the successive return addresses are pushed onto the stack in last-in, first-out order so that each method can return to its caller. The program-execution stack also contains the memory for the local variables used in each invocation of a method during a program s execution. This data, stored as a portion of the program-execution stack, is known as the activation record or stack frame of the method call. When a method call is made, the activation record for that method call is pushed onto the program-execution stack. When the method returns to its caller, the activation record for this method call is popped off the stack and those local variables are no longer known to the program. Argument Promotion and Casting Another important feature of method calls is argument promotion converting an argument s value, if possible, to the type that themethod expects to receive in its corresponding parameter. For example, a program can call Math method sqrt with an int argument even though a double argument is expected. The statement System.out.println( Math.sqrt( 4 ) ); correctly evaluates Math.sqrt(4) and prints the value 2.0. Themethod declaration s parameter list causes Java to convert the int value 4 to the double value 4.0 before passing the value to method sqrt. Such conversions may lead to compilation errors if Java s promotion rules are not satisfied. These rules specify which conversions are allowed that is, which ones can be performed without losing data. In the sqrt example above, an int is converted to a double without changing its value. However, converting a double to an int truncates the fractional part of the double value thus, part of the value is lost. Converting large integer types to small integer types (e.g., long to int, or int to short) may also result in changed values. The promotion rules apply to expressions containing values of two or more primitive types and to primitive-type values passed as arguments to methods. Each value is promoted to the highest type in the expression. Actually, the expression uses a temporary copy of each value the types of the original values remain unchanged. Figure 6.4 lists the primitive types and the types to which each can be 5

promoted. The valid promotions for a given type are always to a type higher in the table. For example, an int can be promoted to the higher types long, float and double. Converting values to types lower in the table of Fig. 6.4 will result in different values if the lower type cannot represent the value of the higher type (e.g., the int value 2000000 cannot be represented as a short, and any floating-point number with digits after its decimal point cannot be represented in an integer type such as long, int or short). Therefore, in cases where information may be lost due to conversion, the Java compiler requires you to use a cast operator (introduced in Section 4.9) to explicitly force the conversion to occur otherwise a compilation error occurs. This enables you to take control from the compiler. You essentially say, I know this conversion might cause loss of information, but for my purposes here, that s fine. Suppose method square calculates the square of an integer and thus requires an int argument. To call square with a double argument named doublevalue, we would be required to write the method call as square( (int) doublevalue ) This method call explicitly casts (converts) a copy of variable doublevalue s value to an integer for use in method square. Thus, if doublevalue s value is 4.5, the method receives the value 4 and returns 16, not 20.25. Method Overloading Methods of the same name can be declared in the same class, as long as they have different sets of parameters (determined by the number, types and order of the parameters) this is called method overloading. When an overloaded method is called, the compiler selects the appropriate method by examining the number, types and order of the arguments in the call. Method overloading is commonly used to create several methods with the same name that perform the same or similar tasks, but on different types or different numbers of arguments. Declaring Overloaded Methods Class MethodOverload (Fig. 6.10) includes two overloaded versions of method square one that calculates the square of an int (and returns an int) and one that calculates the square of a double (and returns a double). Although these methods have the same name and similar parameter lists and bodies, think of them simply as different 6

Object Oriented Methods : Deeper Look Lecture Three methods. It may help to think of the method names as square of int and square of double, respectively The compiler distinguishes overloaded methods by their signature a combination of the method s name and the number, types and order of its parameters. If the compiler lookedd only at method names n during compilation, the code in Fig. 6. 10 would be ambiguous the compiler wouldd not know how to distinguish between the two square methods (lines 14 19 and 22 27). Internally, the compiler uses longer method namess that include the original method name, the types of each parameter and the exact order of the parameters to determine whether the methods in a class are unique in that class. 7

In discussing the logical names of methods used by the compiler, we did not mention the return types of the methods. Method calls cannot be distinguished by return type. If you had overloaded methods that differed only by their return types and you called one of the methods in a standalone statement as in: square( 2 ); the compiler would not be able to determine the version of the method to call, because the return value is ignored. When two methods have the same signature and different return types, the compiler issues an error message indicating that the method is already defined in the class. Overloaded methods can have different return types if the methods have different parameter lists. Also, overloaded methods need not have the same number of parameters. 8