130 likes | 290 Views
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.
E N D
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
Possible Methods • Convert Celsius to Fahrenheit • Convert Fahrenheit to Celsius
Pseudocode • Main()Prompt the user for the conversion type • fahrenheitToCelsius()Prompt the user for a temperature in degrees FahrenheitConvert the temperature to degrees CelsisusDisplay the temperature • celsiusToFahrenheit()Prompt the user for a temperature in degrees CelsiusConvert the temperature to degrees FahrenheitDisplay the temperature
Creating a method • public: method-modifier • Static: method-modifier • Void: return-type • celsiusToFahrenheit(): method-name • The body of the method follows in a block of code defined by {//code goes here}
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
Assessment of Learning • March 8 – Test • March 8 – Programming assignment