Lecture 3, CS 302-7 and 8, January 27

 

  1. Reminders
    1. Labs start Tuesday, January 31 (next week)
    2. Exam conflicts – online form (lab – next week)
  2. Review
    1. Structure of a program
    2. Compiled and interpreted languages

                                                               i.      Compiled language: different compiler for different types of processors

                                                             ii.      Interpreted language – eg Java – compile to Java Byte Code, then Java Virtual Machine (JVM) converts to machine code on the fly when you run the program.

    1. Java files

                                                               i.      Source.java – program file (source code)

                                                             ii.      Source.class – byte code

  1. Hello World – our first program
    1. Class – fundamental building blocks of java programs, more details on this later in the course
    2. Curly brackets – differentiate blocks of code.  More later
    3. Main method – starting point for program.  When you execute a program, it begins executing instructions here.

                                                               i.      Some hand-waving here – public static void main(String[] args)

                                                             ii.      We’ll talk about what those words mean later.

    1. Other methods – can do other things.  More later – we’ll only work with the main method for now.
    2. Statements.  These are the lines of code that actually do things, for instance:

System.out.println(“text”);

    1. Note that statements always end with a ;
    2. Case sensitivity

                                                               i.      Java is case sensitive.  “System” is different than “system” (and “system” wouldn’t actually work).

  1. Fun - Hello World collection
    1. http://www.roesler-ac.de/wolfram/hello.htm
  2. System.out.print(“text”);
    1. Almost the same as System.out.println(“text”);
    2. The only difference is that there is no new line printed after the output.
  3. Strings and numbers
    1. Note how we put the text in println(“text”) in quotation marks.  This indicates that we want to treat it as text rather than, say, a number.
    2. Strings

                                                               i.      This is the technical term for things that we want to treat as text.  The following are all strings:

                                                             ii.      “Hello”

                                                            iii.      “Hello World!”

                                                           iv.      “1+2”

    1. Numbers

                                                               i.      We can print numbers by omitting the “” in println(“text”);

                                                             ii.      For example: System.out.println(1);

                                                            iii.      If we do this, it’s important to only include numbers.  The following will not work:

1.      System.out.println(hello!);

                                                           iv.      We can do Math with numbers and operators within println statements:

1.      for instance, System.out.println(1+1);

  1. Errors, exceptions, etc
    1. Compile time errors

                                                               i.      Syntax error – cannot compile due to misspelling etc.

1.      System.ou.println(“Hello World”);

    1. Run time

                                                               i.      Logical error

1.      System.out.println(“Hello Word!”);

2.      Note – world is misspelled, but Java doesn’t know this J.  So the program still runs, just doesn’t give desired output.

                                                             ii.      Run-time exception

1.      System.out.println(1/0);

2.      Because we can’t divide by zero.  So, the program will compile ok, but crash when we run it.

    1. Note – line breaks don’t mean much to java, just semicolons

                                                               i.      Not ok:

System.out.println(“Hello World”)

                                                             ii.      Ok:

System.out

      .println(1+2+"3"+

      4+5);

                                                            iii.      Not ok –

System.out.print

      ln(1+2+"3"+

      4+5);

  1. HW – Read Ch 2.1-2.4