- •About the Author
- •Dedication
- •Author’s Acknowledgments
- •Contents at a Glance
- •Table of Contents
- •Introduction
- •Who Should Buy This Book
- •How This Book Is Organized
- •Part I: Programming a Computer
- •Part II: Learning Programming with Liberty BASIC
- •Part III: Advanced Programming with Liberty BASIC
- •Part VI: Internet Programming
- •Part VII: The Part of Tens
- •How to Use This Book
- •Foolish assumptions
- •Icons used in this book
- •Why Learn Computer Programming?
- •How Does a Computer Program Work?
- •What Do I Need to Know to Program a Computer?
- •The joy of assembly language
- •C: The portable assembler
- •High-level programming languages
- •Database programming languages
- •Scripting programming languages
- •The program’s users
- •The target computer
- •Prototyping
- •Choosing a programming language
- •Defining how the program should work
- •The Life Cycle of a Typical Program
- •The development cycle
- •The maintenance cycle
- •The upgrade cycle
- •Writing Programs in an Editor
- •Using a Compiler or an Interpreter
- •Compilers
- •Interpreters
- •P-code: A combination compiler and interpreter
- •So what do I use?
- •Squashing Bugs with a Debugger
- •Writing a Help File
- •Creating an Installation Program
- •Why Learn Liberty BASIC?
- •Liberty BASIC is easy
- •Liberty BASIC runs on Windows
- •You can start using Liberty BASIC today
- •Installing Liberty BASIC
- •Loading Liberty BASIC
- •Your First Liberty BASIC Program
- •Running a Liberty BASIC program
- •Saving a Liberty BASIC program
- •Getting Help Using Liberty BASIC
- •Exiting Liberty BASIC
- •Getting input
- •Displaying output
- •Sending Data to the Printer
- •Storing Data in Variables
- •Creating a variable
- •Assigning a value to a variable
- •Declaring your variables
- •Using Constants
- •Commenting Your Code
- •Using variables
- •Working with precedence
- •Using parentheses
- •Manipulating Strings
- •Declaring variables as strings
- •Smashing strings together
- •Counting the length of a string
- •Playing with UPPERCASE and lowercase
- •Trimming the front and back of a string
- •Inserting spaces
- •Yanking characters out of a string
- •Looking for a string inside another string
- •Using Boolean Expressions
- •Using variables in Boolean expressions
- •Using Boolean operators
- •Exploring IF THEN Statements
- •IF THEN ELSE statements
- •Working with SELECT CASE Statements
- •Checking a range of values
- •Checking a relational operator
- •Boolean expression inside the loop
- •Looping a Fixed Number of Times
- •Counting with different numbers
- •Counting in increments
- •Anatomy of a Computer Bug
- •Syntax Errors
- •Fun with Logic Errors
- •Stepping line by line
- •Tracing through your program
- •Designing a Window
- •Creating a new window
- •Defining the size and location of a window
- •Adding color to a window
- •Putting Controls in a Window
- •Creating a command button
- •Displaying text
- •Creating a check box
- •Creating a radio button
- •Creating text boxes
- •Creating list boxes
- •Creating combo boxes
- •Creating group boxes
- •Storing Stuff in Text Files
- •Creating a new text file
- •Putting stuff in a text file
- •Adding new stuff to an existing text file
- •Retrieving data from a text file
- •Creating a new binary file
- •Saving stuff in a binary file
- •Changing stuff in a binary file
- •Retrieving stuff from a binary file
- •Creating a Graphics Control
- •Using Turtle Graphics
- •Defining line thickness
- •Defining line colors
- •Drawing Circles
- •Drawing Boxes
- •Displaying Text
- •Making Sounds
- •Making a beeping noise
- •Playing WAV files
- •Passing Data by Value or by Reference
- •Using Functions
- •Defining a function
- •Passing data to a function
- •Calling a function
- •Exiting prematurely from a function
- •Using Subroutines
- •Defining a subroutine
- •Passing data to a subroutine
- •Calling a subroutine
- •Exiting prematurely from a subroutine
- •Writing Modular Programs
- •Introducing Structured Programming
- •Sequential instructions
- •Branching instructions
- •Looping instructions
- •Putting structured programming into practice
- •The Problem with Software
- •Ways to Make Programming Easier
- •Breaking Programs into Objects
- •How to use objects
- •How to create an object
- •Creating an object
- •Starting with a Pointer
- •Defining the parts of a linked list
- •Creating a linked list
- •Managing a linked list
- •Making Data Structures with Linked Lists
- •Stacks
- •Queues
- •Trees
- •Graphs
- •Creating a Record
- •Manipulating Data in Records
- •Storing data in a record
- •Retrieving data from a record
- •Using Records with Arrays
- •Making an Array
- •Making a Multidimensional Array
- •Creating Dynamic Arrays
- •Insertion Sort
- •Bubble Sort
- •Shell Sort
- •Quicksort
- •Sorting Algorithms
- •Searching Sequentially
- •Performing a Binary Search
- •Hashing
- •Searching by using a hash function
- •Dealing with collisions
- •Picking a Searching Algorithm
- •Choosing the Right Data Structure
- •Choosing the Right Algorithm
- •Put the condition most likely to be false first
- •Put the condition most likely to be true first
- •Clean out your loops
- •Use the correct data types
- •Using a Faster Language
- •Optimizing Your Compiler
- •Programming Computer Games
- •Creating Computer Animation
- •Making (And Breaking) Encryption
- •Internet Programming
- •Fighting Computer Viruses and Worms
- •Hacking for Hire
- •Participating in an Open-Source Project
- •Niche-Market Programming
- •Teaching Others about Computers
- •Selling Your Own Software
- •Trying Commercial Compilers
- •Windows programming
- •Macintosh and Palm OS programming
- •Linux programming
- •Testing the Shareware and
- •BASIC compilers
- •C/C++ and Java compilers
- •Pascal compilers
- •Using a Proprietary Language
- •HyperCard
- •Revolution
- •PowerBuilder
- •Shopping by Mail Order
- •Getting Your Hands on Source Code
- •Joining a Local User Group
- •Frequenting Usenet Newsgroups
- •Playing Core War
- •Programming a Battling Robot
- •Toying with Lego Mindstorms
- •Index
- •End-User License Agreement
Chapter 19: Playing with Object-Oriented Programming 263
void monster::moveme(int new_x, int new_y)
{
x_coordinate = x_coordinate + new_x; y_coordinate = y_coordinate + new_y;
}
void monster::initialize(int init_x, int init_y)
{
x_coordinate = init_x; y_coordinate = init_y;
}
The initialize method defines the X coordinate and Y coordinate of any object that you derive from the class monster. You use the moveme method to move an object’s position a certain distance in the X (right and left) direction and Y (up and down) direction.
Creating an object
After you define a class and write any methods that you declare within that class, you still need to define a variable to represent that class. This variable represents the actual object in object-oriented programming.
To create an object, you declare a variable to represent that object. The following program defines an object named zombie to represent the monster class definition:
#include <iostream.h> class monster
{
public:
int x_coordinate; int y_coordinate;
void moveme(int, int); void initialize(int, int); };
void monster::moveme(int new_x, int new_y)
{
x_coordinate = x_coordinate + new_x; y_coordinate = y_coordinate + new_y;
}
void monster::initialize(int init_x, int init_y)
{
x_coordinate = init_x; y_coordinate = init_y;
}
void main()
{
264 Part IV: Dealing with Data Structures
monster zombie; zombie.initialize(12, 15);
cout << “The X-location of the zombie is “ << zombie.x_coordinate << “\n”;
cout << “The Y-location of the zombie is “ << zombie.y_coordinate << “\n”;
zombie.moveme (34, 9);
cout << “The new X-location of the zombie is “ << zombie.x_coordinate << “\n”;
cout << “The new Y-location of the zombie is “ << zombie.y_coordinate << “\n”;
}
The main C++ program starts with the void main() line. Just examining this portion, line by line, makes the computer do the following:
1.The first line tells the computer, “This line is the start of the C++ program.”
2.The second line tells the computer, “This line is the start of all instructions inside the main C++ program.”
3.The third line tells the computer, “Create the object zombie and base it on the class definition monster.”
4.The fourth line runs the method initialize, which defines the X coordinate (12) and Y coordinate (15) of the zombie object.
5.The fifth line prints the message, The X-location of the zombie is 12.
6.The sixth line prints the message, The Y-location of the zombie is 15.
7.The seventh line runs the moveme method, which moves the zombie object 34 units of measurement in the X direction and 9 units in the Y direction.
8.The eighth line prints the message, The new X-location of the zombie is 46. (That’s 12, the original X-location of the zombie as Step 4 defines, plus 34 as Step 7 defines, which equals 46.)
9.The ninth line prints the message, The new Y-location of the zombie is 24. (That’s 15, the original Y-location of the zombie as Step 4 defines, plus 9 as Step 7 defines, which equals 24.)
10.The tenth line tells the computer, “This line represents the end of the C++ main program.”
Although the C++ program may look confusing, just remember these important points and you’re fine:
To create an object, you must define a class first.
A class contains data and instructions (methods) for manipulating the object’s data.
Chapter 19: Playing with Object-Oriented Programming 265
Common terms associated with object-oriented programming
Although you aren’t likely to become an expert in object-oriented programming from this brief introduction, you can at least understand the purpose behind objects, which is to make modifying and reusing programs easier.
If you plan to continue studying computer programming, you’re likely to run into object-ori- ented programming over and over (or at least until a new programming methodology pops up to make programming easier). To add to your limited knowledge of object-oriented programming, remember the following common objectoriented terms that you’re likely to see at one time or another:
Encapsulation means lumping all related data and instructions to manipulate that data in a single location.
Inheritance means passing data and instructions from one object to a second object that derives from the first object.
Message is a subprogram (also known as a method ) that manipulates the data inside an object.
Object is a collection of data and instructions for manipulating that data, grouped together in a self-contained unit.
Choosing an Object-Oriented Language
After you understand the general principles behind object-oriented programming, you may be eager to start trying it out on your own. Liberty BASIC doesn’t support object-oriented programming, so you need to switch to a different programming language. The two types of programming languages that you can choose from are as follows:
Hybrid object-oriented languages
True (or pure) object-oriented languages
A hybrid object-oriented language simply takes an existing language and slaps object-oriented features on top of it. Some of the more popular hybrid languages include Pascal (as implemented in Delphi), BASIC (as implemented in Visual Basic), and C++.
The main advantage of using a hybrid language is that if you already know how to use a language such as Pascal, BASIC, or C++, you can quickly learn to use the object-oriented features of these languages with a minimum of training, anguish, and experimentation. If you’re not quite sure about the benefits
266 Part IV: Dealing with Data Structures
of object-oriented programming, you can write a small part of your program using objects and write the bulk of your program using old-fashioned programming methods.
Of course, the main disadvantage of hybrid languages is that they enable programmers to mix traditional and object-oriented programming techniques, which can become an untidy mess. Hybrid languages enable programmers to use none, some, or all object-oriented programming techniques, so using hybrid languages often creates programs that don’t take full advantage of objects, destroying the advantage of using objects while making the program harder to read and understand.
That’s why many people prefer pure object-oriented languages that force you to use objects right from the start. Some popular pure object-oriented languages include the following:
SmallTalk
Eiffel
C#
Java
Whether you decide to stick to a conventional language (and use its objectoriented hybrid) or jump straight into a pure object-oriented language, get used to the idea behind breaking your program into objects. Object-oriented programming techniques alone don’t make software easier to write and more reliable, but they can make you more aware of the problems in writing software and how object-oriented programming can solve those problems.
In the long run, nobody really cares what language you use, whether you use any object-oriented programming techniques, or whether you write software while sitting in your underwear and listening to Barry Manilow albums at
3 a.m. The important point is to write software on time that works. If you can do that, you can focus on producing results and leave trivial details, such as wearing a tie, dealing with corporate politics, and fighting each other for a cubicle near a window, for your co-workers to worry about.
Part V
Algorithms: Telling
the Computer
What to Do
In this part . . .
Aprogram is nothing more than a list of instructions, but you can create instructions in various ways to
tell the computer how to perform the same task. If you want to give directions to tell someone how to get from the airport to your house, for example, you probably can tell that person two or three different ways. Each way eventually gets the person to your house, but one way may prove easier, another way may prove faster during the day, and the third way may prove more scenic.
In the world of computer programming, a specific way to accomplish a task is known as an algorithm. By choosing the fastest set of instructions (the algorithm), you can make your program faster and more efficient. This part of the book introduces you to several common algorithms for accomplishing different tasks so that you can understand the foundation on which you build most programs.
Chapter 20
Sorting
In This Chapter
Sorting data in many ways
Picking a sorting algorithm
Programs typically accept data from the outside world (such as from someone typing on the keyboard), manipulate that data somehow, and
spit that data back out in a format that someone finds useful.
A database is fairly useless if it enables you to store information without enabling you to do anything to rearrange that information. You may, for example, want to rearrange your data alphabetically by last name, numerically by telephone area code, or by some other criterion, such as by those people who’re single and earn $75,000 or more every year. Your program needs to know how to sort data.
Although sorting may seem a fairly mundane topic, it can actually get rather complex. That’s because whenever a program sorts data, it needs to sort the information as quickly as possible. After all, a program that sorts names and addresses is useless if it takes three hours just to sort 15 names.
So part of computer science centers on studying and developing the most efficient sorting methods (known as sorting algorithms) possible. Because many types of programs need to sort data, nearly every programmer needs to know the different sorting algorithms available and how they work. Throughout this chapter, you can type various Liberty BASIC programs and see for yourself exactly how a particular sorting algorithm does its sorting magic.
Computer scientists have created a variety of sorting algorithms — but no single, perfect algorithm that your program should use all the time. The most efficient algorithm depends partly on the data that you want to sort and partly on the data structures that your program uses to store data.