using methods

13
Using Methods

Upload: duke

Post on 05-Jan-2016

42 views

Category:

Documents


0 download

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 Presentation

TRANSCRIPT

Page 1: Using Methods

Using Methods

Page 2: Using Methods

Problem

How to create a program that can convert temperature?

Page 3: Using Methods

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

Possible Methods

Convert Celsius to FahrenheitConvert Fahrenheit to Celsius

Page 5: Using Methods

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

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

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

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

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

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

Return public static Double circleArea(Double r) { return 3.14 * r * r; }

Page 12: Using Methods

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

Assessment of Learning

March 8 – TestMarch 8 – Programming assignment