Wednesday, November 5, 2014

Batting Average class Examples(Evening)

Here is the BattingAverage class

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

namespace BattingAverageCalculator
{
    class BattingAverage
    {
        //fields
        private int hits;
        private int atBats;
     

        //public properties for the fields
        public int AtBats
        {
            //the get returns the value of the field
            //lets the calling class see it
            get { return atBats; }
            //the set lets the calling class
            //change the value of the underlying field
            //value is a built in variable
            set { atBats = value; }
        }
        public int Hits
        {
            get { return hits; }
            set { hits = value; }
        }

        public double CalculateBattingAverage()
        {
            //this method calculates the Batting Average
            return ((double)Hits / AtBats) * 1000;
           
        }
        



    }
}


Here is the Display class

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

namespace BattingAverageCalculator
{
    class Display
    {
        //declare the BattingAverage Calss
        private BattingAverage ba; 

        //this is the constructor
        //it is called when the class 
        //is made new
        public Display()
        {
            //instantiate (load into memory) 
            //the BattingAverage class
            //
            ba= new BattingAverage();
            //call the GetInput() method
            GetInput();
        }
        private void GetInput()
        {
            //Get the input
            Console.WriteLine("Enter the total at bats");
            //assign the input to the set of the AtBats
            //Property in the BattingAverage class
            ba.AtBats = int.Parse(Console.ReadLine());

            //do the same for the Hits property
            Console.WriteLine("Enter the total hits");
            ba.Hits = int.Parse(Console.ReadLine());

            //call the ShowBattingAVerageMethod
            ShowBattingAverage();

        }

        private void ShowBattingAverage()
        {
            //display the Batting average by calling
            //the CalculateBattingAverage() method
            //in BattingAverage (ba)
            Console.WriteLine("The batting average is " 
                + ba.CalculateBattingAverage());

            
        }
    }
}


Here is the Program

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

namespace BattingAverageCalculator
{
    /********************
     * The program class contains the main
     * It really should do nothing
     * but call the class that starts the program
     * *******************/
    class Program
    {
        static void Main(string[] args)
        {
            //Call the display Class (runs the constructor)
            Display d = new Display();
            Console.ReadKey();
        }
    }

}

No comments:

Post a Comment