3 Methods and Behaviors C Programming From Problem
3 Methods and Behaviors C# Programming: From Problem Analysis to Program Design 4 th Edition 1
Calling Class Methods • Also called invoking a method • Call to method that returns no value [qualifier]. Method. Name(argument. List); • Qualifier – Square brackets indicate optional – Class or object name • Call to method does not include data type • Use Intelli. Sense C# Programming: From Problem Analysis to Program Design 2
Intelli. Sense After typing the dot, list of members pops up Method signature(s) and description 3 -D box — member methods Figure 3 -2 Console class members C# Programming: From Problem Analysis to Program Design 3
Predefined Methods • C# has extensive class library – Classes have number of predefined methods • Console class (in System namespace) – Overloaded methods • Multiple methods with same name, but each has different number or type of parameter C# Programming: From Problem Analysis to Program Design 4
Intelli. Sense Display string argument expected string parameter 18 different Write( ) methods Figure 3 -3 Intelli. Sense display C# Programming: From Problem Analysis to Program Design 5
Predefined Methods • Console class – Overloaded methods • Write( ) • Write. Line( ) – Read( ) • Not overloaded • Returns an integer C# Programming: From Problem Analysis to Program Design 6
Read( ) Method Figure 3 -4 Console. Read ( ) signature C# Programming: From Problem Analysis to Program Design 7
Call Read( ) Methods int a. Number; Console. Write("Enter a single character: "); a. Number = Console. Read( ); Console. Write. Line("The value of the character entered: " + a. Number); Enter a single character: a The value of the character entered: 97 C# Programming: From Problem Analysis to Program Design 8
Call Read( ) Methods (continued) int a. Number; Console. Write. Line("The value of the character entered: " + (char) Console. Read( )); Explicit cast Enter a single character: a The value of the character entered: a C# Programming: From Problem Analysis to Program Design 9
Read. Line( ) • Returns string • Not overloaded • Returns characters entered up to the enter key Figure 3 -5 Console. Read. Line ( ) signature C# Programming: From Problem Analysis to Program Design 10
Call Read. Line( ) 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 Design 11
/* Age. Incrementer. cs Author: Doyle */ using System; namespace Age. Example { public class Age. Incrementer { public static void Main( ) { int age; string a. Value; Console. Write("Enter your age: "); a. Value = Console. Read. Line( ); age = int. Parse(a. Value); Console. Write. Line("Your age next year" + " will be {0} ", ++age); Console. Read( ); } } } C# Programming: From Problem Analysis to Program Design 12
Read. Key( ) • Obtains next character entered by user • Used to hold screen • Can be called if you want to allow user to press any key after they finished reading • Not overloaded Console. Read. Key( ); C# Programming: From Problem Analysis to Program Design 13
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 Design 14
/* Square. Input. Value. cs Author: Doyle */ using System; namespace Square { class Square. Input. Value { static void Main( ) { string input. String. Value; double a. Value, result; Console. Write("Enter a value to be squared: "); input. String. Value = Console. Read. Line( ); a. Value = double. Parse(input. String. Value); result = Math. Pow(a. Value, 2); Console. Write. Line("{0} squared is {1} ", a. Value, result); Console. Read. Key( ); } } } // Curly braces should be lined up C# Programming: From Problem Analysis to Program Design 15
Call Parse( ) (continued) string s. Value = " true "; Console. Write. Line (bool. Parse(s. Value)); // displays True string str. Value = "q"; Console. Write. Line(char. Parse(str. Value)); // displays q C# Programming: From Problem Analysis to Program Design 16
Call Parse( ) with Incompatible Value • Console. Write. Line(char. Parse(s. Value)); when s. Value referenced “True” Figure 3 -6 System. Format. Exception run-time error C# Programming: From Problem Analysis to Program Design 17
Convert Class • More than one way to convert from one base type to another – – – System namespace — Convert class — static methods Convert. To. Double( ) Convert. To. Decimal( ) Convert. To. Int 32( ) Convert. To. Boolean( ) Convert. To. Char( ) int new. Value = Convert. To. Int 32(string. Value); C# Programming: From Problem Analysis to Program Design 18
Methods in the Math class Table 3 -2 Math class methods C# Programming: From Problem Analysis to Program Design 19
Math class (continued) Table 3 -2 Math class methods C# Programming: From Problem Analysis to Program Design 20
Math class (continued) Table 3 -2 Math class methods C# Programming: From Problem Analysis to Program Design 21
Math class (continued) Table 3 -2 Math class methods C# Programming: From Problem Analysis to Program Design 22
Math( ) Class Each call double a. Value = 78. 926; returns a value double result 1, result 2; result 1 = Math. Floor(a. Value); // result 1 = 78 result 2 = Math. Sqrt(a. Value); // result 2 = 8. 88403061678651 Console. Write("a. Value rounded to 2 decimal places" + " is {0}", Math. Round(a. Value, 2)); a. Value rounded to 2 decimal places is 78. 93 C# Programming: From Problem Analysis to Program Design 23
Method Calls That Return Values In an assignment Line 1 int a. Value = 200; statement Line 2 int b. Value = 896; Line 3 int result; Line 4 result = Math. Max(a. Value, b. Value); // result = 896 Line 5 result += b. Value * Part of arithmetic expression Line 6 Math. Max(a. Value, b. Value) – a. Value; // result = 896 + (896 * 896 - 200) (result = 803512) Line 7 Console. Write. Line("Largest value between {0} " Line 8 + "and {1} is {2}", a. Value, b. Value, Line 9 Math. Max(a. Value, b. Value)); Argument to another method call C# Programming: From Problem Analysis to Program Design 24
- Slides: 24