Download presentation
Presentation is loading. Please wait.
Published byÊἙρμῆς Κανακάρης-Ρούφος Modified over 6 years ago
1
CS1S467 GUI Programming LECTURE 15 Methods (2 of 2)
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
3
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
4
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
5
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
6
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" );
7
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
8
New Stuff ! Scope of Methods Public and Private Scope of Variables
Local and Global Overloading Methods
9
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
10
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
11
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)
12
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( ); spd = Car.GetSpeed( ); Console.WriteLine ("Speed is: " + spd); } //end of Main } //end of Driver class
13
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
14
New Stuff ! Scope of Methods Public and Private Scope of Variables
Local and Global Overloading Methods
15
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?
16
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 …")
17
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 …")
18
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, ……..)
19
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"
20
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
21
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 )
22
Naming Conventions
23
New Stuff ! Scope of Methods Public and Private Scope of Variables
Local and Global Overloading Methods
24
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 ) {
25
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
26
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
27
END OF LECTURE 15
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.