Monday, November 3, 2014

First Class Example (Evening)

Here is the Mileage Class

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

namespace ClassExamples
{
    //fields --class level variables that describe the class
    //properties--make field accessible
    //methods--what the class does
    //constructors--initializing the class

 

    class Mileage
    {
        /************************
         * this class calcuates simple mileage
         * it is more work that you need 
         * for such a simple calulation but it shows
         * the basic parts and concepts of a class
        ***************************/
        //private fields 
        private double gallons;
        private double miles;

        //we have two constructors
        //constructors are methods that
        //initialize the class
        //you can have as many constructors
        //as make sense as long as they
        //have distinct signatures
        //you can only initialize a class
        //one way at a time, so a user
        //has to decide which constructor
        //to invoke
        public Mileage()
        {
            //initialize values
            Miles = 0;
            gallons = 1;
        }
        //overloaded constructor that takes two arguments
         public Mileage(double miles, double gallons)
        {
             //initialize values to what has been passed in
             //through the constructor's parameters
            Miles = miles;
            Gallons = gallons;
        }
        //public properties. A property "encapsulates"
        //a private field and exposes it to other
        //classes to see or change
        public double Miles
        {
            //lets the user see the value
            get { return miles; } 
            //lets the user change the value
            set { miles = value; }
        }

        public double Gallons
        {
            set 
            { 
                //one can do validation in a property
                if(value <=0)
                {
                    //an exception is an error message
                    //we can create our own
                    //because there is no way to display
                    //the error message in this class
                    //we throw it back to where the set
                    //message is called--in our case
                    //the Main() method in Program
                    Exception ex = new Exception("Enter a valid number for gallons");
                    throw ex;
                }
                else { 
                    //if the value is good just assign it to the field
                gallons = value;
                }
            }
            get { return gallons; }
        }

        //public method
        public double CalculateGasMileage()
        {
            return Miles / Gallons;
        }

    }
}

Here is the Program class

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

namespace ClassExamples
{
    class Program
    {
        static void Main(string[] args)
        {
            //try catches "try" all the code. When they encounter
            //an error they fall immediately to the catch
            //skipping any lines after the error.
           try
            {
            Console.WriteLine("Enter the Miles traveled");
            double miles = double.Parse(Console.ReadLine());
            Console.WriteLine("Enter the gallons");
            
                double gallons = double.Parse(Console.ReadLine());
               //this uses the overloaded constructor of Mileage
                Mileage mileage = new Mileage(miles, gallons);
               //we call the CalculateGasMileage method
               //of the MileageClass
                Console.WriteLine("You MPG is " + mileage.CalculateGasMileage().ToString());
            }
            catch(Exception ex)
            {
                //this is a general catch. It will catch any error message 
                //and display the error object's message
                //you can do more that display error messages in a catch
                //You can redirect the code or do things to manage
                //the error
                Console.WriteLine(ex.Message);
                Console.ReadKey();
                return;
            }

           
            Console.ReadKey();
        }
    }
}

No comments:

Post a Comment