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;
}

No comments:

Post a Comment