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:
- You use while loops when you don't know
how many times you need to loop.
- You use for loops when
you do know (when the loop starts)
how many times you need to loop.
- There are multiple programs to write for this lab.
You can create a separate project for each program,
or
you can put them all in the same project.
If you do put them all in the same project,
remember to use the Run File command
instead of the Run Project command.
- The starter programs this week
are set up to run in a package named l04.
Remember that you need to change that name
to match the name of the package in your project
(or delete the package command entirely
if you are using the <default package>.
- 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:
- SumIntegers.java
- PensionEligibility.java
- ManyArrows.java
You will be graded on the following:
- SumIntegers reads a number from the user
- ... and uses a loop to sum all the numbers up to and including that number
- ... without looping past that number
- ... has a suitable prompt for input and the correct output
- PensionEligibility accepts the first age between 0 and 120
- ... including ages 0 and 120
- ... reading a replacement for any number over 120 (including later values)
- ... reading a replacement for values under 0 (including later values)
- ... and uses the final value entered in the pension eligibility calculations
- ManyArrows reads lengths until a negative length is read
- ... for each non-negative number draws an arrow with that many hyphens
- ... ... and correct left/right arrow-heads
- ... and does not check any user-input more than once
- All code in all programs is in correct style
SUBMIT
/
CHECK