CS 1063  Week 15:  For Loops and Review


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;                            int k;
while(k <= 5){                        for(  k = 1; k <= 5; k++){
    System.out.println(k);                  System.out.println(k);
    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. 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 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 */
  public static int sumOfProperDivisors(int number)

 
/* returns true if the number is perfect and false otherwise A perfect number is a number
   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
                                // This method should NOT change the value of the beginning salary


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:      Years Experience       Salary
                            -----------------------------
                                        1                     16000
                                        2                     16640
                                        3                     17306
                                        :
                                        :
                                      10                     22773  (value returned by salaryAfterExperience() )

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


Last  Modified:  November 15, 2004 at 12:01pm