Sunday, November 24, 2013

Assignment 7: Division

Division is by far the hardest part of the assignment. Now that you have struggled with it for awhile, lets talk through the process of how you would handle it.

The code for this example can be found at https://github.com/spconger/ThirdGradeMath

First you should identify what the unique elements of division are. Remember we are doing third grade level, integer division.

* For one the first number should be bigger than the second
* we want to avoid 0 in the divisor or the dividend
* because we are using integer division we must handle remainders

First we add a division method and a remainder method to the BasicMath class



You should add some error checking to these methods

Next we add a division button and a textbox called "txtRemainder" to the WebForm. Double click the button to get the click event. Then we call the GetNumbers() method just like the other operators:



We are going to have to make some alterations to the GetNumbers() method, but first lets create a new method that makes sure the first number is bigger than the second number.



We need to make other changes.

Number1 and Number2 need to be declared at the class level



Then we need to change GetNumbers() so that it looks like the code below. Basically, we need to check to see if the operator is subtraction or division, then we need to check if the first number is less than the second one. If it is we call our swap method. The other main difference is that we now assign the random numbers to the variables number1 and number2 rather than directly to the labels. We test them first and then assign them to the labels.



We also have to make some changes to the check answer method. Division needs to be handled differently because of the remainder issue.

So at the top of the CheckAnswer method we add the following if statement:



Next we will create a new check for division. We need to make sure both the answer and remainder session variables exist. Then I use a try parse to check that the users answers are valid. Here is the complete check Division method:



Here is the button click method for division:


Wednesday, November 20, 2013

SQL Part Two: Joins, Insert, Update, Delete

use TutorManagement

Select * From TutorCourse

--here is a simple inner join
--joins are used to "join" two or more tables
--Here the tutor and tutorCourse tables
--to find the names of all the tutors who
--teach Web110
Select Tutor.TutorKey, TutorFirstName, TutorLastName, CourseKey
From Tutor
inner join TutorCourse
on Tutor.TutorKey=TutorCourse.TutorKey
Where CourseKey = 'Web110'

--same query but with the tables aliased.
--it just saves time
Select t.TutorKey, TutorFirstName, TutorLastName, CourseKey
From Tutor t
inner join TutorCourse tc
on t.TutorKey=tc.TutorKey
Where CourseKey = 'Web110'

--an old way to do a join. Shorter but more dangerous
--especially as you start joining more tables
Select t.TutorKey, TutorFirstName, TutorLastName, CourseKey
From Tutor t, TutorCourse tc
Where t.TutorKey=tc.TutorKey
And CourseKey='Web110'

--creates a cross join
--a cross join is where each record in the first table
--is matched to every record in the second table
--this can happen on purpose or by accident
--if you forget to add a realtionshio
Select t.TutorKey, TutorFirstName, TutorLastName, CourseKey
From Tutor t, TutorCourse tc
Where CourseKey='Web110'

Select * From Session

--A join with three tables
Select TutorLastName, SessionDateKey, SessionTimeKey,
Coursekey, StudentLastName
From Tutor
Inner Join [Session] 
on Tutor.TutorKey=[Session].TutorKey
Inner Join Student
on Student.StudentKey=[Session].StudentKey

Select * From tutor
order by TutorLastName

--Insert new records into the tutor table
--if you have an auto increment (identity) column, do not include it in the 
--fields for the insert
Insert into Tutor(TutorKey, TutorLastName, 
TutorFirstName, TutorPhone, 
TutorEmail, TutorHireDate, TutorStatus)
Values('980000000', 'Smith', 'John', '2065552356','js@gmail.com',GetDate(),'Active'),
('980000001', 'Jones', 'Jason', '2065552956','jj@gmail.com',GetDate(),'Active')


--this begins a manual transaction
--this allows for the possiblity of an undo
Begin tran

--updates the tutor table
--essential that this has a where clause
--unless you wish to update all the rows
--in the table
Update Tutor
Set TutorFirstName = 'Jonathan'
Where TutorKey='980000000' 

Select * from Tutor

rollback tran --cancels the transaction, your undo
Commit tran--commits the transaction to the database
--once commited there is no undo

--Deletes a row from the table Tutor
--this is best done in a transaction also
--you cannot delete a parent that has children
--unless you delete the children first
Delete from Tutor
Where TutorKey='980000000' 


Monday, November 18, 2013

Addition ASP Net Page

Here is the RandomGenerator.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// 
/// this class returns a random number
/// between the minimum and maximum values
/// 
public class RandomGenerator
{

    //private variables
  
    private int maximum = 0;
    private int minimum = 0;
    private Random rand;

    //only one constructor that requires a 
    //minimum and maximum value
 public RandomGenerator(int max, int min)
 {
        //initialize the random number 
        rand = new Random();
        maximum = max;
        minimum = min;
     }

    public int GetNumber()
    {
        //return a new random number
        return rand.Next(minimum, maximum);
    }
}

Here is the BasicMath.cs class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// 
/// This class is for doing the most basic
/// of math. I have included only addition
/// 
public class BasicMath
{
    //the private fields
    private int number1;
    private int number2;

    //the empty default constructor
 public BasicMath()
 {
        number1 = 0;
        number2 = 0;
 }

    //an alternate overloaded constructor
    public BasicMath(int num1, int num2)
    {
        NumberOne = num1;
        NumberTwo = num2;
    }

    //public properties
    public int NumberOne
    {
        get { return number1; }
        set { number1 = value; }
    }

    public int NumberTwo
    {
        get { return number2; }
        set { number2 = value; }
    }

    //addition method
    public int Addition()
    {
        return number1 + number2;
    }
}

here is Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>math time</title>
    <link href="BasicMathStyleSheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h1>Math Play Time</h1>
        <p>
        <asp:Button ID="getNumbers" runat="server" Text="+" OnClick="addition_Click" />

        </p>
        <p>
            <asp:Label ID="lblNumber1" runat="server" Text=""></asp:Label>
            <asp:Label ID="lblOperator" runat="server" Text=""></asp:Label>
            <asp:Label ID="lblNumber2" runat="server" Text=""></asp:Label>
            <asp:TextBox ID="txtUserAnswer" runat="server"></asp:TextBox>
            </p>
        <p>
            <asp:Button ID="btnCheckAnswer" runat="server" Text="Check Answer" OnClick="btnCheckAnswer_Click" />
            <asp:Label ID="lblResult" runat="server" Text=""></asp:Label>
        </p>

    </div>
    </form>
</body>
</html>


Here is the Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    //initialize the basic math class
    BasicMath m = new BasicMath();
    int answer = 0; //declare answer variable

    protected void Page_Load(object sender, EventArgs e)
    {
        //this is to provide a problem when the page first loads
        //the !IsPostback guarantees it runs only
        //on the first page load not on button clicks
        if (!IsPostBack)
        GetAddition();
    }
    protected void addition_Click(object sender, EventArgs e)
    {
        //call the get addition method
        GetAddition();
    }

    protected void GetAddition()
    {
        // call the getNumbers method and pass
        //it the min, max, and operator
        GetNumbers(0, 1000, "+");
        //get the answer from the class
        answer = m.Addition();
        //store it in a session variable
        Session["answer"] = answer;
    }

    protected void GetNumbers(int min, int max, string oper)
    {
        //get the random numbers and display them in the labels
        //including the operation
        RandomGenerator rg = new RandomGenerator(max, min);
        lblNumber1.Text = rg.GetNumber().ToString();
        lblNumber2.Text = rg.GetNumber().ToString();
        m.NumberOne = int.Parse(lblNumber1.Text);
        m.NumberTwo = int.Parse(lblNumber2.Text);
        lblOperator.Text = oper;

    }
    protected void btnCheckAnswer_Click(object sender, EventArgs e)
    {
       
        //if the session exists
        if (Session["answer"] != null)
        {
            //get their answer --should be try parse
            int userAnswer = int.Parse(txtUserAnswer.Text);
            //get the actual answer from the session variable
            answer = (int)Session["answer"];
            //if the anser is correct congrats
            if (userAnswer == answer)
            {
                lblResult.Text = "Great job";
                //assign a cssClass on the fly
                lblResult.CssClass = "correct";

            }
            else //otherwise try again
            {
                lblResult.Text = "Try again";
                txtUserAnswer.Text = "";
                txtUserAnswer.Focus();
                lblResult.CssClass = "mistake";
            }
        }
    }
}

Here is BasicMath.css

body {
}

.correct {
    color:green;
}

.mistake {
    color:red;
}

h1 {
    color:navy;
}

SQL (part one)

Use Tutormanagement

--basic sql statement

/*
this is a multiline
comment
*/

Select StudentLastname, Studentfirstname from Student;

Select * From Student

Select StudentLastName as [Last Name], StudentFirstName as [First Name]
From Student
order by StudentLastName desc, StudentFirstname

Select StudentLastName [Last Name], StudentFirstName [First Name]
From Student

Select StudentLastName LastName, StudentFirstName FirstName
From Student

Select distinct TutorKey from [Session]

Select 5 + 14 * (3 /2 )


Use CommunityAssist

Select * from Donation

Select donationamount, donationAmount * .8 as charity, donationamount * .2 as organization
from donation

Use TutorManagement

Select * from Student

Select StudentLastName, StudentEmail from Student
Where StudentLastName= 'Bradbury'

Select StudentLastName, StudentAge
from Student
Where StudentAge > 24

-->,< >=, <=, !=, =,<>

Select StudentLastName, StudentAge
from Student
Where StudentAge > 24
And StudentLastName like 'C%'

Select StudentLastName, StudentAge
from Student
Where StudentAge > 24
or StudentLastName like 'C%'

-- % for any number of characters
-- _ for a single character

-- MIC 1_2  MIC 122, MIC 102,

Select * From Student

Select StudentLastName, StudentEmail
From Student
Where studentEmail is not null


Select StudentLastName, StudentEmail
From Student
Where studentEmail is null

--scalar only operates on one row at a time

Select * from Session
Select distinct Year(SessionDateKey) as [Year] from Session
order by year(SessionDateKey) Desc

Select distinct Month(SessionDateKey) as [Month] from Session
order by Month(SessionDateKey) Desc

Select  Year(SessionDateKey) as [Year], Month(SessionDateKey) as [Month]
From Session
order by year(SessionDateKey) Desc,Month(SessionDateKey) Desc

Select DATEDIFF(dd, '11/18/2013', '12/11/13')
Select DateAdd(yy,  20,'11/18/13')

Select GetDate()

--aggregate operate across multilple rows

Select * From [Session]
Select count(StudentKey) as NumberOfStudents from Student
Select count (Distinct SessionDateKey) as [Sessions] from [Session]
Select count (SessionStatus) as [Sessions] from [Session]

Select Sum(StudentAge) / Count(StudentKey) From Student

Select avg(StudentAge) From Student

Select Max(StudentAge) From Student

Select Min(StudentAge) From Student

Select * From Student

Select Year(SessionDateKey) as [Year], Count(StudentKey) as Students 
from Session
Group by Year(SessionDatekey)

Select TutorKey, Count(StudentKey) as students
from Session
Group by tutorkey




Wednesday, November 13, 2013

Evening Web Example

the Web Form html and asp

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link href="ExampleWebPage.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h1>Enter Quotes</h1>
        <p> Enter your favorite quotes  <asp:TextBox ID="txtQuote" runat="server"></asp:TextBox> </p>
        <p>
            <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" /></p>
        <p>
            <asp:Button ID="btnQuote" runat="server" Text="Get Quote" OnClick="btnQuote_Click" />
        <asp:Label ID="Label1" runat="server" Text="Label" 
            CssClass="response"></asp:Label></p>
        <asp:Button ID="btnGetAllQuotes" runat="server" Text="Get all Quotes" OnClick="btnGetAllQuotes_Click" />
        <asp:BulletedList ID="BulletedList1" runat="server"></asp:BulletedList>
    </div>
    </form>
</body>
</html>

The code behind the web page

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    Quote q = new Quote();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        //if the session exists
        //get the existing instance of the class
        //from the session and use it
        if (Session["quotes"] != null)
        {
            q = (Quote)Session["quotes"];
        }
        q.AddQuote(txtQuote.Text);
        Session["quotes"] = q;
        txtQuote.Text = ""; //clear textbox

    }
    protected void btnQuote_Click(object sender, EventArgs e)
    {
        //get the current instance saved in the session
        q = (Quote)Session["quotes"];
        //call its method
        Label1.Text = q.GetQuote();
    }
    protected void btnGetAllQuotes_Click(object sender, EventArgs e)
    {
        q = (Quote)Session["quotes"];
        BulletedList1.DataSource = q.Quotes;
        BulletedList1.DataBind();
    }
}

the Quote class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// 
/// Summary description for Quote
/// 
public class Quote
{

    // create a list object to store strings
    private List quotes;

    //initialize the list in the constructor
 public Quote()
 {
        quotes = new List();
 }

    //read only property
    public List Quotes
    {
        get { return quotes; }
    }

    //add quotes to the list
    public void AddQuote(string aQuote)
    {
        quotes.Add(aQuote);
    }

    //return a random quote
    public string GetQuote()
    {
        string quoteResult = null;
        Random rand = new Random();
        if (quotes.Count != 0)
        {
            int number = rand.Next(0, quotes.Count );
            quoteResult = quotes[number];
        }
        return quoteResult;
    }
}

the style sheet such as it is

body {
}

h1 {
    background-color:navy;
    color:white;
}

.response {
    color:green;
}

Tuesday, November 12, 2013

boreing stuff ASP.Net page

Here is the web page Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link href="BoreingStuff.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h1>Quote Generator</h1>
        <p>
            Enter a favorite Quote: 
            <asp:TextBox ID="txtQuote" runat="server"></asp:TextBox> <br />
            <asp:Button ID="btnEnter" runat="server" Text="Enter" OnClick="btnEnter_Click" />

        </p>
        <p>
            <asp:Button ID="btnGetQuote" runat="server" Text="Get Quote" OnClick="btnGetQuote_Click" />
            <asp:Label ID="lblResult" runat="server" Text="Label"></asp:Label>
        </p>
    </div>
    </form>
</body>
</html>

Here is the code behind Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    //create a new instance of the Quotes class
    Quotes q = new Quotes();

    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    protected void btnEnter_Click(object sender, EventArgs e)
    {
        //see if the session has something in it
        if (Session["quotes"] != null)
        {
            //if it does use it instead of a new Quotes object
            q = (Quotes)Session["quotes"];
        }
        //write the quote from the textbox into the list
        q.AddQuote(txtQuote.Text);
        //save the object to the session
        Session["quotes"] = q;
        //clear the textbox
        txtQuote.Text = "";
       
    }
    protected void btnGetQuote_Click(object sender, EventArgs e)
    {
        //recall the session and get the quote object
        q = (Quotes)Session["quotes"];
        //call the GetQuote method and assign the string
        //to the label
        lblResult.Text = q.GetQuote();
    }
}

Here is the Quotes.cs class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// 
/// Summary description for Quotes
/// this class takes quotes
/// and stores them in a list object
/// the list object behaves a lot like
/// an array but you don't have to 
/// give it a fixed size
/// 
public class Quotes
{

    //declaring a list object  
    //is generic syntax. It assigns a 
    //type for the list at runtime
    private List myQuotes;

 public Quotes()
 {
        //initialize the list in the constructor
        myQuotes = new List();
 }

    public List MyQuotes
    {
        //will let the user get the list
        get { return myQuotes; }
        
    }

    public void AddQuote(string quote)
    {
        //adds a quote to the list
        MyQuotes.Add(quote);
    }

    public string GetQuote()
    {
        //create a random object
            Random rand = new Random();
         // get a number between 0 and the number of elements
        //in the list (minus 1 to account for starting at 0)
            int index = rand.Next(0, MyQuotes.Count - 1);
        //return the quote at that index
            return myQuotes[index];
    }


}

And for what it's worth here is the boreingstuff.css file

body {
}
h1 {
    color:white;
    background-color:navy;
}

.testclass {
    color: green;
}

Monday, November 4, 2013

WPF application with Try Parse and Try Catch

The form XAML

<Window x:Name="myWindow" x:Class="MileageApplicationMorning.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Label Content="Enter Beginning Mileage" HorizontalAlignment="Left" Margin="29,25,0,0" VerticalAlignment="Top"/>
        <TextBox x:Name="txtBeginMileage" HorizontalAlignment="Left" Height="23" Margin="208,28,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
        <Label Content="Enter Ending Mileage" HorizontalAlignment="Left" Margin="41,72,0,0" VerticalAlignment="Top"/>
        <TextBox x:Name="txtEndingMileage" HorizontalAlignment="Left" Height="23" Margin="208,72,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
        <Label Content="Enter Gallons" HorizontalAlignment="Left" Margin="38,113,0,0" VerticalAlignment="Top"/>
        <TextBox x:Name="txtGallons" HorizontalAlignment="Left" Height="23" Margin="208,116,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
        <Button x:Name="btnCalculate" Content="Calculate" HorizontalAlignment="Left" Margin="53,171,0,0" VerticalAlignment="Top" Width="75" Click="btnCalculate_Click"/>
        <Label x:Name="lblResult" Content="Label" HorizontalAlignment="Left" Margin="208,168,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.352,-0.24" Width="205" Height="60"/>

    </Grid>
</Window>

The code behind the form


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MileageApplicationMorning
{
    /// 
    /// Interaction logic for MainWindow.xaml
    /// 
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            //this sets the background to a gradient color pattern
            myWindow.Background = new LinearGradientBrush(Colors.PowderBlue, Colors.White, 45);

        }

        private void btnCalculate_Click(object sender, RoutedEventArgs e)
        {
            //initialize the mileage class
            Mileage m = new Mileage();
            //declare values for the out parameters of the try parses
            int begin;
            int end;
            double gallons;

            //this uses the TryParse in the if statement
            //you can do this because the tryParse returns
            //a boolean--true or false
            if (int.TryParse(txtBeginMileage.Text, out begin))
            {
                //if it is good assign it to the class
                //using the set function of the property
                m.BeginMileage = begin;
            }
            else
            {
                //otherwise show a message box with a prompt
                MessageBox.Show("Enter a valid integer");
                return;
            }

            if (int.TryParse(txtEndingMileage.Text, out end))
            {
                m.EndMileage = end;
            }
                
            else
            {
                MessageBox.Show("Enter a valid integer");
                txtEndingMileage.Clear();
                return;
     
            }
            if (double.TryParse(txtGallons.Text, out gallons))
            {
                //for gallons because we threw the error
                //if it was a 0 we need to catch the error
                //the try tries code. If there is no error 
                //it just executes the code; if it encounters
                //an error it drops to the catch and does
                //whatever it says to do, in this case
                //just display the error message
                try
                {
                    m.Gallons = gallons;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return;
                }
            }
            else
            {
                 MessageBox.Show("Enter a valid double");
                 txtGallons.Clear();
                 txtGallons.Focus();
                return;
            }
            double mpg;
            try
            {
                mpg = m.CalculateGasMileage();
                //uses the ToString() method of the class
                //to display the results of the calculation
                lblResult.Content = mpg.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }

         //changes background colors based on the mpg value
            if (mpg > 40)
            {
                lblResult.Background= new SolidColorBrush(Colors.Green);
            }
            else if (mpg > 30)
            {
                lblResult.Background = new SolidColorBrush(Colors.Purple);
            }
            else if (mpg > 20)
            {
                lblResult.Background = new SolidColorBrush(Colors.Yellow);
            }
            else
            {
                lblResult.Background = new SolidColorBrush(Colors.Red);
            }

           

        }
    }
}

The Mileage class code with the throw exception in the Gallons Property

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

namespace MileageApplicationMorning
{
    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 = 1;
            BeginMileage = 0;
            EndMileage = 0;
            Gallons = 1; //had to change to 1 to keep 
            //from throwing the error on calling
            //the class
        }

        //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
            {
                //here we are going to test to see
                //if the value passed in is equal to 0
                //if it is we will throw an exception
                //that we can handle in the form
                if (value == 0)
                {
                    throw new DivideByZeroException();
                }
                else
                {
                    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("##.##");
        }


    }
}