Wednesday, October 15, 2014

First Methods (Evening)

Here are our two examples of using methods

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MethodsExample
{
    class Program
    {
        /// 
        /// This class shows the use of methods
        /// specifically it shows the use of methods
        /// that have a return type of int
        /// steve 10/15/2014 Evening
        /// 
    
        static void Main(string[] args)
        {
            //instantiate the program by making it new
            //because main is static it is loaded into
            //memory automatically, but the rest
            //of the class is not. Making it new
            //loads it into memory. p is the local variable name
            //of the class. The dot stands for membership
            //p.Display() calls the GetDisplay()
            //method which is a member of Program
            Program p = new Program();
            
            // I only need to call the Display method because
            //it calls the GetSum() method and the GetSum()
            //method calls the GetNumber() method. 
            p.Display();
            p.EndProgram();
        }

       
        //the get number method gets the user's input
        //of a number and returns that number
        private int GetNumber()
        {
            Console.WriteLine("Enter Number");
            int number = int.Parse(Console.ReadLine());
            return number;
        }
        //this method gets two numbers and adds them.
        //Notice it calls the GetNumber() method twice
        //this is an example of reuse. We only have to write
        //the method once. but can use it whenever we need
        //it. The GetNumber() method returns an integer
        //so the addition is not adding the methods it is adding
        //the integers returned by the method
        private int GetSum()
        {
            int sum = GetNumber() + GetNumber();
            return sum;
        }

        private void Display()
        {
            //Call GetSum to get the sum and display it
            int sum = GetSum();
            Console.WriteLine("the sum is " + sum);
        }


        private void EndProgram()
        {
            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
        }
       
    }//end class
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MethodsWithParameters
{
    class Program
    {
        /// 
        /// This class shows the use of methods
        /// and passes values as parameters
        /// from one method to another.
        /// Specifically, it gets the diameter of
        /// a circle in one method, passes it to 
        /// a separate method for calculation
        /// and then to a final method for display
        /// steve  10/15/2014 Evening class
        /// 
        /// 
        //declare a constant for PI
        private const double PI = 3.14156;
        static void Main(string[] args)
        {
            //instantiate the program by making it new
            //because main is static it is loaded into
            //memory automatically, but the rest
            //of the class is not. Making it new
            //loads it into memory. p is the local variable name
            //of the class. The dot stands for membership
            //p.GetDiameter() calls the GetDiameter()
            //method which is a member of Program
            Program p = new Program();
            p.GetDiameter();
            //call end program method
            p.EndProgram();
        }

        private void GetDiameter()
        {
            //ask the user for the diameter
            Console.WriteLine("Please give the diameter of your circle");
            double diameter = double.Parse(Console.ReadLine());

            //call the GetCirucumerance method and pass it
            //diameter as a parameter
            GetCircumference(diameter);
        }

        //GetCircumference method which takes a parameter
        //that is a double in type
        private void GetCircumference(double diam)
        {
            double circumference = diam * PI;

            //call Display and pass it the Circumference
            Display(circumference);
        }

        private void Display(double circ)
        {
            //display the parmater that was passed
            Console.WriteLine("the circumference is " + circ);
        }

        private void EndProgram()
        {
            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
        }
    }//end class
}

No comments:

Post a Comment