CSCI A201/A597

Lecture Notes 2

Spring 2000


A taste of Java: integers, booleans, expressions, loops. Challenging problems for the ambitious.

We start from the first program:

public class Hello {
  public static void main(String[] args) {
    System.out.println("Hello, and welcome to A201!"); 
  } 
}
Quick note: strings can be concatenated with the + operator. So for example the line in blue in the program above could have been written as:
System.out.println("Hello, and " + "welcome to A201!");
or
System.out.println("Hello, and " + "we" + "lcome to A201!");
or even
System.out.println("Hello, " + "and welcome" + " to " + "A201!");
End of note.

We identify a pattern that we need to be using for now without completely understanding it:

public class _______ {
  public static void main(String[] args) {
    _______________;
    _______________; 
    ...
    _______________; 
  } 
}
So let's write a program that prints:
I will not skip classes again!
I will not skip classes again!
I will not skip classes again!
I will not skip classes again!
I will not skip classes again!
I will not skip classes again!
This is straightforward, and it could look like this:
public class Penitence {
  public static void main(String[] args) {
    System.out.println("I will not skip classes again!"); 
    System.out.println("I will not skip classes again!"); 
    System.out.println("I will not skip classes again!"); 
    System.out.println("I will not skip classes again!"); 
    System.out.println("I will not skip classes again!"); 
    System.out.println("I will not skip classes again!"); 
  } 
}
We'll discuss this program later, and see if we can make it shorter. The more lines in the output of this program the more important the length of the program becomes. Can you think of a program like the one above whose size is independent of the number of lines of output that it has to produce? (We need to use a loop, we'll show you this below).

Individual work:

1. Write a program that produces the following output:

       4
      4
     4
    4   4
   44444444
        4
        4
(The program should produce a big 4).

2. Also write a program that produces the following output:

Please       your 
       bring 
  own        floppies!

End of individual work.

Now we start looking at the structure and components of a Java program. Java programs are composed of classes. The action happens in methods. The methods are composed of statements, which are made up of expressions. We start in the simplest possible way.

We know what an integer number is and we can give examples of such a number. 5, or 3, or 19 are such numbers. For all practical purposes we refer to integer numbers in Java by the keyword int: 5 is an int, 3 is an int, and so is 19. All these are constants, integer constants.

From math we know that there are a few operations defined on the set of ints:

and many others. We will only work with these three for now.

If we write

5 + 3
Java will understand that we want to add the numbers up and will evaluate this expression to 8.

Of course, it's important where (the context, that is, in which) you can write this expression. We'll look at that in a second. Meanwhile, by the same token, if we write

12 - 103
Java will understand, evaluate, and return (will give us the answer of) 91.

Expressions like these:

5 < 3
or
5 < 13
will evaluate to the special values of false, and true respectively.

true and false are booleans.

boolean is a type just like int.

How many boolean values do you think there are? How many ints?

When it evaluates an expression Java gives us the resulting value, and if we don't store it anywhere it simply gets lost. We can store results of expressions in named locations. The names are called identifiers, and resemble notation in mathematics. The locations where we store the result of an expression must be of the same type as the result that needs to be stored.

So, for example, we can store the result of

1 + 2
in a location called result as follows:
result = 1 + 2
The equals sign (=) represents an assignment operator, and the entire line represents an assignment statement. The right hand side of the statement is an arithmetic expression whose result (or value) is 3. This value is then stored in location named result as a result of the assignment statement.

If we write several statements one after another we need to separate them by semicolons (;) as we shall promptly see in the example below.

Names of locations can be used on the right hand side of an assignment statement, in which case they will be replaced (when the expression is evaluated) by their current value.

Here are some examples:

a = 2 + 4;
b = a + 3;
will set a to 6, and then b to 9. Can you see why?

Individual work:

What's the value of i at the end of the following sequence of statements:

i = -3;
i = i + 1;
i = i + 1;
i = i + 1; 

End of individual work section.

The type of the named locations that we plan to use must be declared ahead of time, before they are used. So in the examples above we need to declare the type of result, a, b, i (to be int) before they are first used.

Declarations have the following format: type followed by name of variable (or named location) followed by semi-colon.

Methods are composed of sequences of declarations and executable statements. main is a method. To start with we will be working with main for a while. We will later write many other methods.

Let's write a program that computes and prints the sum of the first 10 integers, from 1 to 10.

Here's a solution:

public class First {
  public static void main(String[] args) {
    int result; 
    result = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10; 
    System.out.println("Sum of first 10 integers is: " + result);
  } 
}
What's wrong with this solution if we want to scale it up? For example how do you write a program that adds up the sum of the first 1000 integers? It would get so much longer, wouldn't it?

while loops

Java has a language construct that looks like this:

while (condition) {
  statements
}
For as long as the condition is evaluated to be true the statements will be executed and then the condition evaluated again.

Here are some examples.

What do the following programs do:

while (0 < 1) {
  System.out.println("I will not skip classes again!"); 
}
int i;
i = 0; 
while (i < 1) {
  System.out.println("I will not skip classes again!"); 
}
int i;
i = 0; 
while (i < 1) {
  System.out.println("I will not skip classes again!"); 
  i = i + 1; 
}
Can you re-write the program that computes the sum of the first 10 integers now?

Here's a solution:

public class Second {
  public static void main(String[] args) {
    int i;     
    int sum;
    i = 0; 
    sum = 0; 
    while (i < 11) {
      sum = sum + i; 
      i = i + 1; 
    } 
    System.out.println("Sum of first 10 integers is: " + sum); 
  } 
}
What happens if we put the println statement inside the while loop?

What will the output of the program be?

Individual work:

1. Write a program that prints all even numbers between 7 to 101.

2. Write a program that computes the sum of the first 10 even numbers.

Big challenge:

Write a program that outputs itself.

(Don't expect this to be easy, but try nonetheless - you know everything you need to know.)

More exercises in lab notes that comprise the first in-lab assignment.


Last updated: January 13, 2000, by Adrian German