CSCI A201/A597

Lecture Notes 10

Spring 2000


Wrap-up of primitive types and Strings. For loops revisited. Classes as collections of data and methods.

First we start with a boolean simplification that we didn't quite do last time. We will prove that

a != true
can be simplified to
!a
Same thing can be said about
a != false
which is the same as
a
for any value of a.

Then we prove that

! (a && b)
is the same as
!a || !b
which is incidentally also known as one of the DeMorgan's laws.

We will solve a few more exercises like that:

  1. !( a || b) same as !a && !b

  2. eligible_female same as !eligible_male && eligible

  3. i < 3 && i > 10 same as false

  4. i < 3 && i < -4 same as i < -4

  5. i < 10 || i > 3 same as true

Once we realize things need to be considered with great care when it comes to logic we will make a note of it and move to a few other loose ends from last time.

We actually go over a few things regarding the Math class and String objects.

We look up the two classes on the web and learn how to read their descriptions.

Then we go over for loops again.

We present the following interpretation of the loops.

First we review a single for loop.

The syntax is like this:

for (<init>; <condition>; <re-init>;) 
  <statement>
Here's a diagram that not only shows the flow of control but also gives some specific examples for the initialization step, condition, re-initialization step and body of the for loop.
This is a simple loop because we first want to make clear we understand the flow of control. The body is as simple as it can be. There is just one statement which is an invocation of a function. A star will be printed for each value of i from 0 to 9.

Once it's clear how it works we blow up the body of the for loop and place in it another for loop. The new path is drawn in red but if we want to ignore it we can and we recover the simple example from above.

This new example works the same except the task of printing one star has now become much more complex. Instead of printing a star for each value of i we now need to give values to j from 0 to 10. Here's a simplified version of the entire mechanism in which we provide the relevant information in the key points of the process.

In this new picture we also add a call to println after the inner loop (whose body consists of the original print a star invocation.

We will discuss these examples and also go over this one, as a test:

int k, p;
for (k = 0; k < 10; k = k + 1) {
  for (p = 0; p < 10; p = p + 1) {
    if (k == 0) {
      System.out.print(" "); 
    } else {
      System.out.print(k); 
    }
    System.out.println(p); 
  }
} 
// System.out.println(100); 
What will the above code fragment print?

If we have time we will also go over the following program:

frilled.cs.indiana.edu%cat Example.java
class Student {
    int age; 
    boolean eligible; 
    String name;
    char gender;
    double gpa; 
    void introduction() {
	System.out.println("Hello!!!"); 
	System.out.println("My name is: " + name); 
	System.out.println("Gender:     " + gender); 
	System.out.println("Eligible:   " + eligible); 
	System.out.println("I am " + age + " years old."); 
	System.out.println("My GPA is:  " + gpa); 
    } 
} 

class Example {
    public static void main(String[] args) {
	Student a = new Student(); 
	a.name = "Larry Bird"; 
	a.gpa = 3.9;
	a.gender = 'M'; 
        a.age = 21; 
	a.eligible = (1 == 1); 
	a.introduction(); 
    } 
} 
frilled.cs.indiana.edu%javac Example.java
frilled.cs.indiana.edu%java Example
Hello!!!
My name is: Larry Bird
Gender:     M
Eligible:   true
I am 21 years old.
My GPA is:  3.9
frilled.cs.indiana.edu%
Except we probably will take it step by step, starting with the simpler:
frilled.cs.indiana.edu%cat Simpler.java
public class Simpler {
    public static void main(String[] args) {
	Student a = new Student(); 
	a.name = "Michael Jordan"; 
	a.age = 23; 
	System.out.println(
          "Student " + a.name + " is " + a.age + " years old."
        ); 
    } 
} 

class Student {
    int age;
    String name; 
} 
frilled.cs.indiana.edu%javac Simpler.java
frilled.cs.indiana.edu%java Simpler
Student Michael Jordan is 23 years old.
frilled.cs.indiana.edu%