L04

Due by the end of Tuesday, October 3

Starter Files:


SUBMIT / CHECK

Today's Activities

This week we're practicing using while loops and for loops. Things to keep in mind are:
Activity 1
Download the file SumIntegers.java. Run the program and examine the code.

It presently prints the sum of the integers from 1 to 100. Revise it so that it prompts the user for the number to sum to, and prints that sum. (For example, the sum of the numbers from 1 to 10 is 55, and from 1 to 1000 is 500500.)

Another example, with sample run, is that the sum of the numbers from 1 to 5 is 15.
Enter a number: 5 I'm adding up the numbers: 1 2 3 4 5 . The sum of the integers from 1 to 5 is 15
Use the loop in the program to find the sum. No points for calculating the sum using a formula.
Activity 2
Download the file PensionEligibility.java. Run the program and examine the code.

Right now the program allows the user to enter any age they like. Revise the program so that it accepts only ages between 0 and 120 (inclusive). If an age out of range is entered, the program will print a warning message and ask for another -- repeating the warning and reading loop until a valid age has been entered.

Activity 3
Create a program called ManyArrows. It repeatedly asks the user for a length, stopping only when a negative length is entered.

For each non-negative number, the program draws a two-headed arrow of that length. The arrow starts with a less-than sign (<) and ends with a greater-than sign (>). In between are as many hyphens as the length of the arrow. For example:

Enter a length: 5 <-----> Enter a length: 1 <-> Enter a length: 12 <------------> Enter a length: -1
Make sure your program demonstrates correct loop control. In particular, the negative length should not be processed in the loop body.

You can submit each file as you finish it (which we recommend), or you can wait until you're finished and submit all three files at once.

Make sure you pass in the correct file and that it has the correct name:

If you want to make changes to a file after you submitted it, you can make those changes and then submit just that file. We will replace the older version of that file with the newer one, and leave the other files unchanged.

The pass in page will print a little "receipt" for your records. Make sure you read it carefully. Make sure it doesn't have any red text on it at all. Make sure the submitted size is larger than the original size (you should have added quite a bit of code).

Submit this/these files:

You will be graded on the following:

  1. SumIntegers reads a number from the user
  2. ... and uses a loop to sum all the numbers up to and including that number
  3. ... without looping past that number
  4. ... has a suitable prompt for input and the correct output
  5. PensionEligibility accepts the first age between 0 and 120
  6. ... including ages 0 and 120
  7. ... reading a replacement for any number over 120 (including later values)
  8. ... reading a replacement for values under 0 (including later values)
  9. ... and uses the final value entered in the pension eligibility calculations
  10. ManyArrows reads lengths until a negative length is read
  11. ... for each non-negative number draws an arrow with that many hyphens
  12. ... ... and correct left/right arrow-heads
  13. ... and does not check any user-input more than once
  14. All code in all programs is in correct style

SUBMIT   /   CHECK