Docstoc

03objects

Document Sample
03objects Powered By Docstoc
					Objects



          Java API
                  Objects
   Creating new objects
   Instance variables
   Constructors
   Calling methods
   Object references
   Comparing variables and objects
          Creating New Objects

    String is a predefined class
    To create new String objects
String aName;               // declare object
aName = new String();       // instantiate object

String aName = new String();
                // declare & instantiate

String aName = new String(“Charles Chan”);
          // declare, instantiate, & initialize
                        String
   String is defined in java.lang package. But
    String is used so frequently that it is
    available automatically with every Java
    program.
   Short-cut for String:
       String aName = “Charles Chan”;
                StringTest
class StringTest {
  public static void main(String[] args) {
    String aName = new String(“Bill Cody");
    String anAddress = new String();
    anAddress = "123 Home on the Range";
    System.out.println("Name: " + aName);
    System.out.println("Address: " +
                       anAddress);
  }
}
             Random Class
import java.util.Random;
class RandomTest {
  public static void main(String[] args) {
    Random n1;
    Random n2;
    n1 = new Random();
    n2 = new Random(7642831);
    System.out.println("Random 1: " +
                       n1.nextDouble());
    System.out.println("Random 2: " +
                       n2.nextDouble());
  }
}
         Notes on RandomTest
   Class Random is defined in package
    java.util.
   Random n1; -- declaration
    n1 = new Random(); -- instantiation
   n1.nextdouble(); -- method invocation
   Output:
    Random 1: 0.7139558128376726
    Random 2: 0.0657112946269028
              Instance variables
   Examine, on next slide, the structure of class
    Point, found in package java.awt.
   Point has two attributes: x, y
   Variables int x, int y are instance variables,
    since every instance of Point will have its own
    values.
   Instance variables are global to the entire class.
    (Every method can access it.)
   Key work this can be used to modify instance
    variables to distinguish it from other variables—
    e.g., parameter.
                 Class Point
class Point{
  public int x; // x coordinate
  public int y; // y coordinate
    public Point(){ x = 0; y = 0; }
    public Point(int x, int y){
      this.x = x;
      this.y = y;
    }
    public void setLocation(int x, int y){
      this.x = x;
      this.y = y;
    }
    // more methods ...
}
                  Constructors
   In the Point class examine the two methods
    named Point.
   Methods that have the same name as the class
    itself is called Constructor.
   Constructor is used to initialize an object of that
    class.
   A class can have several constructors.
   Q: Write a program named PointTest which uses
    class Point.
    A: PointTest.java
                Calling methods
   Methods are called “functions” or “procedures” in
    other programming languages.
   Here are some String methods.
   In Java, a method call takes the following form:
    objectName.methoName();
    E.g., aPoint.setLocation(3, 8);
   A method can return 0 or 1 value.
    E.g., String s = new String(“Hawaii”);
          int len = s.length();     // returns 6
          char ch = s.charAt(0); // returns ‘H’
          int pos = s.indexOf(‘a’); // return 1
          String sub = s.substring(0, 1); // returns “Ha”
         Primitive Type Variables
int n1 = 3;              n1   3
int n2 = 4;              n2   4

                         n1   3
n2 = n1;
                         n2   3


                         n1    3
n3 = 4
                         n2    3
                         n3    4
              Object references
String s1 = new String(“OAHU”);   s1   OAHU
String s2 = new String(“MAUI”);
                                  s2   MAUI


                                  s1   OAHU
String s2 = s1;                   s2
                                       MAUI



                                  s1   OAHU
String s3 = new String(“OAHU”);   s2
                                       MAUI

                                  s3   OAHU
             Object references
S1 == s2 ?               s1      OAHU

                         s2      MAUI


                         s1      OAHU
S1 == s2 ?               s2
                                 MAUI



                         s1      OAHU
s1 == s3 ?               s2
                                 MAUI
s1 == “OANU” ?
S1.compares(“OANU”) ?    s3      OAHU
Comparing Variables and Objects
   Comparison operators >, <. >=, and <= are
    meaningful only for primitive types.
    (What does aPoint > anotherPoint?)
   Given:
    String str1 = new String(“HAWAII”);
    String str2 = new String(“HAWAII”);

    str1 == str2 is false—because they point to
    different objects, even though their contents are
    equal.
   More Examples
    Equality of Reference Type Objects
   Consider the following primitive type
    objects.
                          +-+
int a = 3;              a |3|
                          +-+
                          +-+
int b = 3;              b |3|
                          +-+

   Question: b == a?       Answer: true
                     Equality (cont.)
   Now, consider the reference type objects.
                                                  0 1 2
                                                 +-+-+-+
int[] lista = {5, 6, 7};               lista --> |5|6|7|
                                                 +-+-+-+
                                                  0 1 2
                                                 +-+-+-+
int[] listb = {5, 6, 7};               listb --> |5|6|7|
                                                 +-+-+-+

       Question: lista == listb?           // false.
       Variables lista and listb contain addresses of
        memory cells, not values.
                  Equality (cont.)
   Consider the assignment of reference type objects.

                                                   0 1 2
                                                  +-+-+-+
Lista = listb;                          listb --> |5|6|7|
                                        lista --> +-+-+-+

   Q: lista == listb?               // true.
   lista and listb contain the same memory address. (The
    other memory block is lost.)
   In general, do not compare arrays, but individual array
    elements. E.g., lista[0] == listb[0]?
               Equality (cont.)

int[] lista = {5, 6, 7};
int[] listb = {5, 6, 7};
                   // lista == listb? false
lista = listb      // lista == listb? true
int[] listc = {5, 6, 7};
                   // lista == listc? false


 Q. Write a code segment to determine whether
 or not the contents of lista and listb are identical.

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:0
posted:5/25/2013
language:Unknown
pages:19
lanyuehua lanyuehua http://
About