Computer Science 1
Top Lectures Assignments Noticeboard

Using Objects, Part II 

    Variable Declarations

    1. Must declare a variable (once) before using it
    2. Form of a declaration:
      1. type identifier;
      2. type identifier, identifier, ...;
    3. Convention - start identifier with lower case - indicate multiple words with uppercase letter
    4. Form of a declaration:
      1. String msg;
      2. String messageOfTheDay, soupOfTheDay;

    Assignment

    1. Must always assign to a variable before using it!
    2. Form of a assignment:
      1. identifier = value;
    3. left hand side (or LHS) refers to id
    4. right hand side (or RHS) refers to value
    5. variable must be declared, types must be consistent
    6. LHS is used to store, while RHS used to retrieve
    7. Examples:
    8. String teamName;
      PrintStream outputMonitor;
      teamname = "Tigers";  // legal
      outputMonitor = System.out;  // legal
      teamName = System.out;  // illegal
      teamName = outputMonitor;  // illegal
      teamname = "The Clemson Tigers";  // legal
      String s;
      s = teamName;  // legal
      String s;      // illegal
      PrintSteam s;  // illegal

    Assignment Changes Variables

    1. Consider:
    2.   String teamName = "Tigers";
        teamName = "Blue Devils";
        teamName = "Tar Heels";
    3. Each assignment sets teamName to a new value.
    4. Old references are gone.

    Reference Variables and Objects

    1. Reference variable refer to objects not reference variables!
    2. Consider:
    3.   String teamName = "Tigers";
        String name;
        name = teamName;
    4. name and teamName refer to same object!

    Variables are Independent

    1. Reference variable refer to objects not reference variables!
    2. Consider:
    3.   String teamName = "Tigers";
        String name;
        name = teamName;
        teamName = "Clemson";
        name = teamName;

    Ordering Statements - More than one way to skin a cat

    1. Consider:
    2. class Program2 {
              public static void main(String arg[]) {
                      String          teamName;
                      String          bigTeamName;
                      teamName = "clemson tigers";
                      bigTeamName = teamName.toUpperCase();
                      System.out.println(teamName);
                      System.out.println(bigTeamName);
              }
      }
      
      class Program2 {
              public static void main(String arg[]) {
                      String          teamName;
                      teamName = "clemson tigers";
                      String          bigTeamName;
                      bigTeamName = teamName.toUpperCase();
                      System.out.println(teamName);
                      System.out.println(bigTeamName);
              }
      }
      
      class Program2 {
              public static void main(String arg[])  {
                      String          teamName = "clemson tigers";
                      String          bigTeamName = teamName.toUpperCase();
                      System.out.println(teamName);
                      System.out.println(bigTeamName);
              }
      }

    More String Methods

    1. Prototypes for String:
    2. Method Returns Arguments Description
      toUpperCase String none Creates uppercase version of given string
      toLowerCase String none Creates lowercase version of given string
      length a number none Length of given string
      trim String none Creates a string with leading and ending blanks removed
      concat String String Creates a string with given string concatenated to String object
      substring String positions of first char and last char + 1 Creates a string that is substring of given string
      substring String position of first char Creates a string that is substring of given string starting at position of first char
    3. Note - Strings are indexed by 0
    4. substring is an overloaded method, i.e., class defines two methods with same method-name but different signatures

    Cascading

    Composition

    Know Terminology and review at end of chapter


Last modified: 1/18/99