Programming Objects

  1. Introduction
  2. A BASIC Comparison
  3. JavaScript Example

Introduction

On the most fundamental level, objects are chunks of built in code that you can use at any time with no need to write code on your own. Objects are well suited to the event driven interfaces we have become accustomed to. Events are actions that a user takes, such as clicking a button. Event oriented programs allow the user to click a button at any time. Line oriented programming first prompts the user and then waits for a response. Event driven programming is more of a philosophy than a result of some programming language. It is possible to create event driven programs with line oriented programming, but it is very difficult. Because of this difficulty, line oriented programs most often lead the user step by step. Keep in mind, however, it is up to the programmer just how much interaction versus linearity exist in a program. The age of event driven programs was ushered in by the Mac and is now the source of the Web’s popularity.

The first step to Java programming is understanding what objects, in programming, are all about. There are numerous strict definitions of what makes up objects in object oriented programming, but for the sake of simplicity, I will take a more general look at programming objects and object-based programming.

Objects, or the code they represent, are often part of the OS (operating system) and can be activated through the API (Application Program Interface). Java’s advantage is that the API for Java programs is the same no matter what computer or OS is being used to run the Java program (see the last column in this series for more details). Java is by no means the only programming language that uses objects. VisualBASIC first popularized what has become the standard programming approach to visual programming. It is a good place to start by contrasting the traditional BASIC, we looked at in previous columns, with a more object-based BASIC.

A BASIC Comparison

The code in Listing 1 is a sub for creating a box on the screen when using traditional BASIC. Every time a box is needed, this sub is called with the parameters of where to locate the box and the dimensions of the box. Since BASIC does not have a clue of what boxes are or what they do, this code is required for everything from dialog boxes to buttons. In contrast, an object programming language has already defined different types of boxes. Figure 1 shows a button from the programming language VisualBASIC. Rather than writing code, the button is simply dropped onto the program form.

To the right of the button is the properties list window. The properties windows is split into two parts. On the left half is a list of all the individual properties, of the button just drawn, which can be modified. On the right half are the corresponding present values of the properties. For example, the code name given to the button in Figure 1 is command 1, while the caption (the text actually shown on the button) is A Button. Any of the properties can easily be changed during this design stage.

Listing 1. A button in BASIC

SUB DrawBox (row1%, col1%, row2%, col2%)
wide% = col2% - col1% - 1
'Across the top
LOCATE row1%, col1%, 0
PRINT CHR$(201);
PRINT STRING$(wide%, 205);
PRINT CHR$(187);
'Down the sides FOR row3% = row1% + 1 TO row2% - 1
LOCATE row3%, col1%, 0
PRINT CHR$(186);
PRINT SPACE$(wide%);
PRINT CHR$(186);
NEXT row3%
'Across the bottom
LOCATE row2%, col1%, 0
PRINT CHR$(200);
PRINT STRING$(wide%, 205);
PRINT CHR$(188);
END SUB

Figure 1. A button in VisualBASIC

While it is clear that the visual programming technique saves time in coding, this is not the most important feature. When a program executes, any of the initially defined properties for the button in Figure 1 can be changed by simply referring to it. For example, the caption could be changed from A Button to OK with the code:

command1.caption = “OK”

With this line of code, the button’s caption will change on screen immediately. In traditional BASIC, all the code to draw the screen would have to be run again with a few changes to update the button’s caption. In object-based programming, the opportunity for programming errors is greatly reduced as the amount of code the programmer must deal with is reduced. In Java programming, objects and properties always go together with the object name followed by a period and then the property name.

To get the feel for object programming, we can try some JavaScript in an HTML document. Listing 2 is everything you need to get started. Simply copy every line into an empty text document. Make sure to save the document as text only and name the file caell.html (or any name you like, but include the ending .html). After saving the file, open your Java compatible Web browser and check out the results by opening the file through the File, Open menu choices (both Netscape and Explorer are acceptable). Comments in the code are preceded by: // and are not actual JavaScript code.

Listing 2. Web page using JavaScript objects example

1. Jack and Jill up the hill.