Universidad Carlos III de Madrid

Ingeniería de Telecomunicación

Enero-Mayo 2010 / January-May 2010

Review: Fundamentals of Java

Lab Section1.  Session 0 (lab): Basic review

Exercise Section1.1.  Setting up the development environment

Introduction

Today's topics:

  • Setting up your working environment.
  • Declaration of a class and its main method.
  • Declaration of variables (not fields!) (strings, booleans & ints).
  • Conditionals (if & if-else statements).
  • Loops (for & while statements).

We will go through them in order and one at a time. Work on the first topic for a few minutes, in pairs. Then, share your impresions with the rest of the class. Repeat for the rest of the topics.

Setting up your working environment

  1. Open the Java API in your Web browser.
  2. Launch a development environment (Eclipse).
  3. Open the course materials in your Web browser.
  4. Open the lab assignment in your Web browser.

Declaration of a class and its main method

  1. Declare a class:
    /*
     * A simple class, just to play with some basic Java statements
     */
    public class Example {
    }
    
  2. Compile it.
  3. Try to run the class in the JVM: What is the problem?
  4. There is no entry point in the program!
  5. Fix it: declare the main method:
    /*
     * A simple class, just to play with some basic Java statements
     */
    public class Example {
    
      public static void main (String[] args) {
      }
    }
    
  6. Now it is working properly!

Declaration of variables (not fields!) (strings, booleans & ints)

  1. Define the variable shortMsg of type String, with an initial value of "Short Message".
    /*
     * A simple class, just to play with some basic Java statements
     */
    public class Example {
    
      public static void main (String[] args) {
        String shortMsg = "Short Message";
      }
    }
    
  2. Compile and run.
  3. Define the variable longMsg of the same type and an initial value of "Looooooooooong message".
    /*
     * A simple class, just to play with some basic Java statements
     */
    public class Example {
    
      public static void main (String[] args) {
        String shortMsg = "Short Message";
        String longMsg = "Looooooooooong message";
      }
    }
    
  4. Define the boolean variable printShort, with an initial value of true.
    /*
     * A simple class, just to play with some basic Java statements
     */
    public class Example {
    
      public static void main (String[] args) {
        String shortMsg = "Short Message";
        String longMsg = "Looooooooooong message";
        boolean printShort = true;
      }
    }
    
  5. Compile and run.
  6. Finally, declare the integer variable counter, but don't define it:
    /*
     * A simple class, just to play with some basic Java statements
     */
    public class Example {
    
      public static void main (String[] args) {
        String shortMsg = "Short Message";
        String longMsg = "Looooooooooong message";
        boolean printShort = true;
        int counter;
      }
    }
    

Conditionals (if & if-else statements)

  1. Use an if statement to print shortMsg to the standard output if printShort is true:
    /*
     * A simple class, just to play with some basic Java statements
     */
    public class Example {
    
      public static void main (String[] args) {
        String shortMsg = "Short Message";
        String longMsg = "Looooooooooong message";
        boolean printShort = true;
        int counter;
    
        if (printShort) {
          System.out.println(shortMsg);
        }
      }
    }
    
  2. Compile and run.
  3. Toggle the printShort, compile and run.
  4. Change the if statement into an if-else statement to print longMsg instead, if printShort is false:
    /*
     * A simple class, just to play with some basic Java statements
     */
    public class Example {
    
      public static void main (String[] args) {
        String shortMsg = "Short Message";
        String longMsg = "Looooooooooong message";
        boolean printShort = false;
        int counter;
    
        if (printShort) {
          System.out.println(shortMsg);
        } else {
          System.out.println(longMsg);
        }
      }
    }
    
  5. Set printShort to true again. Compile and run.
  6. Add some code to print "This message is always printed" at the end of the program execution, no matter the value of printShort:
    /*
     * A simple class, just to play with some basic Java statements
     */
    public class Example {
    
      public static void main (String[] args) {
        String shortMsg = "Short Message";
        String longMsg = "Looooooooooong message";
        boolean printShort = false;
        int counter;
        String alwaysMsg = "This message is always printed";
    
        if (printShort) {
          System.out.println(shortMsg);
        } else {
          System.out.println(longMsg);
        }
        System.out.println(alwaysMsg);
      }
    }
    
  7. Compile and run.
  8. Set printShort to false. Compile and run.

Loops (for & while statements)

  1. Use a for loop to print "Are we there yet?" ten times in a row:
    /*
     * A simple class, just to play with some basic Java statements
     */
    public class Example {
    
      public static void main (String[] args) {
        final int NUM_QUESTIONS = 10;
    
        String shortMsg = "Short Message";
        String longMsg = "Looooooooooong message";
        boolean printShort = false;
        int counter;
        String alwaysMsg = "This message is always printed";
        String questionMsg = "Are we there yet?";
    
        if (printShort) {
          System.out.println(shortMsg);
        } else {
          System.out.println(longMsg);
        }
        System.out.println(alwaysMsg);
        for (int i = 0; i<NUM_QUESTIONS; i++) {
          System.out.println(questionMsg);
        }
      }
    }
    
  2. Compile and run.
  3. Modify the for loop to print the messages "Are we there yet?" and "Nop" intersperced 5 times each:
    /*
     * A simple class, just to play with some basic Java statements
     */
    public class Example {
    
      public static void main (String[] args) {
        final int NUM_QUESTIONS = 5;
    
        String shortMsg = "Short Message";
        String longMsg = "Looooooooooong message";
        boolean printShort = false;
        int counter;
        String alwaysMsg = "This message is always printed";
        String questionMsg = "Are we there yet?";
        String answerMsg = "Nop";
    
        if (printShort) {
          System.out.println(shortMsg);
        } else {
          System.out.println(longMsg);
        }
        System.out.println(alwaysMsg);
        for (int i = 0; i<NUM_QUESTIONS; i++) {
          System.out.println(questionMsg);
          System.out.println(answerMsg);
        }
      }
    }
    
  4. Compile and run.
  5. Add a while loop at the end that prints "I'm going to pass this course." 7 times in a row. Use the variable counter as a counter:
    /*
     * A simple class, just to play with some basic Java statements
     */
    public class Example {
    
      public static void main (String[] args) {
        final int NUM_QUESTIONS = 5;
        final int NUM_SELFMSG = 7;
    
        String shortMsg = "Short Message";
        String longMsg = "Looooooooooong message";
        boolean printShort = false;
        int counter;
        String alwaysMsg = "This message is always printed";
        String questionMsg = "Are we there yet?";
        String answerMsg = "Nop";
        String selfMsg = "I'm going to pass this course.";
    
        if (printShort) {
          System.out.println(shortMsg);
        } else {
          System.out.println(longMsg);
        }
        System.out.println(alwaysMsg);
        for (int i = 0; i<NUM_QUESTIONS; i++) {
          System.out.println(questionMsg);
          System.out.println(answerMsg);
        }
        counter = 0;
        while (counter < NUM_SELFMSG) {
          System.out.println(selfMsg);
          counter++;
        }
      }
    }