CSCI A201/A597

Hindsight 1

Spring 2000


In which we look back on Week One (Jan 11-14, 2000).

So what did we learn this week? That programs are sequences of instructions written in a certain language.


What are we programming? Computers, robots, automata. Essentially things with little, if any, common sense.

So we need to be very precise and exact in all of our programs. Precisely so.

Did we program a robot in class? Yes, we did. We planned a sequence of moves for it to go and retrieve a book. The sequence of actions was very much determined by where the robot was, where the book was, and the kinds of movements that the robot could do.

Ours was not a very evolved robot, was it? Well, it was able to do three things: move one step forward, turn left 90 degrees, and grab a book that is in its range (within a few or two distance in front of it). But this was about all that it could do.

Okay, and we called these things: primitive actions. Primitive or atomic. We can't (or don't need to) explain them in terms of other, simpler actions. We just assume them, and we rely on them being built into the robot's ability to be programmed, when it comes from the factory.

So this is the language in which we can program this robot, more or less. Was the robot able to find the book if not programmed? No. The program was everything. If you had given it the wrong program it would have done the wrong thing.

But given the language you can write more than one program for the robot. Was the robot able to turn right? Only if we had provided it with a definition of what that means, in terms of what the robot already knew.

So all we had to do was to define turning right as as a sequence of three turns left. Yes, the final result is basically the same, as if the robot had turned right.

And how was this related to Java? Well, Java is just another programming language. You use it to write computer programs, that is: sequences of instructions. When you write your instructions you have to use Java's primitive actions, and you have to express your programs in terms of that, just like you did with the robot.

Did we actually see a program written in Java?
How about this one?
public class Hello {
  public class static void main(String[] args) {
    System.out.println("Hello, and welcome to A201?"); 
  } 
}

Looks familiar. But where is the program, the sequence of instructions? The program is actually that one line in the middle, the rest is just boilerplate (for now). So this is a sequence of instructions of length one.

Well, this is a very simple program. Yes, it only prints a message. It does that through a function call, though.

Is that what the System.out.... is about? Yes, System is a class that groups together various tools useful for basic system work.

So System is where we look for such a tool, called out, that contains the println method. And System.out.println is how we refer to this method. We pass it a string of characters as a a parameter and expect it to print it to the screen.

How do we actually accomplish that? We edit, compile and run.

Compiling is done with javac and to run you need to use java. Is there anything special about editing? Not really. Choose the right name for your file, and save the file in plain text, with a .java extension.

So if I call my class Example then the source code should go into a file called Example.java, right? Yes, and the convention is to always use uppercase for the names of the classes. We'll refine this later but for now this is a good rule to remember.

So I can use any editor I want?
Yes, here's a session with pico on Unix:
frilled.cs.indiana.edu%pwd
/nfs/whale/home/user1/dgerman
frilled.cs.indiana.edu%pico Example.java
frilled.cs.indiana.edu%ls -l Example*
-rw-------   1 dgerman       145 Jan 24 14:49 Example.java
frilled.cs.indiana.edu%javac Example.java
frilled.cs.indiana.edu%ls -l Example*
-rw-------   1 dgerman       452 Jan 24 14:49 Example.class
-rw-------   1 dgerman       145 Jan 24 14:49 Example.java
frilled.cs.indiana.edu%java Example
Hello, world! This is my first Java program.
frilled.cs.indiana.edu%

But that was not all. We also talked about print, a method that is related to println in that it prints but does not move the cursor to the beginning of the next line after printing.

And we introduced variables and loop control structures that allowed us to print a certain line over and over again. Like printing the following message 1000 times with minimal effort:
I won't skip class again!

Is this what you mean by minimal effort?
public static Pledge {
  public static void main(String[] args) {
    int i = 0; 
    while (i < 1000) {
      System.out.println("I won't skip class again!"); 
      i = i + 1; 
    } 
  } 
}
Yes, this is shorter than 1,000 lines, and if I need to write 10,000 lines all I'd have to do is add a zero in the right place, recompile, and run it.


Sounds good to me. Where there any exercises that we looked at?
How about writing a program that produces this:
          .8.           8 888888888o          ,o888888o.
         .888.          8 8888    `88.       8888     `88.
        :88888.         8 8888     `88    ,8 8888       `8.
       . `88888.        8 8888     ,88    88 8888
      .8. `88888.       8 8888.   ,88'    88 8888
     .8`8. `88888.      8 8888888888      88 8888
    .8' `8. `88888.     8 8888    `88.    88 8888
   .8'   `8. `88888.    8 8888      88    `8 8888       .8'
  .888888888. `88888.   8 8888    ,88'       8888     ,88'
 .8'       `8. `88888.  8 888888888P          `8888888P'

Well, that looks nice and is not hard to reproduce at all. Was there any other representative problem for this week?

Yes. Sequences, don't you remember? Ah, yes, we had to produce them and sometimes sum their components up.

Like the following sequence that you are supposed to be producing up to an arbitrary n: Yes. Good luck!
1, -2, 3, -4, ..., -100, 101, ...