Welcome to the fascinating world of computer science and to the art and craft of computer programming. Like many modern disciplines, computer science is broad and includes a variety of different (and sometimes difficult) topics. Many students learn computer science and computer programming better by experimenting with the topics in a laboratory setting, just as students in chemistry, physics, and biology benefit from labs in those disciplines.
While computer science is not the same as computer programming, computer programs and computer programming help ground many of the central ideas of computer science, from architecture to computational complexity. As such, it is important for students interested in computer science to have some experience reading, writing, and analyzing computer programs.
The best computer scientists and computer programmers regularly ``play with'' new ideas they encounter, whether these new ideas be language structures, algorithms, machines, or even theorems. However, few novices have the intuition or skills for such experimentation. Hence, in many of the experiments in this manual, you will play with various aspects of the Java programming language, as well as a few concepts in computing. For example, you may change your program slightly and see what happens. You may also try some things that the language definition is (or seems to be) unclear about. As you gain more experience with such experiments, it is likely that you will find new ways to test new ideas.
This manual (and, presumably, your textbook and course) uses Java as the programming language, rather than one of the many other options, such as Ada, C, Pascal, C++, Scheme, or Haskell. Why? For a number of reasons. First, Java is one of the few truly platform-independent languages. A Java program written for one computer should run correctly and identically on almost any other modern computer. While programs written in other languages, such as Pascal and C, can be compiled on a variety of platforms, the same program will often behave differently on the different platforms because of inconsistencies in the designs of the compilers or machines. Second, the core of the Java language is relatively small, so that it is possible to learn all of the basics in a moderate number of laboratory sessions. Third, Java provides an extensive set of libraries, including standard libraries for graphical programs.
Different languages provide very different perspectives on programming and problem solving. In recent years, computer science educators have suggested that students should learn different paradigms early in their careers. Importantly, Java is both an object-oriented and an imperative programming language. Because Java is object-oriented, programmers can design Java programs that are collections of communicating objects. Because Java is imperative, it is possible to design programs or subprograms that look like traditional ``recipes'' in that they are a series of steps with some additional control. The exercises in the manual emphasize both aspects of the language.
Because Java has both imperative and object-oriented features, there are (at least) two approaches to teaching programming (and computer science) in Java. One can use an objects-first approach, in which objects are discussed and used as early as possible. Some claim that this helps students better understand and work with the object-oriented paradigm. One can also use an objects-late approach, in which objects are not introduced until students have mastered the basic imperative concepts, such as loops and conditionals. Some claim that this empowers students to work in a greater variety of language and helps ameliorate the sense of being overwhelmed that many students encounter in their introductory courses.
I will admit to being a proponent of the objects-first approach, and this lab manual reflects my preferences. As one reviewer notes, I use objects in the first program, even though they are not strictly necessary. However, I firmly believe that students better master the use of objects if they use them as early as possible.
The manual is separated into a number of parts. The first part presents the core of the Java language. These eight sessions will give students experience with the core features and principles of the Java programming language. While these sessions do not cover the complete language, they will give students enough experience and breadth to learn more on their own or in further courses. Importantly, these sessions should give student the skills to experiment with new concepts they encounter so that they can learn more than just what they are told.
Computer science is more than computer programming. To many computer scientists, algorithms form the core of computer science. The second part of the manual helps you explore some issues pertaining to algorithms. You will begin by considering recursion, an important technique for algorithm design. You will then visit a few important searching algorithms. Finally, you will conclude this part by considering some ways in which one might analyze algorithms.
In the final part a number of Java-oriented laboratory sessions cover additional topics not described in the core sessions. The additional topics include graphics, inheritance, and object design.
As you may know, Java supports at least two approaches to programming. In the application model, programs are written to operate by themselves without other supporting programs. (Of course, such applications still rely on an operating system and may communicate with other programs). In the applet model, programs are written to fit into other programs or documents. In particular, applets are often written for inclusion in Web pages.
This book primarily focuses on applications rather than applets. Why? As a teacher, I've found that students often encounter difficulty when dealing with the interaction between applets, the multiple versions of Java, and different mechanisms for running applets. For example, at the time I wrote this section, Java was at version 1.2 (at least on some platforms). However, some browsers, such as the standard version of Netscape Navigator 4.x, still seem to support only Java version 1.0. Unfortunately, the Java applet model changed significantly between Java 1.0 and Java 1.1, which means that applets written for the newer standards don't always work in older browsers. For that reason, I relegated Applets to an optional exercises and an optional laboratory (G1).
I am the first to admit that there are a number of successful strategies for teaching Java. Hence, at the suggestion of some reviewers, I have also added some applet-based exercises to sections J1--J5. These applet-based exercises replicate G1 (or vice versa), and you are discouraged from doing both. If you do applets as part of J1-J5 (the first session), you may find it necessary to be quite selective in which parts to do/assign, or you may wish to spread J1 and J2 over four laboratory sessions rather than two.
As you may have noted, each laboratory has four or five sections, including an introductory cover page. The cover page presents a short introduction to the laboratory. This page typically includes one or two paragraphs of introductory material. A list of new skills you should develop while completing this laboratory follows. These are not the only skills you will develop, but they are the focus of the particular laboratory session. Next comes a list of prerequisite skills. When beginning a lab, you should review this list and make sure you have developed those skills (or at least understand what those skills should be). Because it is appropriate and necessary for you to use and modify existing programs, there are a number of required files.
The introductory section is followed by a discussion of the topic or topics covered in the laboratory session. You should read the discussion section before coming to lab! In fact, this lab manual separates the discussion from the experiments to encourage you to read first. During lab times, you will most likely want to refer back to the discussion section for ideas and suggestions.
The discussion is followed by a series of experiments. These experiments are intended to help reinforce the discussion and to give you more experience with the concepts. There are typically more experiments than one can complete in a typical laboratory session. Your instructor will tell you which of the proposed experiments to perform. If you finish early, you may decide to do others. While there is no answer key to the experiments, it is likely that you will be able to determine the correct answer in most cases. In some cases, experiments raise difficult issues. In many cases, you will find notes and solutions for such problems at the end of the laboratory. You should only refer to these notes after you attempt the problem on your own.
Laboratories typically include many post-laboratory problems, which are intended to help you enhance your skills and understanding. Your instructor may assign some of these, or you may choose to do them on your own. You can think of the experiments as tools to help you develop your understanding of concepts and the post-laboratory problems as tools to exercise your understanding.
Finally, selected problems have notes. A few of the problems are particularly difficult or may have particularly important implications. For these problems, you will find notes at the end of the section.
How do you use this manual? Before you go to lab, read the appropriate section and scan the experiments. When you come to lab, do the experiments. After lab, go home, relax, think about what you've learned, and consider the post-laboratory problems. Finally, come back to lab (or to some computer) and do the post-laboratory problems. More importantly, have fun and learn!
All Java code and program output appear in a monospace font. Reserved words in the language are underlined to highlight them. Most comments are boldfaced to remind you that you should read comments before reading code. Comments that mark the end of a section are not boldfaced. Strings are italicized.
You may find that your editor or program development environment uses different choices (or perhaps none at all). Do not worry if programs are formatted differently when you type them in; the appearance is secondary to the underlying meaning and often out of control of the programmer.
As stated in the preface, this manual is intended to introduce you to a number of fundamental structures in Java. It is also intended to help you improve your understanding of these structures by encouraging you to experiment with them: trying variations of the same program or statement, testing limits, and otherwise experimentally exploring the meaning of the structures.
This manual is not a complete introduction to Java, to computer science, to structured or object-oriented design, or to the many other topics you will typically learn in an introductory computer science course. Your class and your textbook will provide additional information on everything from syntax (i.e., the way in which you write the various Java expressions) to philosophical underpinnings (e.g., why a language includes looping structures).
This manual is also intended to be ``textbook independent'', except for its choice to approach Java objects-first. That is, to permit a wider variety of classes to benefit from these experiments, they are independent of any particular textbook. This means that some language structures will be used in different ways than you are accustomed to, that the layout of programs may be different, and that topics may be covered in a different order. Part of your growth as computer scientist and computer programmer involves being able to learn from these different perspectives, and I hope you will find these differences one of the benefits of this manual.
Computer science laboratories can and should be exciting, interesting, and fun. However, successful learning and enjoyment do require you to do some work outside of the laboratory session. You will need to prepare for each laboratory session in advance of that session and to reflect on each laboratory session after the session.
How should you prepare for a laboratory session? You should begin by reading over the instructions for the laboratory. Most of these laboratories include a few pages of text that introduce the topic. As your time in the lab will be limited, it is essential that you read and understand these pages in advance. You may also need to read some sections of the text to understand the discussion. It is important that you read the text actively: when you encounter a question, make sure you think about the answer before reading on. It is very important that you read the discussion section before you begin the laboratory. To reinforce this importance, I have separated discussion from experiments.
It will also be helpful if you read over the associated experiments, although the results and purposes of experiments are often best understood when you are conducting the experiment. You may want to ask yourself what the purpose of each experiment is and predict what is supposed to happen. Expect to spend at least one hour preparing for each lab.
During laboratory sessions, you should think about your background readings and predictions. Feel free to experiment when working on the exercises; many students learn best by developing their own variants of these exercises. The structure of lab sessions makes it impossible to cover all the details of any particular topic. To gain the most from a lab, you will need to experiment, discover, reflect, and refine.
In other sciences, you often write laboratory reports after a lab session. While these laboratories typically do not require reports, they do require you to reflect after the laboratory is complete. To help you reflect, each session ends with a number of post-laboratory problems. Those of you who work particularly quickly may want to consider working on some of these problems during the session. Depending on the number of post-laboratory problems your instructor assigns, plan to spend at least one hour on post-laboratory work.
As suggested above, this manual contains more laboratory sessions than can be covered in a typical course. I encourage you to pick the topics that best fit the intent of your course and the level of your students. If you want to give your students more experience with Java, add the optional Java laboratories. If you want to expose your students to some core algorithms, add those laboratories.
Here are a few sample laboratory sequences for courses with twelve lab meetings, one for orientation and eleven for sessions from this manual.
The following organization includes the core Java topics, some additional Java, and a little bit of graphics for fun.
The following organization is appropriate for students who will benefit from increased exposure to data structures and algorithms.
The following organization will give students broader exposure to object-oriented programming.
By replacing lab O1 (Object-Oriented Design) in that organization with X1 (Primitive Types), you can cover more Java issues. Lab X1 might also be covered earlier in the sequence. Lab G1 (Graphics and Applets) might be used in place of another lab, preferably O3 (Interfaces and Polymorphism).
In addition to providing enough laboratories to allow you to customize your students' laboratory experience, I have also made the individual laboratories customizable. Each laboratory session contains a number of experiments, perhaps more than your students will need to complete in a two-hour laboratory session. You are encouraged to select the experiments that emphasize the topics you wish to cover. Each laboratory session also includes a number of post-laboratory problems. You may find it appropriate to assign select problems during laboratory and additional problems for later.
You may also find that it is possible to combine laboratory sessions through judicious selection of experiments. Particularly appropriate combinations are
All of the laboratories are designed to be run with Sun's Java Development Kit (JDK), version 1.1. While other development environments are available, the JDK provides a standard development platform that is relatively easy to learn, that is free, and that is generally available. If you do not use the JDK, you may need to provide custom instructions for some of the laboratories. You will also have to provide some custom instructions for the first laboratory, which is intended to get students familiar with the local computing environment.
If your students will have regular web access, it may be useful to install local copies of the Java API
and The Java Tutorial
Many of the laboratories rely on existing classes. The source and compiled versions of all of these classes are available on the lab web site at
You are encouraged to make a local copy of those classes.
Thanks to Julie Dunn of Addison Wesley Longman for suggesting that I develop this laboratory manual and for providing ready assistance during its development. When Julie left AWL to pursue her interests in editing history, Lisa Kalner took over. Amy Rose of the Publishers Group also helped guide me through many of the final stages. Vivek Venugopal of Grinnell helped me check over copyedits. I appreciate their help and patience.
Many of the ideas and exercises in this manual are based, in part, on exercises in Experiments in Computer Science: A C++ Laboratory Manual. I thank Mary P. Boelk and J. Glenn Brookshear for those contributions.
Clif Flynt tested many of the experiments before we unleashed them on our students. I thank him for his valuable comments, particularly his ``real programmer's'' insights. Ryan Gerling provided additional comments on a number of laboratories.
Clif's students and my students ``beta tested'' many of these experiments. I thank them for their enthusiasm, patience, and suggestions. Oleksiy Andriychenko, Erik Hanson, Rachel Heck, Eric Otoo, Paul Carlson, Justin Rose, Shekhar Shah, and Siera Soleil gave the manuscript a particularly careful and insightful reading and caught many small and subtle errors. Henry Walker of Grinnell College used the draft versions of some experiments in his classes. I thank him for his comments and contributions.
A number of anonymous reviewers provided suggestions on everything from organization of the manual to particular wording. They were particularly helpful in making me think about different development environments and different course organizations. I thank those reviewers for their help.
The development of this manual was supported, in part, by Addison Wesley Longman, Grinnell College, the Technology Studies concentration at Grinnell College, and by the National Science Foundation's Division of Undergraduate Education through grant DUE#98-50546. Any opinions, findings, and conclusions or recommendations expressed in this material are those of the author and do not necessarily reflect the views of the National Science Foundation or the other supporters.
Finally, thanks to my family, Michelle, William, and Jonathan, for their patience during the time spent writing this manual.
This manual is not intended to provide a comprehensive introduction to the Java language. Rather, it is intended to give students sufficient experience that they can read more on their own. Among the topics not covered in this manual are
A number of excellent tutorial guides to Java are available. Two that I have found particularly useful are
For programmers moving on in Java, I'd recommend some standard references. The first provides a great deal of detail about each of the library classes that are part of the standard Java distribution. The second simply lists all those methods (with some introductory examples), but is nonetheless surprisingly useful, particularly if you don't quite remember the calling strategy for a method.
Copyright (c) 2000 Addison Wesley Longman. All rights reserved.
Source text last modified Tue Oct 26 12:47:01 1999.
This page generated on Fri Jan 28 08:09:15 2000 by Siteweaver.
Contact our webmaster at email@example.com