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:




Navigating through the Terminal

Now that everyone has Java installed on their computer, let's run our first program through the Terminal. Although it's been a lot simpler to run this on the Java Visualizer, by the end of the semester (and in your future classes) you won't find that website very useful because it can only run a few lines of code in a single file.

Class participation activity 1 :[5 minutes] Open your Terminal on your computer.
  1. Type pwd in the terminal and hit enter. This will show you the path of the current directory you are on in your terminal.
  2. Now type ls on Mac (or dir on Windows) and hit enter. This will show you all the sub-folders from the current directory.
  3. In VSCode, open a new file and copy-and-paste the Hello World example from the previous class into a file. Then, save the file as Hello.java, and remember where you saved this file, that is, under what folder(s). You will need this information in the next step.
  4. Using the cd command in the Terminal (which stands for "change directory"), type cd FolderName where FolderName is a folder that you can see when you did lson Mac (or dir on Windows) in step 2 that is on the path where you saved the file in step 3.
  5. Repeat using the cd and lson Mac (or dir on Windows) commands as above until you are in the directory were you saved Hello.java; you should be able to see the file in the current directory when you do ls.
Now that you can see your Hello.java in the files under the current directory you are in on the Terminal, you are ready to run your code!

Class participation activity 2 :[5 minutes] In your terminal from above:
  1. Type javac Hello.java in the terminal and hit enter. This will compile your Java code into a lower level language (bytecode) that you don't need to deal with, but the computer needs to convert it to binary instructions (eventually) that can be run on the CPU.
  2. Now type lson Mac (or dir on Windows) and hit enter. You should be able to see Hello.class in the current directory; this is the bytecode file.
  3. Next, type java Hello and hit enter. Note that there is no extension to Hello -- confusing (for now)! If everything worked, you should have seen your computer print Hello, World! to the screen.
Help your neighbors at your table to get their code to compile and run and print Hello, World! to the terminal. When you're finished, upload a screenshot to Blackboard under "week 2 participation A" (each person uploads their own screenshot). If you didn't finish this today in class, make sure to get help with it in your next lab, and submit it by the end of that session.




Files vs Programs

Keep in mind:
Rule 1: Verify that your file has the same name as the class it contains.

Your code is in a file called Hello.java

You are using a program, the Terminal, to access all the files on your computer.

You will use another program, javac, to compile your file. Then, you'll use a different program, java to execute your compiled file (which will be called Hello.class, as you will see in a minute).

Now the confusing part: technically, all programs are also files stored somewhere on your system. For example, if you navigate to certain folders (such as Applications or Programs) you can find some of your programs there. The operating system, which you have direct access to in your Terminal program, allows you to run other programs (such as javac and java) from the Terminal.




Multiple Print Instructions

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?");
    }
}
      
Class participation activity 2 :[2 minutes] Modify your file above by adding two print statements as shown above. Save your file. Then, repeat the steps to compile and run your program, making sure the output updated.




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.

  • 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 classes, variables, or methods (more on these later). 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!");
        }
    }

  • 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 or String, (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 blocks 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!");
        }
    }




    Code Formatting: White spaces and Indentation

    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 style guidelines. One of those will be to indent your code at a new level each time you open a set of curly braces:

    |-- 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?");
        }
    }
          

    In fact, we will be using (and assigning points for passing) a style checker for all the non-quiz programming assignments you will have this semester. You'll learn about it more in your lab this week. A style checker is an automated way, using a program, that we can check that our code conforms to a pre-defined coding standard. This option is especially useful when you're working on large codebases with many other developers, and for this class, we'll get used to writing clean and legible code using this tool.

    We will download and use the Java CheckStyle command line tool throughout this semester to perform automated style checking. Although there are many common and slightly different opinions on what is good coding style, we're going to pick just one of these during the semester, for the sake of learning to conform to a coding standard and being able to use this tool. More details in lab this week!

    Your homework assignments will grade your solutions based on how many test cases they pass (which you will have access to), and then will provide you with instructions how to call the command line style checker on your code to earn those points.




    Strings and escape sequences

    In Java, we use strings to represent text that is human-readable, rather than code. For example, "Hello, World!" is a string. We use double quotes to indicate what the start and end of a string should be to Java, as we saw above.

    Some things inside a String denote literal text and others try to indicate formatting. Since Strings are surrounded by double quotes like in:
    "How are you?"
    , it would be hard to try to write double quotes as part of a text. like in
    " She said "How are you?", and smiled ".
    In that example, the compiler doesn't know you mean for the two internal quotes to be taken literally.

    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.

    Class participation activity 3 :[2 minutes] Modify the code here, using escape sequences, to make it print the following on the Visualizer:
      She said
      "Hello!"
      to me.

    Only use a single print statement! Your answer must use \n to receive full credit. When you're finished, upload a screenshot to Blackboard under "week 2 participation B" (each person uploads their own screenshot). If you didn't finish this today in class, make sure to get help with it in your next lab, and submit it by the end of that session.




    Next class:

    We'll discuss variables and numeric types. Study for the 3-minute open-note BB quiz we'll have at 11:10am the next class on the upcoming Lecture 03 notes.