Computer Science 1
Top Lectures Assignments Noticeboard

Using Objects, Part I

  1. PrintStream Objects

    We have already seen how to use the predefined object System.out PrintStream objects have behaviors other than printing a line of text (i.e., can send other kinds of messages): Thus, classes may define more than one behavior.
  2. Exercise

    Write two different sequences (each containing three statements) that display a single line, Fly me to the moon And let me play among the stars . How would you change the program so that each phrase was printed on a different line, double spaced?
  3. Java terminology

  4. Optional exercise

    Identify the object references, method-names and arguments in the Java statements below:
    System.out.print ("CLEMSON!");
    car54.ask ("Where are my fish?");
    smart.get ();
    x.y ("a", "b");
  5. The String class

    Another predefined Java class is String, which models a sequence of characters. References can also be used to send messages to the objects to which they refer. System.out is a reference to to a PrintStream object.

    If we can have references to String objects, what behaviors does the String class provide?

    Using the form reference . methodname ( arguments ) we can work out how to send a message to the String object referenced by "sc".
    "sc".toUpperCase()
    The above message does not change "sc", rather it creates a reference to a new String object that contains "SC". What do we already know that we can do references?

    Let's look at the methods we know so far:
     
    Class Method Return value Arguments
    PrintStream println none none
    PrintStream println none String reference
    PrintStream print none String reference
    String toUpperCase String reference none
    Some more terminology:

  6. Reference variables

    A reference variable is an identifier that can be given a value that is a reference. The given value can vary.
    Suppose we had a variable called chant that could contain a reference to a String object, containing "Go, tigers go!"
    We can use this variable in our program, thus:
    System.out.println (chant);
    System.out.println (chant);
    System.out.println (chant);
    System.out.println ("Go, go, go!!");
    Because chant is a String reference, we can even send it messages, e.g.,
    System.out.println (chant.toUpperCase ());
  7. Declaring reference variables and saving references

    To introduce a reference variable into a Java program, it must first be declared. A declaration consists of the class of the object to be referenced followed by the name (a valid Java identifier) of our choice. E.g.,
    String      chant;
    PrintStream output;
    If you do not declare a variable, but use it in your program, an error will be issued at compile time.

    To give a variable a value, we use an assignment statement:

    chant = "Go tigers, go!"
    We can then use chant as we did above.

    The general form of an assignment statement is:

    variable = value
    Some other examples of assignment and use:
     
    Assignment Use
    output = System.out output.println ("Hello");
    loudChant = chant.toUpperCase (); output.println (loudChant);
  8. Summary

    In this section, we have looked at the following topics:
    Last modified: 1/12/99