Nothing Special   »   [go: up one dir, main page]

Hour 1. Becoming A Programmer

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

Hour 1.

Becoming a Programmer
Computer programming is insanely difficult. It requires a four-year degree in computer science,
thousands of dollars in computer hardware and software, a keen analytical intellect, the
patience of Job, and a strong liking for caffeinated drinks. If you're a programming novice, this is
probably what you've heard about computer programming. Aside from the part about caffeine,
all of the rumors are greatly exaggerated.

Programming is a lot easier than most people think, although there are several reasons why you
might believe otherwise:

 Computer programmers have been telling people for years that programming is hard.
This belief makes it easier for us to find high-paying jobs (or so I've heard), and gives us
more leeway to goof off during business hours.
 Computer programming manuals are often written in a language that only a Scrabble
player could appreciate. Strange acronyms like OOP, RAD, COM, and MUMPS are used
frequently along with newly invented jargon like instantiation, bytecode, and makefile.
 Many computer programming languages have been available only with software
packages costing $200 or more, which is a lot of cabbage.

Because of the growth of the Internet and other factors, this is a great time to learn
programming. Useful programming tools are being made available at low cost (or no cost), often
as downloads from World Wide Web sites. Thousands of programmers are distributing their
work under "open source" licenses so people can examine how the programs were written,
correct errors, and add their own improvements.

The goal of this book is to teach programming to the person who has never tried to program
before, or the person who tried programming but hated it with an intense passion. The English
language will be used as much as possible instead of jargon and obscure acronyms, and all
new programming terms will be thoroughly explained as they are introduced.

If I've succeeded, you will finish Sams Teach Yourself Java 2 in 24 Hours with enough
programming skill to be a danger to yourself and others. You'll be able to write programs, dive
into other programming books with more confidence, and learn programming languages more
easily. You also will have developed skills with Java, the most exciting programming language
to be introduced in a decade.

The first hour of this book provides some introductory material about programming and gives
you instructions on how to set up your computer so you can write Java programs. The following
topics will be covered:

 Choosing which programming language to learn first


 What Java is
 Using programs to boss your computer around
 How programs work
 How program errors (called bugs) are fixed
 Acquiring a Java development tool
 Getting ready to write programs
Choosing a Language
As you might have surmised at this point, computer programming is not as hard as it's cracked
up to be. If you're comfortable enough with a computer to create a nice-looking resume, balance
a checkbook with software such as Intuit Quicken, or create your own home page on the Web,
you can write programs.

The key to learning how to program is to start with the right language. The programming
language you choose often depends on the tasks you want the computer to accomplish. Each
language has things it is well-suited for, as well as things that are difficult—perhaps impossible
—to do with the language. For example, many people use some form of the BASIC language
when they are learning how to program because BASIC was created with beginners in mind.

Note

The BASIC language was invented in the 1960s to be easy for students and beginners to learn
(the B in BASIC stands for Beginner's). The downside to using some form of BASIC is that it's
easy to fall into some sloppy programming habits with the language. Those habits can make it
much more difficult to write complex programs and improve them later.

Microsoft Visual Basic combines the ease of BASIC with some powerful features to aid in the
design of Windows software. (VBScript, which is short for Visual Basic Script, offers the
simplicity of BASIC for small programs that run in conjunction with World Wide Web pages.)
Visual Basic has been used to write thousands of sophisticated programs for commercial,
business, and personal use. However, Visual Basic programs can be slower than Windows
programs written in other languages, such as Borland C++. This difference is especially
noticeable in programs that use a lot of graphics—games, screen savers, and the like.

This book covers the Java programming language, which was developed by Sun Microsystems.
Though Java is more difficult to learn than a language such as Visual Basic, it is a good starting
place for several reasons. One of the biggest advantages of learning Java is that you can use it
on the World Wide Web. If you're an experienced Web surfer, you have seen numerous Java
programs in action. They can be used to create animated graphics, present text in new ways,
play games, and help in other interactive efforts.

Another important advantage is that Java requires an organized approach for getting programs
to work. The language is very particular about the way programs must be written, and it balks if
programmers do not follow all of its rules. When you start writing Java programs, you might not
see the language's choosy behavior as an advantage. You'll write a program and have several
errors to fix before the program is finished. Some of your fixes might not be correct, and they will
have to be redone. If you don't structure a program correctly as you are writing it, errors will
result. In the coming hours, you'll learn about these rules and the pitfalls to avoid. The positive
side of this extra effort is that your programs will be more reliable, useful, and error-free.

Java was invented by Sun Microsystems developer James Gosling as a better way to create
computer programs. Gosling was unhappy with the way that the C++ programming language
was working on a project he was doing, so he created a new language that did the job better.
It's a matter of contentious debate whether Java is superior to other programming languages, of
course, but the amount of attention paid to the language today shows that it has a large number
of adherents. Book publishers obviously dig it—more than 1,000 books have been published
about the language since its introduction. (This is my tenth, and I will keep writing more of them
until prohibited by municipal, state, or federal law.)

Regardless of whether Java is the best language, it definitely is a great language to learn today.
There are numerous resources for Java programmers on the Web, Java job openings are
offered in many cities, and the language has become a major part of the Internet's past,
present, and future. You'll get a chance to try out Java during Hour 2, "Writing Your First
Program."

Learning Java or any other programming language makes it much easier to learn subsequent
languages. Many languages are similar to each other, so you won't be starting from scratch
when you dive into a new one. For instance, many C++ programmers find it fairly easy to learn
Java, because Java borrows a lot of its structure and ideas from C++. Many programmers are
comfortable using several different languages and will learn new ones as needed.

Note

C++ is mentioned several times in this hour, and you might be tripping over the term wondering
what it means, and more importantly, how it's pronounced. C++ is pronounced C-Plus-Plus, and
it's a programming language that was developed by Bjarne Stroustrop and others at Bell
Laboratories. C++ is an enhancement of the C programming language, hence the Plus-Plus part
of the name. Why not just C+, then? The Plus-Plus part is a computer programming joke you'll
understand later on.

Telling the Computer What to Do


A computer program, also called software, is a way to tell a computer what to do. Everything
that the computer does, from booting up to shutting down, is done by a program. Windows XP is
a program. Ms. Pac-Man is a program. The dir command used in MS-DOS to display file names
is also a program. Even the Klez email worm is a program.

Computer programs are made up of a list of commands the computer handles in a specific order
when the program is run. Each of these commands is called a statement.

If you're a science fiction fan, you're probably familiar with the concept of household robots. If
not, you might be familiar with the concept of henpecked spouses. In either case, someone
gives very specific instructions telling the robot or spouse what to do, something like the
following:

 Dear Theobald,
 Please take care of these errands for me while I'm out lobbying members of Congress:
 Item 1: Vacuum the living room.
 Item 2: Go to the store.
 Item 3: Pick up butter, lozenges, and as many bottles of Heinz E-Z Squirt green ketchup
as you can carry.
 Item 4: Return home.
 Love,
 Snookie Lumps
If you tell a loved one or artificially intelligent robot what to do, there's a certain amount of
leeway in how your requests are fulfilled. If lozenges aren't available, cough medicine might be
brought to you instead. Also, the trip to the store can be accomplished through a variety of
routes. Computers don't do leeway. They follow instructions literally. The programs that you
write will be followed precisely, one statement at a time.

The following is one of the simplest examples of a computer program, written in BASIC. Take a
look at it, but don't worry yet about what each line is supposed to mean.

1 PRINT "Shall we play a game?"


2 INPUT A$

Translated into English, this program is equivalent to giving a computer the following to-do list:

 Dear personal computer,


 Item 1: Display the question, "Shall we play a game?"
 Item 2: Give the user a chance to answer the question.
 Love,
 Snookie Lumps

Each of the lines in the computer program is a statement. A computer handles each statement
in a program in a specific order, in the same way that a cook follows a recipe, or Theobald the
robot followed the orders of Snookie Lumps when he vacuumed and shopped at the market. In
BASIC, the line numbers are used to put the statements in the correct order. Other languages,
such as Java, do not use line numbers, favoring different ways to tell the computer how to run a
program.

Figure 1.1 shows the sample BASIC program running on the Liberty BASIC interpreter, a
shareware program that can be used to develop Windows and OS/2 programs. Liberty BASIC,
which was developed by Carl Gundel, is among many BASIC interpreters that can be found on
the Internet for Microsoft Windows, Apple Macintosh, Unix, and Linux systems. You can find out
more about it at http://www.libertybasic.com.

Figure 1.1 An example of a BASIC program running in Liberty BASIC.

Note

The quote "Shall we play a game?" is from the 1983 movie WarGames, in which a young
computer programmer (portrayed by Matthew Broderick) saves mankind after nearly causing
global thermonuclear war and the near-extinction of humankind. You'll learn how to do that in
the next book of this series, Sams Teach Yourself to Create International Incidents with Java in
24 Hours.

Because of the way programs operate, it's hard to blame the computer when something goes
wrong while your program runs. After all, the computer was just doing exactly what you told it to
do. Unless your hardware is on the fritz, a pesky virus is attacking your system, or your
operating system is having a bad day, the blame for program errors lies with the programmer.
That's the bad news. The good news is that you can't do any permanent harm to your computer
with the programming errors you make. No one was harmed during the making of this book, and
no computers will be injured as you learn how to program with Java.

How Programs Work


Most computer programs are written in the same way that you write a letter—by typing each
statement into a word processor. Some programming tools come with their own word processor,
and others can be used with any text-editing software. If you don't already have a tool that can
be used for Java programming, you can use the free Java 2 Software Development Kit, which
you will learn about later in this hour, with any of your favorite editors.

When you have finished writing a computer program, you save the file just like saving any other
document to disk. Computer programs often have their own filename extension to indicate what
type of file they are. Java programs have the extension .java; an example of a Java program file
name is Calculator.java.

Note

If you use a fancy word processing program that has features such as bold-faced text, different
font sizes, and other stylistic touches, do not use those features while writing a computer
program. Programs should be prepared as text files with no special formatting. For example,
when using Microsoft Word to write a program, save the file in Text Only mode instead of saving
it as a Word document. Notepad, a word processor that comes with Windows, saves all files as
unformatted text. The vi editor on Linux systems can also be used to create text files without
formatting.

To run a program you have saved as a file, you need some help. The kind of help that's needed
depends on the programming language you're using. Some languages require an interpreter to
run their programs. The interpreter is a program that interprets each line of a computer program
and tells the computer what to do. Most versions of BASIC are interpreted languages. The
advantage of interpreted languages is that they are faster to test. When you are writing a BASIC
program, you can try it out immediately, spot any errors, fix them, and try again. The primary
disadvantage is that interpreted languages run more slowly than other programs.

Other programming languages require a compiler. The compiler takes a computer program and
translates it into a form that the computer can understand. It also does what it can to make the
program run as efficiently as possible. The compiled program can be run directly without the
need for an interpreter. Compiled programs run more quickly than interpreted programs, but
they take more time to test. You have to write your program and compile it before trying it out. If
you find an error and fix it, you must compile the program again to verify that the error is gone.

Java is unusual because it requires a compiler and an interpreter. You'll learn more about this
later as you write Java programs.

How Programs Don't Work


Many new programmers become discouraged when they start to test their programs. Errors
appear everywhere. Some of these are syntax errors, which are identified by the computer as it
looks at the program and becomes confused by what you wrote. Other errors are logic errors,
which are only noticed by the programmer as the program is being tested, if they are noticed at
all. Logic errors sneak by the computer unnoticed, but they will cause it to do something
unintended.

As you start to write your own programs, you will become well-acquainted with errors. They're a
natural part of the process. Programming errors are called bugs, a term that dates back a
century or more to describe errors in technical devices. The process of fixing errors has its own
term also: debugging. Whether you want to or not, you'll get a lot of debugging experience as
you learn how to write computer programs.

Next Stop: Java


Before you can start writing Java programs, you need to acquire and set up some kind of Java
programming software. Although several different products are available for the development of
Java programs, including many terrific ones that make programming much easier, the starting
place for most new Java programmers is the Software Development Kit, a set of tools for
writing, debugging, and running Java programs. All of the examples in this book were created
and tested using the Kit, but they can be created with any Java development tool that supports
the current version of the language.

The Software Development Kit (also referred to as the SDK) is in version 1.4 as of this writing.
Whenever Sun releases a new version of Java, the first tool that supports it is the Kit.

To create all of the programs in this book, you must either use Software Development Kit 1.4 or
another Java programming tool that fully supports all of version 1.4's features.

There are many different software packages that offer the capability to create Java programs,
but all of these are not created equally when language support is concerned. Some of these
programming tools only support Java 1.0, the initial version of the language, which was released
by Sun Microsystems in late 1995. Other tools support Java 1.1, which was released in mid-
1997, version 1.2 from 1998, or version 1.3 from 2000.

Some operating systems such as Mac OS X and Red Hat Linux 7.1 include a copy of the SDK,
but it's probably not the current version. The kit in OS X supports version 1.3 of Java and the
one in Red Hat 7.1 supports version 1.1. Before using an SDK that was included with your
operating system, make sure it supports Java version 1.4.

Users of Microsoft Windows systems may be dismayed to learn that the Software Development
Kit is not graphical. You run programs from a command line (the C:\> or D:\> prompt that will be
familiar to MS-DOS users) instead of using a mouse and a point-and-click environment. Figure
1.2 shows the Kit in use in an MS-DOS window on a Windows 2000 system. The Java program
WarGames.java is compiled, and then it is run.
Figure 1.2 A program being compiled and run with the Software Development Kit.

Note

The examples in this book were prepared on the Microsoft Windows XP and Red Hat Linux
operating systems. For this reason, some advice is offered for Windows and Linux users
running the Software Development Kit. However, all of the Java code in this book will work
regardless of the operating system you are using, as long as you have development software
that supports all the features of SDK 1.4.

Official Documentation

Sun Microsystems offers comprehensive documentation for the Java language in Web page
format. You don't need this information to use this book because each topic is discussed fully as
it is introduced, but these pages will come in handy when you write your own programs.

You can download the entire documentation, but it might be more convenient to browse it as
needed from Sun's Web site. The most up-to-date Java documentation is available at
http://java.sun.com/j2se/1.4/docs.

Workshop: Installing a Java Development Tool


Every hour of this book ends with a workshop, a Java programming project you can undertake
to enhance your knowledge of the subject matter while it is percolating in your brain.

However, you won't be doing any Java programming at all until you install a Java development
tool on your computer.

If you have a program such as Borland JBuilder, IntelliJ IDEA, Sun ONE Studio, or MetroWerks
CodeWarrior, you can use that to develop the tutorial programs in the next 23 hours. However,
you should already have some familiarity with how to use the program—learning to use Java
and a complex development tool at the same time can be daunting.

If you don't have a Java development tool, or the last paragraph scared you away from using
one that you own, you can use the Java 2 Software Development Kit, which is available for free
from Sun's Java Web site at http://java.sun.com.

Though the process is relatively straightforward, there are some configuration issues that cause
problems for many people trying to get started with the kit, especially on a Windows system. To
find out how to download and install the kit and fix any configuration problems that arise, read
Appendix B, "Using the Java 2 Software Development Kit."

Summary
During this hour, you were introduced to the concept of programming a computer—giving it a
set of instructions that tell it what to do. You also might have downloaded and installed a Java
development tool that will be used as you write sample programs throughout the book.
If you are still confused about programs, programming languages, or Java in general, that's
understandable at this point. Everything will make more sense to you in the next hour, "Writing
Your First Program," which takes a slow trip through the process of creating a Java program.

Q&A
1. What is it about BASIC that makes it easier to fall into bad habits while writing
programs?

One thing you'll learn as you start writing Java programs is that you have to be
organized. If you don't structure your program in the correct way, it won't work. BASIC
doesn't have this kind of requirement. You can write in a disorganized manner and still
get the program to work successfully. Later on, however, you'll have a much harder time
figuring out how the program functions when you try to fix a bug or add an improvement.

2. BASIC? C++? Java? What are the names of these languages supposed to mean?

Like many programming languages, BASIC gets its name from an acronym that
describes what it is: Beginner's All Symbolic Instruction Code. C++ is a programming
language that was created to be an improvement on the C language, which itself was an
improvement of the B programming language. Java goes against the tradition of naming
a language with an acronym or other meaningful term. It's just the name that Java's
developers liked the best when brainstorming for possible monikers—beating out
WebRunner, Silk, and others.

3. Are there really more than 1,000 books about Java programming?

According to the official JavaWorld count, there are more than 2,400. The online Java
magazine, which is available at http://www.javaworld.com, maintains a guide to all
upcoming and in-print books related to Java and other Internet technology. This guide is
available at http://www.javaworld.com/javaworld/books/jw-books-index.html.

4. Why are interpreted languages slower than compiled ones?

For the same reason that a person interpreting a live speech is a lot slower than a
translator interpreting a printed speech. The live interpreter has to think about each
statement that's being made as it happens, while the other interpreter can work on the
speech as a whole and take some shortcuts to speed up the process. Compiled
languages can be much faster than interpreted languages because they can do things to
make the program more efficient.

5. Is C++ harder to learn than Java?

It's a matter of personal opinion, but Java does seem more approachable for beginners
than C++. C++ and its predecessor C are widely regarded as "programmer's languages,"
meaning that they were designed for the needs of experienced programmers. There are
a lot of features in C and C++ that make them faster—and more powerful—during
program creation, but these features often come at the expense of understandability.
Java takes a more simplified approach to programming than C++ and is probably a
better place to start.

6. I am not able to install the SDK from Sun because it's a huge 25 megabyte file.
How can I get the SDK?

Sun has offered the SDK on CD for the cost of postage in the past, but that appears to
have been discontinued. If you can't download the SDK over the Web, your best bet is
probably to acquire a book that contains SDK 1.4 on a CD. There are several, and one
of them is Sams Teach Yourself Java 2 in 21 Days Professional Reference Edition,
Third Edition. The book is a more expensive hardcover, but it also includes seven bonus
chapters on advanced Java topics. For more information, visit the Web site
http://www.java21pro.com.

Quiz
Test your knowledge of the material covered in this chapter by answering the following
questions.

Questions

1. Which of the following is not a reason that people think computer programming is
painfully difficult?
a. Programmers spread that rumor to improve their employment prospects.
b. Jargon and acronyms are all over the place.
c. Mind-control waves are sent out by the CIA promoting this belief.
2. What kind of tool runs a computer program by figuring out one line at a time?
a. A slow tool
b. An interpreter
c. A compiler
3. Why did James Gosling hole up in his office and create Java?
a. He was unhappy with the language he was using on a project.
b. His rock band wasn't getting any gigs.
c. When you can't download any MP3 files at work, the Internet is pretty dull.
Answers

1. c. Of course, the CIA could have forced me to say this.


2. b. Compilers figure out the instructions beforehand so the program can run faster.
3. a. At the time Gosling created Java, people thought that "MP3" was some kind of British
secret service designation.

Activities
If you'd like to better introduce yourself to the subjects of Java and computer programming, do
the following activities:

 Visit Sun's official Java site at http://java.sun.com/java2/whatis and read the company's
introduction to the Java technology.
 Using English sentences instead of a programming language, write a set of instructions
to add 10 to a number selected by a user and then multiply the result by 5. Break the
instructions into as many short one-sentence lines as you can.

To see solutions to the activities at the end of each hour, visit the book's Web site at
http://www.java24hours.com.

Activities

 Solution 1: Visit Sun's official Java site and read some of the Getting Started with Java
Technology pages.
 Solution 2: An example of instructions (in English) to handle the task:
o Dear personal computer,
 Item 1: Give the user a chance to select a number.
 Item 2: Add 10 to this number.
 Item 3: Multiply the resulting number by 5.
 Item 4: Display the final value of the number.
o Thanks,
o George Owens

You might also like