1.   Elements of Programming


Overview.

Our goal in this chapter is to convince you that writing a computer program is easier than writing a piece of text such as a paragraph or an essay. Writing prose is difficult: we spend many years in school to learn how to do it. By contrast, just a few building blocks suffice to take us into a world where we can harness the computer to help us solve all sorts of fascinating problems that would be otherwise unapproachable. In this chapter, we take you through these building blocks, get you started on programming in Java, and study a variety of interesting programs.


Java programs in this chapter.

REF PROGRAM DESCRIPTION
1.1.1 HelloWorld.java everyone's first Java program
1.1.2 UseArgument.java use a command-line argument
1.2.1 Ruler.java concatenate strings to print the subdivisions of a ruler
1.2.2 IntOps.java demonstrate the arithmetic operations on integers
1.2.3 Quadratic.java find the roots of a quadratic equation
1.2.4 LeapYear.java check whether a given year is a leap year
1.2.5 RandomInteger.java cast to get a random integer in a given range
1.3.1 Flip.java simulate a fair coin flip
1.3.2 TenHellos.java your first while loop
1.3.3 PowersOfTwo.java compute the powers of two
1.3.4 DivisorPattern.java your first nested loop
1.3.5 Harmonic.java compute Harmonic numbers
1.3.6 Sqrt.java apply Newton's method to compute square roots
1.3.7 Binary.java convert a decimal integer to binary
1.3.8 Gambler.java simulate a gambler's ruin
1.3.9 Factors.java compute the prime factorization of a given integer
1.4.1 Sample.java select a random sample
1.4.2 CouponCollector.java coupon collector problem
1.4.3 PrimeSieve.java tabulate primes using the Sieve of Eratosthenes
1.4.4 SelfAvoidingWalk.java simulate a self-avoiding walk
1.5.1 RandomSeq.java generate a random sequence of integers
1.5.2 TwentyQuestions.java play twenty questions interactively with user
1.5.3 Average.java average a stream of numbers
1.5.4 RangeFilter.java filter from standard input to standard output
1.5.5 PlotFilter.java filter from standard input to standard drawing
1.5.6 BouncingBall.java animate a bouncing ball using standard draw
1.5.7 PlayThatTune.java synthesize a tune using standard audio
1.6.1 RandomSurfer.java simulate the behavior of a random web surfer
1.6.2 Markov.java use matrix multiplication

Click on the program name to access the Java code; click on the reference number for a brief description; read the textbook for a full discussion.


Exercises.