Lecture 3, CS 302-6, September 9

  1. Review
    1. Structure of a program – compiled and interpreted

                                                               i.      Java, Machine Code, Byte Code, JVM

    1. Java - why java?

                                                               i.      Portability - -trade off!

                                                             ii.      Ease of programming

                                                            iii.      Important/useful language

                                                           iv.      Java class library

                                                             v.      Error Reporting

    1. Java files

                                                               i.      Source.java

                                                             ii.      Source.class

  1. Hello World
    1. Class – fundamental building blocks of java programs, more details later
    2. Main method – starting point for program.  When you execute a program, it begins executing instructions here.  Introduce the idea of a thread
    3. Other methods – can do other things.  More later
    4. Statement;
    5. Some hand-waving here – public static void main(String[] args)
    6. Note – semi colons, curly brackets, etc

                                                               i.      Semi colons – statements

                                                             ii.      Curly brackets – blocks of code.  More later

    1. Strings vs numbers

                                                               i.      Strings – made of characters

                                                             ii.      Numbers – math operations

    1. Case sensitivity

                                                               i.      Java is case sensitive – HelloWorld is not the same as helloworld

  1. Hello World collection
    1. http://www.roesler-ac.de/wolfram/hello.htm
  2. System.out.print()
    1. Same as System.out.println, but no new line
    2. Note – the new line from .println comes After the text.
  3. What do the following inputs to System.out.println() display?
    1. “Hello” + “World!”
    2. “Hello + 1 +2”
    3. 1+2
    4. “1”+2
    5. 1+”2”
    6. "1"+2+3
    7. 1+2+”3”
    8. 1+2+"3"+4+5
    9. “1” + (2+3)
  4. Errors, exceptions, etc
    1. Compile time

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

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

                                                             ii.      Compiler error – cannot compile because compiler is confused

1.      System.out.println(1,2,3);  Can’t find method

a.       We’ll examine this one more later

2.      System.out.printLn(“Hello World”);

3.      System.out.println(Hello World!);

    1. Run time

                                                               i.      Logical error

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

                                                             ii.      Run-time exception

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

    1. Question – why can’t you test a program for run time errors if it has compile time errors?
    2. Note – line breaks don’t really mean anything to java, just semicolons

                                                               i.      Not ok:

System.out.println(“Hello World”)

                                                             ii.      Ok:

System.out

               .println(1+2+"3"+

               4+5);

                                                            iii.      Exception :

System.out.print

               ln(1+2+"3"+

                  4+5);

  1. Variables – Demo - input, name and age

import javax.swing.JOptionPane;

 

public class NameAndAge

{

      public static void main(String[] args)

      {

     

                  String name=JOptionPane.showInputDialog("What is your name?");

                  int age=Integer.parseInt(JOptionPane.showInputDialog("What is your age?"));

                 

                  System.out.println("Hello " + name);

                  System.out.println("You are " + age + " years old");

                 

                  System.exit(0);

      }

}

 

  1. Variables
    1. Concept – allow your program to remember things.
    2. Variables store values
    3. They must be declared and initialized before use

                                                               i.      Declaration

1.      int number;

2.      Sets aside a spot in memory for a variable

3.      Type

a.       int, double, etc…

                                                             ii.      Initialization

1.      number=10;

2.      Assigns a value to a variable

3.      Don’t need to declare value first, though

    1. Declaring a variable is a statement – need to have a ;
    2. One use - long math equations.  Instead of having to do it ‘by hand’, we can just use the symbol to represent it.  Like algebra.
    3. Football example

                                                               i.      Variables for team score, point values for different ways to score, etc

  1. HW – Read Ch 2.1-2.4