GWU

CS 1111

Introduction to Software Development

GWU Computer Science


Lecture Notes 02: Parts of a Program


Objectives

By the end of this module, for simple HelloWorld-like programs, you will be able to:




Before Starting

If you do not have your Codio course ready, use any text editor or simple IDE. Some possibilities are:




Files Vs Programs

From Codio chapter 2.01, you should remember:
Rule 1: Verify that your file has the same name as the class it contains.




The HelloWorld program ... Again

Activity 1: Follow along and verify your work in Codio book chapter 2.02:

Step 1: Type the following program fragment in the Editor window and compile it.

public class MyHello {

}

Note 1: Even though this does not compile, this is the proper way to start a class.
Note 2: We have some keywords here to tell Java details about the class (more on this later).
Note 3: We open the bracket and then immediately close it!! This creates the class block!


The Class: Highest Level of Abstraction!

In Java, the idea is to package all the features and behaviors that a program ca do. We do this so we can eventually use them without having to think of the details. This is using the program as a black box.
Using our running example, the Class is like a pizzeria: The Programming Pizzeria.


Step 2: Now add the following section (inside the grayed-out code):

public class MyHello {
    public static void main(String[] args) { 
        
    }
}

Note 1: The class now compiles even though it actually does nothing.
Note 2: We open the bracket and then immediately close it!! This creates the method block!.


The Method: Abstraction's Next Level Down

The idea is to package sequences of instructions that ultimately perform one behavior or task.
Using our running example, the Method is like a chef: The pizza maker.


Step 3: Now add the following section:

public class MyHello {
    public static void main(String[] args) { 
      System.out.println("Hello, Myself!");   
    }
}

The Statement or Instruction: Abstraction's Next Level Down

The single instruction or statement is like a single action.
Using our running example, a Statement is like a chef adding cheese to the pizza.

From Codio chapter 2.02, you should remember:
Rule 2: If you want to run (execute) the class, Make sure that the program contains a class block with a method called main inside. We'll talk about this later (when we discuss the "Path of execution").




Multiple Print Instructions

Activity 2: Follow along and verify your work in Codio book chapter 2.03:

You can have as many instructions as you like inside the main method. Here, we have two print statements:

public class Hello2 {

    public static void main(String[] args) {
        // generate some simple output
        System.out.println("Hello, World!");
        System.out.println("How are you?");
    }
}
      

You can also use the other type of print statement:

public class Goodbye {

    public static void main(String[] args) {
        // generate some simple output
        System.out.print("Goodbye, ");
        System.out.println("cruel world");
    }
}
      

Note that in this one, we use

          System.out.print ...
        
Which has no ln at the end, which causes the next print statement to simply continue where the previous one left off.




Additional Structural Elements

Reserved Words:
We'll Highlight in red some words in HelloWorld which we call reserved words

These words tell Java something about the properties of the block, variable, or method (more on this later).
public class MyHello {

    public static void main(String[] args) {
        // generate some simple output
        System.out.println("Hello, Myself!"); 
    }
}

  • Words like public and class are called reserved words
    They come defined in the Java language and can only be used in some ways.

  • The reserved word public has to do with visibility, an advanced topic
    For simplicity, we'll leave everything public for now.

  • We generally have one class per file. There are reasons and ways to have more than one, but in this class we will not use more than one per file.

  • static has to do with ownership of the method, an advanced topic
    For simplicity, we'll leave all methods as static for now.

  • Reserved Words are CASE SENSITIVE (public is different than Public)

  • Identifiers:
    We'll Highlight in green some words in HelloWorld which we call identifiers

    These words are names that we pick to refer to named elements of the code, like blocks, variables, or methods. Some are named by us, some have been named by others and are now standard in Java.

    public class MyHello {
    
        public static void main(String[] args) {
            // generate some simple output
            System.out.println("Hello, Myself!"); 
        }
    }

  • Words like MyHello are words we pick for our convenience
    They are called identifiers.

  • As we'll see, there are many kinds of identifiers. The main two are:
    • Those we choose for things WE created, like MyHello, and
    • Those someone else chose for code that is now standard use in Java, like:
    • System.out.println (which we will explain later).
    • Lastly, the dot means, in Java: "Look Inside and Use". One analogy could be that when you want to use your Phone's calculatorApp to see what is the result for "355/113", you might do:
                      Backpack.Phone.calculatorApp(355/113)

  • Identifiers we pick must:
    • start with a letter and
    • contain only letters, numbers and underscores.

  • Once defined, Identifiers are CASE SENSITIVE (MyHello is different than myhello)

  • Braces:
    We'll Highlight in blue the different types of braces used in Java.

    The different types of brackets define the size and reach of blocks of code, parameters, arguments, or types (more on this later).
    public class FutureExample {
    
        public static void main(String[] args) {
            ArrayList<Integer> mylist = new ArrayList<Integer> ();
            System.out.println("Hello, Past!");
        }
    }

  • Curly Brackets like { } are block delimiters. They always come in pairs.
    They indicate the limits of that particular black box.

  • Parentheses like ( ) are grouping symbols. They always come in pairs.
    They can work as mathematical grouping (like you've used them before), or for indicating the set of arguments or parameters that a method takes (more on this later).

  • A pair of Angle Brackets < > are used to specify a particular type (or flavor) of something.
    One analogy could be that when you order an ice cream, you would do so by requesting:
    Cake<Chocolate>

  • Double Quotes " " are used to specify the start and end of a literal string or phrase. They always come in pairs.
    They serve to tell Java not to interpret that section as an instruction but rather to use it as a literal phrase. It is like the use of quotes in prose:

    Martha said “Something I don't want to tell you”

    Without the quotation marks, that would be very confusing!




  • Code Formatting: White spaces and Indentation

    As we saw in Codio section 2.05, some code formatting elements are optional but recommended.
    The code shown below compiles but is very hard to read and understand.

    public 
      class Hello2 {public static void main(String 
        args [] ) {// generate some simple output
            System.out.
        println("Hello, World!");
    System.out.println("How are you?");
        }}
          

    That is why, in this class, we will follow a strict set of programming
    Take a look at the following simplified CS1111CodeStyle. For now, look only at the Beginner


    The result is code that is easy to read and write:

    |-- indent Level 1
        |-- indent Level 2
            |-- indent Level 3
    
    public class Hello2 {
    
        public static void main(String[] args) {
            // generate some simple output
            System.out.println("Hello, World!");
            System.out.println("How are you?");
        }
    }
          




    Escape sequences

    Some things inside a String denote literal text and others try to indicate formatting.

    That's why, we use a special symbol: \ to tell the compiler that there starts an escape sequence, which is a sequence of characters that the compiler needs to interpret in a different way.
    In Java, these are the most common escape sequences:


    Note that since a single backslash starts an escape sequence, we use two in a row to tell the compiler to interpret the sequence to place a single literal backslash in the String.

    Activity 3: Complete Codio book chapter 2.06:

    The objective is to correct the internal quotes in this message:

    public class Escape {
    
        public static void main(String[] args) {
            // generate some simple output
            System.out.println("She said "Hello!" to me. ");
        }
    }
          

    Then make it print:

      She said
      "Hello!"
      to me.
          




    Reading Programs

    We can't emphasize this enough: reading programs is an all-important skill.

    Start with the class:

    Next, go to main:

    Next, read the one statement inside:

    As we proceed, we will point out good reading habits.




    Reading Exercises

    Activity 4: The following programs might have Syntax errors that will cause the compiler to complain. Try to identify them by eye:

    Program 1:

    Program 2:

    Program 3:

    Program 4:

    Program 5: