Decomposing A Program Into Functions. 2 3 Program/Function Decomposition.

Slides:



Advertisements
Similar presentations
JQuery MessageBoard. Lets use jQuery and AJAX in combination with a database to update and retrieve information without refreshing the page. Here we will.
Advertisements

Get number The ginput command creates crosshairs. When the user clicks the x,y values of the crosshairs are returned.
Designing Algorithms Csci 107 Lecture 4.
JQuery CS 268. What is jQuery? From their web site:
PHP Tutorials 02 Olarik Surinta Management Information System Faculty of Informatics.
Flow Charting. Goals Create Algorithms using Flow Charting procedures. Distinguish between Flow Charting and Pseudocode. Top-Down Design Bottom-up Design.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
JavaScript Lecture 6 Rachel A Ober
Iteration. Adding CDs to Vic Stack In many of the programs you write, you would like to have a CD on the stack before the program runs. To do this, you.
Mastering Char to ASCII AND DOING MORE RELATED STRING MANIPULATION Why VB.Net ?  The Language resembles Pseudocode - good for teaching and learning fundamentals.
Nonvisual Arrays and Recursion by Chris Brown under Prof. Susan Rodger Duke University June 2012.
Java Programming Constructs 3 MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation.
Working with Files. Learning Objectives By the end of this lecture, you should be able to: – Examine a file that contains code with unfamiliar material,
JavaScript, Fourth Edition
Module 4: I/O and Strings #1 2000/01Scientific Computing in OOCourse code 3C59 Module 4: I/O In this module we will cover Keyboard/screen input and output.
Conditional Statements.  Quiz  Hand in your jQuery exercises from last lecture  They don't have to be 100% perfect to get full credit  They do have.
Loops.  (No Quiz)  Hand in Assignment #1  Last chance for Q+A on the midterm  Loops 2.
Validation final steps Stopping gaps being entered in an input.
Radio Buttons.  Quiz  Radio Buttons  Check boxes 2.
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
Functions.  Assignment #2 is now due on Wednesday, Nov 25 th  (No Quiz)  Go over the midterm  Backtrack and re-cover the question about tracing the.
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Logical Operators.  Quiz  Let's look at the schedule  Logical Operators 2.
Lecture Exercise (Wk4) CS1301 Introduction To Computer Programming (11-12 Semester B) Page 1 Question 2 (a)Complete the code.
Check Boxes. 2 Check boxes  Image from:  HTML:  Each box gets it's own.
11 Project 2 Temperature Conversion. 22 Project 2: Temperature Conversion Write an ASP.NET Web Forms app to convert temperatures from Fahrenheit to Celsius.
Form Data (part 2) MIS 3502, Fall 2015 Brad N Greenwood, PhD Department of MIS Fox School of Business Temple University 11/10/2015 Slide 1.
Murach’s Visual Basic 2008, C7, modified or added© 2008, Mike Murach & Associates, Inc. Slide 1.
Introduction to Programming Python Lab 8: Loops 26 February PythonLab8 lecture slides.ppt Ping Brennan
Python - Iteration A FOR loop is ideal if we know how many times we want to repeat. Try this code: for loopCounter in range(10): print(loopCounter) There.
Arrays. What is an array? An array is a collection of data types. For example, what if I wanted to 10 different integers? int num1; int num2; int num3;
A little PHP. Enter the simple HTML code seen below.
Logical Operators.  Quizzes!  Let's look at the schedule  Logical Operators 2.
BIT116: Scripting Lecture 05
Online PD Basic HTML The Magic Of Web Pages
Form Data (part 2) MIS 3502, Fall 2015 Jeremy Shafer Department of MIS
A little PHP.
Introduction to Programming
Python - Iteration A FOR loop is ideal if we know how many times we want to repeat. Try this code: for loopCounter in range(10): print(loopCounter) There.
Introduction to Programming
Learning to Program D is for Digital.
BIT116: Scripting Loops.
Arrays: Checkboxes and Textareas
Project 9 Creating Pop-up Windows, Adding Scrolling Messages, and Validating Forms.
Barb Ericson Georgia Institute of Technology May 2006
BIT116: Scripting Functions.
Introduction to Programming
Siti Nurbaya Ismail Senior Lecturer
Conditions and Ifs BIS1523 – Lecture 8.
In Class Program: Today in History
More Loops.
For -G7 programing language Teacher / Shamsa Hassan Alhassouni.
Introduction to Programming
Functions, Regular expressions and Events
Text Analyzer BIS1523 – Lecture 14.
Introduction to Programming
Python programming exercise
Arrays.
Mobile Computing With Android ACST 4550 Toast
Flowcharts and Pseudo Code
BIT116: Scripting Radio Buttons.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
BIT116: Scripting Functions.
Introduction to Programming
Introduction to Programming
The IF Revisited A few more things Copyright © Curt Hill.
Lecture 20 – Practice Exercises 4
Chapter 1: Creating a Program.
Reading information from files
Presentation transcript:

Decomposing A Program Into Functions

2

3 Program/Function Decomposition

 Each function should have a single, well-defined purpose  User I/O (Input/Output) counts as a function  Therefore everything else should NOT interact with the user  The 'main' function's purpose is to sequence all the steps that need to happen 4

5

6

 Anonymous 'on click' handler will serve as our 'main' function and organize all the other steps  Three different functions: 1. Get the user's input 2. Space out the digits 3. Display the output on the page 7

 Three different functions: 1. Get the user's input  Get the user's input, convert to a number, check if that actually is a number, display an error message if it's not (and stop all further processing), then return that number to 'main' 2. Space out the digits  Convert the number to a string, and use a loop to accumulate the spaced-out string: for each iteration glue another digit on, then another space 3. Display the output on the page  Set up the output string (including the text above and below the actual spaced-out number), then put it on the page 8

9 Here's what the 'main' / on click handler looks like: $(document).ready( function() { $("#functionDemo").click( function() { var num = GetUserInput(); var spaced = SpaceNumberOut(num); SetOutput( spaced ); }); Notice that each function has a single, well-defined purpose

10 Here's what the 'main' / on click handler looks like: function GetUserInput ( ) { var numAsInput = $("#num1").val(); var num = Number( numAsInput ); if( isNaN(num) ) { $("#output").html( "Error! " + numAsInput + " is not a number!"); throw new Error(numAsInput + “epep is not a number!"); } return num; } $(document).ready( function() { $("#functionDemo").click( function() { var num = GetUserInput() ; var spaced = SpaceNumberOut(num); SetOutput( spaced ); }); Notice that each function has a single, well-defined purpose

11 Here's what the 'main' / on click handler looks like: function SpaceNumberOut( num ) { var spacedOutNumber = ""; var numAsString = num.toString(); for( var i= 0; i < numAsString.length; i++) { spacedOutNumber += numAsString.charAt(i) + " "; } return spacedOutNumber; } $(document).ready( function() { $("#functionDemo").click( function() { var num = GetUserInput(); var spaced = SpaceNumberOut(num ); SetOutput( spaced ); }); Let's look at.toString(),.charAt(i),.length Accumulator Table ispacedOutNumber 0"" + "1" + "□" = "1□" 1"1□" + "2" + "□" = "1□2□" 2"1□2□" + "3" + "□" = "1□2□3□" (I'm going to assume that 123 was typed into the text box)

12 Here's what the 'main' / on click handler looks like: function SetOutput( szNum ) { szNum = "The number is: "+szNum+" Isn't that awesome!!!"; $("#output").html( szNum ); } $(document).ready( function() { $("#functionDemo").click( function() { var num = GetUserInput(); var spaced = SpaceNumberOut(num); SetOutput( spaced ); }); Notice that each function has a single, well-defined purpose

 Work on Exercise #1 for this part of this lecture 13

14

 Anonymous 'on click' handler will serve as our 'main' function and organize all the other steps  Four different functions: 1. Get the user's first, lower number 2. Get the user's second, higher number 3. Accumulate the numbers 4. Display the numbers on the page 15

 Four different functions: 1. Get the user's first, lower number  Get the user's input, convert to a number, that that is a number, display an error message if it's not (and stop all further processing), then return that number to 'main' 2. Get the user's second, higher number  Get the user's input, convert to a number, that that is a number, display an error message if it's not (and stop all further processing), check if this second, higher number is actually larger than the first, smaller number (display an error message & stop if it's not), then return that number to 'main' 3. Accumulate the numbers  Use a loop to accumulate the numbers: for each iteration glue another number on, then another HTML line break element 4. Display the output on the page  Set up the output string (including the text above and below the actual spaced-out number), then put it on the page 16

17 Here's what the 'main' / on click handler looks like: $(document).ready( function() { $("#functionDemo").click( function() { var num1 = GetFirstNumber(); var num2 = GetSecondNumber(num1); var nums = AccumulateNumbers(num1, num2); SetOutput( nums ); }); Notice that each function has a single, well-defined purpose

18 Here's what the 'main' / on click handler looks like: function GetSecondNumber( firstNum) { var numAsInput = $("#num2").val(); var num = Number( numAsInput ); if( isNaN(num) ) { $("#output").html( "Error! " + numAsInput + " is not a number!"); throw new Error(numAsInput + " is not a number!"); } if( num < firstNum) { $("#output").html( "Error! " + num + " should be lower than " + firstNum + " but it's not!"); throw new Error(num + " should be lower than " + firstNum + " but it's not!"); } return num; } $(document).ready( function() { $("#functionDemo").click( function() { var num1 = GetFirstNumber(); var num2 = GetSecondNumber(num1); var nums = AccumulateNumbers(num1, num2); SetOutput( nums );}); }); "GetFirstNumber" is pretty much the same as "GetUserInput" from the previous slides

19 Here's what the 'main' / on click handler looks like: function AccumulateNumbers( loNum, hiNum ) { var nums = ""; console.log("lo: " + loNum + " hi: " + hiNum); for( var i= loNum; i < hiNum; i++) { nums += i + " "; console.log("nums is: " + nums); } return nums; } $(document).ready( function() { $("#functionDemo").click( function() { var num1 = GetFirstNumber(); var num2 = GetSecondNumber(num1); var nums = AccumulateNumbers(num1, num2); SetOutput( nums ); }); "SetOutput " is pretty much the same as "GetUserInput" from the previous slides so we're not going to talk about that, either Accumulator Table inums 0"" + "1" + " "  "1 " 1"1 " + "2" + " "  "1 2 " 2 "1 2 " + "3" + " "  "1 2 3 " (I'm going to assume that 1 and 3 were typed into the text boxes)

 Exercise #2: Given more complicated code and some starting functions finish the decomp  Exercise #3: Given more complicated code sketch out the functions (without actually filling them in) 20