Thursday, September 28, 2006

ICA-CP 9/28/06: Run the demo Java code programs

Good afternoon!

Read all of the blogs since last class: Loop Demo Java Code, Podcasts on Loops, and Class Summary 9/26/06: I read all of your blogs. Listen to the podcasts. Then, run each of the 5 demo loops in BlueJ or JCreator. Then, add comments to each line and fix each class to follow our style rules.

Thursday, September 07, 2006

Class Info: Java syntax for HelloWorld

We analyzed the HelloWorld program line-by-line. Since we know computers take everything we command them to do literally, we've found that we have to be very careful when entering programs into BlueJ. For example, a computer views "string" and "String" with a capital "S" as two completely different words. So, here's what we've discovered about Java so far:

1) /* is the opening command for a multiline comment. */ is the closing command for a multiline comment. They are a matched pair, and one must use both of them.
2) // is an end of line comment command.
3) In Java, there are commands which are organized into classes which are organized into libraries.
4) There must be a class line in a Java program. This line must have the name of the class which exactly matches the the file name. For example, the line public class Convert must have the file for the class named Convert and not CONVERT.
5) In Java, "things" come in pairs: braces { and }, parenthesies ( and ), double quotes " to begin and " to end, and square brackets [ and ].
6) There also must a a main line in a Java program. An example is:
public static void main (String args[]) {
7) If we wish to use a library, we must import it first. An example is:
import TerminalIO. If the library isn't imported first, Java will complain that the command does not exist.
8) We learned that the command, System.out.println, has the syntax which requires a (" at the beginning of the text we wish to display, and a ") at the end of it. An example is: System.out.println("Hello World"); which will display the text, Hello World, on the screen.
9) Each command line, like the println example above, must end with a semicolon.
10) After writing a Java program, we must compile the code before being able to run it. If we get any of the above wrong, like maybe we forget to put a semicolon at the end of a line or to match an opening brace { with a closing one }, the compiler will tell us we made a mistake and will not compile the program.