Computer Science Java Style
Introduction
Fundamental Notions and First Constructs

Opening Definitions and Cornerstone Conceptions

The definitions presented in "Computer Science Java Style" tend to be slanted. Firstly, they are slanted in both language and conception towards object-oriented programming. Secondly, they draw on the subject matter of Cognitive Science. These leanings are consistent in spirit and substance with my main goal, that being to present a direct course of study in object-oriented programming and object oriented problem solving.

What is a Computer?

A computer is an object (machine) which can be programmed to behave according to specification when sent a particular message. In short, a computer is a general purpose object. A computer can, for example, be programmed to perform account activation, to perform grade computation, to play chess, or to compose music.

What is a Program?

A program is a finite specification of a computation. Consider the following sequence of instructions.
  1. Choose an arbitrary positive integer, A.
  2. write down A.
  3. if A = 1, then stop.
  4. if A is even, then replace a by A / 2 and go to step 2.
  5. if A is odd, then replace a by 3A + 1 and go to step 2.
This is a program, a finite specification of a computation, which generates a sequence of numbers for any value of A that you choose in step 1. It is conjectured that the computation, as well as its specification, is finite. Evidently, this has yet to be proven. This "program" is the original specification of the set of sequences provided by Lothar Collatz [1]. The notation in which a program is specified is called a
programming language. Accordingly, the 5 step "text" above could be considered a program in some (largely unspecified) programming language. In passing, and knowing that you are not in a position to fully appreciate it, you might enjoy observing a Java program which describes this computation.
public void collatz(int a)
{
   IO.println(a);
   if ( a == 1 ) { return; }
   if ( Predicates.even(a) )
   {
      collatz(a/2);
   }
   else
   {
      collatz(3*a+1);
   };
}
Also in passing, and for the sake of subsequent discussion, you might enjoy taking a look at the Lisp equivilent.
( defun collatz ( a )
   ( println a )
   ( if ( = a 1 ) ( return-from collatz nil ) )
   ( if ( evenp a )
       ( collatz ( / a 2 ) )
       ( collatz ( + ( * 3 a ) 1 ) ) )
   )
)
Lisp, which made its initial appearence in 1959, is a vital programming language, classic in every sense, a language which continues to significantly influence programming language evolution. In Chapter 8 I craft a Lisp interpreter in Java, largely as a study in hierarchical class design. For now, I am merely interested in making a a statement about programs and programming languages.

A View of Computer Programming

On the surface, when viewed as just a sequences of tokens, the programs appear quite different. A programming language token is a sequence of characters which forms a meaningful unit (word, number, puctuation symbol, etc) of the programming language. Even at a glance, however, you may be able to appreciate the fact that under the surface, structurally, they are quite similar. When people speak of beauty in programming, they speak largely of the structural beauty of programs, which is intimately related to mechanisms of communication. Programming can be compellingly fun if one has the opportunity to create beautiful programs which do interesting things. This book is largely dedicated to this proposition. By studying it you will come away with a substantial understanding of what a large number of Computer Scientists believe to be beauty in programming. I hope that you will take away from your experience the belief that programming is fun.

Computing in Clay

I would like to present one more, very small, example of a program before embarking upon a presentation of the Java language. This program plays a "major scale", up one octave and then back down.
scale = up, down.
up = [ play, rp ] * 8.
down = [ lp, play ] * 8.
The program is written in a very simple programming language, a subset of a much larger language called Clay.
Clay is an object-oriented programming language expressly designed for use in the study of problem solveing and learning. In later chapters we will implement the various simple subsets of Clay which are designed to be used in the context of "global objects with local state". Subsets of Clay designed to be used in the context of global objects with local state are termed Colored Clay languages. One of the global objects that has relevance to the above program is an "underlying note", modelled in terms of a pitch (an abstract pitch class) and a duration. Another is the "scale". (The commands rp and lp perform the actions of raising and lowering the pitch of the underlying note within the scale.)

There are three primary reasons that I have chosen to devote a number of chapters to the study of a programming model of such simplicity. First, it provides a small, self contained opportunity to discuss issues of programming language specification and interpretation. Second, it provides a wealth of opportunity for modelling interesting objects, sonic objects, visual objects, objects of chance, etc. Third, the simplicity provides a perspecitive from which to easily study various conceptual aspects of programs in a formal manner.

What is Object-Oriented Programming?

Returning to more immediate matters, Java is an object-oriented programming language recently developed at SUN Microsystems. An object-oriented programming language is a programming language which directly supports object-oriented programming. The central theme of this book is an exploration of object-oriented programming in the context of Java.

Consequently, I would like to present a couple of definitions of "object-oriented programming" for you to consider before moving on to a pragmatic introduction to the Java language. The first definition, caste in problem solving terms, is very informal.

Object-oriented programming is a programming methodology which features naturalness of expression. It strives to (1) faithfully model the essential structural and behavioral features of the objects of a problem domain, and (2) engage the modelled objects in interactions which result in problem solution.
The second definition, given in Grady Booch's words, is a classic technical definition.
Object-oriented programming is a programming methodology in which (1) programs are organized as cooperative collections of objects, (2) each object represents an instance of some class, and (3) each class is a member of a hierarchy of classes united via inheritance relationships.
It is sometimes helpful to establish goals when embarking upon a new study. Rendering these definitions meaningful, in the course of the next several chapters, might be one goal that you would like to adopt.

Craig Graci
Last modified: Tue Jan 21 12:48:51 EST