Thursday, November 8, 2012

Class example from Class

Here is the CalculateCubicYards class


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

namespace ClassExample
{
    class CubicYardCalculator
    {
        //Fields 
        private int width; //feet
        private int depth;//inches
        private int length;//feet
        //36 * 36 * 36
        const int CUBICYARDININCHES = 46656;
        const int FOOT=12;

        public CubicYardCalculator()
        {
            Width = 0;
            Length = 0;
            Depth = 0;
        }

        public CubicYardCalculator(int w, int l, int d)
        {
            Width = w;
            Length = l;
            Depth = d;
        }

        //public properties
        public int Width
        {
            set 
            { 

                width = value;
                WidthInInches();
            }
            get { return width; }
        }

        public int Length
        {
            get { return length; }
            set 
            { 
                length = value;
                LengthInInches();
            }
        }

        public int Depth
        {
            get { return depth; }
            set { depth = value; }
        }

        //methods
        private void WidthInInches()
        {
            width=width * FOOT;
        }

        private void LengthInInches()
        {
            length=length * FOOT;
        }

        public int GetCubicYards()
        {
            int cubicYards=(Length * Width * Depth) / CUBICYARDININCHES;
            if((Length * Width * Depth) % CUBICYARDININCHES !=0)
                cubicYards+=1;
            return cubicYards;
        }
    }
}

Here is the Program class

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

namespace ClassExample
{
    class Program
    {
        static void Main(string[] args)
        {
            Program p = new Program();
            //p.DefaultConstructor();
            p.AlternateConstructor();
            Console.ReadKey();
        }

        private void DefaultConstructor()
        {
            CubicYardCalculator cubic = new CubicYardCalculator();
            cubic.Length = 80;
            cubic.Width = 60;
            cubic.Depth = 6;
            Console.WriteLine("You need {0} Cubic Yards", cubic.GetCubicYards());
        }

        private void AlternateConstructor()
        {
            CubicYardCalculator cubic = new CubicYardCalculator(80, 20, 6);
            Console.WriteLine("You need {0} Cubic Yards", cubic.GetCubicYards());
        }
    }
}

No comments:

Post a Comment