1 / 33

Methods and Behaviors

3. Methods and Behaviors. C# Programming: From Problem Analysis to Program Design 3rd Edition. Part II. Math( ) Class . Each call returns a value. double aValue = 78.926; double result1, result2; result1 = Math.Floor(aValue); // result1 = 78

ronni
Download Presentation

Methods and Behaviors

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Part II C# Programming: From Problem Analysis to Program Design

  3. C# Programming: From Problem Analysis to Program Design

  4. C# Programming: From Problem Analysis to Program Design

  5. C# Programming: From Problem Analysis to Program Design

  6. Math( ) Class Each call returns a value • 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 C# Programming: From Problem Analysis to Program Design

  7. Method Calls That Return Values In an assignment statement Line 1int aValue = 200; Line 2int bValue = 896; Line 3int 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)); Part of arithmetic expression Argument to another method call C# Programming: From Problem Analysis to Program Design

  8. 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 C# Programming: From Problem Analysis to Program Design

  9. class method Writing Your Own Class Methods – void Types A call to this method looks like: DisplayInstructions( ); public staticvoid 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( ); } C# Programming: From Problem Analysis to Program Design

  10. 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); C# Programming: From Problem Analysis to Program Design

  11. 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 C# Programming: From Problem Analysis to Program Design

  12. Value-Returning Method (continued) public staticdouble 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 C# Programming: From Problem Analysis to Program Design

  13. CarpetExampleWithClassMethods /* CarpetExampleWithClassMethods.cs */ using System; namespace CarpetExampleWithClassMethods { publicclass CarpetWithClassMethods { publicstaticvoid Main( ) { double roomWidth, roomLength, pricePerSqYard, noOfSquareYards; DisplayInstructions( ); // Call getDimension( ) to get length roomLength = GetDimension(“Length”); C# Programming: From Problem Analysis to Program Design

  14. CarpetExampleWithClassMethods (continued) /* CarpetExampleWithClassMethods.cs */ using System; namespace CarpetExampleWithClassMethods { publicclass CarpetWithClassMethods { C# Programming: From Problem Analysis to Program Design

  15. publicstaticvoid 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); } C# Programming: From Problem Analysis to Program Design

  16. public staticvoid 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( ); } C# Programming: From Problem Analysis to Program Design

  17. publicstaticdouble 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); } C# Programming: From Problem Analysis to Program Design

  18. public staticdouble GetPrice( ) { string inputValue; // local variables double price; Console.Write(“Enter the price per Square Yard: "); inputValue = Console.ReadLine( ); price = double.Parse(inputValue); return price; } C# Programming: From Problem Analysis to Program Design

  19. public staticdouble DetermineSquareYards (double width, double length) { constint SQ_FT_PER_SQ_YARD = 9; double noOfSquareYards; noOfSquareYards = length * width / SQ_FT_PER_SQ_YARD; return noOfSquareYards; } public staticdouble DeterminePrice (double squareYards, double pricePerSquareYard) { return (pricePerSquareYard * squareYards); } C# Programming: From Problem Analysis to Program Design

  20. public staticvoid 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 C# Programming: From Problem Analysis to Program Design

  21. CarpetExampleWithClassMethods (continued) Figure 3-7 Output from CarpetExampleWithClassMethods C# Programming: From Problem Analysis to Program Design

  22. 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 C# Programming: From Problem Analysis to Program Design

  23. Types of Parameters (continued) Figure 3-9 Call by reference versus value C# Programming: From Problem Analysis to Program Design

  24. Optional Parameters • May assign default values to parameters • When you assign a default value to a parameter, it then becomes an optional parameter publicstaticvoid 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 Design

  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 Design

  26. JoggingDistance Example Figure 3-11 Problem specification for JoggingDistance example C# Programming: From Problem Analysis to Program Design

  27. Data for the JoggingDistance Example C# Programming: From Problem Analysis to Program Design

  28. JoggingDistance Example (continued) Figure 3-12 Prototype C# Programming: From Problem Analysis to Program Design

  29. JoggingDistance Example (continued) Figure 3-13 Class diagrams C# Programming: From Problem Analysis to Program Design

  30. Figure 3-14 Structured English for the JoggingDistance example C# Programming: From Problem Analysis to Program Design

  31. Coding Standards • Naming Conventions • Spacing Conventions • Declaration Conventions • Commenting Conventions C# Programming: From Problem Analysis to Program Design

  32. Chapter Summary • Components of a method • Class methods • Parameters • Predefined methods • Value and nonvalue-returning methods C# Programming: From Problem Analysis to Program Design

  33. Chapter Summary (continued) • Types of parameters • Optional parameters • Named parameters C# Programming: From Problem Analysis to Program Design

More Related