CS1S467 GUI Programming LECTURE 15 Methods (2 of 2)

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Spring Semester 2013 Lecture 5
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
CIT 590 Intro to Programming Java lecture 4. Agenda Types Collections – Arrays, ArrayLists, HashMaps Variable scoping Access modifiers – public, private,
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 6 Functions.
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 10: 1 TEST!!
 To be able to write larger programs ◦ By breaking them down into smaller parts and passing data between the parts.  To understand the concepts of Methods.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 6 Functions.
Chapter 6: Functions Starting Out with C++ Early Objects
Parameters. Overview A Reminder Why Parameters are Needed How Parameters Work Value Parameters Reference Parameters Out Parameters.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Learners Support Publications Functions in C++
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
Chapter 4 Introduction to Classes, Objects, Methods and strings
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
User defined functions
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
1 Chapter 6 Methods. 2 Motivation Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.
Method Parameters and Overloading Version 1.0. Topics The run-time stack Pass-by-value Pass-by-reference Method overloading Stub and driver methods.
CSE 1341 Honors Note Set 2 1. Overview  Java vs. C++  Functions in C++  First Programming Packet  Development Environment 2.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
C# Programming Methods.
Methods.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
COP 2220 Computer Science I Topics –Breaking Problems Down –Functions –User-defined Functions –Calling Functions –Variable Scope Lecture 4.
Chapter 6 Functions. 6-2 Topics 6.1 Modular Programming 6.2 Defining and Calling Functions 6.3 Function Prototypes 6.4 Sending Data into a Function 6.5.
Functions CMSC 202. Topics Covered Function review More on variable scope Call-by-reference parameters Call-by-value parameters Function overloading Default.
Comp1004: Building Better Objects II Encapsulation and Constructors.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
C++ Features Function Overloading Default Functions arguments Thinking about objects – relationship to classes Types of member functions Constructor and.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Class Design II Lecture 7, Thu Jan
Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.
Information and Computer Sciences University of Hawaii, Manoa
Topic: Classes and Objects
User-Written Functions
BİL527 – Bilgisayar Programlama I
COMP 170 – Introduction to Object Oriented Programming
Functions Chapter 5 CS12 - Computer Programming 1 Chapter 5.
Functions, locals, parameters, and separate compilation
A Lecture for the c++ Course
Java LESSON 7 Objects, Part 1
Lecture 11 C Parameters Richard Gesick.
Encapsulation and Constructors
Group Status Project Status.
Chapter 4 Topics: class declarations method declarations
Chapter 6 Methods.
Methods and Data Passing
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
Scope of variables class scopeofvars {
CS1S467 GUI Programming LECTURE 14 Methods (1 of 2)
Lecture 11 Parameters CSE /26/2018.
Functions Imran Rashid CTO at ManiWeber Technologies.
Lecture 18 Making C# Objects
CMSC 202 Lesson 6 Functions II.
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Parameters, Overloading Methods, and Random Garbage
Methods (a.k.a functions)
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
C++ Object Oriented 1.
Methods and Data Passing
Chengyu Sun California State University, Los Angeles
Presentation transcript:

CS1S467 GUI Programming LECTURE 15 Methods (2 of 2)

Recap: METHODS METHODS are used whenever: a problem can be broken down into sub-problems similar or identical code is required at several places in the program Consequences of using METHODS: less typing re-using of code produces better quality and easier to maintain programs

Recap: Structure of a METHOD To call a method: Returned stuff = Method name ( List of stuff to pass into method ); The method itself: Scope Returned stuff Method name (List of stuff passed in) body of the method where computations are made return statement (if anything gets returned) { } // end of method

Recap: Parameter Lists The formal parameter list static int MaxOf( int x, int y ) { if ( x>y ) return x; else return y; } // end of method 'MaxOf' The 'MaxOf' method may be called using 'actual parameter lists' variables as parameters: MaxOf( a, b ); // a and b are int variables actual figures as parameters: MaxOf( 7, 100 ); // 7 and 100 also int or a mixture of both: MaxOf( a, 100 ); // a and 100 are int The actual parameter list

Recap: Parameter Lists Formal and Actual parameter lists must match static int MaxOf( int x, int y ) { if ( x>y ) return x; else return y; } // end of method 'MaxOf' The 'MaxOf' method may NOT be called using unsuitable parameters types: MaxOf( a, 7.5 ); // 7.5 is a float the wrong amount of parameters: MaxOf( 7, 100, 200 ); // 3 parameters MaxOf( 100 ); // only 1 parameter

Recap: Methods and Classes To call a method in the same class use just the name of the method MaxOf(4, 5); If the method is located in another class call it using the format: ClassName.MethodName( formal parameter list ) Console.ReadLine(); Console.WriteLine( "bla" );

Summary So Far The structure of a method call is: Returned parameter = Class.Method name (Actual parameter list) ; Single parameter, must match in type (void if none) Parameters must match in: number type order The structure of a method is: Scope Returned type Method name (Formal parameter list) body of the method where computations are made return statement (if anything gets returned) { } // end of method

New Stuff ! Scope of Methods Public and Private Scope of Variables Local and Global Overloading Methods

Scope of Methods (1 of 4) Scope Access to Method Scope Returned type Method name (Formal parameter list) body of the method where computations are made return statement (if anything gets returned) { } // end of method public from ALL other classes and the same class private from inside the same class only protected like ‘private’ but also from derived classes friend from inside project only - none - ‘private’ is the DEFAULT Scope Access to Method

Scope of Methods (2 of 4) Scope Access to Method public public static void LoginError () { Console.PrintLine(“Your username and password do not match”); //nothing to return } // end of method public from ALL other classes and the same class private from inside the same class only protected like ‘private’ but also from derived classes friend from inside project only - none - ‘private’ is the DEFAULT Scope Access to Method

Scope of Methods (3 of 4) Why different Method Scopes ? Preventing full ("public") access to Methods: allows only certain 'safe and tested' parts of classes to be used elsewhere hides away unimportant detail (e.g. 'helper' methods)

Scope of Methods (4 of 4) An Example Scenario public class Car { private static int speed = 0; public const int FOR=0, BACK=1; public static void SetSpeed( int newSpeed ) speed=newSpeed; if(speed > 70) speed =70; if(speed < 0) speed = 0; } public static int GetSpeed( ) return speed; } // end of Car class class Driver { static void Main(string [ ] argStr) int direction, spd; direction = Car.BACK; // this works Car.speed = 20; // this won't work Car.SetSpeed( 50000 ); spd = Car.GetSpeed( ); Console.WriteLine ("Speed is: " + spd); } //end of Main } //end of Driver class

Full Example Code in Visual Studio using System;   namespace ScopeDemo1 { public class Car private static int speed = 0; public const int FOR = 0, BACK = 1; public static void SetSpeed(int newSpeed) speed = newSpeed; if (speed > 70) speed = 70; if (speed < 0) speed = 0; } public static int GetSpeed() return speed; } // end of Car class    class Driver static void Main(String[] argStr) int direction, spd; direction = Car.BACK; // this works // Car.speed = 20; // this won't work Car.SetSpeed(50000); spd = Car.GetSpeed(); Console.WriteLine("Speed is: " + spd); Console.ReadLine(); } //end main } //end of Driver class } // end of namespace Full Example Code in Visual Studio

New Stuff ! Scope of Methods Public and Private Scope of Variables Local and Global Overloading Methods

Scope of Variables (1 of 4) Variables declared INSIDE a method ("method variables") are only 'known' and accessible INSIDE that method. public class Test { private static float AddThem( float x, float y ) { Console.WriteLine("a is " +a); return x+y; } // end of method 'AddThem' public static void Main ( string [ ] argStr ) { float a=4, b=5, c; c = AddThem ( a, b ); // calling the 'addThem' method Console.WriteLine("The sum of " +a+ " and " +b+ " is " +c); Console.WriteLine("x is " +x+ " and y is " +y); } // end of Main } // end of class Test What is the problem here? And here?

Scope of Variables (2 of 4) Variables inside different methods can even have the same name, but they are different !! public class Test { private static float AddThem( float a, float b ) { float sum =a+b; a=2000; // this affects only the variable a in the AddThem method return sum; } // end of method 'AddThem' public static void Main ( string [ ] argStr ) { float a=4, b=5, c; c = AddThem ( a, b ); // calling the 'AddThem' method Console.WriteLine("The sum of " +a+ " and " +b+ " is " +c); } // end of Main } // end of class Test "The sum of 4 and 5 is 9" (NOT: "The sum of 2000 …")

Scope of Variables (3 of 4) If you really need access to the same variable inside all or most methods of a class use "class variables" public class Test { private static float a=4; // declaration of "class variable" a (keyword ‘static’) private static float AddThem( float x, float y ) { a=2000; // this now does affect the class variable a return x+y; } // end of method 'AddThem' public static void Main ( string [ ] argStr ) { float b=5, c; c = AddThem ( a, b ); // calling the 'AddThem' method Console.WriteLine("The sum of " +a+ " and " +b+ " is " +c); } // end of Main } // end of class Test "The sum of 2000 and 5 is 9" (NOT: "The sum of 4 …")

Scope of Variables (4 of 4) Class variables and Method variables can have the same name but "local beats global" public class Test { private static float a=4; // declaration of "class variable" a (keyword ‘static’) private static float AddThem( float a, float x ) { a=2000; // this does NOT affect the class variable a return a+x; } // end of method 'AddThem' public static void Main ( string [ ] argStr ) { float b=5, c; c = AddThem ( a, b ); // calling the 'AddThem' method Console.WriteLine("The sum of " +a+ " and " +b+ " is " +c); } // end of Main } // end of class Test "The sum of 4 and 5 is 2005" (well, ……..)

Summary: Scope of Variables Variables declared INSIDE a method ("method variables") are only 'known' and accessible INSIDE that method. Variables inside different methods can even have the same name, but they are different !! If you need access to the same variable inside all or most methods of a class use "class variables" Class variables and method variables can have the same name but "local beats global"

Check your knowledge now: Spot the mistakes and figure out the printout: public class ScopeTest { private float x=5.0; private int AddToX( a ) { return x + a; } // end of method 'addThem' public static void Main ( argStr[ ] ) { float x, y=6.0, z; z = addToX( x, y ); x = addToX( z ); Console.WriteLine("z is "+z+" and x is "+x); // what gets printed? } // end of main } // end of class private static float x=5.0; private static float addToX( float a) { (string [ ] argStr ); float x = 0.0, y=6.0, z; z = addToX( y ); z is 11.0 and x is 16.0

Naming Conventions Quick reminder: Variable names lowerUpperUpper (e.g. myVariable, theLongVariable) Class name UpperUpperUpper (e.g. CurrencyInput, TheTestClass ) Method names UpperUpperUpper( ) (e.g. ReadLine( ), AddToX( ) ) Constant names ALL_UPPER (e.g. PI, INCHES )

Naming Conventions

New Stuff ! Scope of Methods Public and Private Scope of Variables Local and Global Overloading Methods

Overloading Methods Consider this method: private static int AddThem( int a, int b ) { return a+b; } What do you do if you want to add floats but don't want to invest into different names, e.g. addInts(…), addFloats(…), addLongs(…) ? You OVERLOAD the method i.e. use the same name but different return types and/or formal parameter lists: private static int AddThem( int a, int b ) { return a+b; } private static float AddThem( float a, float b ) {

Overloading Example Results 7 12 4.0 public class OverLoadingTest { private static int AddThem( int a, int b ) return a+b; } private static int AddThem( int a, int b, int c ) return a+b+c; private static float AddThem( float a, float b ) public static void Main(string [ ] argStr) int x=3, y=4, z=5, iRes; float n=1.5f, m=2.5f, fRes; iRes = addThem( x, y ); iRes = addThem( x, y, z ); fRes = addThem( n, m ); Overloading Example Results 7 12 4.0

Summary: METHODS Why: maintenance, usability, readability, safety Parameter list: formal - actual, match (amount, order, type) Return types: void, int, float, String, …. Method scope: private, public, (friendly) Variable scope: class variables, method variables --> "safety" Accessing methods: ClassName.methodName Overloading methods: same name, different parameter lists

END OF LECTURE 15