This assignment is due at the start of your discussion section on Friday, January 10.

(a) Do everything on the course syllabus under the heading, "What to do this week to get started in ICS H22." This includes your questionnaire and photo.

(b) If you aren't already familiar with using Windows, spend some time in the lab trying things out.

(c) Be sure you know how to read your mail on your UCInet account. (If you prefer to read your mail on a different account, make sure you have redirected your UCInet ID's mail to your preferred account; you can do this at We won't mention this again.) Also be sure you know how to use a web browser to find, for example, the home page for this class.

(d) The course outline specifies the readings in the Goodrich and Tamassia text that apply to each class meeting; you should consider these readings part of every assignment, even though we won't usually mention them explicitly. In particular, read the first two chapters for a concise (re)view of Java.

(e) Read the "Writing Professional Programs" handout attached to the syllabus.

(f) The officially supported Java environment for ICS H22 is a text editor, such as TextPad, the command-line compiler javac, and the command-line virtual machine java, all of which are available on the machines in the ICS labs. You should be(come) familiar with running programs in this environment. The "Development environment" section of Alex Thornton's ICS 22 Lab Manual ( gives details about acquiring a similar environment for your own machine. If you would like to use DrJava ( or BlueJ (, which are environments that make it easy to test your classes without writing a separate test class, you may, but we can't promise to provide support for learning that software or installing it on your own machine.

(g) Even professional programmers often need to check how a particular Java class works; there's too much to the language and libraries to memorize completely. Locate Sun's Java 2 documentation at and bookmark it so you can refer back to it as needed.

(h) [only for students who took ICS H21] Using a text editor and the command-line Java interface, write a small program that contains a Person class with fields for a name and an age, appropriate getter and setter methods, and two simple boolean methods: canVote(), which returns true if the person's age is at least 18, and canBePresident(), which returns true if the age is at least 35. Also write a test class with a main method that uses BufferedReaders for console I/O and that has an interaction like this with the user (where the user's input is shown in bold face):

   Hello. What's your name?

   Peter Programmer

   How old are you, Peter Programmer?


   Peter Programmer, you are eligible to vote.

   Peter Programmer, you are not eligible to be President.


Of course the program should work correctly for any reasonable age. Feel free to embellish it further (perhaps by identifying unreasonable ages or by repeating the exchange with multiple users).

(i) [only for students who did not take ICS H21] Object-oriented programming is an excellent approach to designing software, but it is not the only good approach or even the best one in every situation. One part of regular ICS 22 is a unit on functional programming and the Scheme programming language. We did this material at the beginning of ICS H21, so for those of you who didn't take ICS H21 we'll do the shorter ICS 22 version of this material early in ICS H22 to bring everyone's backgrounds closer together.

(i.1) Locate and launch the DrScheme software. It's on the lab machines; it's also available for nearly any platform, free of charge, from

Each DrScheme window has two panes: The bottom half is the interactions (or transcript) window, where you can type Scheme expressions and see the interpreter evaluate them. To type programs you wish to save, you will use the top pane (the definitions window) and click the green "Execute" arrow to evaluate the code (this makes the code available for use in the interactions window below). You can print the contents of each window. Printing the interactions window gives a record of your activity; do that as you complete each part below so you can turn it in.

The DrScheme system actually includes many different versions of Scheme. The one we'll be using is called "Pretty Big." You can see whether it's the one installed by looking for the word "Language" in the lower pane. If it doesn't say "Pretty Big," you can fix it this way: From the Language menu, select "Choose Language"; in the resulting window, click on "PLT" and then choose "Pretty Big." Click "OK" and then click on the green Execute arrow at the top of the DrScheme window, and you're set.

(i.2) Experiment with DrScheme to get familiar with it

Try evaluating some expressions, like (* 3 4 5) and (expt 2 5) and (gcd 15 230) and (/ pi 2). In our version of Scheme, the value of pi and the computation of greatest common divisors are predefined (built in).

Type in some definitions of symbols in the interactions window, like (define number-of-students 21) and (define number-of-staff 2) and then try (+ number-of-students number-of-staff). (You may get a yellow warning message. This is telling you that you've typed in the interactions window instead of the definitions window, so what you've typed won't be saved. That's fine for the experimentation we're doing now, but as you start making your own definitions, you'll want to type them in the top window, save them periodically, and have Scheme evaluate them by clicking Execute.)

(i.3) The factorial function (written with an exclamation point, so "n factorial" would be n!) is used in calculating how many ways there are to arrange things. The value of n! is n * (n-1) * (n-2) * ... * 1, so 5! = 5 * 4 * 3 * 2 * 1 = 120.

Type the following function definition into the definitions window. Actually do the typing so you can get used to the way it works; don't just copy and paste. (We'll go over the details of this code in class some time soon; we don't expect it to be particularly clear now.)

; Compute n! (n factorial).

(define fact

   (lambda (n)


       ((<= n 0) 1 ) ; 0! is 1 by definition

       (else (* n (fact (- n 1)))))))

Notice how the environment indents and highlights blocks of code so you don't get the parentheses confused.

Don't forget to click Execute. Now try evaluating expressions like (fact 5), (fact 50), and (fact 500). Next, evaluate (fact (fact 5)). What will happen when you evaluate (fact (fact 50))? Before you try it, save your work; (50!)! is awfully big.

What is the value produced by (/ (fact 5) (expt 7 2))? This result is called "exact representation"--it's not what we expect to see on a computer, but it's useful in further calculations because nothing is lost by rounding off to a decimal representation.

Enter this definition (you can copy and paste it):

 (define decimal-format

  (lambda (num)

  (string->number (number->string (exact->inexact num)))))

Then evaluate (decimal-format (/ (fact 5) (expt 7 2))).

(i.4) (extra credit) What's the longest number you can generate in DrScheme, without running out of memory and taking no more than 60 seconds of elapsed time? Generating the big numbers is one part of the question; counting the digits is another.

Try to count digits using (string-length (number->string your-big-number)). How do you get your-big-number into that expression without copying and pasting it (or typing the entire number)?

Try to count the digits using some tool(s) other than Scheme (or any programming language).

Using your wristwatch (or slow, measured counting), time how long it takes for Scheme to calculate and display your big number. Now, time how long it takes to calculate the big number and then its length (by nesting the expression to generate the big number inside the length-calculating expression above). You'd expect the second, combined task to take longer, but on some Scheme systems it doesn't. Does it on your system? Why might the generate-and-calculate-length task take less time than generating the number without calculating its length?

What to turn in:

-- For part (h), turn in a printed copy of your Java code along with a printed copy of one or more interactive sessions showing thoroughly how your program works.

-- For part (i), turn in a printed copy of the interactions window showing what you did for each part. Don't print out more than two solid pages of digits, though.

Based in part on ICS H22 assignments by David G. Kay from Winter 2000 and earlier; modified by David G. Kay, Winter 2001.

Modified by David G. Kay, Winter 2003, including some logistical information from the ICS 22 Lab Manual by Alex Thornton.

David G. Kay, 406B Computer Science
University of California, Irvine
Irvine, CA 92697-3425 -- (949) 824-5072 -- Fax (949) 824-4056 -- Email

Friday, January 10, 2003 -- 2:15 PM