GTECH 731 Lab Session 3 Lab 2 Review, Lab 3 Intro 9/20/10 Lab 2 Review Lab 3 Overview
Lab 2 – Review Introduced to variables Key part of geographic modeling because they hold the current “state” of object being modeled Traffic model of vehicles moving through space Current X, Y coordinates, direction, speed, odometer and fuel Dynamic modeling requires change of “current state” of objects Moving vehicle Current X, Y coordinates would constantly be recalculated Assigning new values to variables based on meaningful calculations X = X + DeltaX Y = Y + DeltaY DeltaX = XVector(Direction, Speed) DeltaY = YVector(Direction, Speed) Speed = SpeedLimit + CongestionFactor + DriverFactor(SpeedLimit) Fuel = Fuel – (MilesTraveled (Odometer)* MPG) X Y DeltaX DeltaY a b
Lab 2 – Review Console program variables declared and initialized (also used in Windows Form) int i = 25;// declaration and initialization in one line Windows Form program variables set in “Properties” window Entered values in Properties window for an object Object name: “lblEquals” Text: “Equals: ” These values are initialized in the “InitializeComponent();” section of code Can override initial value with new assignments i = 25;//assign value via literal i = x;//assign value via other variable i = 2 + 2; //assign value via expression i = Convert.ToInt32(“25”); //assign value via function
Lab 2 – Review Naming conventions: no steadfast rules but consistency is key Member Variables should be nouns iFeet //integer variable for units in “feet” lblOutput.text//label holding the output results Methods should be verbs in Pascal notation btnMilesConversion_Click //button method converting from feet to miles btnMetersConversion_Click //button method converting from feet to meters Notation: “Camel” for variables Start with lowercase letter and init-cap words that make-up variable name Can start with lower case letter of the variable type as in “Hungarian” notation int iMyInteger string sMyString float fMyFloatingPoint Book talks about constants in Pascal notation (init cap) but website talks about constants as all uppercase with words separated by underscore
Lab 3 – Working with Functions Last lab Wrote simple in-line code in console to convert from feet to meters In Windows Form, we used similar code in multiple methods to convert from feet to several different units of measure Typing same or similar code over and over... Can be time consuming in larger programs Is problematic when changes to code are needed later (need to edit multiple locations) Functions provide means to write code in one place that can be called from many places when needed Functions are self-contained blocks of code that perform a given task Should be simple enough to test and know it is working properly Want function to be as generic as possible
Lab 3 – Working with Functions Functions usually perform a single, easily-identified task. Key questions about any function are: What should its name be? Action verb or statement describing what the function does. What are the parameters? Input values function acts upon. What is the return type? Does it return a number a text string or a boolean type? How does it work? What are the steps needed to accomplish the task? public int AddTwoNumbers(int a, int b) { return a+b; } public bool NumbersAreEqual(int a, int b) { if (a == b) { return True; } else { return False; } if NumbersAreEqual(x, y) { z = AddTwoNumbers(x, y); }
Lab 3 – Working with Functions Overview of Lab 3 – Console Using Debug tools to find and solve problems F11 Step into code Breakpoints Locals (variables) window Mouse-over variables and mathematical operators Overview of Lab 3 – Windows Form