Warm-up Exercises
CS 162 - Spring 2008 - Ms. Katz


Last updated January 10, 2008

At the beginning of lecture, I sometimes write one or more questions on the board. As students come into class, they write down their answer to the warm-up question. I do not collect or grade these, but we go over them sometime during the lecture. They usually pertain to the current assignment, the lecture's topic, or a problem I've seen while grading.

It is my goal with these questions to get the students thinking more actively about the class as they come in from their day's activities.

The dates are provided here to help students refer to the appropriate section of their notes for answers.

These won't normally be here before the class. I'm setting up the pages.


Just the Questions

1. (Jan. 14) Write a Java function sumPos that takes one parameter - an array called nums of integers. As its sole action, the function returns the value of the sum of its positive integer elements.


Questions and Answers

1. (Jan. 16) Write a Java function sumPos that takes one parameter - an array called nums of integers. As its sole action, the function returns the value of the sum of its positive integer elements.

   public static int sumPos(int[ ] nums) {
      int sum = 0;

      for (int i = 0; i < nums.length; i++) {
         if (nums[i] > 0) {
            sum += nums[i];
         }
      }
      return sum;
   }
or
   public static int sumPos2(int[ ] nums) {
      int sum = 0;

      for (int elem : nums) {
         if (elem > 0) {
            sum += elem;
         }
      }
      return sum;
   }

 
 

[ Beth Katz ] [ CS162 ] [ Millersville CS ]

Beth Katz, katz@cs.millersville.edu