FHTW Berlin

FHTW Berlin
Fachbereich 4
Internationale Medieninformatik
PROG2: Programmierung II
Sommersemester 2003


Exercise 0

So, we need to get our brains back in gear for another semester! I want to do this little exercise with you to help you get back into Java-Mode and for you to see if JBuilder has gotten any better in the new version. We will do this lab the first week, section 1 will be in the lab VG 212 in block 3 and section 2 in block 4. 

Work in groups of two and refer to the following program for parts 1-3:
 

  import java.applet.Applet;
  import java.io.*;

  public class WakeUp extends Applet {

    public final static int SIZE = 100;

    int find(int[ ] A) { 

      int j; 

      for ( j = 0; j < SIZE; j++ ) { 
               if (A[j] < 0) { 
                      return j; 
               } 
      }
      return -1; 
    }

    public void init( ) {

       int[] A = new int[SIZE]; 
       int i; 

       for (i = 0; i < SIZE; i++) A[i] = i*i;
       A[17] = -A[17];

       System.out.println("What you are looking for is = " + 
                           find(A));
    }
   }

  1. What does this program do? Provide some useful comments!
  2. A programmer has suggested that the following version of the method find(A) is much better:

  3.  

    int find(int[ ] A) { 

          int i=0; 

          while ((A[i] >= 0) && (i < SIZE)){
             i++;
          }

          return (i < SIZE) ? i : -1; 
        }
     

    Are there any flaws in the implementation of this program? If there are, suggest a way to fix the program. If not, explain why.
     

  4. Another programmer claims that the following version of the program for the method find(A)correctly solves the problem:


  5. int find(int[ ] A) { 

          int i, result; 

          result = -1;
          for (i = SIZE-1; i >= 0; i--)if (A[i] < 0) result = i;

      
          return result; 
        }
     


    In what respect, if any, does this proposed solution fail to satisfy the statement of the problem?
     
  6. Now fire up JBuilder and see if you are right. If you used JBuilder and not your heads for the previous exercises, -10 points for you!

  7. For the bored: Let A and B be two sets of integers each represented by an array of distinct integers. Write a Java program to compute the intersection of A and B.

Letzte Änderung: 15.03.03 - 22:23