CS 1063  Week 14:  For Loops

Objectives

Assignments


For Loops

You use a for loop when a variable runs from a starting to an ending value with a constant increment or decrement.

While loop For loop
int k = 1;
while ( k <= 5 ) {
    System.out.println(k);
    k++;
}
int k;
for ( k = 1; k <= 5; k++ ) {
    System.out.println(k);
}

Activity 1:

1.  Locate the following parts to each of the loops above.

Questions 2, 3, and 4 refer to the following code:

int sum = 0;
int count;

for ( count = 0; count <= 4; count++ ) {
    sum += count;
}
System.out.println("sum = " + sum);

2. What would you find on the screen?
  a. sum = 1
  b. sum = 6
  c. sum = 10
  d. an error message

3. What is the value of count after execution of the for loop?
  a. 0
  b. 4
  c. 5

4. How many times are the statements inside the loop executed?
  a. 0
  b. 4
  c. 5
  d. 6

Activity 2: The  Calculate  Class using static Methods

Write the class Calculate that contains all static methods.  The public interface for this class is below.

/*  Constructor  */
public Calculate()

/*  returns the factorial of number.
    The call Calculate.fact(5) should return 120.  (1 * 2 * 3 * 4 * 5 = 120)
*/
public static double fact(int number)

/*  Outputs all of the divisors for number including 1 and the number
    The call Calculate.printAllDivisors(24) outputs 1 2 3 4 6 8 12 24
*/
public static void printAllDivisors(int number)
 
/*  returns true if the number is prime and false otherwise.
    A number is prime if is has no divisors except itself and 1.
    The smallest possible prime number is 2.
    The call Calculate.isPrime(5) returns true
    The call Calculate.isPrime(12) returns false
*/
public static boolean isPrime(int number)

/*  returns the sum of all of the divisors except the number itself
    The call Calculate.sumOfProperDivisors(24) returns 36.
    1 + 2 + 3 + 4 + 6 + 8 + 12 = 36
*/
public static int sumOfProperDivisors(int number)
 
/*  returns true if the number is perfect and false otherwise.
    A perfect number is a number such that 
    the sum of its proper divisors equals the number
    The call Calculate.isPerfect(6) returns true because 1 + 2 + 3 = 6
    The call Calculate.isPerfect(10) returns false because 1 + 2 + 5 != 10
*/
  public static boolean isPerfect(int number)

Test your class using the examples described in the interface.

Activity 3: The  TeacherSalary  Class

Teachers are paid on a salary schedule that provides a salary based on their number of years of teaching experience.  Write a class TeacherSalary that stores a beginning salary for a teacher, the number of years experience and the percent increase for each year (as a decimal).  This class should contain the following methods:

double getBeginningSalary()  // returns the beginning salary for this teacher
int getYears()  // returns the years experience
double getPercent()  // returns the percent increase for each year
double salaryAfterExperience()  // returns the salary after years experience
                                // The beginning salary is NOT changed.

For example, a teacher with a beginning salary of $16000, 10 years experience and a 4% increase each year would produce the following table.

Sample output (the Salary column should be values returned by the salaryAfterExperience method):

Years Experience       Salary
----------------       ------
      1                 16000
      2                 16640
      3                 17306
      :                   :
      :                   :
     10                 22773

Test your class with at least 2 sets of data.  Use a do-while loop in the main method to do this.