header

Overview of Programming

Truth be told, computer programming is a complex activity and many people find it difficult. Programming involves many different aspects; all of which must be learned, more or less, at the same time. The purpose of this overview is to give you a general idea of the kinds of skills you must have in order to be a computer programmer.

Topics

Mastery of the Language

Perhaps the most obvious skill needed by a programmer is mastery of the programming language. In many respects, learning a programming language is similar to learning any other language. First you must learn the vocabulary of the language; the "words" from which sentences or statements are constructed.

Second you must learn the syntax of the language; the grammar rules that dictate how words and punctuation are used to form grammatically correct statements. Unlike most languages, computer languages require the strictest possible level of adherence to the syntax rules. A single misspelled word or punctuation error makes the entire statement unintelligible.

Finally, you must understand the semantics of the statements; what they mean. When executing a program, the computer does exactly what it is told to do; nothing more and nothing less. It is the job of the programmer to make sure that the program statements mean what he or she intended them to mean.

Life Cycle of a Program

The life cycle of a program consists of five phases: design, write, test, document, and maintain. In the design phase, you clarify the program specifications, determine how your program will be organized, and choose the appropriate data structures. The program specifications include

At first it may seem backwards to begin with the output requirements. However, you can't even begin to think about input and process until you understand what the output is to be. To use an example from the kitchen, you must know what it is you want to make before you look up the recipe to get the list of ingredients and the steps needed to transform those ingredients into a tasty dish.

Once the design is complete, you write and test the program. The preferred process is to write a portion of the program and then test it to make sure it works properly. Then, write a little more, and test the new code. This incremental process continues until the entire program has been written and tested.

In a beginning course, like this, students really never make it past these first three phases. However, in a more realistic environment, there are two more phases. In the documentation phase, documents like the reference manual and user's guide are written. During the maintenance phase, uncorrected errors are fixed and the program is enhanced with new or improved features.

The Programming Environment

The programming environment is comprised of the software tools used to write, compile, and test your computer programs. The simplest environment consists of a text editor and a compiler. The text editor is used to edit the source code; the program written in a high-level language (Java, in our case). The source code is stored as an ordinary text file; usually with an extension that identifies the language in which it was written. Java source code files are stored as ordinary text files with a "java" extension.

The compiler checks your program for syntax errors, and translates the source code into executable code that can be run on your computer. For many languages, the executable code consists of machine language instructions that can be executed directly by the central processing unit of your computer. In Java, however, the executable code (also known as Java bytecode) is run by the Java virtual machine which is a program that simulates a computer that can run Java's machine language instructions. There is a Java virtual machine for each operating system that supports Java and your Java programs should run substantially the same way on any of those systems.

Often, a debugger is available to help find errors (bugs) in your program. The debugger allows you to execute instructions one at a time and provides access to your data allowing you to more easily track down and eliminate logic errors.

For most languages, there is also an integrated development environment (IDE) that includes all of these tools within a single program. In some ways, it is a little harder to learn how to use an IDE because it is more complex. However, once you have mastered the basics, an IDE actually simplifies the task of writing, compiling, and testing a program. In this course, you will be using the BlueJ IDE.

Primitive Data Types

Primitive data types are used to store a single value of a specific kind of data. The table below lists the primitive data types available in Java. A byte is the smallest addressable block of memory and consists of an 8-digit binary number (8 binary digits or 8 bits). A binary digit can have one of two values: 0 or 1.

Type Description Size in Bytes Example(s)
byte Small integer value 1 34
short Medium size integer value 2 8182
int Large integer value 4 2347862
long Really large integer value 8 485948594859
float Single-precision floating point value 4 3.14159
double Double-precision floating point value 8 3.14159265358979
char A single character 1 'a', 'A', '3', '.'
boolean A logical value   true, false

The integer data types (byte, short, int, and long) should be used to store numeric values that are inherently integers (such as the number of items in a list). The float and double types should be used to store values that are inherently decimal (floating point) numbers. The float type is a single precision value accurate to about 6 decimal digits while the double type is a double-precision value accurate to about 15 decimal digits. While seldom of practical importance, the double-precision data type can also represent a larger range of values than the single-precision data type.

Use the Java applet below to investigate the range of values that can be stored in each numeric data type. An integer type can store any integer within its range. Mathematically, there are an infinite number of floating point values within any given range. However, the float and double types can store only a finite number of different values within their respective ranges.

The floating point output in the applet is in exponential notation where the integer value following the 'E' is the power of 10. For example, 3.4028235E38 means 3.4028235 x 1038.

Your browser is ignoring the <APPLET> tag!

See the data types quick reference for a summary of data types.

Data Structures and Classes

A data structure is used to organize a collection of data elements. For example, an array is used to store multiple values of the same type. A one-dimensional array is used to store a list of values and a two-dimensional array is used to store a table of values. Higher dimensional arrays are also possible.

A class can be used to store multiple values which may be of the same type or of different types. However, classes can do far more than just store multiple values. They usually implement methods that manipulate the data as well.

Variables and Constants

A variable is a data element whose state can be observed and/or altered as the program executes. In general, the state of a variable consists of the values of its components (keeping in mind that a primitive data type has only one component). Each variable must be declared and initialized before it can be used. A variable declaration specifies the variable's data type and its name (identifier). A variable is initialized by assigning values to each of its components. In Java, a variable can be initialized when it is declared or later on; whichever the programmer prefers.

A named constant is a data element whose state can be observed but not altered. A constant declaration specifies the constant's data type, identifier, and state. Following its declaration, the state of a constant cannot be altered.

An unnamed constant is a constant with no corresponding identifier. In essence, the state of an unnamed constant is observed just once.  A literal constant is a specific value for a primitive data type and is an example of an unnamed constant. For example, the integer 324 and the character 'a' are literal constants.

See the data declarations quick reference for summary information on declaring data elements.

Control Structures

Control structures determine the sequence in which program instructions are executed. In a sequential structure, instructions are executed in order; one after the other. In a selection structure, the program determines which block of code to execute next based on its current state. In a looping structure, the program repeatedly executes the same block of code. See the control structures page for a more complete description of Java control structures.

Algorithm Development

One of the most challenging (and satisfying) aspects of computer programming is developing an algorithm (a sequence of steps) to perform a specific task. For example, given a list of floating point values, how does one go about sorting them? Or, given an integer value, how does one display that value with the proper grouping (e.g., commas every third digit)?

User Interface

The user interface refers to the way in which the user interacts with a computer program during its execution. A command line interface is a text-based interface in which the program prompts the user for input and the user supplies the necessary data via the keyboard. The application initiates the interaction and the user responds appropriately.

A graphical user interface uses graphic elements such as text boxes and buttons to allow the user to interact with the application. The user initiates the interaction and the application responds appropriately.