Data Structures Week 4 INFM 603. The Key Ideas Structured Programming  Modular Programming  Data Structures Object-Oriented Programming.

Slides:



Advertisements
Similar presentations
INFM 603: Information Technology and Organizational Context Jimmy Lin The iSchool University of Maryland Thursday, September 19, 2013 Session 3: JavaScript.
Advertisements

Programming Languages and Paradigms
1 JavaScript: Control Structures II. 2 whileCounter.html 1 2
JavaScript Forms Form Validation Cookies. What JavaScript can do  Control document appearance and content  Control the browser  Interact with user.
Arrays.
Programming Week 9 LBSC 690 Information Technology.
Java Programming, 3e Concepts and Techniques Chapter 5 Arrays, Loops, and Layout Managers Using External Classes.
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
Web Infrastructure Week 3 INFM 603. The Key Ideas Questions Structured Programming Modular Programming Data Structures Object-Oriented Programming.
LBSC 690 Session #10 Programming, JavaScript Jimmy Lin The iSchool University of Maryland Wednesday, November 5, 2008 This work is licensed under a Creative.
Programming Week 5 LBSC 690 Information Technology.
Structured Programming and UML Overview Session 2 LBSC 790 / INFM 718B Building the Human-Computer Interface.
XP 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial 10.
Information Technology Center Hany Abdelwahab Computer Specialist.
LBSC 690: Session 10 Programming, JavaScript Jimmy Lin College of Information Studies University of Maryland Monday, November 12, 2007.
Computer Science 103 Chapter 4 Advanced JavaScript.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
1 JavaScript/Jscript: Arrays. 2 Introduction Arrays –Data structures consisting of related data items (collections of data items) JavaScript arrays are.
2012 •••••••••••••••••••••••••••••••••• Summer WorkShop Mostafa Badr
Introduction to JavaScript for Python Programmers
Advanced Decisions and Loops Chapter Some Simple Schoolroom Statistics.
CST JavaScript Validating Form Data with JavaScript.
1 JavaScript: Functions and Arrays October 18, 2005 Slides modified from Internet & World Wide Web: How to Program (3rd) edition. By Deitel, Deitel,
DOM, Content Management Systems, Drupal, Object- Oriented Design Week 8 INFM 603.
JS Arrays, Functions, Events Week 5 INFM 603. Agenda Arrays Functions Event-Driven Programming.
2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap.
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
WML II (“Son of WML”) WML WMLScript. WML - A Quick Review Document structure ,... Text and image controls ...,,..., Navigation controls ,,, Events.
1 Week Three: Using Scripts and Forms in HTML (Week Three) By Dr Fadi Safieddine.
A Level Computing#BristolMet Session Objectives U2#S6 MUST identify different data types used in programming aka variable types SHOULD describe each data.
Structured Programming Week 3 INFM 603. Muddiest Points Emergent behavior of the Web HTML class attribute The details of JavaScript … p.style1 { font-family:arial;
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - JavaScript: Arrays Outline 11.1 Introduction 11.2 Arrays 11.3 Declaring and Allocating Arrays.
Functions and Data Structures Week 4 INFM 603. Programming in Four Parts Structured Programming  Data Structures  Modular Programming Object-Oriented.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
JavaScript Part 1.
CSS Class 7 Add JavaScript to your page Add event handlers Validate a form Open a new window Hide and show elements Swap images Debug JavaScript.
1 Introduction to Javascript Peter Atkinson. 2 Objectives To understand and use appropriately some of the basic elements of Javascript: –alert and prompt.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
Introduction. 2COMPSCI Computer Science Fundamentals.
XP Tutorial 10New Perspectives on Creating Web Pages with HTML, XHTML, and XML 1 Working with JavaScript Creating a Programmable Web Page for North Pole.
Topic: An Introduction to JavaScript - from Beginning JavaScript by Wilton (WROX)
1 Forms and Form elements CSD 340 McCoey. 2 Form Object Properties action Usually a call to a server elements encoding method post or get target Methods.
CompSci Today’s topics Java Review Just a bunch of headings meant to trigger questions A contraction of previous notes Upcoming Midterm Exam Reading.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
Pointers OVERVIEW.
USING UNITY JAVASCRIPT. CONVENTIONS AND SYNTAX IN JAVASCRIPT Case Sensitivity All keywords like var or function must be in lowercase. All variable names,
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 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
Practical Programming COMP153-08S Week 5 Lecture 1: Screen Design Subroutines and Functions.
JavaScript, Fourth Edition
1 CSC160 Chapter 7: Events and Event Handlers. 2 Outline Event and event handlers onClick event handler onMouseOver event handler onMouseOut event handler.
Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
Controlling Program Flow with Decision Structures.
JavaScript II ECT 270 Robin Burke. Outline Functions Events and event handling Form validation.
Tutorial 11 1 JavaScript Operators and Expressions.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
JavaScript and Ajax (JavaScript Functions) Week 5 Web site:
Programming Logic and Design Fifth Edition, Comprehensive Chapter 6 Arrays.
Introduction to Javascript. What is javascript?  The most popular web scripting language in the world  Used to produce rich thin client web applications.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
JavaScript and Ajax (JavaScript Environment) Week 6 Web site:
XP Tutorial 10New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - JavaScript: Arrays Outline 11.1 Introduction 11.2 Arrays 11.3 Declaring and Allocating Arrays.
Chapter 11 - JavaScript: Arrays
HTML & teh internets.
Web Programming and Design
Intro to Programming (in JavaScript)
Presentation transcript:

Data Structures Week 4 INFM 603

The Key Ideas Structured Programming  Modular Programming  Data Structures Object-Oriented Programming

Arrays A set of elements –For example, the number of days in each month Each element is assigned an index –A number used to refer to that element For example, x[4] is the fifth element (count from zero!) –Arrays and iteration work naturally together “Constructor” allocates space –var myArray = new Array(5); // all unitialized –var myArray = [42, 17,, 22, 1]; // partially initialized

Array Example // allocate five-element Array (indexed 0..4) var n = new Array(5); // assign values to each element of Array n1 for (var i=0; i<n.length; i++) { n[i] = i; } // output index and value of each element for (var i=0; i<n.length; i++) { document.writeln(i + “: “ + n[i]); }

Data Structures Constant –Names given to unchanging values (for readability) Scalar –Single-valued item (int, float, boolean) Object –Multi-valued item, mixed data types [+methods] Array –Integer-indexed set of objects (usually of one type) Associative array (“hash table”) –Object-index set of objects (usually of one type)

Associative Arrays in JavaScript var myArray = new Array(); myArray['one'] = 1; myArray['two'] = 2; myArray['three'] = 3; // show the values stored for (var i in myArray) { // skips unitialized alert('key is: ' + i + ', value is: ' + myArray[i]); }

Common Uses of Arrays Iterative computation Queue (FIFO) Stack (LIFO)

Functions Reusable code for complex “statements” –Takes zero or more values as “parameters” –Returns at most one value as the “result” function convertToCelsius(f) { var celsius = 5/9 * (f-32); return celsius; } c = convertToCelsius(60); function convertToCelsius(f) { var celsius = 5/9 * (f-32); return celsius; } var f = 60; c = convertToCelsius(f);

Uses of Functions Compactness –Minimizing duplicative code Modular programming –Abstraction –Reusability Avoid “side effects” for modular programming

Scope of a Variable In JavaScript, var “declares” a variable var mystery;create a variable without defining its type var b = true;create a boolean b and set it to true var n = 1;create an integer n and set it to 1 var s = “hello”;create a string s and set it to “hello” Variables declared in a function are “local” Function parameters are implicitly declared (local) Same name outside function refers to different variable All other variables are “global”

Parameter Passing Scalars are copied –“Pass by value” Arrays (and all objects) pass “by reference” –The values in the array are not copied Be careful to make “side effects” explicit –No need to return the same reference Functions can also be passed as parameters –Unchangable, so “by reference” = “by value” Returned values work the same way

Recursion A function can call itself –Local variables are different each time Every invocation of the function must end –There must be a path that ends the recursion –That path must eventually be taken –The usual way to do this is an initial if statement Never essential –But sometimes more elegant than iteration

Binary Search with Recursion function binarySearch(theArray, key, low, high) { var middle; if (low>=high) { // Safety check! if (key==theArray[low]) { return low; } else { return -1; } } else { middle = Math.floor((low+high)/2); // Explicit! buildOutput( theArray, low, middle, high ); if (key<=theArray[middle]) { // Equality! return binarySearch(theArray, key, low, middle); } else { return binarySearch(theArray, key, middle+1, high); }

Using JavaScript with Forms HTML: Please enter a number: The sum of all numbers up to the number above is JavaScript: var num = eval(document.input.number.value); document.output.number.value = 10; Reads in a value from the first form (eval method turns it into a number) Changes the value in the second form

HTML Form Element Types Textarea (multiple lines) Input –Text (single line) –Password (like text, but masked) –Hidden (like text, but not displayed at all) –Button –Checkbox (multiple selection) –Radio (single selection) Select (dropdown list) see for examples

Linking Forms to Functions Events: –Actions that users perform An “event handler” is triggered by an event –onClick: the user clicked on the item –onMouseover: the mouse moved onto the item –onMouseout: the mouse moved off of the item

Referring to Form Content Enter integer search key … var inputVal = document.getElementById("inputVal"); var searchKey = inputVal.value; Please enter your age … var age = document.years.box.value;

Design Tips Protect against unexpected values –Test the value of all user input –Test the value of critical function parameters Verify that every loop will always terminate –Include a bailout condition, and report it Always test for conditions explicitly –Trap unexpected conditions with the final else

Programming Tips Attention to detail! –Careful where you place that comma, semicolon, etc. Don’t get cute with the logic or the layout –Reflect the structure of your problem clearly –Use standard “design patterns” Write a little bit of code at a time –Add some functionality, make sure it works, move on Debug by viewing the “state” of your program –Print values of variables using document.writeln();

Documentation Tips Reflect your pseudocode in your code –Use meaningful variable names –Use functions for abstractable concepts And name those functions well –Use comments to fill remaining gaps Add a comment to identify each revision –Give author, date, nature of the change Waste space effectively –Use indentation and blank lines to guide the eye

Why Program? Data manipulation Simulation Control –Interaction –Embedded

Before You Go On a sheet of paper, answer the following (ungraded) question (no names, please): What was the muddiest point in today’s class?