Banner
Introduction to Java 2


Introduction to Java for ABM 2

Spring 2012

Psychology 120


Logical Operators

As with any programming language there are logical operators that correspond very roughly to logical operators in natural languages such as “and”, “or”, “not”, “greater than”, “less than”, “equals”, and “does not equal”. Here is a list:

1. && – means roughly “and”

2. || – means roughly “or”

3. == – means roughly “equals”

4. ! – means roughly “not”

5. != – means roughly “not equal to”

6. > – means “greater than”

7. >= – means “greater than or equal to”

8. < – means "less than" 9. <= – means "less than or equal to" The best way to understand how these logical operators work is to see how they work in the context of control statements to which we will turn to shortly.

Arithmetic Operators

There are four arithmetic operators that you should know:

1. + – Additive operator but it is also used for String concatenation.

2. – – Subtraction operator

3. * – Multiplication operator

4. / – Division operator

Control Statements

If-Then

If-Then is one of the most commonly used control statements is all programming languages. The basic form of an if-then statement is, for example:

      if(x == 3) {  //if x is equal to 3
          x = x*x; // then take x*x and assign that value to x.
                   // Now x == 9
      }

It is also possible, if there is only one statement in the body of the if-then statement to write it as:

     if(x == 3) 
         x = x*x; 

You can run into problems when you do it this way. Consider the following statement:

     if(x == 3) 
         x = x*x; 
         x = 3;
         x = x*x*x;

What does x equal? In this case, x = 27 Why?

For Statements

Probably, the next most commonly used control statement is a for statement. for control statements are one of several control statements that allow you to perform a number of operations over and over again for a specified number of steps (the others are while and do-while). for statements typically have three statements as arguments and then a body that is repeated (there are variations on this theme). The general form is this:

     for(<starting conditions>;<test condition>; <increment>) {
       <statement>
       <statement>
       .
       .
       .
       <statement>
     }

The is an integer for which we will typically increment the value (though you can also decrement its value in certain cases). For example, a starting statement would often be:

     for(int i = 0;<test condition>; <increment>) {
       <statement>
       <statement>
       .
       .
       .
       <statement>
     }

The next statement is the for stopping the iterative process. If you want the for to run for n steps, then a test condition for stopping is:

     for(int i = 0;i < n; <increment>) {
       <statement>
       <statement>
       .
       .
       .
       <statement>
     }

Next, we want to increment the variable i until the test condition for stopping is met. This can be done by adding one more statement that increments the value of i by 1:

     for(int i = 0;i < n;i++) {
       <statement>
       <statement>
       .
       .
       .
       <statement>
     }

In this case, when x is greater than or equal to n the for loop stops. Finally, we place one or more statements in the body of the for loop to be executed n times.

     int n = 10;
     int m = 0;
     int o = 0;
     int a = 0;
     int z = 0;
     for(int i = 0;i < n;i++) {
         m++;
         m++;
         o = o + 2;
         a += 2;
         z = i;
     }

What is the value of m?
What is the value of o?
What is the value of a?
What is the value of z?

What do you think would happen with the following for loop?

     int m = 0;
     for(;;) {
         m++;
     }

Switch Statements

A switch statement allows one to neatly perform a number of if-then-else statements and a bit more. Here is an example of a switch statement:

public class FindMonth {

    public static void main(String args[]) {
        String[] months = {"January","February","March,
                          "April","May","June","July",
                          "August","September","November",
                          "December"};

        int month = 4;
        String theMonth; //no value assigned
        switch (month) {
            case 1:  theMonth = months[month];
                     break;
            case 2:  theMonth = months[month];
                     break;
            case 3:  theMonth = months[month];
                     break;
            case 4:  theMonth = months[month];
                     break;
            case 5:  theMonth = months[month];
                     break;
            case 6:  theMonth = months[month];
                     break;
            case 7:  theMonth = months[month];
                     break;
            case 8:  theMonth = months[month];
                     break;
            case 9:  theMonth = months[month];
                     break;
            case 10: theMonth = months[month];
                     break;
            case 11: theMonth = months[month];
                     break;
            case 12: theMonth = months[month];
                     break;
            default: theMonth = "unknown";
        }

      System.out.println(theMonth);
    }
}

What will

      System.out.println(theMonth);

print out?

Suppose

int month = 24;

What will

      System.out.println(theMonth);

print out?

Suppose we remove some of the break control statements.

public class FindMonth {

    public static void main(String args[]) {
        String[]months = {"January","February","March,
                          "April","May","June","July",
                          "August","September","November",
                          "December"};

        int month = 4;
        String theMonth; //no value assigned
        switch (month) {
            case 1:  theMonth = months[month];
            case 2:  theMonth = months[month];
            case 3:  theMonth = months[month];
            case 4:  theMonth = months[month];
            case 5:  theMonth = months[month];
            case 6:  theMonth = months[month];
            case 7:  theMonth = months[month];
            case 8:  theMonth = months[month];
            case 9:  theMonth = months[month];
            case 10: theMonth = months[month];
            case 11: theMonth = months[month];
            case 12: theMonth = months[month];
                     break;
            default: theMonth = "unknown";
        }

      System.out.println(theMonth);
    }
}

What will

      System.out.println(theMonth);

print out?

Variable Scope

The scope of a variable is the region of a program within which, a variable can be referenced. In Java, the largest scope a variable can have is at the level of the class. So, if variables are declared in a class field, they can be referenced anywhere in the class including inside methods. Suppose we declare the simple class:

public class MyClass {
    int x = 72;
    public static void main(String[] args) {
      for (int x = 0; x < 5; x++) {
          System.out.println(x);
      }
      System.out.println(x);
    }
}

What values of x will be printed out by

System.out.println(x);

this system print statement and why?

Suppose we use the this key word?

public class MyClass {
    int x = 72;
    public static void main(String[] args) {
      for (int x = 0; x < 5; x++) {
          System.out.println(this.x);
      }
      System.out.println(x);
    }
}

What values of x will be printed out by

System.out.println(x);

this system print statement and why?