JAVA  שיעור # 3   
						
						

1. StringBuffer loc = "Node12";
    StringBuffer loc = new StringBuffer("Node12");
    loc.append("c");
    StringBuffer fileName = new StringBuffer("DataFile");
    fileName.insert(4,"1.txt");
    fileName.setCharAt(4,'h');

2. class User {
        String userId, emailAddress;
        int noOfAccesses;
        public void setUserId(String usIDVal) {
            userId = usIdVal;
        }
        public void access() {                   john.noOfAccesses++;
           noOfAccesses++;
        }
        public boolean zeroAccesses() {
           return noOfAccesses == 0;
        }
        User john = null;

3. public protected private    friendly
    static int version;     
    double val = Math.sin(angle);
    static final int MAX = 12;

4. constructor
    class Coordinate {
       private int xPos, yPos, zPos;
       public Coordinate(int xVal, int yVal, int zVal) {
          xPos = xVal;  yPos = yVal; zPos = zVal; 
       }
       public Coordinate(int xVal) {
          xPos = xVal; yPos = 0; zPos = 0;
       }
 
      public Coordinate(int xVal, int yVal) {
          xPos = xVal; yPos = yVal; zPos = 0;
     }
     Coordinate newCoord = new Coordinate(2,2,2);
     Coordinate newCoord = new Coordinate(3);

5.  finalizer     garbage collection
     class IdPool {
        int oldIdPool, newIdPool;
        static int classCount, totObjects;
     public idPool ( int firstVal, int secVal)  {
         if (classCount < totObjects) {
               oldIdPool = firstVal;   newIdPool = secVal;
               classCount++;
         } else {
               //  Code to handle case of too many objects
               //     (raise exception)
         }
     }
     protected void finalize {
             classCount--;
         }
     public static int numObjects() {
        return classCount;    }
     IdPool.numObjects();

6. Inheritance     extends     overload     subclass, superclass
    Software reuse     Java class libraries   Class hierarchy  Object
    super           super(x,y,r)
                        this.r    super.r       super.super.r
                        ((B)this).r     ((A)this).r
    if (jones == roberts)  
    public boolean equals(Emp e) {
       return (this.address == e.address  && this.name == e.name                              
                     && this.salary == e.salary);   }
     oldEmp.equals(roberts)
     newEmp.copy(oldEmp);     newEmp = oldEmp;
7. class A{ int i=1;     int f() { return i; }   }
    class B extends A {
       int i=2;                    // Shadows variable i in class A.
       int f() {return -i; }   // Overrids method f in class A.
    }
    public class override_test {
       public static void main(String args[]) {
           B b = new B();
           System.out.println(b.i);    // Refers to B.i; prints 2.
           System.out.println(b.f()); // Refers to B.f(); prints -2.
           A a = (A) b;              // Casts b to an instance of class A.
           System.out.println(a.i);    // Refers to A.i; prints 1.
           System.out.println(a.f()); // Refers to B.f(); prints -2.
       }
     }
   class C extends A {
      int i;                 // This variable shadows i in A.
      int f() {            // This method overrides f() in A.
         i=super.i+1;  // It retrieves A.i this way,
         return super.f()+i;  // And it invokes A.f() this way.
      }
   }
dynamic method lookup: final, static,private methods  final class
super.finalize();

8. class A {                      class B extends A{
     int p,q,r;                         int s;
    public A() {                 public B(a,b,c,d) {
     this(0,0,0);                     super(a,b,c);
    }                                      s=d;
    public A(int a) {            }   …
      this(a,0,0);                         
    }                                9. public class HelloWorld {
    public A(int a, b) {        public static void main(String args[]){
      this(a,b,0); }                   System.out.println("Hello World");
    public A(int a,b,c) {        }
      p=a; q=b; r=c; } …      }