FHTW Berlin

FHTW Berlin
Fachbereich 4
Internationale Medieninformatik
Info2: Informatik II
Sommersemester 2006


Exercise 0 : Wake up to Java without Cream and Sugar

So, we need to get our brains back in gear for another semester! And while we are at it, I want you to learn that you do not actually need to use Eclipse in order to make Java work. But first you need to be working with your heads only. No pre-lab this week, because classes are just starting!

Work in groups of two and refer to the following program for questions 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 not.
     

  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?
     

Editing files without Eclipse

There are many ways of editing a file without using the Eclipse editor. Start the computers in the lab to the LINUX operating system. Look around for some editors, there should be a number of them. I will suggest two, vi and emacs. The use of one or the other editor is rather a religious question in computing. vi has the advantage of always being there, in every flavor of Unix. It runs when all else fails, but the syntax is, well, cryptic. emacs has, as the saying goes, everything but the kitchen sink in the way of functionality. You can customize it to death, and there are lots of cute Easter Eggs hidden inside. You have to use Bucky bits to type in most of the commands, but if you can program Lisp, you can even make emacs behave as if it were vi.

So how do we edit with them? With vi you can check the man pages. For emacs you might want to look for an online handbook. You man use either, and report on how it was using them in your post-lab report.

Compiling the file

Once you have typed in your file and saved it (gosh, this is difficult, no indication if I made an error or not?!) you now have to compile the file. The Java compiler is called javac and should be installed so that you just type in

% javac Filename.java

Give it a try, and then look and see if there was a .class file created! What? There were some errors? Well, back to the editor and get them fixed, pronto.

Running the main

Once you have compiled a file with a main class in it, you have to make sure that the class path is set properly. This is done either by telling the Java interpreter which path to use

% java Filename -cp ./path/to/the/files/needed

or by setting the environment variable properly. The syntax for this will vary, depending on which shell you are using. Find out which shell you are using and how to set a class path for this shell, and record the results in your lab report.

If you have an applet, you do not have a main, so you will have to set up a HTML file that can be loaded into a browser that will load the class file. Figure out how to do this and record it in your notes.

 

  1. Now type in the file above, compile and run it and and see if you are right. Record the results in your notes!
  2. So, now let's do something from scratch. A string transformer is a method that takes a String as a parameter, does something interesting with it, and returns a new string that has the transformation applied. Set up either an Applet or a class that gets called from a Main class that offers at least 3 different methods for transforming strings. One could turn the string around front-to-back, one might put a space in between each letter of the string, a third might shift each letter down one. Set up a simple GUI that reads a String from a TextField, and then applies a transformation to the String and outputs the results.
  3. Can you set up Buttons, one for each transformation, and have the transformation happen when you click on the button? OOh, remember all the ActionListener stuff from last Semester?
  4. (For the bored) Okay, you still have half the exercise time left. Make a transformation that transforms the strings into Pig-Latin. Or any other transformer that does something to a word.

Your report is due by 9.30 the morning of your next lab! As in Informatik 1, I am more interested in process than in product, although we are now getting more interested in products as well. Your report should include any collaborators, summarize what you learned, and note the time you invested in this exercise.

 


Copyright 2006, 2007 Prof. Dr. Debora Weber-Wulff
All rights reserved.
Questions or comments: <weberwu@fhtw-berlin.de>