CSCI 3328 Object Oriented Programming in C# Chapter 6: Methods 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539

Slides:



Advertisements
Similar presentations
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
Advertisements

Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Chapter 5 C Functions The best way to develop and maintain a large program is to divide it into several smaller program modules, each of which is more.
Chapter 5 Functions.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 5 Function Basics.
Math class methods & User defined methods Introduction to Computers and Programming in JAVA: V
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
 2007 Pearson Education, Inc. All rights reserved C Functions.
Review for Midterm 2 Nested loops & Math class methods & User defined methods.
1 Chapter 5 Methods. 2 Introducing Methods A method is a collection of statements that are grouped together to perform an operation.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "methods" in Java Purpose Reuse code Modularize the program This.
CSCI 3327 Visual Basic Chapter 6: Methods: A Deeper Look UTPA – Fall 2011.
Dale Roberts Procedural Programming using Java Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
More with Methods (parameters, reference vs. value, array processing) Corresponds with Chapters 5 and 6.
CSE 1302 Lecture 7 Object Oriented Programming Review Richard Gesick.
 2008 Pearson Education, Inc. All rights reserved Function Call Stack and Activation Records Data structure: collection of related data items.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
* * 0 Chapter 6 Java Methods. * * 0 Method Syntax [access specifier][qualifier] return type method name(argument list) Access specifier public – method.
1 Introduction Modules  Most computer programs solve much larger problem than the examples in last sessions.  The problem is more manageable and easy.
 2005 Pearson Education, Inc. All rights reserved. 1 Methods Called functions or procedures in other languages Modularize programs by separating its tasks.
 2005 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Android How to Program, 2/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Part II © Copyright by Pearson Education, Inc. All Rights Reserved.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
4-Methods Dr. John P. Abraham Professor UTPA. Common ways of packaging code Properties Methods Classes Namespaces (related classes are grouped into a.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 5: Functions Xiang Lian The University of Texas – Pan American Edinburg, TX
CSCI/CMPE 4341 Topic: Programming in Python Review: Exam I Xiang Lian The University of Texas – Pan American Edinburg, TX 78539
CSCI 3328 Object Oriented Programming in C# Chapter 4: C# Control Statement – Part I 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX.
 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
Methods: A Deeper Look. Template for Class Definition public class { } A.Import Statement B.Class Comments C.Class Name D.Data members E.Methods (inc.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
1. 2 Framework Classes and libraries: 3.
CSCI 3328 Object Oriented Programming in C# Chapter 9: Classes and Objects: A Deeper Look – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
Review for Nested loops & Math class methods & User defined methods.
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapters 4 and 5: Excerpts l Class and Method Definitions l Information.
Creating and Using Class Methods. Definition Class Object.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "___________________" in Java Purpose –Reuse code –Modularize.
C# Programming Methods.
CSCI 3328 Object Oriented Programming in C# Chapter 6: Methods – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539
CSCI 3328 Object Oriented Programming in C# Chapter 4: C# Control Statement – Part I – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
(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,
Lecture 02 Dr. Eng. Ibrahim El-Nahry Methods. 2 Learning Objectives Class Definition includes both methods and data properties Method Definition and Declaration.
A DVANCED P ROGRAMMING C HAPTER 7: M ETHODS : A D EEPER L OOK Dr Shahriar Bijani Spring 2016.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
Functions.
7 Methods: A Deeper Look.
Java Primer 1: Types, Classes and Operators
Methods Chapter 6.
Chapter 5 Function Basics
Programming Fundamentals Lecture #7 Functions
6.11 Function Call Stack and Activation Records
Chapter 6 Methods: A Deeper Look
More Object Oriented Programming
Chapter 5 - Functions Outline 5.1 Introduction
Advanced Programming Chapter 7: Methods: A Deeper Look
Chapter 5 Function Basics
Chapter 6 Methods: A Deeper Look
MSIS 655 Advanced Business Applications Programming
The University of Texas – Pan American
CSCI 3327 Visual Basic Chapter 6: Methods: A Deeper Look
CSCI 3328 Object Oriented Programming in C# Chapter 6: Methods
6 Methods: A Deeper Look.
Java Programming Language
Corresponds with Chapter 5
Presentation transcript:

CSCI 3328 Object Oriented Programming in C# Chapter 6: Methods 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX

Common Ways of Packaging Code Properties Methods Classes Namespaces (related classes are grouped into a namespace) Prepackaged codes are available in.Net framework class library 2

Class Math From namespace System Provides a collection of methods for mathematics – Sqrt(x) Console.WriteLine(Math.Sqrt(900.0)) – Abs(x) – Ceiling(x) Rounds x to the smallest integer, greater than x. Int32 numofGalons = Convert.ToInt16(Math.Ceiling(totalSqFt/450)); – Floor(x) Rounds x to the largest integer, less than or equal to x. – Pow(x,y) x raised to the power of y 3

Accessing Static Methods of a Class Syntax – ClassName.MethodName(arguments) – Dot. is a member access operator Access a method – Math.Cos(x) – Math.Exp(x) (i.e., e x ) Access constants – Math.PI (i.e., …) – Math.E (i.e., …) 4

Example of Floor and Ceiling Floor: – Math.Floor(2.10) = 2 – Math.Floor(2.00) = 2 – Math.Floor(1.90) = 1 – Math.Floor(1.80) = 1 Ceiling: – Math.Ceiling(0.00) = 0 – Math.Ceiling(0.10) = 1 – Math.Ceiling(0.20) = 1 – Math.Ceiling(0.30) = 1 5

Main Declared as Static Why must Main be declared static? – It is the applications entry point – Declaring Main as static allows the execution environment to invoke Main without creating an instance of the class public static void Main (string args[]) The args allows one to call the application and pass parameters in from the command line 6

Declaring Methods with Multiple Parameters Parameters are specified as a comma-separated list – Actual parameters appear in the call – Formal parameters appear in the method declaration Both actual and formal parameters must have exactly same number of parameters Both actual and formal parameters must agree in their types Values are assigned according to the order of appearance rather than variable names 7

Method Declaration and Call Method declaration public double Maximum(double x, double y, double z) { // code here return maximumValue; } Method call – double result = Maximum (num1, num2, num3); 8

Method-Call Stack and Activation Records When an application calls a method the return address of the calling method is pushed on to the program-execution stack (method-call stack) If series of methods are called, successive return addresses are pushed onto the stack (LIFO) The program-execution stack also contains the memory for the local variables used in each invocation of a method during the application’s execution – This is known as the activation record (stack frame) of the method call When the method completes and return 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 application If more methods calls occur than available memory for program-execution stack, stack overflow error occurs 9

Illustration of Method-Call Stack Stack public double Maximum(double x, double y, double z) { // … } public static void Main (string [] args) { double a, b, c; // … Maximum(a, b, c); } 10 a Main() stack b c x y z Maximum()

Argument Promotion and Casting Math.Sqrt(4) – Expects a double argument – However, accepts an integer and promotes it to a double and returns the result of 2.0 When a method expects an integer – if you pass a real number, you get an error, since the method cannot truncate numbers arbitrarily – float can be promoted to double, but double cannot be converted into float implicitly If you want to go the other way, you have to cast it 11

The.Net Framework Library Some examples – System.Windows.Forms – System.Windows.Controls (for WPFs) – System.Linq (for language integrated query) – System.IO (for files, keyboards, monitors, etc.) – System.Web (creating web applications) – System.Text (manipulate characters and strings) 12

Random Number Generation Needed for your card game assignment coming up later. Objects of class Random can produce random byte, int and double values. – Random randomNumbers = new Random(); – int randomValue = randomNumbers.Next(); Generates random values in the range 0 to +2,147,483,646 This uses the current time as seed value to generate a psuedorandom number If you place number.Next(1,53), it will generate a random value 1 to 52 – Random randomNumbers = new Random(seedvalue); You can use a seed value when creating a Random object This can lead to a repeating exact same sequence 13

Random Number Generation (cont'd) Random randomNumbers = new Random(); – randomNumbers.Next(); [0, +2,147,483,646] – randomNumbers.Next(6); [0, 6) i.e., 0, 1, 2, 3, 4, 5 – randomNumbers.Next(1, 7); [1, 7) i.e., 1, 2, 3, 4, 5, 6 14

Example of Rolling a Die Random randomNumbers = new Random(); int face; for (int counter = 1; counter <= 20; counter++) { face = randomNumbers.Next(1, 7); Console.Write("{0} ", face); if ( counter%5 == 0) Console.WriteLine(); } 15

Method Overloading Methods of the same name can be declared in the same class, as long as they have different set of parameters(number, type and order). Examples: public int Square (int intValue) { return intValue*intValue; } public double Square (double doubleValue) { return doubleValue*doubleValue; } 16

Distinguishing Between Overloaded Methods Distinguish between overloaded methods by signature – Method's name – The number of parameters – Types of parameters – Order of parameters – Parameter passing methods Return data types cannot distinguish overloaded methods 17

Recursion Method that calls itself A recursive method solves only a simple problem (base case) For any thing other than the base case, it calls itself with a slightly simpler problem – Eventually it becomes the base case for which it knows the answer 18

Example of Factorial // n! public static long Factorial (long number) { // base case if (number <= 1) return 1; else return number * Factorial(number -1); } 19

Passing Arguments: Value and Reference Applying the ref keyword to a parameter declaration allows you pass a variable to a method by reference – The method now will be able to modify the original variable – The original variable must be initialized before passing to the method If you do not want to initialize the variable, you can use the modifier: out – This means that the called method will assign a value to the original variable – If not, a compiler error will result 20

Example of Parameter Passing void SquareRef(ref int x) { x = x*x;// modify caller's variable; // x must be initialized } void SquareOut(out int x) { x = 6;// x does not need to be initialized x = x*x; } int Square (int x) { x=x*x;// local variable x within method return x; } 21 // call methods int y= 5, z; SquareRef(ref y); SquareOut(out z); Square(y); Square(z);

22