Copyright ©2005  Department of Computer & Information Science JavaScript Modularity.

Slides:



Advertisements
Similar presentations
Programming with Microsoft Visual Basic 2008 Fourth Edition
Advertisements

Programming with Microsoft Visual Basic th Edition
Chapter 3: Using Variables and Constants Programming with Microsoft Visual Basic 2005, Third Edition.
Chapter 11: Classes and Objects
Objectives In this chapter, you will learn about:
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
True or false A variable of type char can hold the value 301. ( F )
Chapter 3 Assignment and Interactive Input. 2 Objectives You should be able to describe: Assignment Operators Mathematical Library Functions Interactive.
1 Committed to Shaping the Next Generation of IT Experts. Chapter 4: Spreadsheets in Decision Making: What If? Robert Grauer and Maryann Barber Exploring.
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
1 Lab Session-6 CSIT-121 Spring 2005 Structured Choice The do~While Loop Lab Exercises.
Chapter 1 Program Design
1 Lab Session-7 CSIT-121 Fall Introducing Structured Choice 4 The do~While Loop 4 Lab Exercises.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 7 – Class Average Application: Introducing.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 6 – Car Payment Calculator Application: Introducing.
McGraw-Hill/Irwin The Interactive Computing Series © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Microsoft Excel 2002 Exploring Formulas.
Presented by Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 12 Boolean Expressions, Switches, For-Loops Chapter 7.
CIS Computer Programming Logic
Introduction to Python
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
General Programming Introduction to Computing Science and Programming I.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
Copyright ©2005  Department of Computer & Information Science Using Number & Math Objects.
Chapter 12: How Long Can This Go On?
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 6 Value-Returning.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Functions.
While Loops Indefinite Iteration. Last lesson we looked at definite loops using the ‘For’ statement. The while loop keeps going while some condition is.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making.
Sample Problems for Testing
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 6 – Car Payment Calculator Application: Introducing.
Property of Jack Wilson, Cerritos College1 CIS Computer Programming Logic Programming Concepts Overview prepared by Jack Wilson Cerritos College.
Chapter 6: The Repetition Structure
CSCI N341: Client-Side Web Programming Copyright ©2004  Department of Computer & Information Science Writing JavaScript Functions.
Copyright ©2005  Department of Computer & Information Science Introducing Dialogue Windows.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
Writing JavaScript Functions. Goals By the end of this unit, you should understand … How to breakdown applications into individual, re-usable modules.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
Chapter Four The Selection Structure Programming with Microsoft Visual Basic th Edition.
REVIEW No curveballs this time …. PROBLEM TYPE #1: EVALUATIONS.
Math, Data Types. Python Math Operations OperationOperator Addition + Subtraction – Multiplication * Division (floating point) / Division (integer) //
Exceptions Chapter 16 This chapter explains: What as exception is Why they are useful Java exception facilities.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
FUNCTIONS. Topics Introduction to Functions Defining and Calling a Void Function Designing a Program to Use Functions Local Variables Passing Arguments.
REVIEW No curveballs this time …. PROBLEM TYPE #1: EVALUATIONS.
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
Chapter 4 Decision Making. Agenda Function Goal Seek command AutoFilter command Worksheet operation Printing worksheet.
Solving Problems with Repetition Version 1.0. Objectives At the end of this topic, students should be able to: Correctly use a while statement in a C#
General Condition Loop A general condition loop just loops while some condition remains true. Note that the body of the loop should (eventually) change.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Loops, Part II IT108 George Mason University. Indefinite Loop Don’t always have access to the number of iterations ahead of time If a condition (user-response,
3.1 Fundamentals of algorithms
Introduction to Programming
Value-Returning Functions
Tutorial 9 - Car Payment Calculator Application Introducing the while Repetition Statement Outline 9.1 Test-Driving the Car Payment Calculator Application.
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Conditions and Ifs BIS1523 – Lecture 8.
Visual Basic Programming Chapter Four Notes Working with Variables, Constants, Data Types, and Expressions GROUPBOX CONTROL The _____________________________________.
Introduction to Programming
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
CST-115 Introduction to Computer Programming
Functions MIS 3406 Department of MIS Fox School of Business
Introduction to Programming
GCSE Computing Mini Assignment.
Presentation transcript:

Copyright ©2005  Department of Computer & Information Science JavaScript Modularity

Copyright ©2005  Department of Computer & Information Science Goals By the end of this lecture you should … Understand how to write JavaScript functions.Understand how to write JavaScript functions. Understand how to perform unit testing.Understand how to perform unit testing.

Copyright ©2005  Department of Computer & Information Science Problem Statement Develop an application to calculate a monthly payment for a car loan, including data validation.Develop an application to calculate a monthly payment for a car loan, including data validation. Display the payment calculation in the format $XXX.YY.Display the payment calculation in the format $XXX.YY. Ask the user if they want to calculate another monthly payment.Ask the user if they want to calculate another monthly payment.

Copyright ©2005  Department of Computer & Information Science Take the next few minutes to identify the inputs, processes & outputs for the application.

Copyright ©2005  Department of Computer & Information Science What are the Inputs? INPUTSOURCE DATA TYPE Sales Price User Input Number (Float) Down Pymnt. User Input Number (Float) APR User Input Number (Float) continued …

Copyright ©2005  Department of Computer & Information Science What are the Inputs? INPUTSOURCE DATA TYPE Loan Length User Input Number (Float) Credit Rating User Input String Another loan? User Input Boolean

Copyright ©2005  Department of Computer & Information Science What are the Processes? Validate for number data, when appropriate.Validate for number data, when appropriate. Validate for appropriate ranges for APR & Loan Length (both must be positive).Validate for appropriate ranges for APR & Loan Length (both must be positive). Validate loan length is within an appropriate range.Validate loan length is within an appropriate range. continued …

Copyright ©2005  Department of Computer & Information Science What are the Processes? Convert loan length from years to months.Convert loan length from years to months. Validate for appropriate ranges for credit rating.Validate for appropriate ranges for credit rating. Adjust APR, according to Credit Rating.Adjust APR, according to Credit Rating. Convert the adjusted APR to monthly figure.Convert the adjusted APR to monthly figure. continued …

Copyright ©2005  Department of Computer & Information Science What are the Processes? Validate for YES/NO response to request for another loan calculation.Validate for YES/NO response to request for another loan calculation. Calculate monthly payment using the formula on the next slide …Calculate monthly payment using the formula on the next slide …

Copyright ©2005  Department of Computer & Information Science Equation for Payment

Copyright ©2005  Department of Computer & Information Science What are the Outputs? Display a splash screen introducing the program.Display a splash screen introducing the program. Display an appropriate message with the monthly payment information.Display an appropriate message with the monthly payment information.

Copyright ©2005  Department of Computer & Information Science Developing a Module for Splash Screen We need to develop a module for a splash screen to start our application.We need to develop a module for a splash screen to start our application. INPUTS: It has none.INPUTS: It has none. PROCESSES/OUTPUTS: Display basic program information, including title, author, version, etc.PROCESSES/OUTPUTS: Display basic program information, including title, author, version, etc. TYPE: SubprogramTYPE: Subprogram

Copyright ©2005  Department of Computer & Information Science Creating a JavaScript Subprogram Remember that we use subprograms to perform some task without returning a value to the procedure that calls it. Conversely, functions perform a task AND return a value.Remember that we use subprograms to perform some task without returning a value to the procedure that calls it. Conversely, functions perform a task AND return a value. JavaScript doesn’t differentiate between the two, however, so their syntax is similar …JavaScript doesn’t differentiate between the two, however, so their syntax is similar …

Copyright ©2005  Department of Computer & Information Science General form of a Function/Subprogram Definition function Identifier (par1, par2, … parN) { //Executable block return value; return value; /* Return value for /* Return value for * functions only */ * functions only */}//End

Copyright ©2005  Department of Computer & Information Science Calling a Subprogram/Function To call a subprogram, just call the subprogram by its identifier: GenerateSplashScreen();To call a subprogram, just call the subprogram by its identifier: GenerateSplashScreen(); To call a function (which returns a value), assign the result of the function to a variable: fltAvg = CalcAverage(1,2,3);To call a function (which returns a value), assign the result of the function to a variable: fltAvg = CalcAverage(1,2,3);

Copyright ©2005  Department of Computer & Information Science Using the file javaScriptTemplate.html, take the next few minutes to develop a module to display a splash screen. Save your new file as: carLoanCalculator.html Then look at the following file: javaScriptModularity_01.html

Copyright ©2005  Department of Computer & Information Science Input Numeric Data Module Consider the validation rules for the Sales Price, the Down Payment and the APR.Consider the validation rules for the Sales Price, the Down Payment and the APR. –They all need to be able to be converted to a numeric data type. –They all need to be a positive amount (>0). We can use the same function to input each …We can use the same function to input each …

Copyright ©2005  Department of Computer & Information Science IPO for Numeric Data Input Function INPUTSPROCESSESOUTPUTS 1.Label for Number (String) 2.Positive Number Indicator (Boolean) 1.While loop to force user to input positive number type. 1.Positive number value

Copyright ©2005  Department of Computer & Information Science Open the file carLoanCalculator.html. Take the next few minutes to develop a function to input Sales Price, Down Payment & APR. Then look at the following file: javaScriptModularity_02.html

Copyright ©2005  Department of Computer & Information Science Loan Length Module We’ll use another function to get the loan amount (a float data type). However, we’ll limit the user’s choice using a integer menu representing the available loan terms (2 years, 5 years, or 6 years).We’ll use another function to get the loan amount (a float data type). However, we’ll limit the user’s choice using a integer menu representing the available loan terms (2 years, 5 years, or 6 years).

Copyright ©2005  Department of Computer & Information Science IPO for Loan Length Function INPUTSPROCESSESOUTPUTS 1.None 1.Determine desired loan length. 2.Validate user’s choice. 1.Float number representing loan amount (in months).

Copyright ©2005  Department of Computer & Information Science Open the file carLoanCalculator.html. Take the next few minutes to develop a function to input the length of the loan. Then look at the following file: javaScriptModularity_03.html

Copyright ©2005  Department of Computer & Information Science Credit Rating Module We’ll create a function to determine the user’s credit rating. Again, we’ll limit the user’s choices using a integer menu (1=superior; 2=good; 3=average; 4=below average; 5=poor).We’ll create a function to determine the user’s credit rating. Again, we’ll limit the user’s choices using a integer menu (1=superior; 2=good; 3=average; 4=below average; 5=poor).

Copyright ©2005  Department of Computer & Information Science IPO for Credit Rating Function INPUTSPROCESSESOUTPUTS 1.None 1.Determine desired credit rating. 2.Validate user’s choice. 1.String value representing customer credit rating.

Copyright ©2005  Department of Computer & Information Science Open the file carLoanCalculator.html. Take the next few minutes to develop a function to input & validate the customer’s credit rating. Then look at the following file: javaScriptModularity_04.html

Copyright ©2005  Department of Computer & Information Science Monthly Interest Rate Module We’ll create a function to determine the monthly interest rate. Based on the credit rating, we’ll add points to the APR (1.0 for good credit rating; 1.05 for average credit rating; 1.1 for below average credit rating; 1.25 for poor credit rating). Finally, we’ll divide the adjusted APR by 12 to get the monthly interest rate.We’ll create a function to determine the monthly interest rate. Based on the credit rating, we’ll add points to the APR (1.0 for good credit rating; 1.05 for average credit rating; 1.1 for below average credit rating; 1.25 for poor credit rating). Finally, we’ll divide the adjusted APR by 12 to get the monthly interest rate.

Copyright ©2005  Department of Computer & Information Science IPO for Credit Rating Function INPUTSPROCESSESOUTPUTS 1.Credit rating (String) 2.Raw APR (Float) 1.Determine adjusted APR, based on credit. 2.Divide adjusted APR by 12 1.Float value indicating the monthly interest rate.

Copyright ©2005  Department of Computer & Information Science Open the file carLoanCalculator.html. Take the next few minutes to determine a monthly interest rate. Then look at the following file: javaScriptModularity_05.html

Copyright ©2005  Department of Computer & Information Science Calculate Payment Module Now we’ll create a function to determine the monthly payment. We’ll need the loan principle, the monthly interest rate and the length of the loan as inputs.Now we’ll create a function to determine the monthly payment. We’ll need the loan principle, the monthly interest rate and the length of the loan as inputs.

Copyright ©2005  Department of Computer & Information Science IPO for Calculate Payment Function INPUTSPROCESSESOUTPUTS 1.Principle 2.Monthly Rate 3.Loan Length (All Floats) 1.Use the equation on the next slide. 1.Float value indicating the monthly payment.

Copyright ©2005  Department of Computer & Information Science Open the file carLoanCalculator.html. Take the next few minutes to develop a function to calculate the monthly payment. Then look at the following file: javaScriptModularity_06.html

Copyright ©2005  Department of Computer & Information Science Display Payment Module Now we’ll create a sub-program to display the monthly payment. We’ll need the monthly payment as an input.Now we’ll create a sub-program to display the monthly payment. We’ll need the monthly payment as an input.

Copyright ©2005  Department of Computer & Information Science IPO for Display Monthly Payment Subprogram INPUTSPROCESSESOUTPUTS 1.Monthly Payment (Float) 1.Create a custom message with monthly payment. 1.Display message.

Copyright ©2005  Department of Computer & Information Science Open the file carLoanCalculator.html. Take the next few minutes to develop a subprogram to display the monthly payment. Then look at the following file: javaScriptModularity_07.html

Copyright ©2005  Department of Computer & Information Science Continue Program Module Now we’ll create a function to ask the user if they wish to calculate another loan. We’ll ask them if want to continue; validate they typed YES or NO. The value yes or no is returned to the main program.Now we’ll create a function to ask the user if they wish to calculate another loan. We’ll ask them if want to continue; validate they typed YES or NO. The value yes or no is returned to the main program.

Copyright ©2005  Department of Computer & Information Science IPO for Continue Program Function INPUTSPROCESSESOUTPUTS 1.Answer to continue program question (string) 1.Validate answer is YES or NO. 2.If YES, return true 3.If NO, return false 1.Boolean value, based on process criteria

Copyright ©2005  Department of Computer & Information Science Open the file carLoanCalculator.html. Take the next few minutes to develop a function to determine if the user wishes to continue. Then look at the following file: javaScriptModularity_08.html

Copyright ©2005  Department of Computer & Information Science Summary Subprograms perform tasks without returning a value; functions do the same, but DO return a value.Subprograms perform tasks without returning a value; functions do the same, but DO return a value. JavaScript uses the keyword function for both functions and subprograms.JavaScript uses the keyword function for both functions and subprograms. continued …

Copyright ©2005  Department of Computer & Information Science Summary We can perform unit testing by delivering test values to completed modules before writing additional modules.We can perform unit testing by delivering test values to completed modules before writing additional modules. We use the main module as the central point through which data is delivered to and from other modules.We use the main module as the central point through which data is delivered to and from other modules.