Programming Assignment Checklist: Loops, Conditionals, I/O

Goals

Write several small Java programs so that you get accustomed to:

Frequently Asked Questions

What preparation do I need before beginning this assignment? Read Section 2.3 and the first part of Section 2.4 (through Average.java) of Intro to CS. You may also find it instructive to work through some of the other exercises and look at the solutions afterwards. Note that problem 7 uses standard input which is covered in Lecture 4. (Problems 1-6 use command line input.)

Where do I find information on using the operating system? Here is a link to a Command Prompt tutorial for Windows users. Here is one for Unix users.

How should I format my Java code? Follow the style from lecture and the booksite. Use JEdit to auto-indent your code. To re-indent after inserting/deleting code, select the code you want to indent and click Edit -> Indent -> Indent Selected Lines.

How much do I need to comment my code? At a minimum, each file should contain your name, precept, login, a description of the program, and how to execute it. Also, include a comment for each variable and for each well-defined chunk of code. Use some discretion here - for example, do not comment a loop index variable i.

How can there be two distinct values in Distinct.java If the inputs are 17, 18, and 17, we consider that two distinct values (17 and 18).

For Zodiac.java, do we need to check for valid input? You should check for a valid month (1 through 12) and a valid date (1 through 31), but you do not need to check for a valid combination. If the user enters "java Zodiac 2 31" (as in February 31st), the program can output "Pisces." If the program does detect an invalid input, then output an error message and stop. The input is via the command line input, so there are no second chances.

I've been reading ahead. May I use arrays for Zodiac.java? Yes, if it makes your code easier to understand. However, we expect most students will use nested if-else statements. We will introduce arrays later in the week, and everyone will get an opportunity to use them on the next assignment.

How many lines of code should Zodiac.java be? For reference, our solution with if-else statements is approximately 30 lines.

For Ordinals.java, what are the rules that dictate when to use "st", "nd", "rd", or "th" for printing the ith Hello? Any integer that ends in 0, 4, 5, 6, 7, 8, or 9 should always be followed by "th". Any integer that ends in 1, 2, or 3 should be followed by "st", "nd", or "rd", respectively, unless it ends in 11, 12, or 13 in which case it should be followed by "th".

For Ordinals.java, if the input is N, how many lines of output should I have? N.

How long should PrimeCounter.java take when N = 1 or 10 million? It will depend on the efficiency of your code and the speed of your computer. On a fast machine, our no frills solution takes about about 1.7 and 38 seconds. Your goal this week is to ensure that the program is correct - don't worry too too much about speed. If your program is substantially slower than ours, feel free to consult a preceptor to learn why.

What exactly is a 4-by-4 checkerboard? It has a total of 16 asterisks, 4 per row. Note that each row has 8 characters, alternating between asterisks and spaces.

* * * *
 * * * *
* * * *
 * * * *

Where to I find the standard input functions? You can find any helper program or data files in an ftp directory associated with each assignment. In this case, you want the file named StdIn.java from the directory loops. Be sure to save StdIn.java in the same directory you are using to store your program.

Do I need to do javac StdIn.java? No. As long as StdIn.java is in the same directory as your program, the appropriate class file will be created when you javac your program.

Do I need to submit StdIn.java? No.

When using standard input, should I output a message to the user indicating what needs to be input? It's not required.

What is meant by "high-level description of your code" in the readme file? Give a general overview of how you instrumented your program. Here's an example for Average.java from lecture.

I read in a sequence of real values from standard input one at a time, without
storing them. After reading in each value, I update two variables: N which
counts the number of data values, and sum which stores the cumulative sum. Upon 
termination of the loop, the average is sum/N.

Testing

Reference solutions. To help you check your work, we have provided reference solutions (the .class files, not the .java source code) to the exercises. Simply download the relevant .class file from the solutions directory and execute as normal. For example to run the program contained in PrimeCounter.class, type

% java PrimeCounter 1000
The number of primes <= 1000 is 168
Note that when you compile PrimeCounter.java, it will overwrite any other version of PrimeCounter.class in the current directory. Thus, to save our reference solutions, you should store them in a subdirectory too, say solutions.

Submission

Submission. Name your programs exactly as we instruct, being careful to use the same capitalization.

readme.txt. As always, download our template readme file and answer any questions. It is the file named readme.txt in the directory loops.

Enrichment

Here are some famous and not-so-famous quotations about learning to program.



COS 126 Assignments
Kevin Wayne