C# Programming: From Problem Analysis to Program Design1 Methods and Behaviors C# Programming: From Problem Analysis to Program Design 3rd Edition 3.

Slides:



Advertisements
Similar presentations
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Advertisements

Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
What have we learned so far… Preprocessor directives Introduction to C++ Variable Declaration Display Messages on Screen Get Information from User Performed.
Lecture 2 Basics of C#. Members of a Class A field is a variable of any type that is declared directly in a class. Fields are members of their containing.
An Introduction to Programming with C++ Fifth Edition
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
C# Programming: From Problem Analysis to Program Design1 Methods and Behaviors C# Programming: From Problem Analysis to Program Design 3rd Edition 3.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 3rd Edition.
C# Programming: From Problem Analysis to Program Design1 Arrays C# Programming: From Problem Analysis to Program Design 3 rd Edition 7.
Advanced Object-Oriented Programming Features
COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
C# Programming: From Problem Analysis to Program Design1 2 Your First C# Program C# Programming: From Problem Analysis to Program Design 2 nd Edition.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
Repeating Instructions
Introduction to Computing and Programming
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
C# Programming: From Problem Analysis to Program Design1 2 Your First C# Program.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
Chapter 7: User-Defined Methods
11 Chapter 5 METHODS. 22 INTRODUCTION TO METHODS A method is a named block of statements that performs a specific task. Other languages use the terms.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Introduction to Classes and Objects (Through Ch 5) Dr. John P. Abraham Professor UTPA.
Reading and Writing to the Console Svetlin Nakov Telerik Corporation
Introduction to Classes SWE 344 Internet Protocols & Client Server Programming.
By: Md Rezaul Huda Reza Decision and iteration statements. Methods. E. I. Teodorescu.
CSCI 3327 Visual Basic Chapter 6: Methods: A Deeper Look UTPA – Fall 2011.
1.  A method describes the internal mechanisms that actually perform its tasks  A class is used to house (among other things) a method ◦ A class that.
CSCI 3328 Object Oriented Programming in C# Chapter 3: Introduction to Classes and Objects UTPA – Fall
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
 Simple C# program  Console applications input and output text in a console window, which in Windows XP and Windows Vista is known as the Command Prompt.
Chapter 6: User-Defined Functions
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
Chapter 10 Introduction to Classes
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Introduction to Visual Basic Programming. Introduction Simple Program: Printing a Line of Text Another Simple Program: Adding Integers Memory Concepts.
C# Programming: From Problem Analysis to Program Design1 4 Methods and Behaviors.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
C# Programming: From Problem Analysis to Program Design
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
1/23/2016Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 31 Title: C# Methods Reference: COS240 Syllabus.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Classes, Interfaces and Packages
C# Programming: Basic Concept Part 2 Computer and Programming (204111)
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Microsoft Visual C#.NET: From Problem Analysis to Program Design1 Chapter 4 Methods and Behaviors Microsoft Visual C#.NET: From Problem Analysis to Program.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 24, 2009.
Lecture 02 Dr. Eng. Ibrahim El-Nahry Methods. 2 Learning Objectives Class Definition includes both methods and data properties Method Definition and Declaration.
Methods. The Structure of a Method Essentially, a method is a block of code with a name. You can execute the code by using the method’s name. You can.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
3 Methods and Behaviors C# Programming: From Problem Analysis to Program Design1 4th Edition Methods and Behaviors 3.
Creating Your Own Classes
Reference: COS240 Syllabus
Introduction to Computing and Programming
C# Programming: From Problem Analysis to Program Design
Java Primer 1: Types, Classes and Operators
Chapter 3: Using Methods, Classes, and Objects
Object Oriented Systems Lecture 03 Method
Chapter 4: Writing Classes
C# Programming: From Problem Analysis to Program Design
C# Programming: From Problem Analysis to Program Design
C# Programming: From Problem Analysis to Program Design
Presentation transcript:

C# Programming: From Problem Analysis to Program Design1 Methods and Behaviors C# Programming: From Problem Analysis to Program Design 3rd Edition 3

C# Programming: From Problem Analysis to Program Design2 Chapter Objectives Become familiar with the components of a method Call class methods with and without parameters Use predefined methods in the Console and Math classes Write your own value- and nonvalue-returning class methods (with and without parameters) Distinguish between value, ref, and out parameter types

C# Programming: From Problem Analysis to Program Design3 Chapter Objectives ( continued ) Explore the use of named and optional parameters with default values Work through a programming example that illustrates the chapter’s concepts

C# Programming: From Problem Analysis to Program Design4 Anatomy of a Method Methods defined inside classes Group program statements –Based on functionality –Called one or more times All programs consist of at least one method –Main( ) User-defined method

C# Programming: From Problem Analysis to Program Design5 /* SquareExample.csAuthor:Doyle*/ using System; namespace Square { public class SquareExample { public static void Main( ) { int aValue = 768; int result; result = aValue * aValue; Console.WriteLine(“{0} squared is {1}”, aValue, result); Console.Read( ); } Required method

C# Programming: From Problem Analysis to Program Design6 Anatomy of a Method ( continued ) Figure 3-1 Method components

C# Programming: From Problem Analysis to Program Design7 Modifiers Appear in method headings Appear in the declaration heading for classes and other class members Indicate how it can be accessed Types of modifiers –Static –Access

C# Programming: From Problem Analysis to Program Design8 Static Modifier Indicates member belongs to the type itself rather than to a specific object of a class Main( ) must include static in heading Members of the Math class are static –public static double Pow(double, double) Methods that use the static modifier are called class methods –Instance methods require an object

C# Programming: From Problem Analysis to Program Design9 Access Modifiers public protected internal protected internal private

C# Programming: From Problem Analysis to Program Design10 Level of Accessibility

C# Programming: From Problem Analysis to Program Design11 Return Type Indicates what type of value is returned when the method is completed Always listed immediately before method name void –No value being returned return statement –Required for all non-void methods –Compatible value

C# Programming: From Problem Analysis to Program Design12 Return Type ( continued ) public static double CalculateMilesPerGallon (int milesTraveled, double gallonsUsed) { return milesTraveled / gallonsUsed; } Compatible value (double) returned Return type

C# Programming: From Problem Analysis to Program Design13 Method Names Follow the rules for creating an identifier –Pascal case style –Action verb or prepositional phrase Examples –CalculateSalesTax( ) –AssignSectionNumber( ) –DisplayResults( ) –InputAge( ) –ConvertInputValue( )

C# Programming: From Problem Analysis to Program Design14 Parameters Supply unique data to method Appear inside parentheses –Include data type and an identifier In method body, reference values using identifier name –Parameter refers to items appearing in the heading –Argument for items appearing in the call Formal parameters Actual arguments

C# Programming: From Problem Analysis to Program Design15 Parameters ( continued ) public static double CalculateMilesPerGallon (int milesTraveled, double gallonsUsed) { return milesTraveled / gallonsUsed; } Call to method inside Main( ) method Console.WriteLine(“Miles per gallon = {0:N2}”, CalculateMilesPerGallon(289, 12.2)); Two formal parameters Actual arguments

C# Programming: From Problem Analysis to Program Design16 Parameters ( continued ) Like return types, parameters are optional –Keyword void not required (inside parentheses) – when there are no parameters public void DisplayMessage( ) { Console.Write(”This is “); Console.Write(”an example of a method ”); Console.WriteLine(“body. ”); return; // no value is returned }

C# Programming: From Problem Analysis to Program Design17 Method Body Enclosed in curly braces Include statements ending in semicolons –Declare variables –Do arithmetic –Call other methods Value-returning methods must include return statement

C# Programming: From Problem Analysis to Program Design18 Calling Class Methods Invoke a method Call to method that returns no value [qualifier].MethodName(argumentList); Qualifier –Square brackets indicate optional –Class or object name Call to method does not include data type Use IntelliSense

C# Programming: From Problem Analysis to Program Design19 Predefined Methods Extensive class library Console class –Overloaded methods –Write( ) –WriteLine( ) –Read( ) Not overloaded Returns an integer

C# Programming: From Problem Analysis to Program Design20 IntelliSense Method signature(s) and description After typing the dot, list of members pops up 3-D fuchsia colored box — methods aqua colored box — fields ( not shown ) Figure 3-2 Console class members

C# Programming: From Problem Analysis to Program Design21 IntelliSense Display string argument expected string parameter 18 different Write( ) methods Figure 3-3 IntelliSense display

C# Programming: From Problem Analysis to Program Design22 IntelliSense Display ( continued ) Figure 3-4 Console.Read ( ) signature Figure 3-5 Console.ReadLine ( ) signature

C# Programming: From Problem Analysis to Program Design23 Call Read( ) Methods int aNumber; Console.Write(“Enter a single character: ”); aNumber = Console.Read( ); Console.WriteLine(“The value of the character entered: ” + aNumber); Enter a single character: a The value of the character entered: 97

C# Programming: From Problem Analysis to Program Design24 Call Read( ) Methods ( continued ) int aNumber; Console.WriteLine(“The value of the character entered: “ + (char) Console.Read( )); Enter a single character: a The value of the character entered: a

C# Programming: From Problem Analysis to Program Design25 Call ReadLine( ) Methods More versatile than the Read( ) Returns all characters up to the enter key Not overloaded Always returns a string String value must be parsed

C# Programming: From Problem Analysis to Program Design26 Call Parse( ) Predefined static method All numeric types have a Parse( ) method –double.Parse(“string number”) –int.Parse(“string number”) –char.Parse(“string number”) –bool.Parse(“string number”) Expects string argument –Argument must be a number – string format Returns the number (or char or bool)

C# Programming: From Problem Analysis to Program Design27 /* AgeIncrementer.csAuthor:Doyle */ using System; namespace AgeExample { public class AgeIncrementer { public static void Main( ) { int age; string aValue; Console.Write(“Enter your age: “); aValue = Console.ReadLine( ); age = int.Parse(aValue); Console.WriteLine(“Your age next year” + “ will be {0}”, ++age); Console.Read( ); } } }

C# Programming: From Problem Analysis to Program Design28 /* SquareInputValue.csAuthor: Doyle */ using System; namespace Square { class SquareInputValue { static void Main( ) { string inputStringValue; double aValue, result; Console.Write(“Enter a value to be squared: ”); inputStringValue = Console.ReadLine( ); aValue = double.Parse(inputStringValue); result = Math.Pow(aValue, 2); Console.WriteLine(“{0} squared is {1}”, aValue, result); }

C# Programming: From Problem Analysis to Program Design29 Call Parse( ) ( continued ) string sValue = “True”; Console.WriteLine (bool.Parse(sValue)); // displays True string strValue = “q”; Console.WriteLine(char.Parse(strValue)); // displays q

C# Programming: From Problem Analysis to Program Design30 Call Parse( ) with Incompatible Value Console.WriteLine(char.Parse(sValue)); when sValue referenced “True” Figure 3-6 System.FormatException run-time error

C# Programming: From Problem Analysis to Program Design31 Convert Class More than one way to convert from one base type to another –System namespace — Convert class — static methods –Convert.ToDouble( ) –Convert.ToDecimal( ) –Convert.ToInt32( ) –Convert.ToBoolean( ) –Convert.ToChar( ) int newValue = Convert.ToInt32(stringValue);