Activity 2.6 - Experimenting with for loops
Topic
This activity practises using for loops.
Materials
In addition to the discussion of the solution in the Solution document, the project
for this activity contains two classes:
- Ex2_6, for which you need to supply additional code;
- Main, which is run to test the code.
Tasks
The class Ex2_6 contains three unfinished methods:
- countDownFrom(int startingValue), whose purpose is to print out the whole
numbers from startingValue down to zero;
- totalFromOneTo(int finalValue), which adds up all the integers from 1 to
finalValue inclusive;
- howManySquaresUpTo(int limit), which works out how many of the squares of
the integers between 1 and limit inclusive are no bigger than limit, and prints
the result. (To find the square of a number, multiply it by itself.)
Instructions
Open class Ex2_6 and complete the unfinished methods in turn. Run the project
after each modification to test the modifications. You can assume that each
method receives a sensible argument. We will discuss how to deal with error
conditions in a later unit.
With the test value given the results of the second task should be 5151 and
10