Programming Assignment Checklist: Loops and Arrays
Frequently Asked Questions
|
What are the goals of this assignment?
To write several small Java programs so that you get accustomed to:
using conditionals, loops and arrays and to debugging your code.
What preparation do I need before beginning this assignment?
Read Sections 1.3 and 1.4 of the textbook.
You may also find it instructive to work through some of the other
exercises and look at the solutions on the
booksite afterwards.
How should I format my Java code? How much do I need to comment my code?
Follow the style guidelines.
Also, don't forget to put your name, precept, and login in the header
of every file.
I have the constants 6, 10, 51, 60, and 61 sprinkled through my
TenDice program. Is there a better way?
Yes, give your constants meaningful symbolic names, such as
SIDES or NUMBER_OF_DICE. This will make your code
easier to read, maintain, and debug. Beginning on this assignment,
you will lose points for not doing so.
Do I have to use command-line arguments to read the inputs?
Yes, or you will lose a substantial number of points.
Submission.
When you submit,
be sure to click the Check All Submitted Files button.
Make sure that you submitted the
right files and that they compile cleanly.
You may resubmit a file after making corrections.
The newly submitted file will overwrite the old one.
These are purely suggestions for how you might make progress. You do
not have to follow these steps. The key to writing correct programs is
to develop them incrementally, testing after each step.
A drunkard's walk.
This is similar in many ways to the gambler's ruin example from
lecture and the textbook. The key to building a larger program
is developing it incrementally.
- Random walker.
First, think about what variables you need to maintain.
You certainly need to read in and store the command-line argument N.
You also need to store the current location (x, y) of the random walker.
What should be the type of the variables x and y?
What should be their initial values?
To choose a random direction, consider using the idiom from Section 1.2 to
generate a random integer in a given range.
-
Random walkers.
To get started, copy RandomWalker.java to a file RandomWalkers.java.
Now, think about what additional variables you need to maintain.
You certainly need to read in and store the command-line arguments N
and T.
In addition to the current location (x, y) of the random walker, you
need an accumulator variable, say totalSquaredDist, that stores the total
sum of squared distances so far.
Nest the loop inside an outer loop that repeats T times and add code
to update totalSquaredDist after each time through the outer loop.
Dice and the Gaussian distribution.
-
Write a program that prints the result of rolling one fair die.
To generate a random die roll, use the idiom from Section 1.2 to generate a random integer
between 0 and N-1, and modify it slightly to get one in the desired range.
-
Add a loop to print the sum of rolling 10 fair dice.
-
Add a second loop to repeat this N times,
printing out the sum after each trial. (This printing of the sum
is for testing purposes only. Remember to remove it from your
final version.)
-
Maintain an array a[] so that
a[k] stores the number of times the sum is exactly k.
Needless to say, you should continue to follow the
guidelines from earlier in the course. Here are some new conventions to keep in mind.
- Efficient and clear code:
- Think about ways to re-organize your code. Can you make it clearer
or more concise?
- Here are two examples dealing with if and if-else logic:
if (condition == True)
should usually be written as if (condition)
if (x>y) { if (y>x)...
is badly written since the second condition can never be true.
- Follow directions.
- Did the directions specify a while or a for loop?
- Are your if-else conditions or loop boundaries missing any special extreme possibilities (corner cases)?
- Avoid hard-wired, unexplained constants.
- 0, 1 or 2 used in an
obvious manner is OK, but anything else should either be named,
or have a comment explaining where it comes from.
- If the same constant is used
more than once in your program it should definitely be named.
- E.g., Using SIDES instead of 6 in TenDice.java
makes your program
easy to understand and easy to modify to simulate a different shaped die.
- Using a name with ALL_CAPS will allow another programmer to
quickly identify that item as a constant which will not change.
Here are some famous and not-so-famous
quotations
about learning to program.
COS 126 Assignments
Kevin Wayne