Tuesday, October 2, 2012

Gas Mileage with Methods

Here is the Gas Mileage calculator without methods that we did as the first assignment

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

namespace GasMileageCalculator
{
    /*********************
     * This program will calculate miles per
     * gallon and price per mile
     * Steve Conger 9/27/2012
     * *********************/
    class Program
    {
        static void Main(string[] args)
        {
            //get inputs 
            //prompt user 
            Console.WriteLine("Enter the Total Mileage");//write prompt
            int miles = int.Parse (Console.ReadLine());

            Console.WriteLine("Enter the total Gallons");
            int gallons = int.Parse(Console.ReadLine());

            Console.WriteLine("Enter the Price Per Gallon");
            double price = double.Parse(Console.ReadLine());

            //Calculate the outputs

            double milesPerGallon = (double) miles / gallons;
            double pricePerMile = price / milesPerGallon;

            // Display outputs
            Console.WriteLine();
            Console.WriteLine("Your miles per gallon is {0:F2} \n", milesPerGallon);
            
            Console.WriteLine("The price per mile is {0:c}", pricePerMile);

            Console.ReadKey();



        }
    }
}


Methods

A few words about methods:

Methods are ways of braking up code into more manageable blocks.

Ideally each method does one thing. It can be one complicated thing involving several lines of code, but still one thing.

In order to execute a method must be called. You call a method by using its name and providing any required parameters. (See Example Two.)

Methods can only be called from other methods. At least one method must be called from the Main method to start the program

Methods can be private meaning they can only be seen by other methods in the current class, or they can be public meaning they can be seen by other classes and programs. (there are other options in between such as protected.)

Methods can be void (as in the first example) meaning that they return nothing to the calling method or they can return a value(see example two)


Here is the code from class. This version makes the variables have class scope, which means they can be accessed by any of the methods in the class.

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



namespace GasMileageCalculatorWithMethods
{
    class Program
    {
        /*
         * get inputs
         * calculate miles per Gallon
         * calculate price per mile
         * Display the outputs
         */
        //declaring variables with class scope
        int miles;
        int gallons;
        double price;
        
        static void Main(string[] args)
        {
            //The Program class is not yet loaded
            //into memory, though the Main method is
            //because it is static, so we have to
            //load the class into memory with the new
            //keyword
            Program p = new Program();
            p.CallMethods();
           
        }//end main



        private void GetMiles()
        {
            Console.WriteLine("Please Enter the total Miles");
            miles = int.Parse(Console.ReadLine());
        }//end GetMiles

        private void GetGallons()
        {
            Console.WriteLine("Please Enter the total Gallons");
            gallons = int.Parse(Console.ReadLine());
        }//end getGallons

        private void GetPricePerGallon()
        {
            Console.WriteLine("Please Enter the price per Gallon");
            price= double.Parse(Console.ReadLine());
        }

        private double CalculateMilesPerGallon()
        {
            double mpg;
            mpg=miles / (double)gallons;
            return mpg;
        }

        private double CalculateCostPerMile()
        {
            return price / CalculateMilesPerGallon();
        }

        private void DisplayResults()
        {
            Console.WriteLine("Your Miles per Gallon is {0:F2}", CalculateMilesPerGallon());
            Console.WriteLine("The cost per mile is {0:C}", CalculateCostPerMile());
            Console.ReadKey();
        }

        private void CallMethods()
        {
            GetMiles();
            GetGallons();
            GetPricePerGallon();
            DisplayResults();
        }

    }//end class
}//end namespace




Here is a second example of the same program. Instead of making variables with class level scope, the variables are passed as parameters to the appropriate methods

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

namespace GasMileageCalculatorMethods
{
    class Program
    {
        //This program use methods with parameters
        // to calculate gas mileage
       
        double price;

        static void Main(string[] args)
        {
            //explain this
            Program p = new Program();
            p.Display();
        }

        private void Display()
        {
            //calling methods
            GetInputs();
            Console.ReadKey();
        }

        private void GetInputs()
        {
            //this method could be broken up as we did
            //in the example above
            Console.WriteLine("Enter the total Miles");
            int miles = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter the Total gallons");
            double gallons = double.Parse(Console.ReadLine());

            //call and pass values
            double milesPerGallon=CalculateMileage(miles, gallons);

            Console.WriteLine("Enter the price per gallon");
            price = double.Parse(Console.ReadLine());

            double pricePerMile = CalculatePrice(price, milesPerGallon);

            DisplayOutputs(milesPerGallon, pricePerMile);


        }

        private double CalculateMileage(int m, double gals)
        {
            //explain return
            return m / gals;
        }

        private double CalculatePrice( double pr, double mpg)
        {
            return pr  / mpg;
        }

        private void DisplayOutputs(double milesPerGallon, double pricePerMile)
        {
            //explain calling the method 
            Console.WriteLine("You got {0:F2} miles per gallon", milesPerGallon);
            Console.WriteLine("The price per mile was {0:C}", pricePerMile);
        }
    }
}

No comments:

Post a Comment