Sei sulla pagina 1di 66

Computer programming: introduction

This class explains the basic components of computer programming. You'll explore many languages,
varying from simple scripting to object-oriented programming. You'll examine database query and
markup languages to help you understand how computers manipulate data and function across the
internet. You'll also get coding examples you can use in the real world.

Lessons
1. A brief history of programming languages: getting started
In this lesson, you'll become familiar with types of programming languages,
programming terminology and their uses. You'll also discover the essentials of
programming, such as how the operating system compiles, interprets and
executes commands.
2. Computer programming tutorial: learning parts of a program
This lesson covers program structure. You'll learn how control statements, loops
and conditional statements change the behavior of a program based on inputs
or other conditions. You'll also discover how variables and constants manage
your data.
3. Exploring subprograms and computer input and output
This lesson introduces subprograms, functions and subroutines, which enable
you to reuse commonly used portions of programs to save development time.
You'll also learn about graphical user interfaces, which make working with your
programs easier.
4. C, C++, C# and Java computer programming languages
Some of the most popular full-featured programming languages today are C,
C++, C# and Java. In this lesson, you'll learn a brief history of these languages
and how to structure simple programs using them.
5. Beginning programming for the web
A majority of programs written today are designed to work on the web. In this
lesson, you'll learn about web programming technologies and languages, such
as HTML, PHP and JSP. You'll also learn how XML exchanges data among
different systems.
6. Reading and storing computer data
In this lesson, you'll learn the important programming concepts of data storage,
file manipulation and memory management. You'll also learn how programs
interact with databases to efficiently retrieve and store large quantities of data.
7. Programming for networks and mobile devices
Knowing how to make computer programs that communicate with other
systems over the web is essential today. This lesson covers basic networking
concepts and programs that retrieve data from the internet, send email and
interact with mobile devices.
8. Discovering your future as a professional programmer
Get an introduction to the world of professional programming, starting with a
survey of various kinds of digital systems. The class ends with a discussion of
the environments and tools you work with as a professional programmer.

A brief history of programming languages: getting started


In this lesson, you'll become familiar with types of programming languages, programming terminology and their
uses. You'll also discover the essentials of programming, such as how the operating system compiles, interprets
and executes commands.
 

Welcome to Computer programming: introduction


Welcome to "Computer programming: introduction." For many of us, the world of programming
reaches far and wide into our everyday lives. You might use a computer daily to write reports,
play games, check your e-mail or process your company's financial records. In all of these
cases, you're using programs that people have written -- programs that perform a set of
actions the programmers want the computer to do.

In this course, you'll learn the fundamental concepts of computer programming and discover
how you can take the leap from using software provided by others to writing your own
computer programs. You'll explore many languages, varying from simple scripting to object-
oriented programming, while exploring topics such as logic, computer input and output and
memory management. You'll also examine database query and markup languages to help you
understand how computers manipulate data and function across the Internet. Finally, you'll
discover real-world programming examples in a variety of languages, including C and Java.

This eight-lesson course is designed for beginning programmers. You should have a
general familiarity with computers, but no prior programming knowledge is required.

Here's what to expect in the lessons:

Lesson 1: A brief history of programming languages: getting started helps you become
familiar with types of programming languages, programming terminology and their uses.
You'll also discover the essentials of programming, such as how a computer compiles,
interprets and executes commands.
Lesson 2: Computer programming tutorial: learning parts of a program covers program
structure. You'll learn how control statements, loops and conditional statements change the
behavior of a program based on inputs or other conditions. You'll also discover how
variables and constants manage your data.
Lesson 3: Exploring subprograms and computer input and output introduces subprograms,
functions and subroutines, which enable you to reuse commonly used portions of programs
to save development time. You'll also learn about graphical user interfaces, which make
working with your programs easier.
Lesson 4: C, C++, C# and Java computer programming languages dives into some of the
most popular full-featured programming languages today: C, C++, C# and Java. In this
lesson, you'll learn a brief history of these languages and how to structure simple programs
using them.
Lesson 5: Beginning programming for the web describes how a majority of programs
written today are designed to work on the Web. In this lesson, you'll learn about Web
programming technologies and languages, such as HTML, PHP and JSP. You'll also learn
how XML exchanges data among different systems.
Lesson 6: Reading and storing computer data explains the important programming
concepts of data storage, file manipulation and memory management. You'll also learn how
programs interact with databases to efficiently retrieve and store large quantities of data.
Lesson 7: Programming for networks and mobile devices describes how to make computer
programs that communicate with other systems over the Web, which is essential today.
This lesson covers basic networking concepts and programs that retrieve data from the
Internet, send e-mail and interact with mobile devices.
Lesson 8: Discovering your future as a professional programmer is an introduction to the
world of professional programming, starting with a survey of various kinds of programming
specialties. It continues with a discussion of the environments and tools you'll work with as
a professional programmer.

Beyond the lessons, complete the accompanying assignments and quizzes, which are
designed to help reinforce important concepts and topics.

Throughout this course, we provide Flash examples. To view these examples, you need
the Adobe Flash Player. Keep an eye out for notes with links that say "See how to ____" or
something similar. Some of these files are very large (10 MB or so) and may take a while
to appear or download if you have a slow connection.

Let's get started with the topics in Lesson 1.

What's programming?
Programming makes computers perform specific actions. For example, when you enter 2*7 on Computers
a pocket calculator, you give the calculator a set of instructions. You're saying, "Multiply the before
number two by the number seven, and give me the result." Most simple calculators can electronics
interpret a limited number of commands, so there are only a few instructions you can give it. A
computer, on the other hand, is capable of doing much more, and because of those
capabilities, you have to give the computer more sophisticated instructions in the form of a The idea of
programming language. mechanical
aids to
computation
Programming language essentials has been
kicking around
since way
A computer is essentially a machine that receives instructions and does something with those before
instructions. Like a calculator, it understands those instructions in a limited way. Computers electronics
don't (yet) understand English, Spanish, or French instructions; they only understand finally made
numbers. At the most basic level, they interpret sets of 1s and 0s, which are the equivalent of general-
on and off, or true and false. This reliance on two states (1 or 0) is why computers are called purpose
binary systems. computers
practical.
Probably the
The problem is, we humans think in terms of words and concepts, so programming languages most famous
were invented to mediate verbal concepts and binary mathematics. A computer, with the try at a
proper set of instructions, can translate words in a programming language into 1s and 0s, and mechanical
then execute the program. computer was
Charles
Babbage's
As you'll see, the history of programming has involved different kinds of programming
Analytical
languages, some of which were almost as complex as typing numbers directly into the
Engine.
computer's memory and others that use somewhat common phrases and commands to
enable programs to work. You might wonder why there are so many programming languages,
but they all fit a need or solve a problem. Some are better suited for working with hardware
devices, such as scanners or printers, whereas others are better for medical equipment, and
still others for Web e-commerce.

The language you choose for a given project depends on many factors, including which
languages you know well. Still, there are many concepts that almost all languages have in
common, which you'll see in the next section. Learning a new programming language is
sometimes just a matter of learning new names for familiar concepts.

Learning the basics of programming


This course is designed to introduce you to the basics of programming so you have a good
understanding of what programming is -- and what it isn't. You'll learn some standard
programming terms and concepts that are used in all programming languages. When you're
ready to learn a particular language, such as Java or C#, you'll already have a good
understanding of programming itself and can focus on the specific language you want to
learn.

The course lessons won't teach you how to program a particular language, although you'll get
a glimpse of the inner workings of BASIC, C, Java, and more. If you want to practice using
some of the programming structures taught in the course, however, you can install a language
editor on your computer and start writing your own programs. A good first language is BASIC.

A beginner's computer language

The first computer language to make programming accessible to beginners was BASIC
(Beginner's All purpose Symbolic Instruction Code). It was invented at Dartmouth College in
1963 for use in undergraduate beginning programming courses. When the microprocessor
revolution made it feasible for individuals to own computers, BASIC was usually the language
used because it was simple enough to run on small systems.

A version of BASIC for microcomputers was Microsoft's first product and the foundation of that
vast software empire. Until the release of Windows 2000, Microsoft operating systems came
with a version called QBASIC. Although Windows no longer includes BASIC, another version,
called Liberty BASIC, is available as a free trial download for all Windows systems. Continued
use of Liberty BASIC beyond the trial period requires a $49.95 license fee.

Microsoft also offers Visual Basic, a friendly, graphical programming language that enables
you to create programs with appealing user interfaces. You can download Visual Basic
Express Edition for free.

Now that you've been introduced to the concept of computer programming, it's time to dig into
the meat of the course. The remaining pages in this lesson delve deeper into what a
programming language is and isn't, and introduce you to the different kinds of programs you
might want to create.

Understanding types of programming languages


So far, you've learned that not all programming languages have the same flexibility or The least you
complexity as others. Just as there are times when your pocket calculator is built to give you need to
what you want quickly, there are many times when more limited programming is useful, or know
desired. For the purposes of introducing programming, and at the risk of oversimplification,
this lesson splits the types of programming and programming languages used into two
categories: scripting languages and full programming languages. If you're
concerned that
you won't be
Scripting languages able to keep
all of the terms
and concepts
A script is a simple, unsophisticated program. Essentially, scripting languages provide a list of associated
instructions for the computer to follow. For example, when you register to take an online class, with
a script program generally takes the information you submit and does the following: programming
straight, don't
Adds your name to the list of people taking the course fear. After you
Adds events to your calendar and modifies your user profile get
Sends you a confirmation e-mail comfortable
with
The capabilities and level of complexity of most scripts are limited; the programmer gives a list programming,
of instructions for the computer to perform every time a certain action or event happens. In the it'll become
online class example, a student clicks the Enroll button and she's registered and enrolled for second nature
the class. Scripts are written in scripting languages such as JavaScript and TCL (Terminal to you. It just
Control Language). You'll look more at JavaScript in lessons 3 and 5 of this course. takes a bit of
practice.
Full programming languages

Full programming languages are what most people associate with programming. Languages
such as C# and Java provide the ultimate in power and flexibility, yet involve much greater
levels of complexity than scripts. Writing a program to perform an action as simple as printing
the words "Hello, World" can take over a half dozen lines of program code -- a scripting
language can do this in one or two lines.

Full programming languages achieve much of their power by means of extensive libraries of
code -- chunks of code that have been thoroughly debugged and documented, and can be
imported into your program with simple statements. Although not part of the language, these
libraries are essential when writing large applications.

When working with a full-featured language, you can write complete applications, or even a
total operating system. The Linux operating system, for example, is written entirely in C.
Today these full-featured programming languages are only a part of programming. These
kinds of languages are covered later in the course.

Companion languages

Programming has a few companions that are often listed as programming languages but
really aren't -- markup languages such as HTML (Hypertext Markup Language) and query
languages such as SQL (Structured Query Language). However, they're often used in
conjunction with programming languages to make programs do more.

Markup languages

Programmers use markup languages to add special information and actions to the basic
information contained in a document. They're called markup languages because special
instructions are added to a text file, and the original data is marked up.

HTML is the basic markup language used to create Web pages. To see what you've done,
you need a viewer, such as a Web browser. The browser displays your text and images
based on the instructions in the HTML embedded in the document. Whereas there's a limited
number of actions you can make Web pages perform, markup languages let you create these
actions quickly and easily.

Formatting documents and preparing Web pages are prime functions of markup
languages. An HTML page can hold images, tables, text, and anything a presentation
slide can, for example. Markup languages don't provide for the control or complexity to
write a full program, however, such as a billing or reservation system.

XML (Extensible Markup Language) is rapidly becoming the most significant markup language
in general use. It's used to move data around the Internet, and is essential to Microsoft's view
of future office application programming.

Dynamic web programming languages

The majority of computer programs developed today exist on the Web because it provides a
convenient, accessible user interface. After all, there's no easier way to interact with a
program than providing users with a URL (Uniform Resource Locator) they can access. Web
programs take input from a user (usually provided in a form), perform some action on it, and
then create an HTML page providing the output. Creating customized Web pages "on the fly"
is a technique known as dynamic Web programming.

The three most popular languages for writing dynamic Web pages are ASP (Active Server
Pages) from Microsoft, the open-source PHP (Perl Hypertext Preprocessor), and Sun's
JSP (Java Server Pages) technologies.

For example, consider the Bankrate.com Mortgage Loan Calculator, shown in Figure 1-1. The
program presents the user with a form that requests basic information about a bank loan: the
loan term, principal amount, and interest rate.

Figure 1-1: Mortgage loan calculator.

Enlarge image

After the user provides the information requested on the form, he or she clicks the
Show/Recalculate Amortization Table button. Clicking this button sends the information back
to the server, which calculates the required table and creates a customized Web page. An
example is shown in Figure 1-2, which contains the information requested by the user.
Figure 1-2: Amortization table.

Enlarge image

Query languages

Similar to scripting languages, query languages consist of a simple set of instructions for the
computer to process. Query languages are used with database systems to add, remove, or
view data. Because they operate similar to scripts, they can be considered a special type of
scripting language. When a query is run, a set of instructions is processed. An example of a
query language is SQL.

In lessons 5 and 6 of this course, you'll explore these special-purpose companion


languages and how you can use them with your favorite programming language to
enhance your programs.

With an understanding of scripting languages, full programming languages, and companion


programs under your belt, let's take a look at interpreters and compilers, which make your
code run.

Examining interpreters and compilers


 
Whatever language you decide to write a program in, it must be translated into code that a
computer understands. A special-purpose program called a compiler or interpreter performs
this translation. The term you use depends on the language you use. Some languages are
designed to be interpreted; others are designed to be compiled. And some languages can be
either interpreted or compiled. Both compilers and interpreters do the same thing, but in
slightly different ways.

Compilers

A compiler gathers all of a program's lines of code and, at one time, translates the entire
program into a format that's closer to machine code. Compiling a program this way takes time,
but after it's done, it's done; you can use the program whenever you want.

You need to make a distinction between compilers that translate to codes used by a specific
kind of hardware and operating system, and those that translate to codes that aren't hardware
specific. A C program compiled for Windows hardware can be run only on Windows because
the codes written by the compiler are specific to Intel microprocessors. If you want to run that
program on a Mac or an IBM mainframe, you're out of luck.

To get around this limitation, the concept of a virtual machine was invented, which is a
software implementation of a computer (the machine) that executes programs like a physical
computer. Java is a prominent language that uses the virtual machine concept. After carefully
considering all of the basic operations a computer might be called upon to do, the designers
of Java came up with a set of instruction codes to which any Java program can be compiled.
Every computer that can run a Java program has a small program specific to that computer
that can read the virtual machine codes and execute the corresponding code for the specific
hardware.

Interpreters

An interpreter, on the other hand, does very much what you'd expect something called an
interpreter to do. It reads one line of your instructions, figures out what this means for the
specific computer, and executes the required machine code. Interpreters allow for flexibility
and experimentation; however, interpreters run programs more slowly than a compiled
program can be executed because they have to translate every line each time the program is
run.

It's important to understand the main distinction between a compiler and an interpreter.
Both translate lines of code from a program into machine code; however, a compiler
translates every line at once, whereas an interpreter translates one line at a time.

Now that you're familiar with interpreters and compilers, let's turn our attention to the
importance of operating systems.

Understanding operating systems


An OS (operating system) is the most important set of instructions the computer has. These
are the instructions that tell it how to do basic actions, such as read and write to and from the
hard disk, CD disc, or flash disk; talk to the modem or Ethernet card; accept input from the
keyboard and mouse; display text and images on the monitor; and so on. Other programs
require the environment provided by the operating system to do these jobs. The most
common operating systems for personal computers are:

Microsoft Windows for PCs


Linux for PCs -- less common, yet gaining popularity
Mac OS for Apple Macintosh or iMac computers

Older PCs used DOS (Disk Operating System) as the operating system. Just to add to the
confusion, there are other operating systems such as OS/2 (now eComStation) and
BeOS. Larger computer systems use yet other OSs.

When some programs are written, instructions are used that are specific to one operating
system; however, this means that you have to change your programs to use them on a
computer with a different OS.

When you compile a program such as a C program, you compile it for one operating system's
set of instructions. If you want to use the program on a separate operating system, you have
to recompile it. You won't be compiling any C programs here, but it's something to remember.

Fortunately, modern languages try to minimize OS-specific commands. Languages such as


HTML and Java, for example, were specifically designed to be independent of which
computer they're interpreted or compiled on. They provide generic instructions for tasks such
as gathering input or writing output. When they're compiled or interpreted, the compiler or
interpreter translates the program with the appropriate instructions for that operating system.
However, for the time being, you don't have to concern yourself with this.

Moving on

In this lesson, you got an overview of basic programming concepts and terminology and were
introduced to different types of programming languages. In Lesson 2, you'll learn more about
the elements and constructs that make up a computer program. You'll also learn how to read
pseudocode, the language commonly used to describe what a computer program does.
Finally, you'll have the opportunity to set up your own computer so you're ready to try your
hand at a BASIC program or two. Before you move on, complete the assignment and take the
quiz for this lesson to reinforce important topics.

Assignment #1
One of the great advantages to learning programming in the Internet age is the huge array of public
resources available. Spend some time getting familiar with these resources, starting with the Tech
Terms Dictionary. Try entering some of the terms discussed in this lesson to learn more about them.

The major Internet search engines also index many sites that can be useful to the beginning
programmer. For example, try entering the phrase XML tutorial at Google or your favorite search
engine.

Assignment #2
Liberty BASIC has an active user community with many current tutorials and books. If your operating
system is Windows, you can download a trial version of Liberty BASIC for free. Save the downloaded
executable file (which is about 3 MB) somewhere convenient and double-click it to start the installation
process. The installed files will take a little over 8.5 MB of space.

Liberty BASIC opens automatically after installation. A six-lesson tutorial is included under the Help
menu (select Help, and then select Liberty BASIC Tutorial). Do the first lesson in the tutorial now.

For a powerful yet still relatively easy-to-learn language (with, let's face it, a cool name) you can find a
version of Python for just about any operating system at the Python Web site. A typical Python
distribution download is about 11 MB, and takes just over 35 MB of disk space. Just double-click the
downloaded executable file to start the installation process. Unless your hard drive is really cramped
for space, you should install all of the options.

Although you can use Python as a scripting language, it supports object-oriented programming and is
powerful enough to do serious work. The documentation furnished with Python includes a tutorial that
assumes a bit more familiarity with programming concepts than the Liberty BASIC tutorial.

Quiz #1
Question 1:
Which of the following are categories of programming languages? (Check all that apply.)
A) Scripting
B) Full
C) Application
D) Array
 
Question 2:
What translates a program's instructions one line at a time as they're needed?
A) An interpreter
B) A compiler
C) A programmer
D) A program companion
 
Question 3:
Which of the following examples are good uses for a markup language? (Check all that apply.)
A) Formatting a document
B) Preparing a Web page
C) Creating a reservation system
D) Writing a billing system
 
Question 4:
Which of the following languages can you use to create dynamic Web pages? (Check all that apply.)
A) ASP
B) BASIC
C) PHP
D) JSP
 
Question 5:
Which of the following jobs are performed by the operating system? (Check all that apply.)
A) Talking to the computer's hardware (disk drives, monitor, and so on)
B) Providing an environment for programs to run
C) Compiling programs
D) Providing debugging capabilities for programs
 

Computer programming tutorial: learning parts of a program


This lesson covers program structure. You'll learn how control statements, loops and conditional statements
change the behavior of a program based on inputs or other conditions. You'll also discover how variables and
constants manage your data.
 

Discovering control statements


Welcome back. In Lesson 1, you learned that a program is just a set of instructions that tells Always plan
a computer what to do in a language it can understand. In this lesson, you'll learn about the before you
different logical structures within a computer program that you use to create those code
instructions.

A key lesson
Every scripting or programming language has a different set of rules -- called syntax -- for experienced
using these different structures in a program written in that language. Although the syntax programmers
might be different across languages, the basic use of each structure is the same. For a know well is
general discussion, it's best to avoid using only the syntax of a single language, so you that you can't
should use pseudocode. Pseudocode is a way of writing instructions in something close to start
natural English but that's related to typical programming languages. programming,
or even decide
which
Scripting languages and full programming languages use structures called control structures to
statements to hold instructions that help computers make decisions about what to do (called use in your
conditional statements) or perform tasks multiple times (called loops). program, until
you know what
your program
needs to do.
More formally, a conditional statement performs different computations or actions
This takes
depending on whether a condition is true or false. A loop is a set of instructions that can
some thought
be executed one or many times, depending on a specific logical condition.
and planning on
your part. A
clear vision of
For example, say you want a computer to read your e-mail for you and then delete all the what you want
messages you've received that look like spam advertisements. In this case, you'd want the the program to
program to do the following: do makes
actual program
Open your e-mail inbox and find your new e-mail messages. writing much
Read the first message. simpler.
Determine if this message looks like annoying spam. Professional
Delete the message if it looks like annoying spam. programmers
Leave the message untouched if it might be a real message. often spend as
Go on to the next message and repeat the process until all messages have been much time
checked. planning as
they do
In this case, you're having the computer do work for you. To do that, it has to be able to
programming.
make decisions. You have to tell it exactly what to decide, and when. This is one of the
fundamental rules of programming: computers only do what their instructions tell them.

In the case of this e-mail routine, the computer has to make a decision with each message
whether or not it should delete it. You have to tell it to check the e-mail subject, and then
what text to look for. What the computer does from there depends on the message passing
this subject-matching test. This example includes a conditional statement (if the message
subject contains certain words, delete the message) and a loop (keep checking each
message until all messages are tested).

Now that you're familiar with basic control statements, let's move on to some more complex
loops and conditional statements.

Understanding loops and conditional statements


To write loops and conditional statements, you use statements that make a logical decision.
The most common conditional statement is an if-then statement. Here's a simple example:

IF (the message subject starts with ADV)


THEN (delete the message)

Different programming languages have slightly different ways of phrasing and punctuating
that statement; however, they all mean approximately the same thing. A common extension
of the if-then statement is the if-then-else statement. This says:

IF (some logical condition is met)


THEN (do something)
ELSE (do something else)

The (do something) part of the if-then-else statement is called the true branch, and the (do
something else) part is the false branch.

The other control statement used in the e-mail testing routine is a loop. There are several
different kinds of loops: loops that are executed according to a counter, and that execute
until a specific condition is met, among others. In some languages, there are even more
possibilities, but the loop examples mentioned here are the most common.

To force message processing to occur for each message in the inbox, you can use a while
loop that looks similar to the following:

WHILE (there are messages that haven't been read)


process the message
END WHILE

The end while statement lets the computer know which instructions are part of the loop, and
where the loop ends. The concept is simple. While the test statement is true (there are
messages that haven't been read), continue doing whatever is in the loop.

Although there are several types of loop statements, you can usually determine which one to
use by phrasing it as if you were giving instructions to a human. For example, you can
phrase a loop as, "Do this until you're out of messages," or, "Do this 100 times."

In pseudocode, the following is an example of a do-until loop:


DO
process the message
UNTIL (there are no more messages to process)

Note that because this loop always executes the process at least once, you must be sure
there's at least one message to process before entering the loop.

Different programming languages have different syntaxes and terms for loop control
statements. The concepts, however, are universal.

Loops with counters

A for loop uses a counter to perform a task a specified number of times. If your e-mail
program knew in advance how many messages were waiting, you could write the processing
loop this way:

FOR (number of messages)


process the next message
END FOR

Now, let's go back to your e-mail checking routine and fill in some details. In terms of the
control statements just discussed, it now looks something like this:

1. Open the mailbox and get any new messages.


2. WHILE (there are more messages to look at)
do the following:
a. Look at the subject of the message.
b. IF (the subject starts with ADV)
c. THEN delete it.
3. Get the next message.
4. END WHILE.
5. Close the mailbox. The program is done.

Notice that it's important to tell the program to get the next message in Step 3; otherwise,
your loop would keep testing the same message repeatedly. Not advancing to the next item
in a loop is a common error. Remember, computers are literal and can't read a programmer's
mind.

Conditional statements

In conditional statements, you use Boolean expressions to instruct the computer to make a
decision. A Boolean expression is one that can be evaluated as either true or false.
Remember back in school when you learned how to compare two numbers? This is the
same idea. A Boolean expression asks the computer to do one of the following:

Tell you if one value is greater than another


Tell you if one value is less than another
Tell you if one value is equal to another

More advanced Boolean operations use greater than or equal to, less than or equal to, and
the idea of "not," but it's essentially grade-school arithmetic in action.

In the example e-mail program, the message is deleted if the subject contains a certain
string. In an actual program, you'd use a Boolean expression to specify this condition in
terms a computer can understand.

Before you can work with control statements efficiently, you need to understand Boolean
expressions.
Programming languages provide syntax for creating Boolean expressions about data of
different types. For example, when comparing numbers, such as the number of letters in a
name compared with the maximum number allowed, you can write an expression that's true
when one number is equal to another. When comparing text, you can write an expression
that's true if one word comes before another alphabetically.

You can also combine Boolean results with the familiar AND, OR, and NOT logic. For
example, if you want to discard long e-mails from Julie, you might write something similar to
this:

IF (the message is from Julie


AND the message is longer than 30 lines)
THEN delete it

For most programs to run properly, they need to use variables to store data. In the next
section of this lesson, you'll take a look at the concept of variables and constants.

Programming with variables and constants


In programming terms, a variable is a name used to store and refer to a value, so variables Strong and
are where items of data live. Programs deal with all types of data, such as numbers, dates, weak typing
names, and letters. Variables provide the convention for naming, storing, and retrieving
these different kinds of information. For example, you can define a variable called count and
set it to store the number 5 by doing the following: Some
programming
languages,
such as Perl
count = 5
and PHP, have
weak typing,
You can then treat this variable the same way you'd treat a number. If you add count to a which means
number, it's just as if you added 5. Another variable, called total, is used in the following: they don't
require you to
declare a type
for variables. In
total = count + 2 fact, these
languages
quickly and
In this example, if count is still storing the value 5, you just set the variable total to equal 7.
easily convert
You can also perform operations such as:
variables to the
type they think
you need. If you
count = count + 10 store a number
as a string, and
try to multiply or
in which you just added 10 to the value held in the variable count. divide that
string later, it'll
convert the
One important point about variables is that, in most languages, you need to define the type variable to a
of information you're going to use them to store. It could be a type of number, letter, or number. Other
something else, such as a collection of variables (discussed shortly). For the previous languages,
examples, you need to define the count and total variables before you use them, as shown in such as Java
the following: and C, are
strongly typed --
they require you
integer: count, total to declare
which kind of
data they'll hold
An integer is a number such as 1, 2, 3, 4, and so on, with no decimal. For other numbers, before you use
you can use a type called real or float (short for floating point). Different languages store text them, and will
in different ways; for these pseudocode examples, you use a type called string that stores give you an
text as a string of letters. The analogy is a string of beads, one after the other. error if you try
to do something
strange (such
There are other special variables called constants. These are variables whose values you as multiply a
set once and never change. For example, in a mathematical program, you might declare a string by 3).
constant called pi, as shown in the following:

Weak typing is
real constant : pi usually a bad
pi = 3.14159 idea: It allows
ambiguity in
your programs
The value of pi doesn't change as you use it, so you store this value in a constant. Different and encourages
languages use different methods of declaring variables and constants; these are covered you to write
later in the course. For now, just use this pseudocode version. sloppy code.
Languages that
use weak typing
Collections of variables often provide an
option to
enforce strong
Most programming languages also enable you to put data values into a list or array. A list or
typing, which
array is like a grocery list in real life: It wouldn't make much sense to write each thing you
you should do.
need from the grocery store on separate sheets of paper. Instead, you put all these items
For example,
into a list.
you can require
explicit variable
In most programming languages, you can step through a list of data one item at a time using declarations in
a control structure such as for-end for. Many programming languages also enable you to Perl by
pick an individual item from a list using an index. For example, if you want to grab the third including the
item in a list, you'd use that list item's index number to access it. command use
strict "vars"; in
the header of
To continue with the earlier e-mail checking example, you might provide the program with an your Perl
array of names of people who are sending you annoying spam. The inner part of the while scripts.
control structure would then look something similar to this:

1. Look at the sender of the e-mail


2. FOR (every N in the SPAMMER array)
1. IF (the sender equals the Nth SPAMMER)
2. THEN delete the e-mail and stop the loop
3. END FOR

By using an array, you now have a program that can handle multiple spammer names with
only one named variable. Arrays combined with the for-end for control structure make it
possible to manipulate large amounts of data with simple commands. In addition, many
languages provide built-in functions that work with arrays. For example, you can sort an
array of strings alphabetically with a single command.

Some programming languages have specialized structures to add additional logic to


variables they hold onto. For example, a stack structure holds onto values in the order
they're added, and enables you to get at them in terms of their position relative to the top of
the stack.

Now that you've been introduced to variables and constants, among other aspects of basic
programming, the next section focuses on converting variables.

Due to conventions that started back in the mists of time, many languages consider the
index of the first item in an array to be zero. If you have an array with 10 items, they have
indexes 0 through 9. That's a little quirk you have to get used to.

Converting variables

Most programming languages require you to define the type of information that's stored there
as in the previous example:

integer: count, total


However, your program might have to deal with a representation of a number that the user
inputs as a series of digits. Languages provide functions that can convert between string and
number representations, such as the following example:

string: inputFromUser = getInput


count = parseStringAsNumber( inputFromUser )

In this example, you might expect the user to type in a number, such as 12 or 100. The user
input is stored as a string, so you can't perform mathematical operations with that input. It
might look like a number to you, but it's a string of characters to the computer. You have to
convert that string to a number.

Some other common functions that typical programming languages provide include
trigonometric functions such as sine, cosine, and tangent. Common functions that
manipulate strings include converting to uppercase.

Variables with multiple parts

Modern object-oriented languages such as C++, C#, and Java enable you to program with
variables that are containers for multiple simple variables. To continue the e-mail example, a
variable representing an e-mail message could contain the name of the sender, the time it
was sent, the subject, the message text, and an attached file.

Using a single variable to represent a complex object is an essential part of OOP (object-
oriented programming). The capability to hide many parts behind a single variable name is
part of what makes OOP so powerful. You'll learn more details about this power in several
upcoming lessons.

Moving on

This lesson introduced you to the basic building blocks of a simple program. You learned
about pseudocode, control and conditional statements, and loops. Boolean expressions were
explained, as well as variables and constants. In Lesson 3, you'll look at other important
structures that computer languages use, and some examples using what you learned in this
lesson. Before moving on, complete the assignment and take the quiz for this lesson.

Assignment #3
For this assignment, work through each of the problems, and then check your answers against the
solution provided:

1. You roll a die 10 times and each time record the number that comes up in an array of integers.
Which type of loop would you use in a program to look at all the numbers in the array?
2. If you flip a coin once and print one message for heads or another for tails, which type of
statement would you use?
3. You're writing a program that, as you move books from a box to a shelf, the title of each book is
printed. You have no advance knowledge how many books are in the box, and it might even be
empty. Which type of loop should you use?
4. Write the pseudocode for a program that prints the area of a simple triangle.

Challenge exercise

Write detailed pseudocode for a program that, given the current date, outputs tomorrow's date.

Solution
The solutions for this assignment are as follows:

1. A for loop provides a convenient index and counter. You could create the same effect with a
separate variable, but the for loop is easiest.
2. For a single simple decision based on a condition to be done once, an if-then-else statement is
the most obvious choice. You actually could write this as a while loop; however, it'd be very tricky
(and pointless) to do.
3. You could phrase this process as, "If there is a book in the box, get it, print the title, and put it on
the shelf." A while-end while loop would accomplish the task because you don't have to know
how many books are on the shelf, and the box could be empty.
4. The pseudocode for determining the area of a triangle and printing the results might look like the
following:

Get values for the base and height.


Calculate the area.
Print results.

Challenge exercise solution

The pseudocode for a program that outputs tomorrow's date given the current date might look like the
following:

1. Get the current date [month, day, year] in a numerical format.


2. If month = 1,3,5,7,8,10,12
If day = 31
increment month
else
increment day
else if month = 4,6,9,11
If day = 30
increment month
else
increment day
else if month = 2
If day = 29 or day = 28 and it's not a leap year
increment month
else
increment day
3. Output tomorrow's date [month, day, year].
increment day
add 1 to current day
increment month
if month = 12
increment year
else
add 1 to current month
day = 1
increment year
add 1 to current year
month = 1
leap year
divisible by 400 or divisible by 4 but not 100

Assignment #4
Think of a complex object that could be described as a collection of simple variables. For example,
consider a book -- you might use the following simple variables:

string: title
string: author
integer: year of publication
integer: number of pages

Based on this example, create a collection of variables for a digital song on your computer.
Solution

The following are possible variables for a digital song stored on a computer:

string: song title


string: artist
string: album name
string: genre
string: file format
integer: length
integer: year of publication

Quiz #2
Question 1:
Which term describes a set of rules used by a scripting or programming language?
A) Pseudocode
B) Syntax
C) Control statement
D) Boolean expression
 
Question 2:
True or False: A variable performs different computations or actions depending on whether a condition is true or
false.
A) True
B) False
 
Question 3:
True or False: Loops can use counters to perform a task a specified number of times.
A) True
B) False
 
Question 4:
Which one of the following variable types could hold the number 5.16 for use in mathematical operations?
A) Binary
B) String
C) Float
D) Integer
 
Question 5:
Which one of the following variable types could hold the value Renee?
A) Binary
B) String
C) Float
D) Integer
 

Exploring subprograms and computer input and output


This lesson introduces subprograms, functions and subroutines, which enable you to reuse commonly used
portions of programs to save development time. You'll also learn about graphical user interfaces, which make
working with your programs easier.
 

Using subprograms to make programming easier


Welcome back. In Lesson 2, you learned about logical control statements and loops that When to write
handle repetitive tasks. In this lesson, you'll learn about the use of subprograms and how subprograms
they facilitate breaking complex programming problems into simpler parts. You'll also explore
a new topic, what programmers call IO -- short for input and output.
Once you get
rolling on
A subprogram is a relatively small chunk of code that you can use repeatedly in other parts writing a
of your program. Without subprograms, creating and modifying large programs would be program, it's
nearly impossible. easy to find
yourself with a
single chunk of
code that's
Different programming languages use different terms when talking about subprograms. pages long.
Some commonly used terms are subroutine, procedure, method, and function. You might think
you can
remember what
For example, suppose you have a chess program that draws the game board onscreen. It all the bits in
would be tedious for you to write the instructions to draw 64 individual squares with or that great
without the chess piece images. Instead, you can create a subprogram to draw a square at a monolithic block
programmable location with a programmable image, and then add instructions to repeat the of code do, but
subprogram as many times as necessary. you'll find it
easier to
understand and
maintain if you
Whenever your program has to repeat certain steps, you have a candidate for a
use
subprogram.
subprograms. If
your code gets
longer than a
In all but the simplest cases, organizing a program into subprograms results in code that's page or so, look
easier to understand and to maintain. It's also easier to improve your code. For example, if for opportunities
you discover a faster way to draw images, you only have to change the subprogram in one to break it up
place rather than changing the same code multiple times throughout your program. into
subprograms.
When you write the code to run a subprogram, you call the subprogram, or invoke it.
Programming languages make a distinction between functions, which return a value to the
calling program, and subroutines or procedures, which don't.

Most programming languages have bundled or included subprograms or functions that


enable you to do certain kinds of work, such as count the number of characters in a
string, open files, and so on.

Defining a function

For this example function, suppose you have a program that frequently needs to calculate
the average of three numbers. You can write a function that averages the three numbers,
and, in most languages, you would then call this function with code similar to:

avg = average3(var1, var2, var3)

This tells the operating system to execute the instructions in the average3 function with the
values in the variables var1, var2, and var3 as input, and then store the resulting value in the
variable called avg.

Now you need to tell the operating system what the average3 function is. You do that by
defining the function. In the function definition, you tell the operating system which type of
value the function is going to return, what information you have to give the function (in this
case, the three numbers), and what the function should do.

In pseudocode, this average3 function definition looks similar to the following:

real function average3(real number1, real number2,


real number3)
average3 = (number1 + number2 + number3) / 3
end function
This pseudocode defines the function by stating which type of value it returns and what types
of values it needs to be given when you call it. In this case, it tells the computer to define a
function called average3 that requires three real values as input and returns a real value as
output. The function adds the three numbers together, divides by 3, and assigns the result to
a variable that has the same name as the function.

This convention of designating the value returned with a variable that has the function's
name is used in BASIC and related languages. In other languages such as Java, a return
command is used in code that looks similar to the following:

return (number1 + number2 + number3) / 3 ;

The values (variables) that you give any type of subprogram when you call it have a special
name called parameters. In the previous example, number1, number2, and number3 are the
parameters.

Defining subroutines

Subroutines are called similar to functions; however, your program doesn't have to send a
return value as output. Subroutines are often used to perform operations such as writing text
to the screen or to a file.

The following is an example of a subroutine that calculates an age and prints it:

subroutine show_age(integer current_year,


integer year_born)
integer new_age;
new_age = current_year - year_born
print "Your age is:" new_age
end subroutine show_age

You can call this subroutine and provide it with the current year and the year in which
someone was born. It does the subtraction and prints the value to the screen. Note that the
variable new_age that's defined and used inside the subroutine can only be used inside
show_age. It's called a local variable.

Some languages make no distinction between subroutines and functions. In JavaScript,


for example, you can define a function as having a returned value or having no returned
value.

Subprograms with complex variables

Recall from Lesson 2 that you can have a variable that contains other variables. Arrays are a
common example used with subprograms, both as input parameters and as returned values.
For example, you might write a subroutine to find the largest number in an array of numbers,
or the longest string in an array of strings. The definition of the subroutine would state that it
takes the name of an array variable as a parameter.

Calling functions and subroutines

After you write a subprogram, you need to call (or run) it from within your larger program.
Most programming languages use syntax similar to the following for calling functions. To call
the average3 function you wrote earlier, you'd write an instruction such as:

the_avg = average3(31.5, 29.8, 22.7)


In this example, you provided the three numbers to be averaged as literal numbers in the call
statement. Alternatively, you could provide variables by name or a mix of variables and literal
numbers. For example, to call the show_age subroutine, you could write something similar
to:

call show_age(this_year, 1972)

Some programming languages require the use of a special instruction (such as call) to run a
subroutine; other languages let you state the name of the subroutine as an instruction.
Fortran is an example of a language that requires a call statement; C/C++ doesn't.

You can embed a call to a function or subroutine in another function or subroutine. In fact,
this happens all the time, even in simple programs. Putting together complex computation by
combining simpler parts in the form of subprograms is essential to programming.

Now that you've learned the basics of building and calling a function or subroutine, you need
to know how to properly document them. That's covered in the next section.

A comment about comments


This is a good time to talk about a good programming practice -- commenting your code. All
programming languages provide a syntax for putting notes in your program that the computer
just skips over. This is usually done by a special character sequence. In this example using
the C convention, the computer ignores all text after the double slash:

// in C any text following // is a comment

Other languages, like Perl and Unix shell, use the hash character for a comment:

# a comment in Perl or Unix shell

And Visual Basic uses an apostrophe:

` a comment in Visual Basic

In some languages, if you have a lot of comments that need to stay together, you can place
them between special character sequences:

/*
In many languages, you can place comments
between special character sequences
*/

One of the marks of a good programmer is providing clear comments that help future users
of your code understand its intent.

At this point, you know how to create and call a subroutine or function and properly
document your program. In the next section, you'll learn another important concept: how to
get data in and out of your subprograms.

 
Using input and output
 
Previous lessons have glossed over something important -- how to get information into a
program, and how to get the results out. This area of programming is called IO. In the
programming example in which a person's age is calculated, a simple statement called print
was included to tell the computer to print text to the screen.

As with any statement, different languages have a different name for the print statement,
so don't be surprised when you see another name for it later.

There's an obvious counterpart to the print statement, which is the input statement (again,
known by other names in other languages). You use this statement to tell the computer to
get some data from the user. For example, to ask the user's age, you might use this syntax:

print "How old are you?"


input (age)

In this case, you tell the operating system to print the message, "How old are you?" and then
read in something the user types, which you store in a variable called age. The convention
for an input command is to accept characters from the keyboard until the Enter key is
pressed, at which point all of the accumulated characters are interpreted as the age
variable.

To see subprograms, input, and output at work, let's write a simple program in pseudocode
that uses the average3 function along with some user input and output. It asks the user for
three numbers, calculates the average, and then prints it:

real function average3(real number1, real number2,


real number3)
average3 = (number1 + number2 + number3) / 3
end function
program do_average
print "This program will ask for three numbers,
and calculate the average."
print "Enter the first number:"
input (number1)
print "Enter the second number:"
input (number2)
print "Enter the third number:"
input (number3)
the_avg = average3(number1, number2, number3)
print "The average is :", the_avg
end program

In a real programming language, you would have to include statements to declare your
variables. They're omitted here for simplicity's sake.

When you run the program, it prints the first messages asking you for a number. When you
type in a number, it stores that number as number1. It then asks in sequence for two more
numbers, and stores them as number2 and number3.

The program then calls the function average3, passing it the parameters stored in number1,
number2, and number3. Because average3 is a function, it returns a value that you store in
another variable called the_avg. You could state the effect of the function call as, "set the
variable the_avg equal to the value the function average3 returns when given number1,
number2, and number3". Finally, the program prints the value of the variable the_avg and
ends.

Exploring other kinds of input and output


Naturally there are other kinds of input and output besides printing to the screen and
accepting keyboard input. For example, you can write a program that reads a mailing list
from a file and prints holiday card envelopes on a printer; however, all forms of input and
output have universal requirements. Your program must describe output in terms of where
it's going and exactly what data is to be output. Code describing input must state where it's
coming from and the variables where the input is to be stored.

Twenty years ago, a discussion of input and output would have stopped here. But modern
programmers have to deal with GUIs (graphical user interfaces) as found in Web browsers
and the Windows, Mac, and Linux operating systems. You'll learn about GUIs in the next
section.

Discovering graphical user interfaces and input events


As you sit at your computer screen using the graphical interface of a Web browser, your
computer might have many processes going on. In addition to keeping track of your input by
keypress or mouse movements, your computer might be sending or receiving data over a
network and running other programs.

As a programmer, you don't have to worry about these details because the operating system
simplifies everything by means of a concept called event oriented programming. The
operating system interprets the hardware actions of moving a mouse or pressing a key as
an event that's passed to your program. For example, if your program has executed the
following statements:

print "Enter the first number:"


input (number1)

The operating system hands your program events that contain the characters that are typed
on the keyboard, and your program interprets these events according to the input statement
rules.

Moving a mouse around generates a huge number of events; every time the mouse pointer
points to a new screen location, it might be significant to the program that controls that part
of the screen. You've probably seen this in action on Web pages where a slight movement of
the mouse causes an image to change.

The following are some examples of mouse events your program might receive. In every
case, the event contains information on exactly where it happened:

Mouse has entered your working area


Mouse has moved with no button pressed
Mouse has moved with a button pressed
Mouse has left your working area

A familiar example of a mouse event

Imagine you're browsing a Web page. You move your mouse over an image and it changes.
What happened is the Web browser got a mouse event and called a function written in a
scripting language. In addition to controlling the way a Web page looks, the conventions of
HTML allow for attaching a JavaScript function to the page so that it can respond to events.

If you haven't worked much with HTML, don't worry about the following examples. HTML
is secondary to the discussion about events. Lesson 5 includes an introduction to HTML
and other markup languages.

You can define properties for certain HTML tags, such as the tags that include images in a
page, to provide instructions to the browser on what to do when the mouse is moved over or
away from that image.

Here you have a basic HTML tag for adding an image to a page. It states that the image is to
be taken from a particular file:

<img src="picture.jpg">

You can add extra information to this tag, giving the image a name and attaching calls to
functions written in JavaScript, resulting in something similar to the following:

<img src="picture.jpg" name="SwapImage"


onMouseOver="functionA(SwapImage)"
onMouseOut="functionB(SwapImage)">

You provide a name for this image (SwapImage), and then give a function call for two
events: onMouseOver and onMouseOut. The first event occurs when your mouse pointer
moves into the image area; the second occurs when you move the mouse out again. When
the event occurs, the corresponding function is called.

Now that you're familiar with essential subprogram topics, it's time to pull all the pieces
together. The next section explores a fun programming example.

Creating a fun example


 
Now for an example that's not only fun but also demonstrates many of the programming
concepts you've learned in the last three lessons. The programming language is JavaScript
embedded in a Web page. When JavaScript appears in a Web page, there's no main
program; the real main program is the browser, and JavaScript provides added functions that
are called by the browser when events occur.

In the following example, the HTML tags create a page that has a title, a line of text, and an
input form. After presenting the entire code, each part is discussed in detail. If you want to
copy the following and play with it, save it as plain text (not a word processor format) in a file
named Example.html.

<html>
<head><title>Example with Input and Events</title>
<script language="javascript"
type="text/javascript">
function doReverse() {
var s = ctrl.msg.value ; // get input
ctrl.msg.value = "" ; // erase input field
for( i = s.length - 1 ; i >= 0 ; i-- ){
ctrl.msg.value += s.charAt( i );
}
}
</script>
</head>
<body>
<p>Type something then press Reverse
<form name="ctrl">
<input name="msg" type="text"
size="40" value="" />
<input name="doit" type="button"
onclick="doReverse();" value="Reverse" />
</form>
</p>
</body>
</html>

The first two lines tell the browser how to show the title of the page. The tag that starts
<script tells the browser that one or more JavaScript functions are going to be defined in the
text that ends with </script> so it doesn't try to display that text. The text in the area between
the <body> tag and the </body> tag is what the browser uses to create the page display.

The page has a short bit of text followed by a form, where the form consists of a single text
input field and a button. Figure 3-1 shows the display with some text typed into the form.

Figure 3-1: The Web page with text typed into the form.

Note that the form is named ctrl, the text input field is named msg, and the button is named
doit. These names identify those tags as variables as far as JavaScript is concerned. In the
tag that creates the button, the code that says:

onclick="doReverse();"

tells the browser to execute the doReverse function when it gets a mouse click on the button.
When you type into the text field named msg, the keypress events sent by the operating
system to the browser are interpreted as characters and are added to the field and
displayed.

How the doReverse function works

The definition of the doReverse function is enclosed in curly braces, following the C
convention. The next lesson discusses the conventions found in C family languages. For
now, however, concentrate on how this function works.

The first line of the function is:

var s = ctrl.msg.value ; // get input

which illustrates a number of things you've already learned. The form named ctrl is an
example of a complex variable made up of simpler parts. To get the text from the text input
box, you have to address the individual parts using the names established in the form. The
following shows how you could describe the way this line of code works:

1. Get the variable named ctrl from the page.


2. Get the variable named msg from ctrl.
3. Get the contents of the msg variable by looking inside of value.
4. Put the contents of that variable in the local variable named s.

JavaScript doesn't require you to declare the variable s as being of any particular type.
Instead, it automatically adapts the variable to the type of value being stored in it. This is
an example of weak typing, which you learned about in Lesson 2.

Next there's a statement that puts an empty string in value. Following that, you have the
code that puts the characters back into the form in reverse order. Remember what you
learned in Lesson 2 about arrays having the first item at position zero? Well, strings in
JavaScript are the same way. To get the last character in the string, you have to use an
index equal to the length of the string minus one. The statement that controls the for loop
states:

for( i = s.length - 1 ; i >= 0 ; i-- ){

In plain language you would say, "starting with an index that points to the last character in
the string s, subtract one every time through the loop, as long as the index is zero or
positive."

The next statement:

ctrl.msg.value += s.charAt( i );

says, "add the character in the i'th position in the string and to those already in the value
variable. On every cycle, the for loop uses the next smaller index to work backwards through
the string." The browser displays the changed content of the variable, and you end up with
the text field filled with the reversed string, as shown in Figure 3-2.

Figure 3-2: The Web page as it appears after clicking the Reverse button.

See how to create the JavaScript example and run it. (1.5 MB)

Although this example is simple, it illustrates an important programming concept you'll use
repeatedly in more complex programs.

Moving on

In this lesson, you were introduced to the important programming concept of subprograms
and how they're used. You saw some common uses of input and output programming
commands, and you got a quick introduction to how modern GUI programming uses events
for user input in JavaScript. Lesson 4 looks at some more traditional programming
languages, and shows you what it takes to create a complete program in one of those
languages. Before you move on, complete the assignments and quiz for this lesson.

Assignment #5
For this assignment, you'll add comments to the following sample function used in Lesson 3:

real function average3(real number1, real number2,


real number3)
average3 = (number1 + number2 + number3) / 3
end function

Add your name, today's date, and a brief description of the function, and show the comment codes for each of these
programming languages:

C
Perl or Unix shell
Visual Basic
Solution

The following are possible solutions:

C:

// <your name>, <date>, function averages three real values and assigns the result to the average3 variable

Perl or Unix shell:

# <your name>, <date>, function averages three real values and assigns the result to the average3 variable

Visual Basic:

` <your name>, <date>, function averages three real values and assigns the result to the average3 variable

Quiz #3
Question 1:
Which of the following are alternate names for subprograms, depending on the language? (Check all that
apply.)
A) Function
B) Subroutine
C) Method
D) Call
 
Question 2:
Which of the following describe how to invoke a function? (Check all that apply.)
A) Call it from a larger program.
B) Use an event on a Web page to trigger it.
C) Call it from another subprogram.
D) Put it in a comment.
 
Question 3:
Which of the following can generate a user input event to a program? (Check all that apply.)
A) Press a key on the keyboard.
B) Display a typed letter on the screen.
C) Click the mouse on a Web page form.
D) Move the mouse out of the program display area.
 
Question 4:
True or False: A Web page display is static and can't be changed by user input.
A) True
B) False
 
Question 5:
Which of the following character sequences denote comments in a program, depending on the language?
(Check all that apply.)
A) //
B) #
C) /* … */
D) `
 

C, C++, C# and Java computer programming languages


Some of the most popular full-featured programming languages today are C, C++, C# and Java. In this lesson,
you'll learn a brief history of these languages and how to structure simple programs using them.
 
Introducing the C programming language
 
Welcome back. In previous lessons, you were introduced to the basic structures of
programming, such as conditional statements, variables, input/output, subroutines, and
more. Now you're going to look at a group of languages that have been highly important
in the history of computing and are the dominant languages in much of computing
today.

For simplicity, these are referred to as the C family of languages, because they have the
C language in their background. The language called C was developed in the 1970s as
a powerful and portable language usable at both the systems and applications level.
Systems-level programming deals with the nitty-gritty details of hardware and operating
systems, whereas applications-level programs run on top of operating systems and deal
with user tasks.

The languages in this family share the following characteristics:

The core of the language is compact and depends on libraries to achieve


functionality. As you might recall from Lesson 1, a library is a collection of programs
and subroutines, and programs must specifically declare (specify or define) the
libraries to be used.
Variables and functions must be declared with type information, and the language
(usually) pays attention to the declared type in all subsequent uses.
There's a simple way to create complex variables that represent a collection of
simpler variables.
Programs are built of short statements written in plain text using the equal sign (=) to
assign values to variables and expressions that look similar to standard algebra.
These statements are included in modules that you can compile separately.

The history of the C language

The C language was developed with the original Unix operating system and still has
some elements (such as terse abbreviations for commands) in common with it. Because
of C's power and flexibility, it's become one of the most widely used computer
languages, and compilers are available for a large number of different systems.
However, because C programs are usually platform-specific, moving a program written
for a Mac, for example, to a PC might take a bit of work and requires the use of a
platform-specific compiler.

When talking about computer languages, platform generally refers to a particular


combination of hardware and operating system. You might see a product for the
Windows platform, the Mac platform, or the Linux x86 platform. This distinction breaks
down a bit in terms of Java, because it runs on so many combinations of hardware and
operating system.

The C language has been standardized, in terms of basic syntax and standard libraries.
The Free Software Foundation offers C compilers for a large number of operating
systems.

The power and flexibility of C come at a price, however. Complex C programs can be
difficult to debug; there are a number of errors commonly made by novice (and even
experienced) programmers that can be hard to find. In addition, the sheer complexity of
many C programs makes them susceptible to viruses and hacker attacks -- it's fairly
easy to add and hide virus code in a large and poorly structured C program. Basically,
it's easy to shoot yourself in the foot with C.

History of the C++ language

C++ (pronounced "C plus plus") builds on the C language as the name indicates.
Because C++ contains all of C, C++ compilers compile strictly C programs without
difficulty; however, the opposite isn't true.
Bjarne Stroustrup, working at Bell labs, started development of C++ in the early
1980s as described in a short history.

C++ popularized the idea of OOP (object-oriented programming), a class of


programming languages in which the variables and functions used by the programmer
are packaged in objects. Thinking of programs this way helps to control the complexity
of very large programming projects. As OOP is a vital part of programming these days,
you'll see that term frequently.

Unfortunately, C++ also inherits from C the many possibilities for hard to find bugs. For
example, it's possible to write a program that writes data all over itself and crashes.
When you see news reports of newly found security problems in some operating
system, the root cause might well be this sort of problem, known as a buffer overflow.

One of the great strengths of C and C++ is the enormous number of libraries of already
debugged code available for your free reuse. In addition to the free open source
libraries, there are also commercial libraries you can license.

Similar to C, the syntax and standard libraries of C++ are under the control of a
standards committee of ANSI (American National Standards Institute). When you see
references to ANSI C++, it means an implementation that adheres to the standard.

History of the Java language

Researchers at Sun Microsystems were doing "blue sky" research in the early 1990s.
They felt strongly that the future of computing would depend heavily on interconnected
(networked) computers and small computing objects communicating with each other. At
that time, there were no languages that made communication over networks easy and
portable.

In speculating on what kind of language would be required, they liked the syntax of C
and the idea of OOP, but decided that many conventions in C++ would have to be
removed or revised to make a more bug-resistant language. In particular, they wanted to
make memory management, multithreading (the ability to run multiple tasks
simultaneously), and network connectivity easier. This is where Java comes into play.
Java incorporates many security features that make fatal bugs less likely.

Java achieved more rapid adoption than any other language up to that time because it
was perfectly positioned to catch the rising interest in the Internet, and it was designed
with network communication in mind. Sun has always made Java implementations
available for free, and this policy has stimulated a huge community of programmers to
create their own freely available resources.

The major technical advance made by the Java team is the concept of platform
independence. Java code executes within the JVM (Java Virtual Machine). When you
compile a Java program, it can run on the JVM under any operating system without
recompilation. So, if you have the JVM installed on a Windows system, for example, you
can use it to execute Java programs compiled on a Macintosh and vice versa.

The Java language specification was developed by Sun Microsystems, and use of the
name Java is still controlled by Sun. However, Sun has involved many individuals and
industry partners in the subsequent development of the language and standardization
proceeds through an organization called the Java Community Process rather than
ANSI.

History of the C# language


A relative newcomer to the game is Microsoft's C# (pronounced "C sharp"). First
released as part of the Microsoft.NET initiative in 2000, C# is an OO (object-oriented)
programming language that starts with the basic building blocks of C++ and adds some
important features. Some of the major differences between C++ and C# are:

Like Java, C# uses a platform-independent compiled code format that may be


transferred from system to system.
C# includes a native Boolean (binary) data type, whereas C++ requires you to use an
integer for this purpose.
C# doesn't use header files to include code like C and C++.

The most common way to use C# is by purchasing a copy of the Microsoft Visual C#
package. However, you can also use a trial version of Microsoft Visual Studio (or the
always free Microsoft Visual Studio Express) to write C# code. Alternatively, if you're
willing to get your hands dirty, you can download and install the open source DotGNU
package that includes a C# compiler.

Now that you're familiar with the history of C, C++, Java, and C#, let's move on to
building a real program in C.

Exploring the contents of a C program


Here's the world's simplest C program that actually performs an action: What does I18N
stand for?

#include <stdio.h>
In discussions about
int main()
{ commercial
printf("Hello, world!\n"); computing
return 0; applications, you
} might run into the
acronym I18N. This
stands for
This code illustrates many important elements about the C family of languages. The
Internationalization,
following section looks at the C program in detail.
(the letter I, followed
by 18 letters,
terminated with N),
Programs that do nothing more than display the words "Hello, world" are famous or the problem of
exercises for learning any language. It, or something similar to it, is usually the first making sure your
task most programmers try to do with a new language they learn. application can
handle the character
sets and language
Using a library conventions of
languages in use all
over the world. If
Libraries are essential to getting anything done in C. At the start of a C program, you you want to dig
have to list the libraries you want to use. The line #include <stdio.h> tells the compiler deeper into this
that you're going to use the standard IO library. Libraries in C and C++ are organized by problem, start at the
means of header files that are usually named by some sort of acronym for what they do. UNICODE
In this case, stdio stands for STandarD Input/Output. By long-standing convention, organization that
header file names end in .h -- therefore, stdio.h. was created to
standardize
character sets.
A header file contains information on how the various functions in a library are
named and the parameters they take. Header files are separate from the actual
library code they describe, so the programmer must be careful to ensure that the
header files match the library that's being used.

Program conventions

By convention, C programs are written with a function named main that's executed when
the program runs. Also by convention, this function must return an integer value used to
indicate whether any errors are encountered. C uses int to indicate an integer number.
The opening curly brace { starts the content of the main function, and the closing curly
brace } shows the end of the function. The C family of languages uses pairs of curly
braces to enclose logical blocks of code statements, just as you saw with the JavaScript
function in Lesson 3.

Also by convention, each complete code statement is terminated by a semicolon. The


statement printf("Hello, world!\n"); is a call to the printf function in the stdio library with a
string constant. The return 0; statement declares that the value returned by the main
function is zero, which is the convention for not having any error.

C variable types

In contrast with typical scripting languages, C family programming languages expect


variables to be declared with a type that indicates the kind of data the variable holds.
Having this knowledge in advance lets the compiler create more efficient code.

Although Java has exact definitions about variables, some important points about
type declaration in C and C++ vary between platforms.

One reason for the difference is that C has been used for many different sized
computers and never got standardized in areas related to computer hardware
limitations. Table 4-1 summarizes the points that cause the most trouble:

Type Abbreviation Size in C/C++ Size in Java


character char 8 bit or 16 bit Always 16 bit
integer int 16 bit or 32 bit Always 32 bit

Table 4-1: Some differences in variable types among C family languages.

Characters in Java always use 16 bits because Java attempts to provide for the
character sets used in writing a large fraction of the languages in use in the world today.
When C got started, practically all programming was done with the small character set
used in English. Now programmers have to worry about I18N, so more bits are needed
to represent more different characters.

C arithmetic expressions

If you remember your algebra, C family arithmetic expressions should look familiar. C
uses parentheses to enforce the order of evaluation, as in the following:

a = ( b + c ) * ( d + e ) ;

In words, that statement says multiply the sum of b and c and by the sum of d and e,
storing the result in a. C also assigns relative precedence to arithmetic operators so that
if an expression is ambiguous, such as a + b * c, the multiplication is performed before
the addition.

C also has some convenience notations that won't be familiar from algebra. For
example, you can attach the symbols ++ or -- to a variable, such as the following:

a = ++b ; // called pre-increment


a = b++ ; // called post-increment

In the first statement, the computer adds 1 to the current b variable; then puts that
incremented value in the a variable. In the second statement, the current value of b is
taken and assigned to a; then the computer adds 1 to b. There's similar notation, called
pre- and post-decrement, which, as its name indicates, subtracts values.

You'll frequently see the post-increment notation used in conjunction with arrays to
automatically increment the array index. For example, suppose you want to fill an array
called buffer with the successive values returned from a function called readData, and
that readData is supposed to return a value of -1 to signal that the last data has been
read. The following code looks plausible; the index n starts at zero and is incremented
with every cycle:

int n = 0 ;
while( (ch = readData() != -1 ) {
buffer[ n++ ] = ch ;
}

That would work as long as the buffer array was created large enough to hold the data.
However, you've made no provision for detecting that n has reached the end of the
array. In C, what happens then is that the data gets stuck in the middle of whatever
happens to follow the array in memory—a buffer overflow. It's the cause of many
mysterious bugs in C and C++ programs and is exploited by virus writers.

Getting a C/C++ programming environment set up and compiling even simple programs
is beyond the scope of this course. Two widely recommended books for learning the
basics of C or C++ are Ivor Horton's Beginning C++ by Ivan Horton and Schaum's
Outline of Programming with C++ by John R. Hubbard.

Now that you have a C programming example under your belt, it's time to get a taste for
another language. The next section takes a look at a simple Java programming
example.

Dissecting a simple Java program


An important driving force for the adoption of OO languages has been the rise of GUIs
starting in the 1980s. To write the C code to handle even the simplest window is
complicated. Just as the use of subprograms enabled early C programmers to start with
a library of reliable functions, using the correct OO language lets you start with some
complicated capabilities, all reliably debugged and documented.

In Java, you write your entire program as a logical unit called a class. All of the code for
a class is contained in a single text file and is compiled by the Java compiler into a
single file of computer instructions. To create a running program, the Java runtime
environment creates one or more objects using the compiled code as a template.

Here's the complete Hello World program in Java:

import java.awt.*;
public class MyFrame extends Frame {
public static void main(String[] args){
MyFrame mf = new MyFrame("MyFrame");
mf.show();
}
public MyFrame(String title ){
super( title );
setSize( 200, 120 );
add( new Label("Hello World", Label.CENTER ));
}
}

Similar to the other C family languages, the code starts with an import line that tells the
computer you're going to use a library called java.awt. The final * indicates you may use
any class in that library. AWT stands for Abstract Windowing Toolkit or, if you're having
a hard time with it, Annoying Windowing Toolkit.
Next you get to the most important line -- the class declaration. This says that your
program is going to be in a class named MyFrame and that it's going to extend a class
named Frame. The Frame class comes from the java.awt library and is the typical
starting point for Java programs that need a GUI window.

Extending a class means to create a new class that inherits (gets by default) the
attributes and methods of the class being extended. Inheritance provides one of the
most significant benefits of OOP -- the ability to easily reuse code.

Because MyFrame extends Frame, your class is able to use all of the cool capabilities
that Frame provides, just like that. Figure 4-1 shows a screen shot of running the
MyFrame program in Windows.

Figure 4-1: The window created by a MyFrame object.

You can drag that window around the screen, resize it, or minimize it because MyFrame
inherits all of those capabilities from Frame.

Okay, back to the code. Look at the next chunk of code in MyFrame, the main function:

public static void main(String[] args){


MyFrame mf = new MyFrame("MyFrame");
mf.show();
}

Similar to C, the convention is that programs start by executing a function named main.
If the Java runtime can't find a main function, it can't run a program. The other parts of
that main function declaration are glossed over here to avoid getting bogged down in
detail. You'll come back to this in later lessons.

The second line creates a variable of the MyFrame type that's named mf and says to
create a new MyFrame object to store in mf. The keyword new that appears in this line
is unique to OO languages. It says to use the MyFrame class as a template to create a
new object. At this point the object has been created, but not yet shown onscreen.

The line mf.show(); says to call the show function that belongs to the MyFrame class.
You don't see a show function defined in the MyFrame class because you inherit that
function from Frame, which inherits from another class called Window. Your MyFrame
class can use functions and variables from a complete hierarchy of classes, all the way
back to the most primitive Java class called Object.

The MyFrame constructor

The remaining code in MyFrame is a constructor, which is a special method for


initializing a new instance of a class. It has one parameter -- the string representing the
title that you want the graphic window to display:

public MyFrame(String title ){


super( title );
setSize( 200, 120 );
add( new Label("Hello World", Label.CENTER ));
}

Although it looks similar to a function declaration, a constructor is in a special category.


Note that it doesn't declare a returned variable. Constructors always get invoked by the
new keyword and carry out a special set of instructions. All of the classes from which
MyFrame inherits, all the way back up the hierarchy to Object, have to be called to build
a MyFrame object.

This is accomplished by the super( title ); line that calls a constructor in the Frame class.
In particular, it calls a version of the Frame constructor that takes a title string
parameter. After that, the line setSize( 200, 120 ); calls a function (inherited from Frame)
to set the size in pixels of the space MyFrame takes up onscreen.

You don't inherit any method from Frame that shows a message because a Frame is
intended to hold other GUI components that do handle messages. To get the words
"Hello World" displayed, you create a Label object and then add it to the Frame. The
Label class is part of the java.awt library you imported. Using the new keyword, you call
the Label constructor with a message and a special constant that tells the new Label to
show the message centered in the area it occupies in the Frame.

That's all it takes to create a custom version of a GUI window display. Of course, it
doesn't do anything but show up on the desktop display, but you can add functionality by
defining more functions in the MyFrame code. In the next section, you'll look at ways
you can extend this example to perform more complex functions.

Enhancing the Java example


 
By extending Frame, the MyFrame code gets access to a bunch of interesting variables
and functions related to its appearance and behavior. Here's a short list:

Background color
Foreground (text) color
Screen position and size
Default font used by the label
Whether or not the user can resize the window
How the mouse cursor appears when it's over the window
The icon that shows up on the tool bar when the window is minimized

For example, you can modify the background and text color by modifying the code
example from the previous page as follows:

import java.awt.*;
public class MyFrame extends Frame {
public static void main(String[] args){
MyFrame mf = new MyFrame("MyFrame");
mf.show();
}
public MyFrame(String title ){
super( title );
setSize( 200, 120 );
setBackground(Color.yellow);
setForeground(Color.blue);
add( new Label("Hello World", Label.CENTER ));
}
}

This produces the window shown in Figure 4-2.


Figure 4-2: The modified MyFrame window.

You could build a user interface by adding GUI elements created from classes in the
java.awt library. Here are some of the elements you could create:

Labeled push-buttons
Check boxes or radio buttons
Scrollbars
Text entry boxes or areas
Drop-down selection lists
Images
A menu bar with drop-down menus

And there's more, much more. GUI programming is a complex and interesting endeavor,
and is a major part of the rich, visual nature of most high-end user-friendly programs.

Moving on

This lesson introduced you to an important group of programming languages that got
started with C. After a quick look at a program in the most primitive of this group, you
made a start on the features of OOP with an emphasis on programs for graphical user
interfaces. In Lesson 5, you'll leave the topic of OOP to look at the markup languages
HTML and XML. Before you move on though, complete the assignment and quiz for this
lesson.

Assignment #6
Programmers who use C, C++, C#, or Java can draw on both free and commercial libraries that
cover a huge number of topics. Browse the open source code offerings and programming tutorials
offered at SourceForge, the Apache Software Foundation, and/or Google Code, or find your own
favorite resources.

Add the Web sites you find most useful to a "Programming Resources" folder in your favorites or
bookmarks list in your Web browser.

Challenge exercise

Try building a program in the language of your choice following one of the tutorials or using open
source code as a base.

Quiz #4
Question 1:
True or False: C was developed on the Windows operating system and still has some things in common with it.
A) True
B) False
 
Question 2:
True or False: There are aspects of C++ that are in C but not the other way around.
A) True
B) False
 
Question 3:
Which of the following programming languages compile into platform-independent code? (Check all that apply.)
A) Java
B) C++
C) C
D) C#
 
Question 4:
What's the name of the logical unit that comprises an entire Java program?
A) Function
B) Routine
C) Class
D) Constructor
 
Question 5:
True or False: To extend a class means to create a new class that inherits the attributes and methods of the
class being extended.
A) True
B) False
 

Beginning programming for the web


A majority of programs written today are designed to work on the web. In this lesson, you'll learn about web
programming technologies and languages, such as HTML, PHP and JSP. You'll also learn how XML exchanges
data among different systems.
 

Understanding markup languages


In Lesson 4 you learned about some of the most popular and widely used programming Who
languages today, such as C++, C#, and Java. This lesson focuses on markup languages, invented
which are important to Web technologies. hypertext?

You use a markup language to add extra information to a text document to control the The use of the
interpretation of the data it contains. A special character set, such as < and >, separates the HTML for
markup information from the document data. Interpretation of this extra information naturally Web pages
requires programs specifically adapted to the particular markup language. Much programming was an
effort is devoted to creating or interpreting marked up documents, so this lesson takes a detour important
into markup country before continuing with programming languages. development
that made the
World Wide
Markup languages control interpretation of data. You're probably familiar with the way HTML Web feasible
controls the appearance of a marked up Web page and the possible hypertext link interactions and incredibly
a user can have with the page. The other highly significant markup language in modern useful.
computing is XML, which is important in the interpretation, transmission, and storage of all However, the
sorts of data. It's extremely likely that you'll run into XML marked up documents more and idea of
more. hypertext was
actually
invented
years before
Hypertext is a string of characters or a graphic that's linked to other text or a location on a
the Web by
page. In Web programming, a hyperlink is an example of hypertext. You can use hypertext
the visionary
to move from one section of a page to another or from one Web page to another.
Ted Nelson,
whose
approach is
Just a little history considerably
more
sophisticated
Throughout programming history, many programmers tried to create platform-independent
than what
markup languages designed to control the exact appearance of printed text in electronic form.
HTML
A famous example comes from the programming field, where computer scientist Donald Knuth
provides.
got annoyed with the poor quality of the typesetting of mathematical expressions in the first
volumes of his monumental series of books on programming algorithms. He set about to
create a typesetting markup language to improve the situation. The language he invented,
called TeX, is widely used in publishing today.

The direct forerunner of HTML and XML was SGML (Standard Generalized Markup
Language). GML, which evolved into SGML, was invented at IBM to make the creation of
complex program documentation easier. SGML is widely used in industries that have large
technical documentation problems, such as aircraft manufacture. It enables you to keep one
master document that can be processed (by various programs) in a number of ways to meet a
number of different needs. There's that tie-in between markup languages and programming
again.

SGML is a highly complex language, and the programs you use to display and edit SGML
documents are very large. The basic idea, however, served as an inspiration for HTML, the
markup language of the Web.

Now that you're familiar with the history of markup languages, let's move on to the most
common markup language in use today: HTML.

Working with HTML tags and elements


 
You can use a simple HTML page to get a grasp of several important principles:

<html>
<head>
<title>A really simple page</title>
</head>
<body>
<p>This is the body text in a paragraph.</p>
<!-- this is a comment -->
</body>
</html>

The parts enclosed in the < and > pairs are HTML tags, which tell a Web browser how to treat
the text between the tags. The preceding example includes extra spaces that indent lines of
code to better illustrate how lines are enclosed by pairs of tags; however, indenting isn't an
HTML requirement.

HTML tags can be uppercase or lowercase to be interpreted correctly by Web browsers.


However, future versions of HTML that are closer to the XML standard will have to keep
tags in lowercase, so why not start using lowercase now?

The entire document is enclosed in the <html> . . . </html> tag pair. The first is called the
openingtag and the second is the closingtag. The <html> . . . </html> tag pair forms the root
element of the document. The <head> . . . </head> and <body> . . . </body> tag pairs form
the head and body elements respectively, which are the only elements that can appear directly
inside the root element. The head and body elements are nested, which means contained,
inside the html or root element.

Note the special markup starting with <!-- and ending with --> character sequences. This is a
comment, not an element, and won't be displayed by a browser. Figure 5-1 shows the HTML
Web page coding example displayed in a Web browser.
Figure 5-1: Example of a Web page appearing in Microsoft Internet Explorer.

The HTML specification clearly defines which types of elements can appear inside other
elements. It's this strict control over what can appear where in an HTML document that makes
it possible for Web pages to be displayed in different Web browsers without too much
variation. In contrast, XML, being extensible, doesn't have this sort of standardization. You'll
learn about XML later in the lesson.

Tag pairs and empty tags

When using HTML tag pairs to define elements, any nested tags inside the pair must be
closed before the element closing tag appears. In the example, the <p> . . . </p> tag pair must
be completely inside the <body> . . . </body> tag pair. An HTML document that adheres to this
rule is said to be well formed.

HTML tags can appear in two ways -- paired as in the previous example, and as a singleton
such as the <br> tag that causes a break in the text. Singleton tags are also referred to as
empty tags because they don't enclose anything. Unfortunately, HTML adopts a convention for
empty tags that's contrary to that used in XML.

It'd be advantageous to programmers if both HTML and XML followed the exact same rules for
forming tags, and there's a determined effort to create a version of HTML that follows the XML
rules. The W3C (World Wide Web Consortium) has a standard called XHTML that
accomplishes this, and establishes a clear roadmap for future HTML extensions.

Tag attributes

An opening tag can have additional information called attributes that take the form of
name="value". You already saw these in action in Lesson 3 where an input element in a form
was defined with the following empty tag:

<input name="msg" type="text" size="40" value=""/>

Conventions dictate that the value of an attribute always be enclosed in quotes, although Web
browsers aren't strict about this. Typical uses for attributes are to set color, fonts, and element
locations.

HTML standardization and the DOM

The W3C is the organization responsible for defining the specifications for HTML and creating
many other Web-related standards. Many W3C projects are related to standardizing and
improving the information carried by markup languages. One of the most fascinating projects is
the Semantic Web -- an attempt to make the resources presented on the Web more directly
usable by programs.

Many W3C standards are in a nearly constant state of revision, reflecting the incredibly rapid
rate of innovation in the World Wide Web as thousands of ideas jostle for acceptance.

The DOM (Document Object Model) is a W3C standard defining how scripting languages and
programs can address and modify the various elements of HTML and XML documents.
Now that you're familiar with HTML basics, read on to learn how HTML and scripting
languages work together.

Understanding how HTML and scripting languages work together


 
Here's a portion of the JavaScript used in the Lesson 3 example of responding to an event:

function doReverse() {
var s = ctrl.msg.value ;
ctrl.msg.value = "" ; // erase input
for( i = s.length + 5 ; i >= 0 ; i-- ){
ctrl.msg.value += s.charAt( i );
}
}

The notation that this code uses to address the value attached to the form field named msg in
the form named ctrl is using the DOM. The DOM enables JavaScript to address many
elements and properties of an HTML page.

You might see the term DHTML (Dynamic HTML) applied to pages that use JavaScript to
change the appearance of a Web page on the fly.

You can also use JavaScript to add content to an HTML page as it's being loaded on the client
(called client-side scripting). The following is an example in which the Web browser executes
the script in the <body> element, and the script writes the entire contents of the body. Figure
5-2 shows the results displayed in a Web browser.

<html>
<head><title>Weekly Planner</title>
<script language="javascript">
dayName = new Array ("Sunday","Monday","Tuesday",
"Wednesday","Thursday","Friday","Saturday");
today = new Date ;
</script>
</head>
<body>
<script language="javascript"
type="text/javascript">
document.write("<h1>Today is " +
dayName[ today.getDay() ] + "</h1>" );
if( today.getDay() == 0 || today.getDay() == 6 ){
document.write("<p>Goof off all day.</p>");
} else {
document.write("<p>Look busy.</p>");
}
</script>
</body>
</html>

Figure 5-2: An example of client-side dynamically created Web page content.

Note that in this example, the JavaScript code is in two pieces -- one in the <head> element of
the page, and one in the <body>. The code in the head defines two variables -- an array of day
names, and the today variable. The today variable is interesting because it's an object that
contains complete information about the instant the object was created. A Date object has
functions that can interpret that time instant as various parts of a calendar date.
The code in the body of the page uses the getDay function to get the day of the week and then
uses that to compose the text that gets written to the body of the HTML page. The if statement
checks to see if the day of the week is 0 (Sunday) or 6 (Saturday); the || symbol is what
JavaScript uses for the Boolean logic OR.

In the document.write statements, the document is the way the DOM refers to the object that
contains the entire page, and the function call write says to output this text at the current
writing point of the page. The document object has ways of addressing all parts of the page,
whether they're named or not.

See how to use the JavaScript Weekly Planner script. (1.7 MB)

It's important to distinguish between code that executes in the Web browser or client
environment and that which operates on the Web server. On the client side, a scripting
language is limited to what it can do by Web browser security settings. A program on the
server side doesn't have these limitations.

In the next section, you'll take a look at two server-side scripting languages: PHP and JSP.

Working with JSP and PHP


 
When HTML was first developed, people immediately figured out that it would be cool to
combine the static content of a page with dynamic data created on the fly. As is typical in
technological revolutions, many different approaches were tried. All of these different schemes
were based on some kind of markup language mixing special tags with the HTML tags and
using special processing programs on the Web server.

Essentially, these special tags form a kind of scripting language. Some of these developments
survived; some have been replaced. But you might recognize one or more in this list: server-
side JavaScript, Cold Fusion, PHP, iHTML (inline HTML), ASP, and JSP.

The basic idea of server-side scripting languages is that when the server is called upon to
send a file, it looks at the file type to see if that file gets special processing. Usually, a marked
up file will have plain HTML with special tags that produce output from the scripting language.
The following is a simple example marked up with JSP tags that start with <% and end with
%>:

<html>
<head><title>Dynamic Page Content Example</title>
</head>
<body>
<h1>The time now:
<%@ page language="java"
import="java.util.Date"%>
<%= new Date() %></h1>
<hr/>
</body>
</html>

When a JSP-capable Web server sees a request for that page, it actually creates a small Java
program that outputs the HTML and adds in the current date, resulting in the Web page shown
in Figure 5-3.

Figure 5-3: Server-side dynamic content from JSP.

PHP is another common server-side scripting language. Instead of being based on Java (as is
the case with JSP), it's based upon the popular Perl scripting language used commonly on
Unix systems for text processing. Here's an example of a PHP page that prints a random
number between 1 and 100 in the user's Web browser:

<html>
<head><title>Random Number Generator</title>
</head>
<body>
<h2>
<?
srand(time());
$random_number = (rand()%100)+1;
print("Your randomly assigned number is
$random_number");
?>
</h2>
</body>
</html>

Notice that the PHP code is separated from the rest of the page by the <? and ?> tags, similar
to the <% and %> tags in JSP.

Scripting languages using specialized markup on the server side are an essential part of the
modern Web. With simple markup tags, you can access databases or draw on resources
distributed all over the Internet to create a custom page for your customer.

Now that you've explored PHP and JSP essentials, you're ready to dive into XML basics. That
topic is covered next.

Learning XML basics


 
Similar to HTML, XML is a markup language that follows a standard defined by the W3C. The
difference is that the standard only defines the syntax conventions, and a few basic rules. You
can invent your own tag vocabulary to apply to your own particular problem. As long as you
follow the rules, your XML formatted documents can be processed with standard toolkits that
are available in many languages.

The XML standard was derived by simplifying SGML and eliminating much complexity and
inconsistencies that make working with SGML a full-time job. The W3C sponsored the XML
development effort because they realized the true potential of the Web for communication
could only be reached if people had a flexible way to describe just about any kind of data. The
use of XML, however, has spread well beyond the Internet and can now be found in many
computing applications, such as the Microsoft Office 2007 suite.

XML has the same requirement for the nesting of tag pairs that HTML does. An XML
document that has tags nested correctly and conforms to a few other requirements is said to
be well formed. Empty tags in XML are required to end with the /> character sequence, so a
program reading XML can be sure that it doesn't have to look for a matching closing tag. In
XHTML, the <br> tag must be written <br/> to comply.

All tag names in XML are case sensitive, so <br/> , <Br/> , and <BR/> are three different tags.
In XHTML, all tags used like HTML tags must be in lowercase, so only <br/> would produce a
text break in an XHTML compliant browser. Another name requirement is that general users
must not use tag names beginning with x or X. These names are reserved for future expansion
of the standard.

Although it's not required, a complete XML document usually begins with a statement in a
specialized format that states the version of the standard it conforms to. The following is an
example:

<?xml version="1.0"?>
Complete details on XML formatting are beyond the scope of this course. You can find the
formal documents at the W3C Web site, and any search engine can help you locate XML
tutorials.

In addition to the concept of well-formedness, XML documents can be valid. The content and
arrangement of elements in a valid XML document conform to a specification in a DTD
(Document Type Declaration) or an XML Schema. An XML document might be well formed
and not valid, but it can never be valid and not well formed.

An example XML document

Suppose you want to store all of the text in one of these lessons as an XML document. The
following is a skeleton of the document structure you could use, showing just the first page and
leaving out the bulk text:

<?xml version="1.0"?>
<lesson number="5" author="Chapple">
<title>Beginning Programming for the Web</title>
<page pagetitle="Learning XML Basics">
<paragraph>Similar to HTML, XML is a markup
language that follows a standard defined by
the W3C. The difference is that ....
</paragraph>
<!-- more paragraphs go here -->
</page>
<!-- more pages go here -->
</lesson>

That sure looks very similar to the HTML you might use to display the lesson, but instead of
using standard HTML tags, you get to make up your own. Why would you want to do that? The
following section takes a look at some possibilities.

Discovering data manipulation with XML


 
The following are some tasks that can be done by processing the XML document representing
this lesson:

Build a table of contents for the lesson by extracting the pagetitle attributes from the <page>
elements.
Reformat the document as a single HTML page.
Reformat the document as a series of HTML pages, one per <page> element.
Reformat the document in the widely used PDF (Portable Document Format).
Reformat the document as a series of small pages in WML (Wireless Markup Language),
used by WAP-enabled cell phones.
Store each <page> element in an XML-enabled database.

Libraries of code for processing XML documents exist in many computer languages. There are
basically two approaches: parsing into separate tags and parts of elements, and building
objects that contain entire elements.

In the separate parts approach, a parsing program takes the document apart at the points
where tags appear and hands the programmer the separate parts in the order of the original
document. Your code has to decide what to do with each part of the document on the fly. This
approach is particularly good if your program has to extract a small chunk out of the whole
document; for example, extracting the pagetitle attributes to build a table of contents.

In the building objects approach, your program is handed an object that contains the whole
document, organized so you can rapidly locate any part of it. This is essentially the document
object model approach used in JavaScript manipulation of a Web page. It's handy when your
program has to access various parts of the document repeatedly; for example, when creating
and editing.
XML and the future of programming

It's safe to say that in the future, almost all programmers need to know the basics of
processing XML, and almost all creators of content for the Web need to know the basics of
creating XML documents.

Web services

If you pay any attention to the business news, you know that something called Web services is
a hot topic. A Web service is a set of tools that's accessible from a network, such as the
Internet, and has a well-defined set of functions it can perform on request.

The idea with a Web service is that a company provides a special sort of Web server that
responds automatically to requests for particular kinds of data. Both the request and the
response are formatted as XML documents that can be read and interpreted by programs.

Popular Web services include remote procedure calls and SOA (service-oriented
architecture), and something called RESTful Web services.

For example, suppose vendors of auto parts provide their entire catalog as Web services. You
could have a program that automatically searches all vendors for the best price and availability
for a particular part. By using XML, it's easy for both humans and programs to understand the
process.

XML versus EDI

Corporations have been using EDI (Electronic Data Interchange) formats to send documents
such as bids, orders, and invoices back and forth for years. The big problem with EDI formats
has been that because they were created when transmission speeds were slow and long
distance connectivity was expensive, data gets transmitted in a compact form using esoteric
codes that can't be read by humans. Now that connectivity is inexpensive and transmission
rapid, it makes more sense to use human-readable XML formats.

XML-enabled Office applications

Microsoft Office 2003 and later versions are capable of reading, writing, and processing XML
documents in addition to its proprietary formats. This move by Microsoft was necessary
because more and more businesses were using XML as a standard for document storage and
interchange. Having corporate data in XML format makes it easy to communicate with your
customers through Web services or to generate the content of your corporate Web site.

OpenOffice.org is a suite of open source applications similar to Office productivity applications.


Like the Office suite, OpenOffice.org applications provide the option of using XML for
documents. For users of Linux and those who don't want to pay Microsoft's licensing fees,
OpenOffice.org is a viable alternative.

Moving on

This lesson introduced you to features of the markup languages HTML and XML, and showed
how markup languages are intimately involved with today's programming tools. You also got
an indication of how marked up documents can be treated as objects. Lesson 6 delves into
storing data in memory as well file manipulation and memory management. Before you move
on, complete the assignment and quiz for this lesson.

Assignment #7
The best way to learn about markup languages is to experiment with examples. For this
assignment:

1. Pick a fairly simple Web page on the Internet, and view the source to see the mixed data text
and markup that creates the page. (In Microsoft Internet Explorer, for example, select View >
Source to view source code.)
2. Save the source on your own computer as an HTML file.
3. Open the file with an editor (Notepad, HTML-Kit, or another program of your choice) and in
your Web browser. Investigate the code and compare it to the rendered page in your browser
to see if you can determine what each tag does.

Challenge exercise

Web services are emerging as a significant modern technology, and are revolutionizing the way
programs are delivered to end users.

To learn more about Web services, take the W3Schools Web Services Tutorial or research this topic
on your own.

Assignment #8
For this assignment:

1. Use Windows Notepad, HTML-Kit, or any text editor of your choice.


2. Use the following HTML markup code as a starter to create a personal Web page for yourself
that includes at least two short paragraphs and comments:

<html>
<head>
<title>My Web Page</title>
</head>
<body>
<p>This is the body text in a paragraph.</p>
<!-- this is a comment -->
</body>
</html>

1. Save the file with an .htm or .html extension.


2. Manually convert the file to XML using the following example as a reference and save it with
an .xml extension:

Remember, you can name the XML tags anything you like. Experiment!

<?xml version="1.0"?>
<lesson number="5" author="Chapple">
<title>Beginning Programming for the Web</title>
<page pagetitle="Learning XML Basics">
<paragraph>Similar to HTML, XML is a markup
language that follows a standard defined by
the W3C. The difference is that ....
</paragraph>
<!-- more paragraphs go here -->
</page>
<!-- more pages go here -->
</lesson>

1. View your HTML- and XML-based Web pages in a Web browser.

 
Quiz #5
Question 1:
Which of the following terms describe a markup language tag? (Check all that apply.)
A) Attribute
B) Singleton
C) SGML
D) Paired
 
Question 2:
True or False: All tag names in XML are case sensitive.
A) True
B) False
 
Question 3:
Which of the following are server-side scripting languages? (Check all that apply.)
A) PHP
B) JavaScript
C) ASP
D) JSP
 
Question 4:
Which tag marks the beginning of PHP code?
A) <%
B) %>
C) <?
D) ?>
 
Question 5:
Which language is commonly used to create Web services?
A) XML
B) HTML
C) BASIC
D) OML
 

Reading and storing computer data


In this lesson, you'll learn the important programming concepts of data storage, file manipulation and memory
management. You'll also learn how programs interact with databases to efficiently retrieve and store large
quantities of data.
 

How to remember stuff


 
Lesson 5 focused on Web-related and scripting markup languages: mainly, HTML and XML.
This lesson segues from languages, initially, to discuss how a computer reads and saves data,
enabling any programs you create to work with it. You'll wrap up the lesson by examining
databases -- which make heavy use of a computer's reading and writing abilities -- and the
base language that supports them.

Different types of computer memory

In the technological saga of the computer revolution, memory has always been one of the
dominant factors. Early computers used some bizarre methods of data storage; for example,
the first Univac stored data as vibrations propagating through thin columns of mercury.
Imagine a room full of Fastrand memory drums rumbling like a power plant and holding less
data than a single hard drive in a modern notebook PC.
As CPUs (central processing units) got faster and more compact, the limitation of the speed of
light became significant and computer designers concentrated on keeping memory as close as
possible to the CPU.

Now, computers remember data with a variety of technologies. As a programmer, you need to
understand what these technologies imply. The following table lays out the territory for further
exploration.

Memory Speed Volatility Capacity Programming Techniques


CPU Ultra Volatile Limited -- Programmer has no way to influence
cache fast hundreds
of
kilobytes
(thousands
of bytes)
Main chip Fast Volatile Many Allocating space for variables, especially
memory megabytes arrays and objects
(millions of
bytes)
Flash Fast Nonvolatile Kilobyte to Specialized according to hardware
memory megabyte
Hard disk Fairly Nonvolatile Many Creating, reading, and writing files on file
memory fast gigabytes systems with serial and random access
to several methods
terabytes
Removable Varies Nonvolatile From File systems -- creating, reading, and writing
media: CD, megabyte files with serial and random access
DVD, USB to many
flash drive, gigabytes
tape
Network Medium Nonvolatile Gigabytes Query systems such as SQL
database to
terabytes

Table 6-1: Characteristics of various kinds of memory.

Modern CPUs have a chunk of high-speed memory, called cache memory, directly on the
CPU chip. Data is moved to and from the main chip memory by CPU logic that tries to predict
what the program is going to want next. This improves the overall speed by having needed
data in the fastest possible memory.

Main chip memory uses an enormous number of tiny transistors to hold the bytes that your
program uses for program code and data. Even the slightest loss of power can cause these
transistors to forget everything. The language you use has a lot to do with how you manage
this memory, as you'll learn later in this lesson.

Flash memory is a special kind of transistor-based memory that can hold a value after the
power goes off. This is the memory that holds your favorite TV channel settings when the
power goes off, or holds your digital camera pictures in a memory stick. The programmer is
usually able to access this memory easily, as the operating system makes it appear identically
to a hard disk.

Hard disk memory stores data as magnetic patterns that can be read or written under the
control of your operating system. It's the operating system's responsibility to present this data
to your program in the form of a file system, which is a method of organizing stored data so
that it's easy to retrieve.

There are several varieties of removable media in use these days, from CDs/DVDs to USB
flash drives. Your operating system generally handles the hardware differences and presents
the programmer with a consistent file system interface. Flash memory, hard disk storage, and
removable media are collectively called nonvolatile storage because continuous power isn't
required to hold the data.
When storing data in a database -- particularly if the database management is handled by
someone other than you -- the programmer's life is greatly simplified.

Bits, bytes and word size

Historically speaking, computer designers have tried many different ways to organize memory.
Fortunately for computer users, designs have converged on using an 8-bit byte as the basic
unit of memory. One reason for this convergence is that with 8 bits, you can represent all of
the numerals, punctuation, and characters in English text, with some left over for special
characters.

In terms of data storage, a byte is a unit of measurement equal to eight bits; a bit is equal
to one binary digit, which is either a 0 or 1. Although the original microcomputers read and
wrote to memory one byte at a time, modern CPUs read and write in 32- or 64-bit (4- or 8-
byte) chunks. These details are hidden from the programmer, however, and the
fundamental unit that you work with is still the byte.

Now that you have a good overview of computer memory, let's turn our attention to managing
main memory.

Managing main memory


The simple programs you've seen in the lessons so far enable you to easily store variables in
memory. However, if you try to dimension some large array with statements such as the
following, you find the program fails mysteriously:

dim verticesX(10000000)
dim verticesY(10000000)

This is because your computer doesn't have an unlimited amount of space in main memory to
store data, and you run into trouble when you request the space to store 20 million numbers.
In fact, if you're running a typical Windows or Mac desktop, there are many programs of
various sizes sharing memory so the amount available for any one program is reduced.

The following are some of the types of programming that require a lot of memory:

Games with complex graphics


CAD (computer-aided design)
Web servers and other utilities that service many users and run for long periods

This brings us to the topic of memory management. You run into two types of memory
management in programming languages -- explicit memory management and automatic
memory management.

Explicit memory management

Using C, you have to explicitly request memory from the operating system for something such
as the large arrays of numbers in the previous example. You must also explicitly tell the
operating system when you're done using it. You must also ensure you don't have two parts of
your program trying to use the same memory for different purposes.

If, through bad programming practice or an error, you forget to return the memory to the
control of the operating system, your program experiences what's called a memory leak. A
memory leak means your program keeps requesting memory but never gives it back to the
operating system. Eventually the operating system refuses to give you any more memory and
the program fails.

A memory leak problem can be one of the most frustrating things in all of computing. Memory
leaks can be subtle and hard to find, which is why modern object-oriented programming
languages such as Java and C# provide for automatic memory management.

Automatic memory management

In object-oriented programming languages, requesting memory from the operating system is


handled behind the scenes by the object creation mechanism. You saw the mechanism in
action using JavaScript in Lesson 5. The following code says to create a new Date type
object:

new Date()

When that code is executed, the Java program either uses some memory space it has already
reserved, or requests more from the operating system and builds a Date object there. The
amount of memory naturally depends on how complicated the object is.

In object-oriented programming, each object is responsible for managing the data it


contains. The memory used is isolated from other parts of the program and access is
controlled by the object.

When your program is no longer using the object, Java detects this fact by a process called
garbage collection. Essentially, the garbage collection process looks at all the parts of your
program and determines all the objects that are still in use. Every other object is considered
garbage. All you have to do is stop using a variable, and the memory is reclaimed
automatically.

In the next section, you'll examine an important part of computer memory: how information is
read from memory.

Examining file reading methods


 
Your operating system is responsible for managing the stored data on your computer's
nonvolatile storage media so that your programs can treat the data in a consistent way. In
other words, the operating system hides the hardware details that are vastly different between
a hard disk and CD, and presents a consistent interface called a file system.

A file system supports locating data stored on the hardware by a directory system and file
name. It also keeps track of handy bookkeeping information such as the file size, the date the
file was created, the last time it was modified, and which users have permission to read and/or
write data to the file.

In turn, your programming language provides methods that bridge the gap between the
operating system and the variables in your program.

Sequential file reading

There are two fundamentally different ways of reading and writing files -- sequential access
and random access. Sequential access means the opened file acts like a stream of bytes that
has to be read in sequence.

Suppose you need to read the 10th line of text in a file named myfile.txt in the c:\documents
folder. The following is some pseudocode that's representative of all computing languages:

OPEN "c:\documents\myfile.txt" AS #input


DO
READ #input INTO astring
IF EOF PRINT "hit end of file" BREAK
INCREMENT linenumber
UNTIL linenumber EQUALS 10
IF NOT EOF use the astring variable
CLOSE #input

This pseudocode illustrates several important features that all programming languages use:

To read from a file, you have to open it first. The operating system ensures that you're
allowed to open that file and causes a fatal error if you're not.
When a file is opened, the programmer uses a variable, #input in the example, to refer to
the file. Using a special character to start the name of a variable that stands for a file is a
common convention. The variable that refers to a file is sometimes called a file handle.
The command that reads the file specifies where to put the data. This example assumes the
READ command reads a single line of text into a string variable. For this to work, the file
must use a convention for special character or characters signaling the end of the line.
With sequential access you have to read and discard everything in the file up to the point
where the data you want lives. In this example, you read 10 lines into the astring variable
with each successive line replacing the previous one.
When reading a file you must always consider the possibility that the file isn't as long as you
expect. You must provide for detecting the end of the file, commonly denoted as the EOF
condition. In the example, hitting EOF before the 10th line causes the program to break out
of the loop.
When you're done with the file, you have to close it to free operating system resources and
enable other programs to use them.

In the pseudocode example, it's assumed that the READ command would read an entire text
line into the string variable. That's actually a common convenience in programming languages
because processing text files is a common problem. Opening a file and reading it in this
manner is called working in the text mode. It assumes the data is text with each line
terminated by one or more control characters.

Control characters

By convention, certain characters known as control characters are used to indicate the end of
the line. Early computers used a number of special control characters to control printers, ring
bells on teletypes, and so on. The characters in common use for end-of-line indication are
called CR (carriage return) and LF (line feed). Back in the days of teletypes, CR returned the
print head to the start of the line, and LF advanced the paper one line.

Unfortunately, not every operating system uses the same combination. Windows systems use
two characters -- CR and LF -- Macintosh uses just CR, and Unix and Linux systems use just
LF. This difference creates an annoying requirement for a translation program when
transferring files between systems. You might also see the term newline for the character or
character sequence that terminates a line.

Reading in binary mode

Programming languages also provide a mode of file reading that doesn't try to find lines of text
terminated by a newline marker. Instead, the exact contents of the file are delivered to your
program without interpretation. In this binary mode, your read command has to designate an
array of bytes to receive the data and explicitly say how many bytes to read.

Random access file reading

In the sequential reading example, after reading nine lines, the next read operation would start
at the beginning of the 10th line. At that moment, the file read pointer (an address or reference
to a location in the program) would be positioned at the 10th line. That position could be
described with an integer as the nth byte in the file, where the first byte in the file is at position
zero, similar to the first element of an array is in position zero.

When reading and writing with random access, you must know exactly where the data starts in
the file. One way to do this is to use the same record size, every time. In the following
pseudocode for a subroutine to read a record, the variable recordBuffer is an array of
characters that has a fixed size so you can calculate where to read:

FUNCTION readCheck( chkNum, recordBuffer )


OPEN "checks.dat" FOR RANDOM AS #ranf
SEEK #ranf TO chkNum * checkRecordSize
READ #ranf recordBuffer
CLOSE #ranf
RETURN

In another part of the program, there would have to be routines that pick the characters out of
the recordBuffer and then interpret them in terms of your check records.

Using random access becomes increasingly helpful as files get larger. Just think how much
harder it'd be to read the last 100 bytes in a 10 MB file by sequential reading instead of
seeking a file position equal to the size of the file minus 100.

Database technology absolutely depends on random access because the files are so large,
but is limited by the fact that after you've decided on a record size for a file, you can't change
it. If your record design changes, you have to write a program that can read all of the old
records and write a new file with the new record sizes.

Now that you have a good understanding of reading files from disk, it's time to take a look at
storing data on a file system using sequential file writing. That's covered in the next section.

Discovering file writing methods


 
Generally, the syntax computer languages use for writing text data to files is similar to that
used for writing to the screen or printer. The language regards the screen or printer as just
another place to send a stream of characters. This makes it easy to test your program with
writing to the screen and then switch to writing to a file. You still have to open the file and use
a special variable to refer to it as shown in the following pseudocode:

OPEN "theoutput.txt" AS #out


PRINT #out "Hello, World"
CLOSE #out

In most languages, opening a file with an existing file name can cause the previous contents
to be overwritten; however, there is usually a syntax for opening a file in append mode so that
new data is written at the end of the file.

You have to be careful about the conventions each particular programming language has with
respect to writing complete text lines. In other words, you need to understand whether or not
the language automatically writes a line terminator character sequence with every PRINT
statement.

For example, if you're writing a file of addresses and want to put the entire address on one
line, you don't have to write a single PRINT command to do the whole thing. But you do have
to be careful when choosing the syntax for the particular language you're using. For example,
in Java, the following three lines of code write a single line of text to a file named out because
only the println command automatically adds a line terminator.
out.print( lastName + ", " + firstName + " - " );
out.print( streetAdr + ", " + city + " - " );
out.println( phoneNumber );

Writing and reading whole objects

Some object-oriented languages, such as Java and C#, provide convenience methods that
enable you to write the entire contents of a complex object to a file. Later you can read the
object back in and resume working with it. This capability can be extremely handy.

For example, suppose you're writing a program that has a number of personal preference
settings for each user. You want to be able to read the settings when the user starts the
program and then save them whenever the user changes them.

Assuming you have designed your program so that all user settings are contained in a single
object named userPref, the pseudocode for saving that object in a file named userWB.dat is
as simple as the following:

OPEN "userWB.dat" FOR OBJECT WRITING AS #obj


WRITE #obj userPref
CLOSE #obj

The real code in Java or C# would be a little longer because you have to take into account the
various errors that could happen, but it would still be quite simple.

When reading and writing objects, you can't change the definition of what goes in the object
and still be able to use objects saved with the old definition.

Now that you understand methods of reading and writing data, let's move on to a data-
intensive subject: working with databases.

Working with databases


Databases are an effective way to store data efficiently in a form that's accessible to a wide Websites
variety of users and applications. Modern relational database systems organize data into what and
are called tables. Think of a table as a ledger or as a spreadsheet: you have rows and databases
columns, and each column stores a particular type of information. To insert information into a
database table, you can't just pick one cell where one row and one column meet and then
write in the value. To make an entry, you must complete the entire row, providing data for A good
each column. example of a
Web site with
a database
Database systems are useful, but what makes them powerful is the ability to retrieve behind it and
information out of them in an organized manner. Most modern database systems (products that uses
such as Oracle, mySQL, and Microsoft SQL Server) can process a special type of language, programming
called a query language. You use these languages to query the database, or ask it to return to help the
information. Using query languages, you write instructions called queries to return the exact Web pages
information you need. Exactly how these queries are created and used in your program talk to the
depends on the facilities in the language you're using. Rather than get into programming database is
language specific issues, let's look at the query language itself. an online
store. You
enter
Introducing the Structured Query Language information
about what
you want to
Almost every database today uses SQL. Using SQL, you can retrieve specific information from buy, where to
a table, such as the ZIP code for everyone in an address table whose last name is Smith. You ship your
can also perform queries that are more complicated. For example, you can have your query purchases to,
return the name, street address, city, state, and ZIP code from the address table for every and how to
name in the flyer mailing table whose last mailing date is older than one month from the pay for them
current date. In this case, your query would match the names between the two tables to find through fields
the address for the names it identifies in the flyer mailing table. on the Web.
The
information
The ability to match data from different tables in this manner is what distinguishes a relational
you enter
database from a flat database. Pieces of data, such as the name in the previous example, can
moves
be used to identify other data based on its relationship to it, in different tables. SQL
through the
accommodates flexible logic in matching information such as this.
Web page to a
database so
your products
A common SQL convention is for keywords and instructions to be capitalized. There are four
can be
basic tasks that SQL can perform on a database table. It can INSERT new rows of data,
packaged,
SELECT data from the table, UPDATE data, or DELETE a row from the table. Look at each of
shipped, and
these four tasks.
billed
accordingly.
INSERT statement Programming
and
databases
You can use an INSERT statement to insert a row of data into a table. To perform an INSERT make all this
operation, you need to specify what table you're adding data to and what data you're adding. possible.
The full statement looks similar to this:

INSERT (value1, value2)


INTO table_name

If you're not filling every column in a specific order in your insert, you have to additionally
specify which values are destined for which column.

SELECT statement

Use a SELECT statement to retrieve values from a database. You can specify which values
you want returned; you don't have to get back a whole row of data. You can also add other
specifications to this statement, as discussed earlier in the flyer mailing example. A typical
SELECT statement uses the following syntax:

SELECT column_name
FROM table_name
WHERE column_name = someValue

The WHERE clause is how you specify what data you want returned. Note that column_name
doesn't have to be the same as the one you're returning. You can add multiple conditions to
the WHERE clause by using AND or OR and then adding more conditions. There are many
ways this statement can be extended to do different tasks and perform more complicated
comparisons.

UPDATE statement

As the name implies, an UPDATE statement modifies data already stored in the database. To
do this, specify which tables you're modifying, and which columns of data you want to update.
You can also use a WHERE clause to specify which rows are updated. (If your code states
WHERE some_column = 2, for example, every row with a value of 2 for some_column will be
updated.)

The UPDATE statement uses the following syntax:

UPDATE table_name
SET column_name = new_value
WHERE conditions ...

DELETE statement
A DELETE statement removes rows from a database. Because databases require information
to be stored in rows, you have to remove an entire row at a time. Specify which table from
which you want to remove rows and what conditions describe the rows you want to delete
(using a WHERE clause). Here's the syntax for a DELETE statement:

DELETE
FROM table_name
WHERE conditions ...

SQL also includes other ways to modify these queries. For example, you can have it sort the
output of a person and address alphabetically by name. There's much more that can be done,
but most SQL is built around these four basic commands, which manipulate database data.

Moving on

This lesson covered the important programming concepts of data storage, file manipulation,
and memory management. You also learned the essentials of database programming. Lesson
7 examines an aspect of modern computing that's related to the data storage and retrieval
techniques you saw in this lesson. Instead of reading and writing data on a local computer,
you communicate across a local network or the Internet, or between mobile devices. Before
you move on, complete the assignment and quiz for this lesson.

Assignment #9
Write a program to read text files line by line and print each line to the screen. Try this program on
various small files you find on your system. What happens when you read a file that isn't in text
format?

If you need additional help, read the tutorial sections on writing files in Liberty BASIC or find
additional information on the Web.

Assignment #10
In this assignment, you'll practice creating SQL statements. Assume you have a specialty foods Web
site and buy products to resell from a variety of vendors. Your ordering database has several tables,
including one called vendors. A small portion of the vendors table follows.

ContactName Company Region City


Alan Weber Weber Fine Foods NE New York City
Mary Barnett Gourmet Delights SW Tuscon
Jackie Drissell Farm Fresh Snacks NE Baltimore
Jacob Berner Jacob's Best NW Seattle

Write SQL statements to produce the following results:

1. Display the contact names and companies of all vendors in the Northeast region.
2. Display the vendors whose representatives have "er" in their names.
3. Delete the Farm Fresh Snacks entry.

Challenge exercise

Think about a data management topic that you encounter at work or school and come up with a
database solution to help manage that issue. Consider the following:
The types of tables would you use in your database
Columns of data would you store in each table

Sketch the tables with column names on paper, or use a spreadsheet program to create the tables and
columns.

Solution

The solutions are as follows:

1. To display the contact names and companies of all vendors in the Northeast region:

SELECT ContactName, Company


FROM vendors
WHERE Region = 'NE'

1. To display the vendors whose representatives have "er" in their names:

SELECT * FROM vendors


WHERE ContactName LIKE '%er%'

1. To delete the Farm Fresh Snacks entry:

DELETE FROM vendors WHERE Company = 'Farm Fresh Snacks'

Quiz #6
Question 1:
True or False: All programming languages provide only for reading binary data. You have to write code to
interpret that data as variables.
A) True
B) False
 
Question 2:
Which of the following types of memory retain data when a computer's power is off? (Check all that apply.)
A) CD
B) Flash memory
C) Cache memory
D) Hard disk
 
Question 3:
In modern relational database systems, data is organized into ______.
A) groups
B) tables
C) chunks
D) sections
 
Question 4:
Which of the following isn't a SQL instruction?
A) INSERT
B) DELETE
C) ALPHABETIZE
D) UPDATE
E) SELECT
 
Question 5:
Which of the following SQL instructions modifies data that's already stored in a database?
A) INSERT
B) DELETE
C) ALPHABETIZE
D) UPDATE
E) SELECT
 

Programming for networks and mobile devices


Knowing how to make computer programs that communicate with other systems over the web is essential
today. This lesson covers basic networking concepts and programs that retrieve data from the internet, send
email and interact with mobile devices.
 

Networking computers
In Lesson 6, you learned how a computer reads and saves data, enabling any programs you create to
work with it. You also explored databases and the foundation of most database languages: SQL. This
lesson focuses on programs that help you communicate across networks -- locally and the Internet -- and
between mobile devices. Let's get started.

Networks enable computers to communicate with each other. They're made up of cables, switches,
routers, and other devices dedicated to ensuring that you're able to get data from any given location on
the network to any other location in an efficient manner. Networks come in all shapes and sizes. The three
most common types of network are:

LAN (local area network): This is two or more computers in a geographically small area, such as a
building, linked with high-speed connections.
WAN (wide area network): This type of network is spread over a larger area, such as a factory or
campus. Usually, a WAN provides connections between LANs using technology more suited to longer
distance transmission.
Internet: This is a network of networks, composed of a hierarchy of smaller networks including
specialized backbone networks that move data between smaller networks.

The specifics of networking are beyond the scope of this course. This lesson concentrates on
interesting tasks you might do with a program to connect to the Internet.

To communicate on the same network, computers must speak the same network language (or protocol).
In the early days of networking, many different protocols were in use in different organizations. After all,
there was no Internet back then. A company might have had a LAN connecting all of its internal systems
together, but external connections were limited. There simply wasn't a need for a single standard network
protocol.

With the evolution of the Internet, that all changed. Now it's commonplace to sit in your living room and
interact over a network with a computer in another part of the country or even on a different continent. It's
easier for you to use your computer to visit a Web page on a server located in China than it is to visit your
corner store. That communication is easy because all computers on the Internet (and most LANs and
WANs, for that matter) have now standardized on a single set of protocols: TCP/IP (Transmission Control
Protocol/Internet Protocol). These protocols define the language used by computers to communicate with
each other and serve as a worldwide standard.

From the perspective of a programmer, you don't need to worry about the details of TCP/IP networking --
the mechanics behind getting data from Point A to Point B are for network engineers to worry about.
However, you do need to know how to write programs that leverage the Internet to share information.

Now that you're familiar with the basic concepts of networking, let's move on to more specific examples. In
the next section, you'll learn how to retrieve data from the Internet within your programs.

 
Retrieving data from the internet
 
No matter which hardware you're using to connect to the Internet -- dial-up modem, cable, DSL (digital
subscriber line), or optical fiber -- you only have to deal with TCP/IP and URLs at the programming level.

A URL is a standardized way of addressing resources on the Internet, and is also known as a URI
(Uniform Resource Identifier). The standard is maintained by the W3C. A URL (or URI, if you prefer) is
composed of at least two parts -- a protocol plus some form of address, optionally followed by additional
data. The following table shows some common Internet protocols and how they're used.

Symbol Protocol Used for


http: HTTP (Hypertext Transfer Web resources including hyperlinks
Protocol)
ftp: FTP (File Transfer Protocol) Standard utility for file transfers
telnet: Terminal Emulator Character-by-character terminal emulation for remote
access

Table 7-1: Common Internet protocols.

A simple internet example

Java is an ideal language for experimenting with grabbing information off the Internet because it was
designed from the beginning to be network aware. The following example demonstrates connecting to a
well-known site run by the United States government to provide accurate time. It uses the following URL:

http://tycho.usno.navy.mil/cgi-bin/timer.pl

The parts of this URL are:

http: This declares the protocol used.


tycho.usno.navy.mil: This is the Internet address of a particular computer. You know this is a military
network site because of ".mil".
cgi-bin/timer.pl: This says that you want a response from the timer.pl function, which happens to be a
script in the Perl scripting language.

You're going to see a Java program example that connects to that computer, accesses that timer.pl script,
and then captures the output. To keep the example compact, the program accomplishes everything in a
single method -- not a particularly good practice for anything larger than this example. This program is
similar to sequential file reading, as discussed in Lesson 6, except that you use a URL instead of a local
file name.

Before you get started, take a look at the US Naval Observatory Master Clock Time Web page in your
browser. You'll see that it produces a page containing the current time in various time zones, as shown in
Figure 7-1.

Figure 7-1: US Naval Observatory Master Clock Time Web page.

Here's a look at a Java program that'll access this Web page (the line numbers are for reference only):
1: package com.pb ;
2: import java.net.* ; // note
3: import java.io.* ;
4: public class UrlExample{
5: public static void main( String[] args ){
6: if( args.length < 1 ){ //
7: System.out.println(
8: "Program expects a URL
on the command line");
9: System.exit(1);
10: }
11: try { //
12: URL theUrl = new URL( args[0] ); //
13: URLConnection conn =
theUrl.openConnection();
14: conn.connect();
15: InputStream in = conn.getInputStream(); //
16: int ch = in.read();
17: while( ch != -1 ) { //
18: System.out.print((char)ch );
19: ch = in.read();
20: }
21: in.close();
22: System.out.println("Done");
23: }catch(Exception e){ //
24: System.out.println
("Problem " + e.toString() );
25: }
26: }
27: }

The following describes some of the significant parts of the code:

Line 6: This is the way a Java program gets information from the command that started it. The program
expects to see an array of strings that have at least one item, namely the URL, to connect to. If there's
nothing there, line 8 of the program will display an error message.
Line 11: This block of code starts with try and ends with catch (on line 23). It's part of Java's error
notification system.
Line 12: This statement creates an object that knows how to create a connection to another computer
using a URL. The connection is represented by an URLConnection object that gets created on Line 13.
Line 15: The InputStream object represents the way Java reads streams of characters. This object
behaves the same way when reading from the connection as it would when reading from a local file.
Line 17: This line reads characters from the stream one at a time and writes them out until it reaches
the end, where the end is indicated by getting a -1 from the InputStream.
Line 23: The code would get executed if the statements between try (line 11) and catch (here on line
23) cause an error, such as not being able to contact the URL.

When executing a program such as this, with the URL on the command line:

java com.pb.UrlExample
http://tycho.usno.navy.mil/cgi-bin/timer.pl

you get the following response:

<title>What time is it?</title>


<h2> US Naval Observatory
Master Clock Time</h2> <h3>
<br><br>June 16, 01:43:28 UTC
<br><br>June 15, 09:43:28 PM EDT
<br><br>June 15, 08:43:28 PM CDT
<br><br>June 15, 07:43:28 PM MDT
<br><br>June 15, 06:43:28 PM PDT
</h3>
Done

Isn't that cool? That's all it takes to grab information from a computer on the Internet. Naturally, things can
get a lot more complicated than that simple example. You could easily extend this program to do all sorts
of interesting things. For example, you could create a program that retrieves the current time from the
Naval Observatory and uses it to decide whether to sound an alarm. Talk about an accurate alarm clock.
Now that you've learned how to retrieve data from the Internet within your program, let's turn our attention
to sending electronic mail in the next section.

Sending email from your programs


 
You'll often want to use your programs to send e-mail directly to recipients based upon the program's
output. A few situations in which you'd want to do this include:

Sending a receipt to a customer after the program processes an order


Sending a notification to system administrators when a program detects a disk that's almost full
Delivering alerts to programmers when programs result in an error

In this section, you'll take a look at how you can send e-mail from within your programs using a simple
example in the Perl programming language.

Email basics

Before you begin creating a program, you need to understand a little background about how e-mail works.
The basic building block of e-mail is a protocol known as SMTP (Simple Mail Transfer Protocol). SMTP is
responsible for routing messages around the Internet and ensuring they reach their intended
destinations.

When you send an e-mail from your computer, the computer uses SMTP to connect to your local mail
server. If you're at work, it's likely that this mail server is run by your employer and resides on your
corporate network. If you're at home, it's likely that the mail server is run by your ISP (Internet Service
Provider). Your local SMTP server has two main responsibilities: accepting messages from you and other
local users and routing them across the Internet and receiving messages from other SMTP servers on the
Internet that are intended for local users.

Once your message reaches your local SMTP server, that server looks at the recipient's e-mail address
and performs a lookup to identify the appropriate SMTP server that should receive the message for
delivery to the recipient. For example, if you're sending a message to mike@mycompany.com, your local
mail server would check a database to determine that the mail server for mycompany.com is a system
known as smtp.mycompany.com. It then connects to that server using SMTP and passes along the
message.

Designing a simple email program

You don't need to worry about the mechanics of SMTP to create e-mail programs. Your program just
needs to know how to compose an e-mail message and get it into the hands of your local SMTP server.
Once you deliver it to your local server, it's that server's responsibility to ensure that it's correctly routed
across the Internet into the e-mail box of the intended recipient.

So, let's take a look at a simple program written in the Perl programming language that connects to a local
SMTP server and creates a short message for delivery to a remote system. As with other examples, line
numbers have been added for clarity of explanation -- they don't appear in the actual program.

1: use Net::SMTP;
2: $smtp = Net::SMTP->new('smtp.myhost.net');
3: $smtp->mail("sender@mycompany.com");
4: $smtp->to("recipient@yourcompany.com");
5: $smtp->data();
6: $smtp->datasend
("From: sender@mycompany.com\n");
7: $smtp->datasend("To:
recipient@yourcompany.com\n");
8: $smtp->datasend("\n");
9: $smtp->datasend("My program likes to send
e-mail!\n");
10: $smtp->dataend();
11: $smtp->quit;
The following describes some of the significant parts of the code:

Line 1: This command loads the Net::SMTP library that enables Perl to send e-mail.
Line 2: This line opens a connection to your mail server. You'll need to replace "smtp.myhost.net" with
the name of the SMTP server used by your organization or ISP.
Lines 3 and 4: These two lines create the "envelope" for the e-mail message. They specify the sender
(in the mail command) and the recipient (in the to command) of the message.
Lines 5-11: The remainder of the program creates the body of the message. You repeat the from and to
addresses here so they appear in the e-mail at the receiver's end. The smtp->dataend() command
indicates you're done creating the body of the message, and the smtp->quit() command closes the
connection to the mail server.

You've now learned how to design a program that interacts with the network: both for retrieving data and
sending e-mail. In the final section of this lesson, you'll learn how programs work with a special class of
devices: mobile phones and PDAs (personal digital assistants).

Interacting with mobile devices


 
There's a new trend you should consider when designing network connectivity and Web-based programs:
the proliferation of mobile computing devices such as PDAs and smartphones. These devices bring
extraordinary power to the hands of travelling users, but they also introduce complexities for Web
programmers.

The major challenge you need to overcome is simple screen real estate. Consider the application
displayed on a common smartphone shown in Figure 7-2. Although mapping programs are available
across the Internet using any type of Web browser, a programmer must use special programming
techniques to render a similar amount of data on the small screen of a mobile device.
Figure 7-2: A map application on a smartphone.

Another major difference that's just beginning to emerge on the mobile computing front is location-
awareness. Newer smartphones and PDAs have built-in GPS (global positioning system) sensors and
applications might make use of that data. For example, you could develop an application in which a user
clicks a button on his or her smartphone, the phone transmits GPS coordinates to a Web server,
compares those to a database of coffee shops (or whatever is desired), and sends the user directions
from his or her current location to the nearest coffee shop.

You'll learn about applications you can use to develop mobile device applications in Lesson 8.

Moving on

This lesson covered how programs interact in a networked environment. It also gave you a short overview
of how you can design programs to work on mobile devices. In Lesson 8, you'll get some practical advice
on programming from a seasoned programmer, and you'll get an overview of tools and techniques used
by professional programmers. Before you move on, complete the assignment and quiz for this lesson.

Assignment #11
Think about an application of interest to you that uses network capabilities. Perhaps you'd like to
write a program that retrieves stock quotes from the Internet and sends you an e-mail alert when a
certain stock reaches a certain price target. Maybe you'd like to write an application that checks a
store's Web site for the availability of this holiday season's hard-to-find toy and notifies you when it's
in stock.

Whatever the application, sketch out the details and write a program in the language of your choice.

Quiz #7
Question 1:
True or False: The Internet is a network of networks, composed of a hierarchy of smaller networks including
specialized backbone networks that move data between smaller networks.
A) True
B) False
 
Question 2:
Which type of network connects computers spread across a large university campus with locations in several
places throughout a city?
A) LAN
B) Intranet
C) WAN
D) Internet
 
Question 3:
What's TCP an acronym for?
A) True Communication Protocol
B) Transmission Control Protocol
C) Transparent Connection Protocol
D) Truly Confusing Protocol
 
Question 4:
Which protocol sends e-mail across the Internet?
A) HTTP
B) SMTP
C) FTP
D) SSH
 
Question 5:
What's the major challenge to developers of Web applications for mobile devices?
A) Bandwidth
B) Data entry
C) Screen size
D) Portability
 

Discovering your future as a professional programmer


Get an introduction to the world of professional programming, starting with a survey of various kinds of digital
systems. The class ends with a discussion of the environments and tools you work with as a professional
programmer.
 

Understanding programmers and the digital revolution


 
This final lesson begins with a short but important discussion of where programming might fit
into your future. Many programming discussion group postings read something similar to:
"Okay, I have gotten pretty good at programming in (C, C++, C#, Java, Python, whatever);
now where can I get a job?" Knowing only a single programming language might get you a
position as a trainee, but the most interesting jobs require competence in additional fields.

Computing is such an integral part of society that there are many people who program daily
but don't consider themselves programmers. Being able to write a program to assist them in
their real work is simply part of their total professional competence. You should regard
programming ability in some language or languages as just part of your total package.

Programming really small things

It might not be obvious, but all of those digital gadgets in your life -- from thermostats to
microwave ovens -- are running programs written by somebody. You might see this field
referred to as embedded computing because the computers are an inseparable part of these
gadgets.

The smallest computers, called microcontrollers, are widely used in modern electronics. With
all that activity, naturally there are highly specialized Web sites where you can learn more
about jobs, tools, and manufacturers that use microcontrollers.

Highly portable computing power

Computer users are having a convergence of computing and communicating power in the
form of portable devices that combines Web browser, text messaging, e-mail handler, desktop
utility, and telephone capabilities.

Because of general standardization on Java as a widely supported language on such devices,


an astonishingly large market for cell phone games has arisen in the last few years. If you like
to program games but don't relish working on the monsters that PC games have become,
maybe this field is for you.

Robots

Robotics is currently another field that might interest you. Programming robots can involve
anything from electrical engineering to artificial intelligence. You don't need to spend big bucks
to get started, either. Lego makes a low-priced but highly capable robot kit that can be
programmed in Java. On the serious side, robot programming can even get you involved in
space exploration.

Scientific computing

Practically all scientific fields involve moderate to heavy use of computer power -- from
computerized lab instruments using embedded computing power to the largest supercomputer
complexes doing climate modeling. Computing power is essential to genome sequencing,
earthquake prediction, and climate modeling, just to name a few.

A wide variety of languages are used in scientific computing, but you can't go wrong by
studying the C family of languages back in Lesson 4.

Dynamic web pages and the arts

Human interface design, especially in the commercial world, is all about perception and
emotional impact. If poorly designed Web pages and ghastly interfaces make your artist's soul
cringe, maybe Web design, which combines both programming and artistic sensibility, is for
you.

The hot technologies in this area are JavaScript, which you have looked at several times in
this course, Adobe Flash, and related technologies. In some cases, Java applets can create a
dynamic effect on a Web page that simply can't be approached by any other technology.

Internet services
All Web sites that use dynamically created Web pages, such as portals, auctions, or shopping
carts, have to be programmed by somebody -- so, why not you? As more commerce and
information retrieval moves to the Web, more job opportunities are created.

Those are certainly some exciting technologies, and the good news is you don't need to create
all of the code to implement them. The next section covers how reusable code makes your life
easier.

Accessing libraries and reusing code


 
No matter which language you're using, there are a tremendous number of resources
available to make your job easier. You might find that someone has already written the exact
subroutines or class libraries that you need.

A common nomenclature for referring to libraries is API (Application Programming


Interface). You'll often see the word "interface" used when talking about language libraries.
A graphical user interface represents the boundary between the user and a computer, and
a programming interface represents the boundary between your program and library
functions.

Standard libraries

Many modern languages come with extensive libraries of their own. During the initial explosion
of interest in Java, Sun created so many different APIs that most people became confused as
to what is a standard part of the language and what is an add-on library. Sun finally got wise
and created three editions of Java:

J2ME (Java 2 Micro Edition): A set of libraries suitable for people working with small
systems, such as cell phones
J2SE (Java 2 Standard Edition): A set of libraries suitable for most programming
J2EE (Java 2 Enterprise Edition): A set of libraries suitable for those working on the largest
scale of commercial Web sites and other applications

Many other commercial language vendors follow a similar approach and have editions at
various levels. For example, the starting version of Liberty BASIC is limited in certain areas
and doesn't permit the creation of stand-alone programs.

The C++ language has seen the STL (Standard Template Library) become a standard part of
the language. The STL contains a wide assortment of mathematical and functional tools, as
well as a full-featured string class. Having these tools available for use can save incredible
amounts of time.

Object-oriented language libraries

One big advantage of OOP is the ease with which you can make use of library functionality.
You have basically two ways to make use of an OOP library: you can create instances of a
library class, or you can extend a library class.

In Lesson 4, you created a new Java class MyFrame by extending a library class named
Frame. Frame is an example of a library class designed to be easily extended. As discussed
in earlier lessons, by extending Frame, you inherit the variables and functions that make it
easy to create a new user interface.

As a refresher, inheritance is using one class as a starting point for another class, which
extends the parent class as needed.
Other libraries are designed with self-contained functionality that you can plug into programs
where needed. For instance, continuing with the MyFrame example, the following line created
an instance of the Label class, customized with the text message and positioning instruction.

add( new Label("Hello World", Label.CENTER ));

Online information

Having a connection to the Internet is similar to having a nearly infinite library at your
command. Unfortunately, you have to act as your own librarian to locate the resources you
need, but you'll find that search engines are a big help.

Many vendors of programming languages have extensive online reference material for their
programming libraries. For example, one starting point to navigate Microsoft's huge supply of
technical information is the MSDN Library site. Here you can find both general articles on
programming techniques as well as specific details on the libraries available for the .NET
family of languages.

Open source libraries

The Internet has made it possible for people from all over the world to involve themselves in
creating programs and programming libraries in astonishing profusion. The products of these
projects are usually freely available to anybody who wants to use them, which is why they're
called open source. Probably the most famous open source product is Linux. Its development
is discussed in the thought-provoking "The Cathedral and the Bazaar" essay by Eric S.
Raymond.

There are many Web sites where these projects are coordinated. If you completed the
assignment for Lesson 4, you know that two good examples are SourceForge and the Apache
Software Foundation, home of the widely used Apache Web server and many other projects in
many languages. SourceForge now hosts over 180,000 open source projects. Google Code is
another popular and ever-growing Web site with about 50,000 open source projects, and
makes it easy for beginners to learn, try, test, and get help from the programming community.

Now that you've learned about reusing code, let's turn our attention to the next section, where
we look at integrated development environments.

Examining programming tools and writing utilities


Every programmer creates a programming environment to suit the needs of particular projects. Have you
In some cases, such as Liberty BASIC, the programming language comes complete with a used an
built-in editor and the capability to save program files. In the C family of languages, IDE?
programmers can use their favorite editing environment with a separate compiler and
debugger, or use an IDE (Integrated Development Environment).
Have you had
enough
You'll learn more about IDEs shortly, but first consider editing programs with a separate exposure to
application. Although it's certainly possible to edit a Web page (including JavaScript) with the programming
Notepad application that comes with Windows, editors designed specifically for programming that you've
can provide some helpful functionality and are less expensive than a full-featured IDE. Some used an IDE?
features to look for include: If so, which
one did you
Syntax color-coding: The editor is aware of key words used in the programming language use and did
you are writing and can color code the various elements. you like it?
Project organization: The editor enables you to define a project file that remembers all of the Post a
files you're using as well as your custom settings. Opening the project file puts you back at message to
the point you were working on when you closed the project. the board and
Tool running capability: The editor enables you to define jobs that can be run without share your
leaving the editing environment. Typical jobs might include compiling the program or experience
copying files. with your
classmates.
You might find a programmer's editor called UltraEdit32 to be exceptionally useful because it
has all of these features.

Put it all together with IDEs

IDEs are very popular tools for writing programs. These environments usually, at a minimum,
combine a customized text editor for entering code and tools for debugging. In most IDEs, for
example, you can run a program line by line, walking through the instructions as they're
processed (called tracing or stepping through a program). The debugger can display the value
of variables as they change when statements are executed. This is a useful feature when
you're trying to figure out which statement draws an incorrect line in a complicated graphic, or
has an error in a long mathematical function or method.

Many IDEs do more than just this. Many programming tasks, such as programming graphical
interfaces (windows, toolbars, buttons, and so on) or writing C code to interface with a SQL
database, for example, are complex but tend to be written in standard ways. Some modern
IDEs provide tools to help write this code for you.

Some of the most popular examples of modern, powerful IDEs include:

NetBeans IDE: This power-packed tool by NetBeans.org lets you work with Java,
JavaScript, C/C++, PHP, and many more programming languages. It includes wizard-driven
screens that walk you through program setup step by step, or let you program in freeform.
Visual Studio: This widely respected IDE from Microsoft provides the same working
environment for C#, Visual Basic, and a number of other Microsoft languages in the .NET
family. The main disadvantage is that it's rather expensive. You can use a "lite" version
called Visual Studio Express, which is available as a free download.
Borland IDEs: Borland makes IDEs for Java, C++, C#, and Delphi programming plus many
design tools. Special personal versions of these tools that run on both Windows and Linux
are available for free downloading, and are ideal for students.
WebSphere Studio: The latest IDE for Java and JavaScript programming from IBM
emphasizes development of Web services.
CodeWarrior: This line of IDEs from Metrowerks is for C, C++, and Java programming on all
desktop environments. Versions for embedded devices such as cell phones are especially
popular. Free or low-cost student versions of some products are available.

Many of the modern IDEs provide tools for generating graphics instructions. For example, you
can actually drag and drop graphical components from a palette onto a drawing board that
represents what becomes the finished user interface. After you've drawn your graphical
components, you can then see the instructions the IDE has written that causes those graphical
components to be drawn.

As you can imagine, it's often easier to have an IDE write hundreds of lines of code to draw an
application's window than it is to do it yourself. The catch is that to have your interface actually
do anything other than trivial tasks (such as just being displayed), you usually have to
customize the code later.

When you have the IDE write a lot of the code, you also lose some control over how you can
rearrange the program later, particularly if you want to use the IDE's tools to generate more
instructions after you've modified what it's created. Nonetheless, using IDEs in this way has
become common in recent years, as the graphics used in most programs have grown more
complex and programming projects try to get more accomplished in less time.

Specialized programming utilities

There are many other utilities that the programmer can call on for assistance in accomplishing
various chores. For example, when dealing with the HTML markup language, you can use
HTML-Kit to create and edit Web pages and HTML TIDY to check for correct usage of markup
tags. You might be surprised at the number of mistakes TIDY can find in Web pages.

If you're programming applications for desktop use, it's easy to try the program on your
development system; however, if you're programming for an environment such as a cell
phone, things get tricky because a cell phone doesn't offer much debugging help.

The emulator is the savior of countless hours of cell phone developer time. A cell phone
emulator creates a virtual cell phone on your desktop where your program can be run under
more control. A good example is the Sun Java Wireless Toolkit. It provides a desktop
interface, as shown in Figure 8-1, that lets you control a development project that targets a cell
phone environment.

Figure 8-1: The wireless toolkit user interface building the games project.

Enlarge image

When the program has compiled without error, you can load it into the cell phone emulator and
run it, as shown in Figure 8-2. All of the emulated cell phone buttons work as expected to
control the program.

Figure 8-2: The games project running in an emulator.


See how to use the Sun Java Wireless Toolkit. (1.2 MB)

Now that you've learned about some emerging programming technologies, the final section of
this course looks at opportunities to expand your horizons even further.

Learning more
 
Being a programmer means you never stop learning. Today's most popular languages are
being enhanced and improved at a rapid rate, and new technologies are appearing every day.
New computer hardware and faster processors make things possible that wouldn't have even
been considered a few years before.

Since the mid-1990s, Internet-related technologies have been developing at a furious pace.
It's safe to say that the field of computer programming will keep growing, meaning there'll be
more languages, computers, hardware, and operating systems to learn. Fortunately, the
Internet also provides Web sites and user groups you can use to keep up with all the current
technology. Some great places to explore are:

Programmers Resource : Provides a developers' forum in addition to articles and code


snippets for many programming languages
ACM (Association for Computing Machinery): Offers links to SIGs (Special Interest Groups),
an extensive publications list, groups, conferences and other events, online communities,
and much more
IEEE: A membership-based, highly regarded technology standards institute that provides a
source of technical and professional information, resources, and services
Dr. Dobbs' Journal: Offers a portal of resources geared toward programmers, such as
downloadable source code, articles, blogs, podcasts, newsletters, and more

Programming is a field that continues to grow. New languages, changes in hardware and
software, and the growth of the Internet all make programming a challenging field.

Moving on

You've been introduced to a lot of material in this course, and inevitably, there's far more to
cover. Hopefully, you now have a feeling for what programming is and the areas in which you
want to learn more. If something you saw particularly interested you -- interactive Web pages
with JavaScript, or mathematical programming in C -- that's a great place to start learning
more. The best way to learn is by pursuing something that interests you. In a field as diverse
as programming, that's especially true. Before you move on, complete the assignment and
quiz for this lesson to reinforce important lesson topics.

Assignment #12
To get a feel for a cell phone development emulator:

1. Download and install the Java J2ME Wireless Toolkit.


2. Open the software, select File > Open Project, and then select a demonstration
project from the Open Project dialog box.
3. When the demo loads, click Run on the toolbar. A cell phone emulator window
appears.
4. Select a demo in the phone interface (if more than one is listed), and then click the
large Select button in the middle of the phone interface.
5. Run through the demo to see how it works.

Quiz #8
Question 1:
Which of the following are ways that programmers reuse code? (Check all that apply.)
A) API
B) ASP
C) CGI
D) STL
 
Question 2:
If you're using the J2ME libraries, what kind of hardware is your program probably going to run on?
A) Mainframe computers
B) Mobile computers
C) Medium-sized desktop computers
D) Enterprise servers
 
Question 3:
What kinds of functionality of an ordinary programming editor aren't found in a simple text editor, such as
Notepad? (Check all that apply.)
A) Erase feature
B) Syntax color-coding
C) Project organization
D) Tool-running capability
 
Question 4:
True or False: Adobe Flash is a type of dynamic Web page technology.
A) True
B) False
 
Question 5:
Which of the following are examples of IDEs? (Check all that apply.)
A) Visual Studio
B) ASP
C) CodeWarrior
D) WebSphere
 

© 2003 - 2010 Powered, Inc.

Potrebbero piacerti anche