Windows Systems Programming: Spring 2004

[ Home | Syllabus | Course Notes | Assignments | Search]


ASP.Net - Part 2

Code Behind


Code Behind Example - Lunar Lander

<%@ Page Inherits="LanderPage" %> 

<html>
  <body>
    <h1>Lunar Lander</h1>
    <form runat="server">
      <hr>
      <table cellpadding="8"> 
        <tr>
          <td>Altitude (m):</td>
          <td><asp:Label ID="Altitude" Text="15200.0"
            RunAt="Server" /></td>
        </tr>
        <tr>
          <td>Velocity (m/sec):</td>
          <td><asp:Label ID="Velocity" Text="0.0"
            RunAt="Server" /></td>
        </tr>
        <tr>
          <td>Acceleration (m/sec2):</td>
          <td><asp:Label ID="Acceleration" Text="-1.6"
            RunAt="Server" /></td>
        </tr>
        <tr>
          <td>Fuel (kg):</td>
          <td><asp:Label ID="Fuel" Text="8165.0" RunAt="Server" /></td>
        </tr>
        <tr>
          <td>Elapsed Time (sec):</td>
          <td><asp:Label ID="ElapsedTime" Text="0.0"
            RunAt="Server" /></td>
        </tr>
        <tr>	
          <td>Throttle (%):</td>
          <td><asp:TextBox ID="Throttle" RunAt="Server" /></td>
        </tr>
        <tr>	
          <td>Burn Time (sec):</td>
          <td><asp:TextBox ID="Seconds" RunAt="Server" /></td>
        </tr>
      </table>
      <br>
      <asp:Button Text="Calculate" OnClick="OnCalculate"
        RunAt="Server" />
      <br><br>
      <hr>
      <h3><asp:Label ID="Output" RunAt="Server" /></h3>
    </form>
  </body>
</html>

 

 

using

Here is the Code Behind this page

using System;
using System.Web.UI;
using System.Web.UI.WebControls;

public class LanderPage : Page 
{
    const double gravity = 1.625;      // Lunar gravity
    const double landermass = 17198.0; // Lander mass

    protected Label Altitude; 
    protected Label Velocity;
    protected Label Acceleration;
    protected Label Fuel;
    protected Label ElapsedTime;
    protected Label Output;
    protected TextBox Throttle;
    protected TextBox Seconds;

    public void OnCalculate (Object sender, EventArgs e)
    {
        double alt1 = Convert.ToDouble (Altitude.Text);

        if (alt1 > 0.0)  {
            // Check for blank input fields
            if (Throttle.Text.Length == 0) {
                Output.Text = "Error: Required field missing";
                return;
            }

            if (Seconds.Text.Length == 0) {
                Output.Text = "Error: Required field missing";
                return;
            }

            // Extract and validate user input
            double throttle;
            double sec;

            try {
                throttle = Convert.ToDouble (Throttle.Text);
                sec = Convert.ToDouble (Seconds.Text);
            }
            catch (FormatException) {
                Output.Text = "Error: Invalid input";
                return;
            }

            if (throttle < 0.0 || throttle > 100.0) {
                Output.Text = "Error: Invalid throttle value";
                return;
            }

            if (sec <= 0.0) {
                Output.Text = "Error: Invalid burn time";
                return;
            }

            // Extract flight parameters from the Label controls
            double vel1 = Convert.ToDouble (Velocity.Text);
            double fuel1 = Convert.ToDouble (Fuel.Text);
            double time1 = Convert.ToDouble (ElapsedTime.Text);

            // Compute thrust and remaining fuel
            double thrust = throttle * 1200.0;
            double fuel = (thrust * sec) / 2600.0;
            double fuel2 = fuel1 - fuel;

            // Make sure there's enough fuel
            if (fuel2 < 0.0) {
                Output.Text = "Error: Insufficient fuel";
                return;
            }
				
            // Compute new flight parameters
            Output.Text = "";
            double avgmass = landermass + ((fuel1 + fuel2) / 2.0);
            double force = thrust - (avgmass * gravity);
            double acc = force / avgmass;

            double vel2 = vel1 + (acc * sec);
            double avgvel = (vel1 + vel2) / 2.0;
            double alt2 = alt1 + (avgvel * sec);
            double time2 = time1 + sec;

            // If altitude <= 0, then we've landed
            if (alt2 <= 0.0) {
                double mul = alt1 / (alt1 - alt2);
                vel2 = vel1 - ((vel1 - vel2) * mul);
                alt2 = 0.0;
                fuel2 = fuel1 - ((fuel1 - fuel2) * mul);
                time2 = time1 - ((time1 - time2) * mul);

                if (vel2 >= -4.0)
                    Output.Text = "The Eagle has landed";
                else
                    Output.Text = "Kaboom!";
            }			

            // Update the Labels to show latest flight parameters
            Altitude.Text = alt2.ToString ("f1"); 
            Velocity.Text = vel2.ToString ("f1");
            Acceleration.Text = acc.ToString ("f1");
            Fuel.Text = fuel2.ToString ("f1");
            ElapsedTime.Text = time2.ToString ("f1");
        }
    }
}

 


Copyright chris wild 1999-2004.
For problems or questions regarding this web contact [Dr. Wild].
Last updated: February 09, 2004.