15-100 Lecture 8 (Friday, September 16, 2005)

Quick Quiz

Today's quiz was a "handin quiz". A perfect score was awarded for submitting a file of non-zero length into the "lab0" handin diretory.

AFS, Shell Commands, &c

AFS is the system that we will be using to hand in our projects. The Mac and Unix clusters have a really nice feature where they automatically log you onto your Andrew space when you sign on. If you are on a home computer or in a PC cluster you need to make use of an AFS Client or a file transfer program or a secure shell (like NiftyTelenet or SSH). You can connect using a host such as unix.andrew.cmu.edu or sun.andrew.cmu.edu

Once you are into your Andrew space there are several commands that you should become familiar with... "ls" "ls -l" "pwd" "cd" "cp" "mkdir" and "man" (followed by Enter). The first two commands (ls and ls -l) do very similar jobs. They both list the items that are present in the directory you are currently "in".

But how do you know which directory the shell is currently working from? That brings me to "pwd" (which stands for "present working directory") type this command and the shell will print out the path name of the file or directory it is working in.

" cd" means "change directory" you can type the whole path name to the directory you want to go to (such as /afs/andrew/course/15/100-kesden). You can also sart from your pwd and build the path name from there (say my pwd is /afs/andrew/course I can get to the 15-100 space by typing "cd 15/100-kesden").

Note the shorthand for your home is "~". Try typing "cd ~" and then "pwd" to see where that command will take you. You should also look at the folders in "~" (remember, you can use the "ls" command to do this) as you now have a very quick way to write the beginning of their path names. If you ever want to go back one directory, but don't want to type it's path name again, there's a command that tells the shell to back up one level. Try "cd .." you should end up one folder back from your path name.

So "cp" means copy. The computer looks at this command and asks copy what? to where? So when using cp use the form "cp pathNameOfFileToCopy pathNameOfDirectoryToCopyTo". There is another shorthand that you should be aware of "." means to the shell "here" so if I typed "cp ~/Desktop/*.java ." That means copy *.java to my pwd. So what does *.java do. It's a nice way to shorten your typing the shell looks in the directory it's copying from and copies all files ending in .java to the copy location. So if you want to copy three java classes this command will copy them all (assuming they are all in the same folder)

The deal with "mkdir"...It stands for "make directory". You can either type just the name of the directory you want to create and the shell will make a folder in your pwd of that name. If you want to make a folder in a space other than your pwd you need to type mkdir and then the path name of the new directory. In this last case only the last file of the path cannot exist (otherwise the shell will be unable to make the directory).

"man" is a useful command to know. If you ever forget what a command does type "man command" and the shell will pull up the specifics of what that command can do and what parameters it needs in order to work. Keep this command in mind, it is the healall for a forgetful memory.

Handing In

So now that you know the general commands used to work in a shell. Let's use them to hand in your first project.

You need to pull up a shell, locate your files to be submitted, make a directory in the lab handin folder, and copy your files to this new directory. Let's take a look at how you would submit Example1.java from the Desktop of a Mac cluster computer.

	cd ~/Dektop
        ls
  		Example1.java
        cd /afs/andrew/course/15/100-kesden/handin/labnumber/
	mkdir andrewID
        cd andrewID
        cp ~/Dektop/*.java .
	ls -l
	   rights AndrewIDOfCreator fileSize DateModified Example1.java

Note that the words in italic you will have to change depending on your andrewID and which lab you are sumbitting, and the bold items are what the console prints out. Also note that as long as the shell prompts you for the next command without printing an error message, your command was successfully completed.

Conditionals

Thus far, our perograsm have basically been a simple set of one-after-another instructions. But, programs can also make decisions. And, the way they do it is very natural to people. Consider the following: If pasta is cheaper than chicken, order the pasta.

The logic above is represented in code as below:

  if (chickenPrice < pastaPrice) {
    System.out.println ("Chicken, please.");
  } else {
    System.out.println ("Pasta, please.");
  }
  

If we consider the code segment above, we observe the reseved word, "if". It begins the decision making process. The next thing to follow is the "predicate". It is a so-called "boolean expression". Basically, it is a statement that is either true or false.

The code block, immediately following the "if (predicate)" and set apart by the {}s is executed if, and only if, the predicate is true. The second block, prefaced with the "else" is executed if, and only if, the predicate is false. An if statement need not have an else. Consider the following example:

  if ( (officeFloor - myFloor) > 0) {
    System.out.println ("Hey, wait. Let's take the elevator.");
  }
  else {
    System.out.println("Hey, let's go to my office.");
  }
  

It is not technically necessary to have the {}s after either the if- or the else- block. Absent {}s, the next statment after the "if" or "else" is assumed to be the body.

If there are multiple statmenets between an "if" and "else", and they aren't contained within {}s, this is a syntax error. But, if there are multiple statements after an if, without an else, or in an else, only the first one is conditional -- the rest are outside of the conditional, regardless of indentation. Notice this problem in the example below:

    if (amountOfMoney < 50.00) 
      System.out.println ("Wait up! I need to use the ATM!");
      amountOfMoney += getMoneyFromATM(50.00);
  

The Operators

In making comparisons, we can use any of several boolean operators: <, >, <=, >=, ==, !=. They all function exactly as you would expect. The only one that is a little tricky is the ==-operator. Please notice that it is ==, not just =. Remember, a single equal sign is an assignment, a double is the equality operator.

These operators work on numbers and "char". We'll talk a bit more about comparing Strings in a few days.

The result of any of these operators is a value, true or false.

The boolean type

Java has a special data type for the binary values true and false. It is the boolean type. It can be assigned literal values, as follows:

  boolean lightsOn = true;
  lightsOn = false;
  

Or, they can be assigned the true or false result of a boolean expression, as follows:

  boolean aliceIsTaller = (aliceHeight > bobHeight);
  

Boolean expressions can be joined together using the logical AND and logical OR operators. && is the logical and. If boolean expressions are joined together with the &&-operator, both must be true for the whole expression to be true. Similarly, if they are joined together with the ||-operator, only one must be true. Please consider the following example:

  boolean needAirConditioning = ( (temp > 75) || (relativeHumidity > 0.65));
  boolean okayToSnack = ( (hoursSinceLastMeal > 3) && (hoursTillNextMeal > 2 ) );
  

Boolean Variables as Predicates

Boolean variables have a value that is either true or false, so they can be used as predicates. For example, consider the following:

  boolean needAirConditioning = ( (temp > 75) || (relativeHumidity > 0.65));

  if (needAirConditioning) {
    turnOnAirConditioning();
  }
  

... is equivalent to, but less cluttered than, the following:

  boolean needAirConditioning = ( (temp > 75) || (relativeHumidity > 0.65));

  if (needAirConditioning == true) {
    turnOnAirConditioning();
  }
  

The ! operator

There is an additional operator that is valid for boolean expressions. It is the !-operator, sometimes known as the negation or NOT operator. Very simple, it turns true into false and vice-versa.

boolean aliceIsTaller = (aliceHeight > bobHeight); if (!aliceIsTaller) { System.out.println ("Bob is at least as tall as Alice."); }