Lecture 11 B Methods and Data Passing

Slides:



Advertisements
Similar presentations
Review What is a virtual function? What can be achieved with virtual functions? How to define a pure virtual function? What is an abstract class? Can a.
Advertisements

Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
Chapter 4: Writing Classes
Chapter 5 Functions.
Lecture 3: Topics If-then-else Operator precedence While loops Static methods Recursion.
Math class methods & User defined methods Introduction to Computers and Programming in JAVA: V
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
Introduction to Methods
Chapter 6: Function. Scope of Variable A scope is a region of the program and broadly speaking there are three places, where variables can be declared:
What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework.
METHODS Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian
CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
CSE 1302 Lecture 7 Object Oriented Programming Review Richard Gesick.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Parameters. Overview A Reminder Why Parameters are Needed How Parameters Work Value Parameters Reference Parameters Out Parameters.
Procedural programming in Java Methods, parameters and return values.
User Defined Functions Chapter 7 2 Chapter Topics Void Functions Without Parameters Void Functions With Parameters Reference Parameters Value and Reference.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
CSE 1301 Lecture 6 Writing Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Working With Objects Tonga Institute of Higher Education.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Classes  Constructors  Principles of OOP  Class type member.
Chapter VII: Arrays.
Classes and Objects.
Static data members Constructors and Destructors
Templates.
Chapter 4: Writing Classes
Java Primer 1: Types, Classes and Operators
Lecture 10: More on Methods and Scope
Chapter 5 Function Basics
Lecture 4 D&D Chapter 5 Methods including scope and overloading Date.
Agenda Warmup Lesson 2.5 (Ascii, Method Overloading)
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Lecture 14 Writing Classes part 2 Richard Gesick.
Lecture Set 4 Data Types and Variables
Lesson 2: Building Blocks of Programming
Inheritance April 7, 2006 ComS 207: Programming I (in Java)
Phil Tayco Slide version 1.0 Created Oct 2, 2017
Lecture 11 C Parameters Richard Gesick.
User Defined Functions
Chapter 6 Methods: A Deeper Look
6 Chapter Functions.
Group Status Project Status.
Classes, Encapsulation, Methods and Constructors (Continued)
Learning Objectives Classes Constructors Principles of OOP
Unit 3 Test: Friday.
Type & Typeclass Syntax in function
CS139 October 11, 2004.
Methods and Data Passing
Workshop for Programming And Systems Management Teachers
Object Oriented Programming in java
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
In this class, we will cover:
Classes, Objects and Methods
Unit-1 Introduction to Java
Corresponds with Chapter 5
Methods and Data Passing
Creating and Using Classes
Presentation transcript:

Lecture 11 B Methods and Data Passing Richard Gesick

Overview Terminology Why we have Methods Data Passing The Mantra Calling Methods Scope of Variables

Terminology A method is a logical grouping of statements Reusable chunks of code Write once Call as many times as you like Benefits Reusable Easy to work at higher level of abstraction Reduces complexity Reduces size of code

AKA (also known as) Methods can be called several things, depending on the book or context Examples: Procedure Module Method (OOP) Behavior (OOP) Member Function (OOP)

You’ve Already Seen Methods Denotes a Method of the class Denotes a property of the class

MidiPlayer.Play(new NoteOn(0, GeneralMidiPercussion.BassDrum, 127)); Invoking Methods MidiPlayer.Play(new NoteOn(0, GeneralMidiPercussion.BassDrum, 127)); Two things going on here Creating a new NoteOn object Passing it three parameters “Constructing” (more later) Invoking the Play method of the MidiPlayer class Passing it one parameter (the NoteOn object) This is a “static” method (more later)

Why have Methods? (see anything similar?) double average; int userNum1, userNum2; Console.WriteLine(“Please enter the 2 numbers”); userNum1 = Int32.Parse(Console.ReadLine()); userNum2 = Int32.Parse(Console.ReadLine()); average = (userNum1 + userNum2) / 2; … // a lot of other code

Why have Methods? (see anything similar?) double average; int userNum1, userNum2; Console.WriteLine(“Please enter the 2 numbers”); userNum1 = Int32.Parse(Console.ReadLine()); userNum2 = Int32.Parse(Console.ReadLine()); average = (userNum1 + userNum2) / 2; … // a lot of other code All of this code is the same!

Basic Idea double average; int userNum1, userNum2; Console.WriteLine(“Please enter the 2 numbers”); userNum1 = Int32.Parse(Console.ReadLine()); userNum2 = Int32.Parse(Console.ReadLine()); average = (userNum1 + userNum2) / 2; … // a lot of other code

Basic Idea Create method instead double average; int userNum1, userNum2; Console.WriteLine(“Please enter the 2 numbers”); userNum1 = Int32.Parse(Console.ReadLine()); userNum2 = Int32.Parse(Console.ReadLine()); average = (userNum1 + userNum2) / 2; … // a lot of other code Create method instead userNum1 = Int32.Parse(Console.ReadLine()); userNum2 = Int32.Parse(Console.ReadLine()); average = (userNum1 + userNum2) / 2;

Give the Method a Name myMethod double average; int userNum1, userNum2; Console.WriteLine(“Please enter the 2 numbers”); userNum1 = Int32.Parse(Console.ReadLine()); userNum2 = Int32.Parse(Console.ReadLine()); average = (userNum1 + userNum2) / 2; … // a lot of other code myMethod userNum1 = Int32.Parse(Console.ReadLine()); userNum2 = Int32.Parse(Console.ReadLine()); average = (userNum1 + userNum2) / 2;

Call the Method (instead of writing all that code) double average; int userNum1, userNum2; Console.WriteLine(“Please enter the 2 numbers”); myMethod … // a lot of other code myMethod userNum1 = Int32.Parse(Console.ReadLine()); userNum2 = Int32.Parse(Console.ReadLine()); average = (userNum1 + userNum2) / 2;

What have we done? Written the code once, but called it many times Reduced the size of our code Easier to comprehend This is called procedural abstraction Tracing of code skips all around (no longer linear)

Scope of Variables Scope – “who can see what” Variables that are defined within a method can only be seen by that method! We need a way to send information to the method We need a way for the method to send back information Example: method1 can’t see myInt method1 method2 char myChar; int myInt;

Examples private int sum(int x, int y) private string getUserChoice() private void isMatch(int guess)

The Mantra All methods follow a mantra The mantra is: Return type, Method name, parameters …

The return type A method has the option to return us (the calling method) some information If the method doesn’t return us any info, the return type is void Otherwise, the return type is the data type it’s going to return Example of return types: int char boolean

Figure out the return type Method Name average double or float getLetterGrade char areYouAsleep bool getGPA double or float printMenu void // Don’t confuse what a method does with it’s // return type! getStudentName string

The Method’s name Similar to naming of variables Can be almost anything except A reserved word (keywords) Can’t begin with a number Can’t contain strange symbols except _ and $ Method names should begin with a lower case Different standards/conventions are used If multiple words in method name, capitalize the first letter in each word (except the first) Example: thisIsAnExample

Parameters Methods cannot see each other’s variables (scope) Special variables used to “catch” data being passed This is the only way the main algorithm and Methods have to communicate! Located between parentheses ( ) If no parameters are needed, leave the parentheses empty

Examples Remember the mantra What can you tell me about these methods? void doSomething (int data) double average (int num1, int num2) boolean didHePass ( ) char whatWasHisGrade ( ) void scareStudent (char gradeOfStudent)

Getting the Method to Work for You Call it by name Pass it the right stuff Pass the right number of parameters If it expects two things, pass it two things! Pass the right type of parameters If it expects a char, don’t pass it a double! Parameters must match exactly If it returns something, do something with it!

Example int num1, num2, num3; double result1, result2; num1 = 5; num2 = 7; num3 = 4; result1 = average (num1, num2); result2 = average (num3, num1); x y Memory double average (int x, int y) { return ( (x+y) / 2.0); } // Note: the average method is currently inactive

Example (declare variables) int num1, num2, num3; double result1, result2; num1 = 5; num2 = 7; num3 = 4; result1 = average (num1, num2); result2 = average (num3, num1); x y Memory num1 num2 num3 double average (int x, int y) { return ( (x+y) / 2.0); } // Note: the average method is currently inactive

Example (declare variables) int num1, num2, num3; double result1, result2; num1 = 5; num2 = 7; num3 = 4; result1 = average (num1, num2); result2 = average (num3, num1); x y Memory num1 num2 num3 double average (int x, int y) { return ( (x+y) / 2.0); } result1 result2 0.0 0.0

Example (set values) Memory int num1, num2, num3; double result1, result2; num1 = 5; num2 = 7; num3 = 4; result1 = average (num1, num2); result2 = average (num3, num1); x y Memory num1 num2 num3 double average (int x, int y) { return ( (x+y) / 2.0); } 5 result1 result2 0.0 0.0

Example (set values) Memory int num1, num2, num3; double result1, result2; num1 = 5; num2 = 7; num3 = 4; result1 = average (num1, num2); result2 = average (num3, num1); x y Memory num1 num2 num3 double average (int x, int y) { return ( (x+y) / 2.0); } 5 7 result1 result2 0.0 0.0

Example (set values) Memory int num1, num2, num3; double result1, result2; num1 = 5; num2 = 7; num3 = 4; result1 = average (num1, num2); result2 = average (num3, num1); x y Memory num1 num2 num3 double average (int x, int y) { return ( (x+y) / 2.0); } 5 7 4 result1 result2 0.0 0.0

Example (invoke method) int num1, num2, num3; double result1, result2; num1 = 5; num2 = 7; num3 = 4; result1 = average (num1, num2); result2 = average (num3, num1); WAKE UP! x y Memory num1 num2 num3 double average (int x, int y) { return ( (x+y) / 2.0); } 5 7 4 result1 result2 0.0 0.0

Example (data passing) int num1, num2, num3; double result1, result2; num1 = 5; num2 = 7; num3 = 4; result1 = average (num1, num2); result2 = average (num3, num1); x y Memory 5 7 num1 num2 num3 double average (int x, int y) { return ( (x+y) / 2.0); } 5 7 4 result1 result2 0.0 0.0

Example (caller sleeps) int num1, num2, num3; double result1, result2; num1 = 5; num2 = 7; num3 = 4; result1 = average (num1, num2); result2 = average (num3, num1); x y Memory 5 7 num1 num2 num3 double average (int x, int y) { return ( (x+y) / 2.0); } 5 7 4 result1 result2 0.0 0.0 // The method is now ACTIVE

Example (method active) int num1, num2, num3; double result1, result2; num1 = 5; num2 = 7; num3 = 4; result1 = average (num1, num2); result2 = average (num3, num1); x y Memory 5 7 num1 num2 num3 double average (int x, int y) { return ( (x+y) / 2.0); } 5 7 4 result1 result2 0.0 0.0 // 5 + 7 is 12; 12 / 2 is 6

Example (value returned) int num1, num2, num3; double result1, result2; num1 = 5; num2 = 7; num3 = 4; result1 = average (num1, num2); result2 = average (num3, num1); x y Memory 6 5 7 num1 num2 num3 double average (int x, int y) { return ( (x+y) / 2.0); } 5 7 4 result1 result2 0.0 0.0

Example (caller resumes) int num1, num2, num3; double result1, result2; num1 = 5; num2 = 7; num3 = 4; result1 = average (num1, num2); result2 = average (num3, num1); sleep x y Memory num1 num2 num3 double average (int x, int y) { return ( (x+y) / 2.0); } 5 7 4 result1 result2 6.0 0.0

Example (call method again) int num1, num2, num3; double result1, result2; num1 = 5; num2 = 7; num3 = 4; result1 = average (num1, num2); result2 = average (num3, num1); WAKE UP! x y Memory num1 num2 num3 double average (int x, int y) { return ( (x+y) / 2.0); } 5 7 4 result1 result2 6.0 0.0

Example (data passing) int num1, num2, num3; double result1, result2; num1 = 5; num2 = 7; num3 = 4; result1 = average (num1, num2); result2 = average (num3, num1); x y Memory 4 5 num1 num2 num3 double average (int x, int y) { return ( (x+y) / 2.0); } 5 7 4 result1 result2 6.0 0.0

Example (caller sleeps) int num1, num2, num3; double result1, result2; num1 = 5; num2 = 7; num3 = 4; result1 = average (num1, num2); result2 = average (num3, num1); x y Memory 4 5 num1 num2 num3 double average (int x, int y) { return ( (x+y) / 2.0); } 5 7 4 result1 result2 6.0 0.0 // The method is now ACTIVE

Example (method active) int num1, num2, num3; double result1, result2; num1 = 5; num2 = 7; num3 = 4; result1 = average (num1, num2); result2 = average (num3, num1); x y Memory 4 5 num1 num2 num3 double average (int x, int y) { return ( (x+y) / 2.0); } 5 7 4 result1 result2 6.0 0.0 // 4 + 5 is 9; 9 / 2 = 4.5

Example (value returned) int num1, num2, num3; double result1, result2; num1 = 5; num2 = 7; num3 = 4; result1 = average (num1, num2); result2 = average (num3, num1); x y Memory 4 5 4.5 num1 num2 num3 double average (int x, int y) { return ( (x+y) / 2.0); } 5 7 4 result1 result2 6.0 0.0

Example (caller resumes) int num1, num2, num3; double result1, result2; num1 = 5; num2 = 7; num3 = 4; result1 = average (num1, num2); result2 = average (num3, num1); sleep x y Memory num1 num2 num3 double average (int x, int y) { return ( (x+y) / 2.0); } 5 7 4 result1 result2 6.0 4.5

Method Rules You cannot define a method inside of another method Methods always reside in a class in C# (as you’ve seen numerous time already) Methods cannot see each other’s variables You’ve seen “private static” in our examples… more on this later

Overloading Methods Method overloading is the process of using the same method name for multiple methods The signature of each overloaded method must be unique The signature includes the number, type, and order of the parameters The compiler determines which version of the method is being invoked by analyzing the parameters The return type of the method is not part of the signature

Overloading Methods float tryMe (int x) { return x + .375; } Version 1 float tryMe (int x, float y) { return x*y; } Version 2 result = tryMe (25, 4.32f) Invocation

Overloading Operators In C#, not only can methods be overloaded, operators can be overloaded as well.