Download presentation
Presentation is loading. Please wait.
1
GTECH 731 Lab Session 4 Lab 3 Review, Lab 4 Intro 9/28/10 Lab 3 Review Lab 4 Overview
2
Lab 3 – Working with Functions Functions are self-contained blocks of code that perform a single, easily-identified task and return a single value “Curly brackets” define scope of function Avoid temptation to add unrelated tasks to function Can write code in one place and call whenever needed Key elements of any function Name is action verb or statement describing what function does Parameters function acts upon Return type Steps to accomplish task May have local variables required to perform function static string Hello(string name) { return "Hello " + name; } static double SquareRoot(double pNumber) { return Convert.ToDouble(Math.Sqrt(pNumber)); } static double PowerOf(double pBase, double pExponent) { double dProduct = 1; for (int i = 0; i == pExponent; i++) { dProduct = dProduct * pBase; } return dProduct; }
3
Lab 3 – Functions in Console Program Newly created Console Project automatically generates hierarchy... Namespace Program class Main method The scope of each level is defined by “curly bracket pairs” Misplaced “curly brackets” source of many errors Everything happens within the “curly brackets” of the Program class Main() method is “entry point” for console program Main logic of program Functions are called from within Main() method namespace ConsoleApplication1 { class Program { static int iVariable = 1; static double dVariable = 0; static double FunctionName(int pInput) { double dOutputVariable = pInput + 1; return dOutputVariable; } static void Main(string[] args) { dVariable = FunctionName(iVariable); }
4
Lab 3 – Functions in Windows Form Program Newly created Windows Form Project automatically generates hierarchy... Namespace Form1 class Form1 method The scope of each level is defined by “curly bracket pairs” Misplaced “curly brackets” source of many errors Everything happens within the “curly brackets” of the Form1 class Form1 is entry point for the program but much of logic is controlled in event handler modules Control of events happens behind the scenes Functions are called from within “Event” handlers for objects in the design window namespace WindowsFormCodeTesting { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private double FunctionName(int Parameter) { double dVariable = 1.0 + Parameter; return dVariable; } private void button1_Click(object sender, EventArgs e) { double dVariable = 0; dVariable = FunctionName(); }
5
Lab 3 – Layout and Readability Indenting Let Visual Studio set your indents for you Grouping Group like items together Constant or variable declarations Keep lines of function together so function displays on one screen White space Even consistent spacing Separate logical groupings Documentation When trying to understand someone else’s code or code you typed in the past Comment functions & methods Statements that need explanation using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static int AddOne(int pInput) /* This function returns pInput plus one. */ { return pInput + 1; // Add one to pInput } static void Main(string[] args) { /*------- Variable Declarations -------*/ int iVar1 = 0; // First Variable... int iVar2 = 0; // Second Variable... int iVar3 = 0; // Third Variable... /*------- Main Body of Program --------*/ iVar1 = AddOne(iVar2); // Explanation... iVar2 = AddOne(iVar3); // Explanation... }
6
Lab 4 – Object Class Previously treated functions and data as separate entities Mouse position e.X, e.Y coordinates Conversion function used coordinates as parameter input OOP allows data and functions to be merged into contained objects Variables represent current state of object Methods represent abilities of object Less need for long parameter lists Objects represent real-world or conceptual things More intuitive and expressive than procedural approach Object-oriented design involves modeling real-world entities through hierarchy of inheritance Animal: Living Thing Mammal: Warm blooded : Living Thing Feline: Fluffy : Warm blooded : Living Thing MyCat: Rosie: Fluffy : Warm blooded : Living Thing
7
Lab 4 – Object Class Code Implementation How do you represent the object in code? Define classes within a namespace Animal: Living Thing Mammal: Warm blooded : Living Thing Feline: Fluffy : Warm blooded : Living Thing MyCat: Rosie: Fluffy : Warm blooded : Living Thing namespace MyProjectName { public class Animal { public bool bHeartBeating = true; public void Walk() { // code to implement walk } public class Mammmal : Animal { public bool bWarmBlooded; public void MammalAction() { // code to implement action } public class Feline : Mammmal { public bool bFluffy; public void Feline() { bFluffy = true; } public void Purr() { // code to implement purr } class Program { static void Main(string[] args) { Feline MyCat = new Feline(); MyCat.bFluffy = false; MyCat.Walk(); MyCat.Purr(); MyCat.bWarmBlooded = true; MyCat.bHeartBeating = true; }
8
Lab 4 – Object Overview of Lab 4 – Windows Form Only
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.