Docstoc

Programming Languages

Document Sample
Programming Languages Powered By Docstoc
					 1/13 Intro to Java, Languages, and
           Environments
• Types of programming languages
  – machine languages
  – assembly languages
  – high-level languages
• Java environment
  – 2.0 platform
           Machine Languages
• natural language of the computer
• numeric language
• hard to read (for humans)

• EX:     +1300042774
          +1400593419
          +1200274027
          Assembly Languages
• abbreviations replace some machine language
• programs called assemblers translate assembly
  language into machine code

• EX:     LOAD BASEPAY
          ADD OVERPAY
          STORE GROSSPAY
         High-Level Languages
• look more like human languages
• programs called compilers convert high-level
  code into machine language
• structured & object-oriented
  – structured: Pascal, C
  – structured & object-oriented: Java, C++
        Structured Programming
• Disciplined approach
• Clearer to read & debug than previous
  programming styles
• Pascal, C
• Ratherthan de cidnbyrs lf hw2use language, you
  agree to abide by certain conventions to help in
  collaboration and usability.
   Object-Oriented Programming
• A OOP program has objects that can
  perform actions on other objects.
• EX:
  – newspaper
    • editor, researcher, writer, layout designer
  – restaurant
    • cook, maitre d’, waiter, dishwasher, customer
             OOP Terminology
• Objects
  – program construction w/ associated data
    & actions
  – EX: a flat head screwdriver
• Methods
  – actions that objects can do
  – EX: turn screws, pry, chisel
• Classes
  – sets of similar objects -- all objects have same kinds
    of data & same methods
  – EX: lots of screwdrivers that look and act the same.
             Java Environment
• Edit (we’ll use Symantec Visual Café)
• Compile (javac creates .class file from .java file)
• Load (java class loader puts .class file into the
  computer’s memory)
• Verify (bytecode verifier to ensure security,
  etc.)
• Execute (interpreter begins running the
  program)
         Our First Java Program
• // Fig. 2.1: Welcome.java
  // A first program in Java

  public class Welcome1 {
     public static void main ( String args[] )
     {
           System.out.println ( “Welcome!” );
     }
  }
                    Comments
                                 Must start with // for a
•                                single-line comment
    // Fig. 2.1: Welcome.java
    // A first program in Java

    public class Welcome1 {
       public static void main ( String args[] )
       {
             System.out.println ( “Welcome!” );
       }
    }
      White space: it doesn’t matter.
                                 White space is empty space
•                                in the program. You can put
    // Fig. 2.1: Welcome.java    as many spaces between
    // A first program in Java   words as you like.

    public class Welcome1 {
       public static void main ( String args[] )
       {
             System.out.println ( “Welcome!” );
       }
    }
                Class Definition
                                 Defines new class called
                                 Welcome1 that is public.
•
    // Fig. 2.1: Welcome.java    The class name (by convention)
    // A first program in Java   should be capitalized, and
                                 MUST be the same as the name
    public class Welcome1 {      of the file (case-sensitive).
       public static void main ( String args[] )
       {
             System.out.println ( “Welcome!” );
       }
    }
                       Braces
                                 Braces (squiggly parentheses)
•                                must enclose everything about a
    // Fig. 2.1: Welcome.java    class. MUST be paired.
    // A first program in Java

    public class Welcome1 {
       public static void main ( String args[] )
       {
             System.out.println ( “Welcome!” );
       }
    }
                     Methods
                                 Methods are sets of
•                                instructions, or actions to be
    // Fig. 2.1: Welcome.java    performed.
    // A first program in Java

    public class Welcome1 {
       public static void main ( String args[] )
       {
             System.out.println ( “Welcome!” );
       }
    }
                     Methods
                                 public refers to access
•                                permissions.
    // Fig. 2.1: Welcome.java    void refers to what will be
    // A first program in Java   returned from the method (the
                                 output).
    public class Welcome1 {
       public static void main ( String args[] )
       {
             System.out.println ( “Welcome!” );
       }
    }
                     Methods
                                 main is the name of the
                                 method.
•                                (String args[] )
    // Fig. 2.1: Welcome.java    defines the parameters for
    // A first program in Java   the method.

    public class Welcome1 {
       public static void main ( String args[] )
       {
             System.out.println ( “Welcome!” );
       }
    }
          Method Definition Body
                                 Entire method body
•                                MUST be surrounded
    // Fig. 2.1: Welcome.java    by braces.
    // A first program in Java

    public class Welcome1 {
       public static void main ( String args[] )
       {
             System.out.println ( “Welcome!” );
       }
    }
                 Method Definition
                                     System.out.println prints
•                                    the string “Welcome!” and then
    // Fig. 2.1: Welcome.java        moves the cursor down to the
    // A first program in Java       next line.
                                     println uses the string
                                     “Welcome!” as its argument.
    public class Welcome1 {
       public static void main ( String args[] )
       {
             System.out.println ( “Welcome!” );
       }
    }       println will print a string, then move to the next
              line. The similar method print will print a string and
              NOT move to the next line.
              Method Definition
                                 A statement is an action;
•                                a function. They work
    // Fig. 2.1: Welcome.java    like sentences.
                                 All statements MUST
    // A first program in Java   end with a semicolon.

    public class Welcome1 {
       public static void main ( String args[] )
       {
             System.out.println ( “Welcome!” );
       }
    }
       Second Java Program: pg. 42
• // Fig. 2.3: Welcome2.java
  // Printing a line with multiple statements

  public class Welcome2 {
    public static void main ( String args [] )
    {
      System.out.print ( “Welcome to” );
      System.out.print ( “ Java Programming!” );
    }
  }
                 Second Java Program: pg. 42
• // Fig. 2.3: Welcome2.java                               comments
  // Printing a line with multiple statements
                 Class definition header
                                           Method header
             public class Welcome2 {
               public static void main ( String args [] )
Class body




               {                      Method name
                 System.out.print ( “Welcome to” );
                 System.out.print ( “ Java Programming!” );
               }
             }                          statements
                 brackets
                Escape Sequences
•   Inline character sets that do basic things with text.
•   \n newline.       Moves cursor to next line.
•   \t tab.           Moves cursor to next “tab stop”
•   \r return.        Moves cursor back to start of line.
•   \\ backslash.          Prints a backslash character.
•   \” double quote. Prints a double quote character.
•   \’ single quote. Prints a single quote character.
                   Third Java Program: pg. 42
• // Fig. 2.4: Welcome3.java                   comments
  // Printing multiple lines with one statement
                 Class definition header
                                           Method header
             public class Welcome3 {
               public static void main ( String args [] )
               {
Class body




                                     Method name
                 System.out.print ( “Welcome to\nJava!” );
               }
             }                          statement
                 brackets
     Next Time: Welcome4.java
• Identifiers
• Import statements
• Getting out of the MSDOS window: dialog
  boxes