Monday, October 28, 2013

Evening Mileage class Revised with comments

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

namespace ClassesExample
{
    class Mileage
    {
        //class variables or fields
        //fields should be private
        private int beginMileage;
        private int endMileage;
        private double pricePerGallon;
        private double gallons;

        //constructors are ways of constructing
        //the class, they set the initial values
        //a constructor always has the same name
        //as the the class and no return type
        //A class can have several "overloaded" constructors
        //only one is used per instance of the class
        //the user chooses which constructor he or she
        //wants to use by the signature (what parameters it takes)

        //this is a default constructor. It has no arguments
        //and sets all the properties to their minumum value;
        //If you write no constructor at all this is what you get
        //if you do write a constructor it is up to you to 
        //provide the initial values
        public Mileage()
        {
            PricePerGallon = 0;
            BeginMileage = 0;
            EndMileage = 0;
            Gallons = 0;
        }

        //this is an overloaded constructor that takes three
        //values. A user could choose to do this rather than
        //set the values using the properties
        public Mileage(int begin, int end, double gal)
        {
            BeginMileage = begin;
            EndMileage = end;
            Gallons = gal;
            PricePerGallon = 0;

        }

        //this constructor takes four values
        public Mileage(int begin, int end, double gal, double price)
        {
            BeginMileage = begin;
            EndMileage = end;
            Gallons = gal;
            PricePerGallon = price;
        }


        //public properties are ways 
        //to expose the fields in a 
        //controlled way
        //you can do some validation
        //and testing in a property
        public double PricePerGallon
        {
            get { return pricePerGallon; }
            set { pricePerGallon = value; }
        }
        

      
        public double Gallons
        {
            get { return gallons; }
            set { gallons = value; }
        }
      

      
        public int BeginMileage
        {
            get { return beginMileage; }
            set { beginMileage = value; }
        }

        public int EndMileage
        {
            get { return endMileage; }
            set { endMileage = value; }
        }

        //a class can contain both private (internal)
        //methods and public methods
        private int TotalMiles()
        {
            return EndMileage - BeginMileage;
        }

        //public method accessible from other
        //classes
        public double CalculateGasMileage()
        {
            int total = TotalMiles();
            double milesPerGallon = total / Gallons;
            return milesPerGallon;
        }

        //this is an overloaded method. It has the same
        //name but a different signature than the previous method
        //again it gives a user of the class another option
        //of how to use the class and call the method
        public double CalculateGasMileage(int bMiles, int eMiles, double gals)
        {
            BeginMileage = bMiles;
            EndMileage = eMiles;
            Gallons = gals;
            int total = TotalMiles();
            double milesPerGallon = total / Gallons;
            return milesPerGallon;


        }

        //this is "overriding" the ToString method
        //which every class inherits from Object
        //the parent of all classes in .NET
        //the combination of overloading and overwriting is 
        //what constitutes the object oriented principle of
        //polymorphism--the ability of a class
        //to behave differently in different environments
        public override string ToString()
        {
            double mpg = CalculateGasMileage();
            return "Your gas mileage is" + mpg.ToString("##.##");
        }


    }
}

No comments:

Post a Comment