using methods
DESCRIPTION
Using Methods. Problem. How to create a program that can convert temperature?. Methods. A method is a member that implements a computation or action that can be performed by an object or class . (C# Language Specification, p. 15) Used for a small block of code that may be resused. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Using Methods](https://reader035.vdocument.in/reader035/viewer/2022062408/56813ae2550346895da336b2/html5/thumbnails/1.jpg)
Using Methods
![Page 2: Using Methods](https://reader035.vdocument.in/reader035/viewer/2022062408/56813ae2550346895da336b2/html5/thumbnails/2.jpg)
Problem
How to create a program that can convert temperature?
![Page 3: Using Methods](https://reader035.vdocument.in/reader035/viewer/2022062408/56813ae2550346895da336b2/html5/thumbnails/3.jpg)
Methods
A method is a member that implements a computation or action that can be performed by an object or class.
(C# Language Specification, p. 15)
Used for a small block of code that may be resused
![Page 4: Using Methods](https://reader035.vdocument.in/reader035/viewer/2022062408/56813ae2550346895da336b2/html5/thumbnails/4.jpg)
Possible Methods
Convert Celsius to FahrenheitConvert Fahrenheit to Celsius
![Page 5: Using Methods](https://reader035.vdocument.in/reader035/viewer/2022062408/56813ae2550346895da336b2/html5/thumbnails/5.jpg)
PseudocodeMain()Prompt the user for the conversion typefahrenheitToCelsius()Prompt the user for a temperature in degrees FahrenheitConvert the temperature to degrees CelsisusDisplay the temperaturecelsiusToFahrenheit()Prompt the user for a temperature in degrees CelsiusConvert the temperature to degrees FahrenheitDisplay the temperature
![Page 6: Using Methods](https://reader035.vdocument.in/reader035/viewer/2022062408/56813ae2550346895da336b2/html5/thumbnails/6.jpg)
Fahrenheit to Celsius method /********************** * fahrenheitToCelsius method * Converts fahrenheit to celsius * ********************/ public static void fahrenheitToCelsius() { Double fTemp, cTemp;//Declare variables Console.WriteLine("Enter a fahrenheit temperature: "); Double.TryParse(Console.ReadLine(), out fTemp); cTemp = (double)5 / (double)9 * (fTemp - 32); Console.WriteLine("The Celsius temperature is " + cTemp.ToString("0.00")); }//end fahrenheitToCelsius
![Page 7: Using Methods](https://reader035.vdocument.in/reader035/viewer/2022062408/56813ae2550346895da336b2/html5/thumbnails/7.jpg)
Celsius to Fahrenheit method public static void celsiusToFahrenheit() { Double fTemp, cTemp;//Declare variables Console.WriteLine("Enter a celsius temperature: "); Double.TryParse(Console.ReadLine(), out cTemp); fTemp = (double)9 / (double)5 * cTemp + 32; Console.WriteLine("The Fahrenheit temperature is " + fTemp.ToString("0.00")); }//end celsiusToFahrenheit
![Page 8: Using Methods](https://reader035.vdocument.in/reader035/viewer/2022062408/56813ae2550346895da336b2/html5/thumbnails/8.jpg)
Main Method /********************** * Main method * Runs at start * ********************/ static void Main(string[] args) { Int32 choice;//Variable declarations //Prompt for conversion Console.WriteLine("1. Fahrenheit to Celsius conversion."); Console.WriteLine("2. Celsius to Fahrenheit conversion."); Console.WriteLine("Enter your choice"); Int32.TryParse(Console.ReadLine(), out choice); if (choice == 1) { fahrenheitToCelsius(); }else{ celsiusToFahrenheit(); }//end if-else Console.ReadLine();// }//end Main method
![Page 9: Using Methods](https://reader035.vdocument.in/reader035/viewer/2022062408/56813ae2550346895da336b2/html5/thumbnails/9.jpg)
Creating a method
public: method-modifier Static: method-modifier Void: return-type celsiusToFahrenheit(): method-nameThe body of the method follows in a block of code defined by {//code goes here}
public static void celsiusToFahrenheit()
![Page 10: Using Methods](https://reader035.vdocument.in/reader035/viewer/2022062408/56813ae2550346895da336b2/html5/thumbnails/10.jpg)
Parameters public static Double circleArea(Double r) { return 3.14 * r * r; }
public static Double rectangleArea(Double w, Double l) { return w * l; }
![Page 11: Using Methods](https://reader035.vdocument.in/reader035/viewer/2022062408/56813ae2550346895da336b2/html5/thumbnails/11.jpg)
Return public static Double circleArea(Double r) { return 3.14 * r * r; }
![Page 12: Using Methods](https://reader035.vdocument.in/reader035/viewer/2022062408/56813ae2550346895da336b2/html5/thumbnails/12.jpg)
Scope
A variable declared within a method is no longer available after the method has run (Local Scope)
The static keyword allows this variable to be used in any method in the class Program
class Program { //Global Scope Variable Declaration static Double dblHST = 0.13;
![Page 13: Using Methods](https://reader035.vdocument.in/reader035/viewer/2022062408/56813ae2550346895da336b2/html5/thumbnails/13.jpg)
Assessment of Learning
March 8 – TestMarch 8 – Programming assignment