Programming Assignment Checklist: Conditionals and Loops
The goal of Assignment 1 is to write several short programs so that you get accustomed to:
- Using conditionals and loops.
- Nesting conditionals and loops.
- Using arrays.
- Debugging your code.
- Declaring and initializing variables.
- Using arithmetic and boolean expressions.
- Using conditionals and loops.
- Nesting conditionals and loops.
- Debugging your code.
Frequently Asked Questions
|
What preparation do I need before beginning this assignment?
Read Sections 1.2 and 1.3 of the textbook.
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 guideline in the Assignment FAQ.
Be sure to use the prescribed header in every file you submit.
I have the magic numbers 6, 10, 51, 60, and 61 sprinkled through my
TenDice program. Is there a better way?
Yes.
- First, define constant variables (such as SIDES and NUMBER_OF_DICE) and use
these symbolic names to refer to the corresponding magic numbers.
- Second, avoid introducing magic numbers that can be derived from existing constants.
For example, don't introduce the magic number 60; instead, use SIDES * NUMBER_OF_DICE.
These two steps will make your code easier to read, maintain, and debug.
Do I have to use command-line arguments to read the inputs?
Yes, or you will lose a substantial number of points.
My output from RGBtoCMYK
is almost the same as the sample on the assignment page.
Only the last digit or two is different. Why is there this tiny discrepancy, and is my answer wrong?
Computers work with limited precision, and so different algebraically equivalent solutions can give slightly different answers. When grading, we typically ignore such tiny discrepancies.
The assignment FAQ says that we should handle all "meaningful" inputs
What does that mean for RandomWalkers.java?
The number of steps n is a non-negative integer and the number of
trials is a positive integer. Of course, if either n or trials is
huge, then your program might not finish in a reasonable amount of time.
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 and debugging after each step.
Noon snooze.
The integer division and remainder operators are the key to solving
NoonSnooze.java.
- Which variables do you need? You certainly need to know the number of
minutes to snooze. What other information would be useful? Starting hour?
Ending hour? Starting minute? Ending minute?
- How will you compute the number of hours since noon? How will you translate
that to an appropriate ending time (using a 12-hour clock)?
-
How will you compute the number of minutes left over?
- How will you determine whether to print am or pm?
A drone's flight.
This is similar in many ways to the gambler's ruin example from
the lecture video and textbook. The key to building a larger program
is developing it incrementally.
- RandomWalker.java:
First, think about which variables you need to maintain.
You certainly need to parse 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.
-
RandomWalkers.java:
To get started, copy RandomWalker.java to a file RandomWalkers.java.
Now, think about which additional variables you need to maintain.
You certainly need to read and store the command-line arguments n
and trials.
In addition to the current location (x, y) of the random walker, you
need an accumulator variable, say totalSquaredDistance, that stores the total
sum of squared distances so far.
Nest the loop inside an outer loop that repeats trials times and add code
to update totalSquaredDistance 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 the sum after each trial. (Printing the sum is for testing purposes only;
be sure to remove it before you submit.)
-
Maintain an array tally[] so that
tally[k] stores the number of times the sum is exactly k.
-
Review your code and eliminate unnecessary magic numbers.
Here are some famous and not-so-famous
quotations
about learning to program.