Intro To Computing & Programming With Java
Intro To Computing & Programming With Java
2005/9/6
page i
_
_
_
_
_
_
_
_
Introduction to Computing and
Programming in Java:
A Multimedia Approach
Mark Guzdial and Barbara Ericson
College of Computing/GVU
Georgia Institute of Technology
PRENTICE HALL, Upper Saddle River, New Jersey 07458
main
2005/9/6
page ii
_
_
_
_
_
_
_
_
ii
Copyright held by Mark Guzdial and Barbara Ericson, 2005.
main
2005/9/6
page iii
_
_
_
_
_
_
_
_
iii
Dedicated to our children Matthew, Katherine, and Jennifer.
main
2005/9/6
page iv
_
_
_
_
_
_
_
_
Preface
This book is intended to introduce computing, including programming, to
students with no prior programming experience. One of the lessons from the re-
search on computing education is that one doesnt just learn to program. One
learns to program something [4, 17]. How motivating that something is can make
the dierence between learning to program or not [6]. Some people are interested
in learning programming just for programmings sakebut thats not most people.
Unfortunately, most introductory programming books are written as if stu-
dents have a burning desire to learn to program. They emphasize programming
concepts and give little thought to making the problems that are being solved in-
teresting and relevant. They introduce new concepts without showing why the
students should want to know about them.
In this book students will learn about programming by writing programs to
manipulate media. Students will create and modify images, such as correcting for
red-eye and generating negative images. Students will modify sounds, like splicing
words into sentences or reversing sounds to make interesting eects. Students will
write programs to generate web pages from data in databases, in the same way
that CNN.com and Amazon.com do. They will create animations and movies using
special eects like the ones seen on television and in movies.
Students in courses taught at Georgia Tech have found these programs inter-
esting and motivating. Students have even reported turning in their programs and
then continuing to work on them to see what else they can make.
This book is about teaching people to program in order to communicate. Peo-
ple want to communicate. We are social creatures, and the desire to communicate
is one of our primal motivations. Increasingly, the computer is used as a tool for
communication, even more than as a tool for calculation. Virtually all published
text, images, sounds, music, and movies today are prepared using computing tech-
nology. This book focuses on how to manipulate images, sounds, text, and movies
as professionals might, but with programs written by the students.
We realize that most people will use professional-grade applications to perform
these same manipulations. So why learn to program these manipulations yourself ?
Why not just leave it to the developers of Photoshop and iMovie? The answer
depends on your interests and career choices.
If you have an interest in becoming a computing professional, then its worth-
while for you to understand how to build programs used in communication.
Much of the software in the future will be used for communications, so this
is a great domain to start learning useful skills. Most computing classes to-
day are taught in Java, so this book presents the right context for learning
programming and in the right language for you.
If you expect to be a user of applications in the future, knowing something
of how your tools works can make you a so-called Power User. Most com-
mon applications today are much more powerful than most users realize.
Many communications applications are actually themselves programmable
iv
main
2005/9/6
page v
_
_
_
_
_
_
_
_
v
with scripting languages that enable users to automate tasks in the applica-
tion. To use all the facilities of an application, it helps to have an under-
standing of what the application is doingif you know what a pixel is, you
can understand better why its useful to manipulate. To use the scripting
facilities of an application, some knowledge of programming is a requirement.
If you are a creative person who wants complete control of your communi-
cations, you want to know how to do without your applications if you need
to, in order to implement your vision. Knowing how to do manipulate media
with your own programs means that you can do what you want, if you ever
need to. You may want to say something with your media, but you may not
know how to make PhotoShop or Final Cut Pro do what you want. Knowing
how to program means that you have power of expression that is not limited
by your application software.
Finally, you may have no interest in programming your applications, or pro-
gramming at all. Is it worthwhile for you to learn this stu? Students who
took our media computation classes at Georgia Tech told us a year later that
the course was relevant in their daily life [15]. We live in a technological
society, and much of that technology is used to manipulate what we see and
hear in our media. If you know something of how that technology works, you
have a way of thinking about how to use it, and how it may be used to change
your perceptions. Students who are not computer science majors told us a
year after nishing the course that they now had a new condence around
computers because they knew something about how they worked [15].
This book is not just about programming to manipulate media. Media ma-
nipulation programs can be hard to write, or behave in unexpected ways. Questions
arise like Why is this same image lter faster in Photoshop? and That was hard
to debugare there ways of writing programs that are easier to debug? Answering
questions like these is what computer scientists do. The last chapters at the end of
the book are about computing, not just programming (chapters 15 and 16).
The computer is the most amazingly creative device that humans have ever
conceived of. It is literally completely made up of mind-stu. The notion Dont
just dream it, be it is really possible on a computer. If you can imagine it, you
can make it real on the computer. Playing with programming can be and should
be enormous fun.
TO TEACHERS
The media computation approach used in this book starts with what students use
computers for: image manipulation, digital music, web pages, games, and so on.
We then explain programming and computing in terms of these activities. We want
students to visit Amazon (for example) and think, Heres a catalog websiteand I
know that this is implemented with a database and a set of programs that format
the database entries as Web pages. Starting from a relevant context makes transfer
of knowledge and skills more likely, and it also helps with retention.
The majority of the book spends time giving students experiences with a
variety of media in contexts that they nd motivating. After that, though, they
main
2005/9/6
page vi
_
_
_
_
_
_
_
_
vi
start to develop questions. Why is it that Photoshop is faster than my program?
and Movie code is slow how slow do programs get? are typical. At that point,
we introduce the abstractions and the valuable insights from Computer Science
that answer their questions. Thats what the last part of this book is about.
Researchers in computing education have been exploring why withdrawal or
failure rates in college-level introductory computing courses have been so high. The
rate of students withdrawing from college-level introductory computing courses or
receiving a D or F grade (commonly called the WDF rate) has been reported in
the 3050% range, or even higher. One of the common themes from research into
why the WDF rate is so high is that computing courses seem irrelevant and
unnecessarily focusing on tedious details such as eciency [22][1].
However, students have found media computation to be relevant as evidenced
by survey responses and the reduction in our WDF rate from an average of 28% to
11.5% for the pilot oering of this course. Spring 2004 was the rst semester taught
by instructors other than Mark Guzdial, and the WDF rate dropped to 9.5% for
the 395 students who enrolled. Charles Fowler at Gainesville College in Georgia
has been having similar results in his courses there.
The approach in this book is dierent than in many introductory program-
ming books. We teach the same computing concepts but not necessarily in the
usual order. For example, while we create and use objects early we dont have
students dening new classes till fairly late. Research in computing education sug-
gests that learning to program is hard and that students often have trouble with
the basics (variables, iteration, and conditionals). We focus on the basics for ten
chapters: three introductory, four on pictures, and three on sounds. We introduce
new concepts only after setting the stage for why we would need them. For exam-
ple, we dont introduce iteration until after we change pixel colors one-by-one. We
dont introduce procedural decomposition until our methods get too long to easily
be debugged.
Our approach isnt the more common approach of introducing one computing
topic per chapter. We introduce computing concepts as needed to do a desired
media manipulation (like using nested loops to mirror a picture). Some chapters
introduce several computing concepts, while others repeat computing concepts in a
dierent medium. We repeat concepts in dierent media to increase the odds that
students will nd an explanation and relevance that works for them, or better yet,
nd two or more explanations that work for them. The famous articial intelligence
researcher Marvin Minsky once said that if you understand something in only one
way, you dont understand it at all. Repeating a concept in dierent relevant
settings can be a powerful way of developing exible understandings.
Memory is associativewe remember things based on what else we relate to
those things. People can learn concepts and skills on the promise that it will be
useful some day, but those concepts and skills will be related only to those promises,
not to everyday life. The result has been described as brittle knowledge [7]the
kind of knowledge that gets you through the exam, but promptly gets forgotten
because it doesnt relate to anything but being in that class. If we want students
to gain transferable knowledge (knowledge that can be applied in new situations),
we have to help them to relate the knowledge to more general problems, so that
the memories get indexed in ways that associate with those kinds of problems [20].
main
2005/9/6
page vii
_
_
_
_
_
_
_
_
vii
Thus, we teach with concrete experiences that students can explore and relate to
(e.g., iteration for removing red-eye in pictures).
We do know that starting from the abstractions doesnt really work for stu-
dents. Ann Fleury has shown that novice students just dont buy what we tell them
about encapsulation and reuse (e.g., [10]). Students prefer simpler code that they
can trace easily, and actually think that code that an expert would hate is better.
Some of the early methods are written the way that a beginning student would
prefer, with values hard coded rather than passed in as parameters. It takes time
and experience for students to realize that there is value in well-designed systems.
Without experience to give the abstractions value, its very dicult for beginning
students to learn the abstractions.
Another unusual thing about this book is that we start using arrays in chap-
ter 4, in our rst signicant programs. Typically, introductory computing courses
push arrays o until later, since theyre obviously more complicated than variables
with simple values. But a relevant context is very powerful [17]. The matrices
of pixels in images occur in the students everyday lifea magnifying glass on a
computer monitor or television makes that clear.
Our goal is to teach programming in a way that students nd relevant, moti-
vating, and social. To be relevant we have the students write programs to do things
that students currently use computers for: i.e. image, sound, and text manipula-
tion. For motivation we assign open-ended creative assignments such as: create an
image collage with the same image at least 4 times using 3 dierent image manip-
ulations and a mirroring. As for the social aspect we encourage collaboration on
assignments and on-line, public posting of student work. Students learn from each
other and try to outdo each other, in a spirit of creative competition.
Ways to Use This Book
This book is based on content that we teach at Georgia Tech. Individual teachers
may skip some sections (e.g., the section on additive synthesis, MIDI, and MP3),
but all of the content here has been tested with our students.
However, we can imagine using this material in many other ways:
A short introduction to computing could be taught with just chapters 2 - 4.
We have taught even single day workshops on media computation using just
this material.
Students with some programming experience could skip or review chapters 1
- 2 and begin at chapter 3. Students with object-oriented experience could
start at chapter 4.
Chapter 7 is about drawing using existing Java classes. It also introduces the
concepts of inheritance and interfaces. The concepts introduced here are also
used in chapter 14 (movies). If you are skipping movies you could skip this
chapter as well.
Chapters 8 through 10 replicate much of the computer science basics from
chapters 4 through 6, but in the context of sounds rather than images. We
nd the replication usefulsome students seem to relate better to the concepts
main
2005/9/6
page viii
_
_
_
_
_
_
_
_
viii
of iteration and conditionals better when working with one medium than
the other. Further, it gives us the opportunity to point out that the same
algorithm can have similar eects in dierent media (e.g., scaling a picture up
or down and shifting a sound higher or lower in pitch is the same algorithm).
But it could certainly be skipped to save time. You might want to at least
cover class methods and private methods in chapter 10.
Chapter 11 explains how to create classes. This is an essential chapter.
Chapters 12 and 13 manipulate text. They also cover exceptions, reading and
writing les, reading from the network, import statements, helper methods,
some collection classes, iterators, generics, and working with databases. We
recommend covering these chapters.
Chapter 14 (on movies) introduces no new programming or computing con-
cepts. While motivating, movie processing could be skipped for time.
We do recommend getting to chapter 15 on speed. This is the rst chapter
that is more about computing than programming.
Chapter 16 is about JavaScript. This gives students exposure to another
language that is similar to Java. It also discusses interpreters and compilers.
It could be skipped to save time.
JAVA
The programming language used in this book is Java. Java is a high-level object-
oriented programming language that runs on most computers and many small elec-
tronic devices. It is widely used in industry and in universities.
The development environment used in this book is DrJava. It was created
at Rice University. It is free and easy to use. DrJava lets the student focus on
learning to program in Java and not on how to use the development environment.
An advantage of DrJava is that you can try out Java code in the interactions pane
without having to write a main method.
You dont have to use this development environment. There are many de-
velopment environments that are available for use with Java. If you use another
development environment just add the directory that has the Java classes devel-
oped for this book to the classpath. See the documentation for your development
environment for how to do this. Of course, you can also use more than one devel-
opment environment. You could use DrJava for the interactions pane as well as
another environment.
TYPOGRAPHICAL NOTATIONS
Examples of Java code look like this: x = x + 1;. Longer examples look like this:
public cl ass Greeter
{
public stati c void main( St r i ng [ ] ar gs )
{
// show t he s t r i ng Hel l o World on t he cons ol e
main
2005/9/6
page ix
_
_
_
_
_
_
_
_
ix
System . out . pr i nt l n ( Hel l o World ) ;
}
}
When showing something that the user types in the interactions pane with
DrJavas response, it will have a similar font and style, but the users typing will
appear after a DrJava prompt (>):
> 3 + 4
7
User interface components of DrJava will be specied using a smallcaps font,
like File menu item and the Compile All button.
There are several special kinds of sidebars that youll nd in the book.
Program 1: An Example Program
Programs (recipes) appear like this:
public stati c void main( St r i ng [ ] ar gs )
{
// show t he s t r i ng Hel l o World on t he cons ol e
System . out . pr i nt l n ( Hel l o World ) ;
}
main
2005/9/6
page x
_
_
_
_
_
_
_
_
x
Computer Science Idea: An Example Idea
Key computer science concepts appear like this.
_