Download presentation
Presentation is loading. Please wait.
1
C# Programming: From Problem Analysis to Program Design1 Methods and Behaviors C# Programming: From Problem Analysis to Program Design 3rd Edition 3
2
Part II C# Programming: From Problem Analysis to Program Design2
3
3
4
4
5
5
6
6 Math( ) Class double aValue = 78.926; double result1, result2; result1 = Math.Floor(aValue); // result1 = 78 result2 = Math.Sqrt(aValue); // result2 = 8.88403061678651 Console.Write(“aValue rounded to 2 decimal places” + “ is {0}”, Math.Round(aValue, 2)); aValue rounded to 2 decimal places is 78.93 Each call returns a value
7
C# Programming: From Problem Analysis to Program Design7 Method Calls That Return Values Line 1 int aValue = 200; Line 2 int bValue = 896; Line 3 int result; Line 4 result = Math.Max(aValue, bValue); // result = 896 Line 5 result += bValue * Line 6 Math.Max(aValue, bValue) – aValue; // result = 896 + (896 * 896 - 200) (result = 803512) Line 7 Console.WriteLine(“Largest value between {0} ” Line 8 + “and {1} is {2}”, aValue, bValue, Line 9 Math.Max(aValue, bValue)); In an assignment statement Part of arithmetic expression Argument to another method call
8
C# Programming: From Problem Analysis to Program Design8 Writing Your Own Class Methods [modifier(s)] returnType MethodName ( parameterList ) { // body of method - consisting of executable statements } void Methods –Simplest to write –No return statement
9
C# Programming: From Problem Analysis to Program Design9 Writing Your Own Class Methods – void Types public static void DisplayInstructions( ) { Console.WriteLine(“This program will determine how ” + “much carpet to purchase.”); Console.WriteLine( ); Console.WriteLine(“You will be asked to enter the ” + “ size of the room and ”); Console.WriteLine(“the price of the carpet, ” + ”in price per square yards.”); Console.WriteLine( ); } class method A call to this method looks like: DisplayInstructions( );
10
C# Programming: From Problem Analysis to Program Design10 Writing Your Own Class Methods – void Types ( continued ) public static void DisplayResults(double squareYards, double pricePerSquareYard) { Console.Write(“Total Square Yards needed: ”); Console.WriteLine(“{0:N2}”, squareYards); Console.Write(“Total Cost at {0:C} “, pricePerSquareYard); Console.WriteLine(“ per Square Yard: {0:C}”, (squareYards * pricePerSquareYard)); } static method called from within the class where it resides To invoke method – DisplayResults(16.5, 18.95);
11
C# Programming: From Problem Analysis to Program Design11 Value-Returning Method Has a return type other than void Must have a return statement –Compatible value Zero, one, or more data items may be passed as arguments Calls can be placed: –In assignment statements –In output statements –In arithmetic expressions –Or anywhere a value can be used
12
C# Programming: From Problem Analysis to Program Design12 Value-Returning Method ( continued ) public static double GetLength( ) { string inputValue; int feet, inches; Console.Write(“Enter the Length in feet: ”); inputValue = Console.ReadLine( ); feet =int.Parse(inputValue); Console.Write(“Enter the Length in inches: “); inputValue = Console.ReadLine( ); inches = int.Parse(inputValue); return (feet + (double) inches / 12); } Return type→ double double returned
13
C# Programming: From Problem Analysis to Program Design13 CarpetExampleWithClassMethods /* CarpetExampleWithClassMethods.cs */ using System; namespace CarpetExampleWithClassMethods { public class CarpetWithClassMethods { public static void Main( ) { double roomWidth, roomLength, pricePerSqYard, noOfSquareYards; DisplayInstructions( ); // Call getDimension( ) to get length roomLength = GetDimension(“Length”);
14
C# Programming: From Problem Analysis to Program Design14 CarpetExampleWithClassMethods ( continued ) /* CarpetExampleWithClassMethods.cs */ using System; namespace CarpetExampleWithClassMethods { public class CarpetWithClassMethods {
15
C# Programming: From Problem Analysis to Program Design15 public static void Main( ) { double roomWidth, roomLength, pricePerSqYard, noOfSquareYards; DisplayInstructions( ); // Call getDimension( ) to get length roomLength = GetDimension(“Length”); roomWidth = GetDimension(“Width”); pricePerSqYard = GetPrice( ); noOfSquareYards = DetermineSquareYards(roomWidth, roomLength); DisplayResults(noOfSquareYards, pricePerSqYard); }
16
C# Programming: From Problem Analysis to Program Design16 public static void DisplayInstructions( ) { Console.WriteLine(“This program will determine how much " + “carpet to purchase.”); Console.WriteLine( ); Console.WriteLine("You will be asked to enter the size of ” + “the room "); Console.WriteLine(“and the price of the carpet, in price per” + “ square yds.”); Console.WriteLine( ); }
17
C# Programming: From Problem Analysis to Program Design17 public static double GetDimension(string side ) { string inputValue; // local variables int feet, // needed only by this inches; // method Console.Write("Enter the {0} in feet: ", side); inputValue = Console.ReadLine( ); feet = int.Parse(inputValue); Console.Write("Enter the {0} in inches: ", side); inputValue = Console.ReadLine( ); inches = int.Parse(inputValue); // Note: cast required to avoid int division return (feet + (double) inches / 12); }
18
C# Programming: From Problem Analysis to Program Design18 public static double GetPrice( ) { string inputValue; // local variables double price; Console.Write(“Enter the price per Square Yard: "); inputValue = Console.ReadLine( ); price = double.Parse(inputValue); return price; }
19
C# Programming: From Problem Analysis to Program Design19 public static double DetermineSquareYards (double width, double length) { const int SQ_FT_PER_SQ_YARD = 9; double noOfSquareYards; noOfSquareYards = length * width / SQ_FT_PER_SQ_YARD; return noOfSquareYards; } public static double DeterminePrice (double squareYards, double pricePerSquareYard) { return (pricePerSquareYard * squareYards); }
20
C# Programming: From Problem Analysis to Program Design20 public static void DisplayResults (double squareYards, double pricePerSquareYard) { Console.WriteLine( ); Console.Write(“Square Yards needed: ”); Console.WriteLine("{0:N2}", squareYards); Console.Write("Total Cost at {0:C} ", pricePerSquareYard); Console.WriteLine(“ per Square Yard: {0:C}”, DeterminePrice(squareYards, pricePerSquareYard)); } } // end of class } // end of namespace
21
C# Programming: From Problem Analysis to Program Design21 CarpetExampleWithClassMethods ( continued ) Figure 3-7 Output from CarpetExampleWithClassMethods
22
C# Programming: From Problem Analysis to Program Design22 Types of Parameters Call by value –Copy of the original value is made Other types of parameters –ref –out –params ref and out cause a method to refer to the same variable that was passed into the method
23
C# Programming: From Problem Analysis to Program Design23 Types of Parameters ( continued ) Figure 3-9 Call by reference versus value
24
Optional Parameters May assign default values to parameters –When you assign a default value to a parameter, it then becomes an optional parameter public static void DoSomething(string name, int age = 21, bool currentStudent = true, string major = “CS”) Can now call DoSomething( ) and send in arguments for the default value or the default values will be assigned to the parameters C# Programming: From Problem Analysis to Program Design24
25
Named Parameters Named arguments free you from the need to remember or to look up the order of parameters for the method call DoSomething (name: “Paul Nelson”, major: “BIO”); C# Programming: From Problem Analysis to Program Design25
26
C# Programming: From Problem Analysis to Program Design26 JoggingDistance Example Figure 3-11 Problem specification for JoggingDistance example
27
C# Programming: From Problem Analysis to Program Design27 Data for the JoggingDistance Example
28
C# Programming: From Problem Analysis to Program Design28 JoggingDistance Example ( continued ) Figure 3-12 Prototype
29
C# Programming: From Problem Analysis to Program Design29 JoggingDistance Example ( continued ) Figure 3-13 Class diagrams
30
C# Programming: From Problem Analysis to Program Design30 Figure 3-14 Structured English for the JoggingDistance example
31
Coding Standards Naming conventions Spacing conventions Declaration conventions Commenting conventions C# Programming: From Problem Analysis to Program Design31
32
C# Programming: From Problem Analysis to Program Design32 Chapter Summary Components of a method Class methods –Parameters Predefined methods Value- and nonvalue-returning methods
33
C# Programming: From Problem Analysis to Program Design33 Chapter Summary ( continued ) Types of parameters Optional parameters Named parameters
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.