Docsity

Understanding High-Level Languages & Event-Driven Programming, Slides of Artificial Intelligence

Toggle sidebar
bg1
1
CSCI 100
Think Like Computers
Lecture 18
Fall 2008
Last Time …
•Algorithms
•Design and Analyzing algorithms
•Evaluating algorithms
Big-O notation for time
•Some Examples
Sequential search
Binary search
Algorithms
•the central concept underlying all computation is
that of the algorithm
an algorithm is a step-by-step sequence of instructions
for carrying out some task
•programming can be viewed as the process of
designing and implementing algorithms that a
computer can carry out
a programmer’s job is to:
create an algorithm for accomplishing a given objective, then
translate the individual steps of the algorithm into a
programming language that the computer can understand
Algorithms and Programming
•programming is all about designing and coding algorithms for
solving problems
the intended executor is the computer or a program executing on t hat
computer
instructions are written in programming languages which are more
constrained and exact than human languages
•the level of precision necessary to write programs can be frustrating
to beginners
but it is much easier than it was 50 years ago
early computers needed to be wired to perform computations
with the advent of the von Neumann architecture, computers c ould be
programmed instead of rewired
an algorithm could be coded as instructions, loaded into the memory of the
computer, and executed
Machine Languages
•the first programming languages were known as
machine languages
a machine language consists of instructions that correspond
directly to the hardware operations of a particular machine
i.e., instructions deal directly with the computer’s physical components
including main memory, registers, memory cells in CPU
very low level of abstraction
machine language instructions are written in binary
programming in machine language is tedious and error prone
code is nearly impossible to understand and debug
•excerpt from a machine language program:
High-Level Languages
•in the early 1950’s, assembly languages evolved from machine
languages
an assembly language substitutes words for binary codes
much easier to remember and use words, but still a low le vel of
abstraction (instructions correspond to hardware operations)
•in the late 1950's, high-level languages were introduced
high-level languages allow the programmer to writ e code closer to the
way humans think (as opposed to mimicking hardware operat ions)
a much more natural way to solve problems
plus, programs are machine independent
Docsity.com
pf3
pf4
pf5

Partial preview of the text

Download Understanding High-Level Languages & Event-Driven Programming and more Slides Artificial Intelligence in PDF only on Docsity!

CSCI 100

Think Like Computers

Lecture 18

Fall 2008

Last Time …

• Algorithms

• Design and Analyzing algorithms

• Evaluating algorithms

Š Big-O notation for time

• Some Examples

Š Sequential search

Š Binary search

Algorithms

• the central concept underlying all computation is

that of the algorithm

Š an algorithm is a step-by-step sequence of instructions

for carrying out some task

• programming can be viewed as the process of

designing and implementing algorithms that a

computer can carry out

Š a programmer’s job is to:

ƒ create an algorithm for accomplishing a given objective, then ƒ translate the individual steps of the algorithm into a programming language that the computer can understand

Algorithms and Programming

  • programming is all about designing and coding algorithms for solving problems Š the intended executor is the computer or a program executing on that computer Š instructions are written in programming languages which are more constrained and exact than human languages
  • the level of precision necessary to write programs can be frustrating to beginners Š but it is much easier than it was 50 years ago Š early computers needed to be wired to perform computations Š with the advent of the von Neumann architecture, computers could be programmed instead of rewired ƒ an algorithm could be coded as instructions, loaded into the memory of the computer, and executed

Machine Languages

  • the first programming languages were known as

machine languages

Š a machine language consists of instructions that correspond directly to the hardware operations of a particular machine ƒ i.e., instructions deal directly with the computer’s physical components including main memory, registers, memory cells in CPU ƒ very low level of abstraction Š machine language instructions are written in binary ƒ programming in machine language is tedious and error prone ƒ code is nearly impossible to understand and debug

  • excerpt from a machine language program:

High-Level Languages

  • in the early 1950’s, assembly languages evolved from machine languages Š an assembly language substitutes words for binary codes Š much easier to remember and use words, but still a low level of abstraction (instructions correspond to hardware operations)
  • in the late 1950's, high-level languages were introduced Š high-level languages allow the programmer to write code closer to the way humans think (as opposed to mimicking hardware operations) Š a much more natural way to solve problems Š plus, programs are machine independent

Program Translation

  • using a high-level language, the programmer is able to reason at a high-level of abstraction Š but programs must still be translated into machine language that the computer hardware can understand/execute
  • there are two standard approaches to program translation

Š interpretation Š compilation

Program Translation

  • real-world analogy: translating a speech from one language to another
  • an interpreter can be used provide a real-time translation Š the interpreter hears a phrase, translates, and immediately speaks the translation Š ADVANTAGE: the translation is immediate Š DISADVANTAGE: if you want to hear the speech again, must interpret all over again
  • a translator (or compiler ) translates the entire speech offline Š the translator takes a copy of the speech, returns when the entire speech is translated Š ADVANTAGE: once translated, it can be read over and over very quickly Š DISADVANTAGE: must wait for the entire speech to be translated

Speech Translation

  • Interpreter:
  • Translator (compiler):

Interpreters

  • for program translation, the interpretation approach relies on a program known as an interpreter to translate and execute high-level statements Š the interpreter reads one high-level statement at a time, immediately translating and executing the statement before processing the next one Š JavaScript is an interpreted language

Compilers

  • the compilation approach relies on a program known as a compiler to translate the entire high-level language program into its equivalent machine-language instructions Š the resulting machine-language program can be executed directly on the computer Š most languages used for the development of commercial software employ the compilation technique (C, C++)

Event-driven Programming

• one popular feature of the Web is its interactive

nature

Š e.g., you click on buttons to make windows appear

Š e.g., you enter credit card information in a form and

submit it

• pages that respond to user actions such as

mouse clicks or form entries are known as

event-driven pages

Š JavaScript code can be combined with HTML

elements such as buttons, text fields, and text areas

to produce event-driven pages

Output via Text Boxes

• a button provides a simple mechanism for user

interaction in a Web page

Š by clicking the button, the user initiates some action

• a text box is an event-handler that can display

text (a word or phrase)

Š unlike an alert window, the text box appears as a box

embedded in the page

Š text can be written to the box by JavaScript code (i.e.,

the box displays output)

Output via Text Boxes

for example, we could reimplement the lucky number page using a text box

the text box containing the random number is embedded in the page

doesn't require the user to close the alert window after each number

Output via Text Boxes

  • general form of a text box element:

Š the TYPE attribute of the INPUT element identifies the element to be a text box Š the NAME attribute gives the element a name so that it can be referenced Š the SIZE attribute specifies the size of the box (number of characters that fit) Š the VALUE attribute specifies text that initially appears in the box

  • to display text in a text box, a JavaScript assignment is used to assign to its value attribute Š as part of the assignment, must specify the absolute name of the box Š the general form is:
  • document. FORM_NAME. TEXTBOX_NAME. value = VALUE_TO_BE_DISPLAYED;

Text Box for Displaying Output

  • when the button is clicked, the function call
  • DisplayNumber (); is executed
  • the function generates the random number and assigns it to the text box
  • as a result, each button click yields a new number in the box

Text Box Example Input via Text Boxes

  • text boxes can also be used for receiving user input Š the user can enter text directly into the box Š that text can then be accessed by JavaScript code via the absolute name of the box document.FORM_NAME.TEXTBOX_NAME.value Š note that the value retrieved from a text box is always a string ƒ if the user enters a number, say 93, then the absolute name will access "93" ƒ similar to prompt, you must use parseFloat to convert the string to its numeric value
  • example: we can revisit our temperature conversion

page

Š the user enters the Fahrenheit temperature in a text box Š at the click of a button, the input is accessed and converted to Celsius Š another text box is used to display the converted temperature

Text Boxes for Input

  • fahrBox is

used for input

  • Convert is

called when

the button is

clicked

  • celsiusBo

x is used for

output

Text Boxes for Input

Input and Output

note: the same text box can be used for both input and output

ƒ can modify the conversion page to allow for entering a temperature in either box, then convert to the other

ƒ can write a simple page in which the user can enter a number, then double it by clicking a button

Homework 3 – Due Nov 19

• JavaScript exercise:

• 1. Write a web page that prompts the user for a

number (as weight in grams), then convert the

weight to pounds and ounces, and display the

result on the same page

• 2. Redo the same problem (on a different web

page), but use a form (textboxs and buttons).

Use one textbox to get the input, and 2

textboxes for output (one for the number of

pounds, and one for ounces)

• 3. Email me the URLs of the two pages.

Homework 3 – Due Nov 19

• Extra credit will be awarded if your page can do

the reverse conversion, i.e. from lbs and ozs to

grams.

• Some hints:

Š Do a research for the formula for the conversions

Š Use Math.floor(x) to get the integer part of a decimal

number, i.e. Math.floor(3.5) will give you 3.

Š Thus, x – Math.floor(x) will give you the fraction part

of x.

Text Areas

  • a text area is similar to a text box but it can contain any number of text lines
  • general form of a text area element:
  • Š the NAME attribute gives the element a name so that it can be referenced Š the ROWS attribute specifies the height (number of text lines) of the area Š the COLS attribute specifies the width (number of characters) of the area Š the WRAP attribute ensures that the text will wrap from one line to the next instead of running off the edge of the text area
  • unlike a text box, opening and closing tags are used to define a text area Š any text appearing between the tags will be the initial text in the text area Š otherwise, the contents of a text area are accessed/assigned in the same way