Fluency with Information Technology

Slides:



Advertisements
Similar presentations
Chapter 3 – Web Design Tables & Page Layout
Advertisements

KompoZer. This is what KompoZer will look like with a blank document open. As you can see, there are a lot of icons for beginning users. But don't be.
Chapter 6 Photoshop and ImageReady: Part II The Web Warrior Guide to Web Design Technologies.
1 HTML Markup language – coded text is converted into formatted text by a web browser. Big chart on pg. 16—39. Tags usually come in pairs like – data Some.
Chapter 20 Thinking Big: Functions. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Anatomy of a Function Functions are packages for.
1 A Balanced Introduction to Computer Science, 2/E David Reed, Creighton University ©2008 Pearson Prentice Hall ISBN Chapter 7 Event-Driven.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Fluency with Information Technology Third Edition by Lawrence Snyder Chapter.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Iteration Principles Once is Not Enough lawrence snyder c h a p t e r 21.
HTML Tables and Forms Creating Web Pages with HTML CIS 133 Web Programming Concepts 1.
1 The Structure of a Web Table beginning of the table structure first row of three in the table end of the table structure table cells You do not need.
Chapter 9 Introduction to ActionScript 3.0. Chapter 9 Lessons 1.Understand ActionScript Work with instances of movie clip symbols 3.Use code snippets.
1 Events Lect 8. 2 Event-driven Pages one popular feature of the Web is its interactive nature e.g., you click on buttons to make windows appear e.g.,
Chapter 4 Dreamweaver: Part II The Web Warrior Guide to Web Design Technologies.
The Bean Counter: A JavaScript Program
Amber Annett David Bell October 13 th, What will happen What is this business about personal web pages? Designated location of your own web page.
1 CSC 121 Computers and Scientific Thinking David Reed Creighton University HTML and Web Pages.
DHTML AND JAVASCRIPT Genetic Computer School LESSON 2 HTML TAGS G H E F.
Announcements Assignment 9 is due Project 2 is due 7/27/04 We will be skipping chapter 22 Test 3 (chapters 18-21) will be on 7/29/04 Tip of the Day : Functions.
Designing a Web Page with Tables. A text table: contains only text, evenly spaced on the Web page in rows and columns uses only standard word processing.
Chapter 5: Windows and Frames
Chapter 22 The Smooth Motion: Case Study in Algorithmic Solving.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Fluency with Information Technology Third Edition by Lawrence Snyder Chapter.
Learning Objectives State and apply the Decomposition Principle
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
Photoshop Image Slicing. Reasons to Image Slide To create links out of sliced images To optimise different areas. (flat areas of colour, such as logos,
1 CSC160 Chapter 7: Events and Event Handlers. 2 Outline Event and event handlers onClick event handler onMouseOver event handler onMouseOut event handler.
Positioning Objects with CSS and Tables
Chapter 4 Frames and Cascading Style Sheets. Frames Frames divide a browser window into two or more separate pieces or panes, with each pane containing.
JavaScript, Sixth Edition
Section 6.1 Section 6.2 Write Web text Use a mission statement
Event-Driven Programming
CNIT 131 HTML5 - Tables.
In this session, you will learn to:
Using DHTML to Enhance Web Pages
Scratch for Interactivity
Chapter 8: Writing Graphical User Interfaces
Positioning Objects with CSS and Tables
Tables and Frames.
Working with Tabs and Tables
Section 17.1 Section 17.2 Add an audio file using HTML
Web Development & Design Foundations with HTML5 7th Edition
JavaScript: Functions
Programming the Web using XHTML and JavaScript
JavaScript: Functions.
Using Procedures and Exception Handling
Chap 7. Building Java Graphical User Interfaces
Chapter 7 Advanced Formatting and Merging Publications with Data
Chapter 10 - JavaScript: Functions
Graphical User Interfaces -- Introduction
Controlling Layout with Style Sheets
MODULE 7 Microsoft Access 2010
Learning Objectives Apply JavaScript rules for functions, declarations, return values, function calls, scope of reference, and local/global variable reference.
Computers and Scientific Thinking David Reed, Creighton University
JavaScript Functions B. Ramamurthy 11/22/2018.
Tutorial 3 – Creating a Multiple-Page Report
Events Comp 205 Fall 2017.
Using HTML Tables SWBAT: - create tables using HTML
Tutorial 6 Creating Dynamic Pages
DREAMWEAVER MX 2004 Chapter 3 Working with Tables
Using Cascading Style Sheets (CSS)
Training & Development
Chapter 20: Programming Functions
Project 4 Creating an Image Map.
The Smooth Motion: Case Study in Algorithmic Solving
University of Warith AL-Anbiya’a
Chapter 7 Event-Driven Pages
Introduction to Programming and JavaScript
Guidelines for Microsoft® Office 2013
Positioning Objects with CSS and Tables
Presentation transcript:

Fluency with Information Technology 7th Edition Chapter 21 A Case Study in Algorithmic Problem Solving

Learning Objectives 1.1 State and apply the Decomposition Principle 1.2 Explain the problem-solving strategy used in creating the Smooth Motion application 1.3 Explain the use of the JavaScript operations for iteration, indexing, arrays, functions, animation controls, and event handlers in Smooth Motion 1.4 Explain how mouse events are handled in Smooth Motion

Case Study: The Smooth Motion Application Our target: The Smooth Motion Application GUI and components Components: Heading: The text "Smooth Motion" Grid: The 7x20 grid of squares Keys: The row of seven orange/yellow boxes Controls: The buttons and radio settings Instructions: the text at the bottom

Figure 21.1 Smooth Motion: GUI and Components The Smooth Motion Application user interface. Try it at pearsonhighered.com/cs-resources.

Smooth Motion: How the App Should Work Starts automatically 5 seconds after loading Fills the grid from the right with stacks of blocks of random height Blocks move steadily to the left at a rate determined by the controls Stack generation continues until the user places the mouse cursor over one of the brown keys When the mouse hovers over key n, a stack of n blocks appears When the user has moved the mouse smoothly enough to create a perfect staircase rising to the right in the grid, the action stops Process started or stopped at any point using Go and Stop buttons. Speed is selectable in milliseconds: the rate at which the blocks move left Test requires a smooth mouse motion across the keys from left to right; rate corresponds to the frame rate of the grid animation

Planning Smoot Motion There are timer events for both the animation and mouse events for the controls that happen simultaneously We approach it in a methodical step-by-step way, applying a standard divide-and-conquer technique to simplify our work Breaking the project into convenient, manageable pieces ensures success

1.1 The Decomposition Principle Divide a large task into smaller subtasks that can be solved separately Combine their solutions to produce the overall solution The Decomposition Principle can be applied to each of the subtasks, producing even smaller subtasks The components become small enough they become solvable

Decomposition: Listing the Tasks Build UI: Create a web page with the table and its five parts Animate Grid: Move the block stacks to the left Sense Keys: Handle the mouse events and transfer the control information to the grid animator Detect Staircase: Recognize, when among a stream of events, the user has "met the test" Build Controls: Implement the actions to control the application Assemble Overall Design: Build the automatic random start-up, handle the starting and stopping, set the speeds, and interconnect the other components Refine Design: Make the page attractive and functional

1.2 A Problem-Solving Strategy The project has a number of steps, some of which may be dependent on each other In what order should the steps be performed?

First, Build a Basic Web Page The Build UI task Provides a place to test and save the solutions of the other tasks The page is an organizing structure, to which we can add JavaScript code Build only the basic primitive page The UI construction is split into two: Working prototype first Embellishment second

Second, Identify Dependent and Independent Tasks Consider the task dependencies: Some tasks rely on or depend on the solution of other tasks Tasks that do not rely on the solution of any other tasks are independent, and should be done first. Tasks that depend on the independent tasks are done next Tasks that depend on them follow

Third, Plan Tasks in Order Plan to schedule the tasks based on the rule: Perform any task when all of the tasks it depends on are solved! If all of the tasks are mutually dependent: Begin the dependent tasks Do them as far as possible until they need the results of another task Work on the other task

Fourth, Use a Tool to Track Dependencies Keeping track of many dependencies can be confusing Engineers and managers draw a task dependency graph, or PERT chart There are several ways to draw them: We'll use circles and use arrows to show dependencies The task at the head of the arrow depends on the task at the tail of the arrow

Figure 21.2 Smooth Motion's PERT Chart A task dependency diagram, also known as a PERT chart. Tasks are in circles, and arrows are read as "task at head of arrow depends on task at tail of arrow."

1.2 Our Strategy: The PERT Chart in Words Build GUI for a basic web page Animate Grid (dependent on Build GUI task) Sense Keys (dependent on Build GUI task) Detect Staircase (dependent on Animate Grid and Sense Keys) Build Controls (dependent on Animate Grid) Assemble Design (working with parts not completed) Refine Design (embellishment)

Build the Basic Web Page UI The full UI for Smooth Motion will be a table: heading, grid, keys, controls, and instructions The structural page includes: Table, heading, and instructions Background color Font style and color Application centered on the page

The Structural Page: Tables Easiest to build tables "inside out" using Copy/Paste Construct a generic table cell with <td> tags Replicate it to make a row, and enclose in <tr> tags Replicate the row to make the table, and enclose in <table> tags Fill it in

The Structural Page: Other Components For the heading text, use an <h1> tag For the instructions, use a <p> tag Because the instructions text has a different color than the other text on the page, set the font color Note the middle three rows are empty because they are white space They are still defined in HTML

Figure 21.3 The Structural Page: Markup and GUI Image and HTML for the structural page. The table appears compressed because rows 2-4 contain nothing. Remember, your browser image may be slightly different.

1.3 Animating the Grid: First Analysis The application only discusses "stacks" of blocks This implies that there is no "motion" of images vertically, only horizontally The horizontal motion is limited to moving from right to left We don’t have to animate individual squares The images can be whole columns This simplification reduces the total number of images in the grid to 20 (or the number of columns) New subtask: define and organize the column frames

Indexing Columns from Left to RIght Consider the "motion" of an image On each time step, any column is replaced by the column to its right If the 20 columns are indexed left to right: The image in column i of the grid is replaced on the next time step by the image in column i+1 The columns will be indexed from 0, left to right

Indexing Columns: The Browser When browsers place images on a page, they are recorded in the array document.images in the order encountered The leftmost column of the grid is document.images[0] The action replaces the contents of document.images[i] with the contents of document.images[i+1]

Indexing Columns: Creating Motion Shifting each column to the left is easy, but the last column is handled differently Handling the last column, 19, is also easy because we only need to assign a new image Which frame is assigned? If in the random start-up phase, it should be a random frame If we are in the user-controlled phase, it should be whichever frame the user has specified by the mouse position

Animating the Grid: Second Analysis Do we need to add subtasks for defining the image-shifting process? No, because both activities are part of the timer event handler The Animate Grid task subtask list has increased by one item: Define/organize the 8 columnar frames Define and place the initial images Prefetch the 8 frames for updating images Set a timer with an event handler

Subtask: Define and Organize the Frames The image files have names indexed in accordance with the block height The images have the necessary colors and lines that will be placed side-by-side to construct the grid

Figure 21.5 Smooth Motion's Block Images The eight fames required for the Smooth Motion application.

Subtask: Define/Organize the Frames Two guidelines to follow when creating frame images for JavaScript animations: Ensure that all images overwriting one another have the same dimensions in pixels Ensure that all files are saved using either the .gif or .jpg formats, and that they are used consistently

Subtask: Define/Place Initial Images This subtask constructs the grid in the second row of the structural page The initial state of the grid is created from 20 copies of Stack0.gif:\ To use JavaScript’s for statement, we place the <script> tags inside the second row’s <td> tags To have the images appear we must place them using the document.write() function: <tr><td> <script> for (var j=0; j<20; j++) { document.write('<img src="gifpix/Stack0.gif" alt=""/>'); } </script> </td></tr>

Subtask: Prefetch the Frame Images Prefetching is necessary to overcome any delays in image retrieval Prefetching can be performed at any time prior to the start of the animation Place the prefetched images with the code from the initialization subtask

Subtask: Three Steps of Prefetching Declare the array into which the images will be fetched. Initialize the array elements to be image objects Define the image structure for each array element using the new Image( ) specification. Assign the names of the files to the src fields of the image objects

Subtask: The Prefetching Code Call the array pics Use a separate iteration for the second and third tasks Insert the code within the <script> tags after the declaration The file names are constructed on the fly: var pics = new Array(8); for (var j=0; j<8; j++) { pics[i] = new Image(); } for (var j=0; j<8, j++) { pics[i].src="gfpix/Stack" + j + ".gif";

Subtask: Set Timer, Build Timer Event Handler Write the event handler to move each grid image one position left Begin constructing that event handler animate() The event handler has three operations: Move all images but the first, one position left Assign a new frame to image 19 Schedule itself for some time in the future

Subtask: Timer & Event Handler – New Frame The Assemble Overall Design task will take care of creating the mechanism for choosing the new frame Assigning a random frame is an easy way to have something different happen on each tick Assigning random frames is already the way the application begins

Subtask: Timer & Event Handler – document.images Recall that browsers store the details of the images they display in an array called images The array is referenced as document.images The source field, src, is the relevant one to change to display a new image

Subtask: Timer & Event Handler – randNum() The randNum() function must be declared in order to use it function randNum (range) { return Math.floor(range * Math.random()); { Add a variable duration to the list of declarations var duration = 125; Include the timerId statement to get the process started automatically timerID = setTimeot("animate()", 5000); Review Figure 21.6 for the state of Smooth Motion so far

The Best Laid Plans… Next, we'd planned to solve key sensing But it’s inconvenient not to have controls to stop and start the animation So we will change our plan and build the controls next Your plans are rarely perfect; sometimes you need to change

Build Controls: Events The controls entry of the table contains seven separate input controls: Go button, Stop button, and 5 speeds The fourth row of the table contains the <form> tags There are three events that could happen when a control is clicked: Go button click-event: Start the animation with setTimeout() Stop button click-event: End the animation by clearing the timer Radio button click-event: Set the timer interval by assigning to selected duration

Build Controls: HTML Markup <form> <input type="button" value="Go" onclick='timerId=setTimeout("animate()",duration)'/> <input type="button" value="Stop"onclick="clearTimeout(timerId)"/> <input type="radio" name="speed" onclick="duration=25"/> 25 <input type="radio" name="speed" onclick="duration=50"/> 50 <input type="radio" name="speed" onclick="duration=75"/> 75 <input type="radio" name="speed" onclick="duration=100"/> 100 <input type="radio" name="speed" onclick="duration=125" checked="checked"/> 125 </form>

Sense the Keys The Sense Keys task implements the ability to recognize when the mouse hovers over a given key Browsers recognize events caused by controls If an image is clicked, a click event is caused The event is processed with an event handler The event handler is specified by using the onclick attribute of the image tag

1.4 Sense the Keys: mouseover and mouseout With the help of the operating system, the browser keeps track of where the mouse is When the mouse moves over an image, a mouseover event is recognized When the mouse moves off the object, a mouseout event is recognized Both events are needed to follow the mouse cursor across the Smooth Motion keys

Sense the Keys: Decomposition Principle Decompose the Sense Keys task: Define and organize the necessary frames Place the initial images and create the keys Prefetch the frames Build the event handlers

Subtask: Define and Organize the Key-Sensing Frames The first subtask involves the two images: OrangeBox.gif YellowBox.gif Moving the files to the gifpix directory with the Stack images completes this subtask

Subtask: Place the Initial Images When the images are placed, the keys are created Seven orange images are placed in the center of the third row of the structural page’s table The JavaScript loop to iterates the document.write of the <img src="..."/> tags The resulting code is temporarily incomplete: for (var j=0; j<7; j++) { document.write('<img src="gifpix/OrangeBox.gif" alt=""/>'); }

Subtask: Prefetch the Frames There are two frames to prefetch, a small array is declared: var keypix = new Array(2); keypix[0] = new Image(); keypix[1] = new Image(); keypix[0].src = "gifpx/OrangeBox.gif"; keypix[1].src = "gifpx/YellowBox.gif";

Subtask: Event Handlers We need to build two event handlers: here() for mouseover gone() for mouseout When the mouse moves over a key: The key must change color to give feedback to the user that the mouse is on or off the key This involves updating the key’s image with YellowBox.gif or OrangeBox.gif

Subtask: Event Handlers - Requirements The mouse-sensing event handlers must tell the Grid Animation event handler which new Stack image to draw The event handler needs the key’s position Assigns the position to a global variable (frame)

Subtask: Event Handlers - Implementation The key’s position must be a parameter here() solves the problem of mismatched indices by adding 1 For gone() we don’t know where the mouse is moving to The mouse could be moving to another key or moving off the keys entirely If the mouse moves to another key, its mouseover event handler will quickly replace the zero from gone() function here (pos) { document.images[20+pos].src = "gifpix/YellowBox.gif"; frame = pos + 1; } function gone (pos) { document.images[20+pos].src = "gifpix/OrangeBox.gif"; frame = 0;

Sense Keys: Combine the Subtasks Modify the initial image placement to add the event attributes The image placement code arranges for the mouse event handlers to be called with the for loop’s iteration variable j To test this arrangement, change the Grid Animation event handler, animate(): document.images[19].src = pics[frame].src; Then the animation will show which key has been detected

Sense Keys: The Code So Far Note: the two declarations and the two event handlers are not shown: <script> var keypix = new Array(2); keypix[0] = new Image(); keypix[1] = new Image(); keypix[0].src = "gifpx/OrangeBox.gif"; keypix[1].src = "gifpx/YellowBox.gif"; for (var j = 0; j < 7; j++) { document.write('<img src="gifpix/OrangeBox.gif" ' + 'onmouseover = "here(' + j + ')" ' + 'onmouseout = "gone(' + j + ')" alt=" "/>'); } </script>

Staircase Detection Animation stops when the user has manipulated the mouse to create a rising “staircase” A staircase occurs when the frame values for seven consecutive animate() calls are 1, 2, 3, 4, 5, 6, 7 The value of frame tells the animate() event handler which Stack frame to display If it displays the seven frames in order on seven consecutive ticks, there is a staircase

Subtask: Recognizing the Staircase Possible approaches for recognizing the seven consecutive frame values: Keep an array of the seven most recent frame values and check each time to see if the desired sequence occurs Look at the src fields in the last seven images of the grid to see if they have the right sequence of file names Predict (and check) the next frame value – we will choose this approach

Subtask: Recognizing Continuity Create the prediction variable next1, and initialize to 1 Modify the animate() function at the point where it is about to set the timer for the next tick If the staircase is found, there will be no next tick if (frame == next1) //Correct prediction? next1 = next1 + 1; //Yes, make another else //No next1 = 1; //Go back to start if (next1 != 8) //Are we still looking? timerId = setTimeout ("animate( )",duration); //Yes, set timer

Assemble Overall Design: Status Check The Build Controls task has been performed out of order Parts of the Assemble Overall Design task have been performed ahead of time The display of randomly selected stacks of blocks isn’t currently working Time to put the Animate Grid task back in

Animate Grid: Process Set image 19 to frame or randNum(8), depending on whether the user has ever passed the mouse over a key The mouseover event handlers record the situation in frame Start out with frame initialized to an erroneous number and test it in the animate() event handler If the erroneous number is there, the mouse has not yet passed over the keys the first time; Anything else means the mouse has passed over the keys the first time

Animate Grid: Code Revision function animate() { shiftGrid () checkStairAndContinue (); } function shiftGrid() { for (var j = 0; j < 19; j++) { document.images[j].src = document.images[j+1].src; if (frame == -1) document.images[19].src = pics[randNum(8)].src; else document.images[19].src = pics[frame].src; function checkStairAndContinue() { if (frame == next1) next1 = next1 + 1; next1 = 1; if (next1 != 8) timerId = setTimeout("animate( )",duration);

Refine the Design The following improvements need to be added: Cell padding Revised instruction colors table {margin-left:auto; margin-right:auto; background-color:#a80000; padding:5%} td {padding:15px}

Assessment and Retrospective Review the solution in three areas: Loops Parametrizing functions for reuse Managing complexity with functions

Assessment: Loops Loops save us from writing the same statement multiple times Loops simplify programming There may be times when a loop will work, but you choose not to use one The computer does the same work either way You decide which method is more convenient

Assessment: Parameterizing Functions for Reuse The here() and gone() functions use a single parameter that is the position of the key in sequence, e.g. Separate functions could have been written in which the key’s position is used explicitly However, this would create many almost- identical functions

Assessment: Managing Complexity with Functions Functions manage complexity Functions clarify how the animation function works People will see the function names and interpret them as describing what the function does The code teaches viewers of the program both how the problem was solved and how the computer was instructed to solve it

Summary To solve a problem, we: Defined the tasks and strategized about the order in which to solve them Used a dependency diagram to show which tasks depended on others and to assist us in strategizing Adjusted the schedule to address aspects like ease of testing Developed the Smooth Motion application using the Decomposition Principle to identify subtasks Used the programming facilities covered in earlier chapters—loops, functions, parameters, and so on—as tools to instruct both the computer and humans looking at the program