ch03_PPT - 3 C Programming From Problem Analysis to Program...

This preview shows page 1 - 14 out of 56 pages.

C# Programming: From Problem Analysis to Program Design 1 Methods and Behaviors 3
Image of page 1

Subscribe to view the full document.

C# Programming: From Problem Analysis to Program Design 2 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 class methods value, ref, and out parameter types named and optional parameters with default values
Image of page 2
C# Programming: From Problem Analysis to Program Design 3 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
Image of page 3

Subscribe to view the full document.

C# Programming: From Problem Analysis to Program Design 4 /* SquareExample.cs Author: 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
Image of page 4
C# Programming: From Problem Analysis to Program Design 5 Anatomy of a Method ( continued ) Figure 3-1 Method components
Image of page 5

Subscribe to view the full document.

C# Programming: From Problem Analysis to Program Design 6 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
Image of page 6
C# Programming: From Problem Analysis to Program Design 7 static Modifier Indicates member belongs to the type itself rather than to a specific object of a class Main( ) must include static in heading Methods that use static modifier - class methods Instance methods require an object Members of the Math class are static public static double Pow( double , double ) double answer = Math.Sqrt(25);
Image of page 7

Subscribe to view the full document.

C# Programming: From Problem Analysis to Program Design 8 Access Modifiers public protected internal protected internal private
Image of page 8
C# Programming: From Problem Analysis to Program Design 9 Level of Accessibility Table 3-1 C# access modifiers
Image of page 9

Subscribe to view the full document.

C# Programming: From Problem Analysis to Program Design 10 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
Image of page 10
C# Programming: From Problem Analysis to Program Design 11 Return Type ( continued ) public static double CalculateMilesPerGallon ( int milesTraveled, double gallonsUsed) { return milesTraveled / gallonsUsed; } Compatible value (double) returned Return type
Image of page 11

Subscribe to view the full document.

C# Programming: From Problem Analysis to Program Design 12 Method Names Follow the rules for creating an identifier Pascal case style Action verb or prepositional phrase Examples CalculateSalesTax( ) AssignSectionNumber( ) DisplayResults( ) InputAge( ) ConvertInputValue( )
Image of page 12
C# Programming: From Problem Analysis to Program Design 13 Parameters Supply unique data to method
Image of page 13

Subscribe to view the full document.

Image of page 14
  • Winter '17
  • Qizhi Dai
  • Program design, Subroutine, Doyle, Own Class

{[ snackBarMessage ]}

Get FREE access by uploading your study materials

Upload your study materials now and get free access to over 25 million documents.

Upload now for FREE access Or pay now for instant access
Christopher Reinemann
"Before using Course Hero my grade was at 78%. By the end of the semester my grade was at 90%. I could not have done it without all the class material I found."
— Christopher R., University of Rhode Island '15, Course Hero Intern

Ask a question for free

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern