6# שיעור  JAVA


1. abstract class Poel {
    //    משתני מופע, קונסטרוקטרים, ושיטות משותפות לכל פועל  
    public abstract int chishuvMaskoret();  }    
    
     class poelChodshi extends Poel {
     //        משתני מופע חדשים, קונסטרוקטורים ושיטות חדשות
    //                   .עבור פועלים המקבלים משכורת חודשית
     public int chishuvMaskoret() {
    //                              קוד לחישוב משכורת חודשית 
       }
     }
2. public interface ClassName {
       public int methodA();
       public void methodB(int a, int b);
       public boolean methodC(int a);
     }
     class UseClass implements ClassName {

3.  public void getContent() {
        try {
          openConnection();     -->    ( * )
          readData();
       }
       catch(IOException e) {
          //   Handle IO error....
       }    ....
    }
     ( * )    public void openConnection throws IOException {
                   openSocket();
                   sendRequest();    ( ** )
                   receiveResponse();
                }
( ** ) public void sendRequest throws IOException {
            write(header);
            write(body);     // IO error!!!
         }

4. interface Teacher {
      void teach();
}
interface ComputerTeacher extends Teacher {
  void programs();
}
interface RoleModel {
  void guide();
}
class EstherParnas implements ComputerTeacher {
  public void teach() { }
  public void programs() { }
}
interface MichlalahTeacher
  extends ComputerTeacher, RoleModel {
    void setsExample();
}
class Michlalah {
  static void j(Teacher e) {e.teach(); }
  static void v(ComputerTeacher p) {
    p.teach();
    p.programs();
  }
  public static void main(String[] args) {
    EstherParnas yael = new EstherParnas();
    j(yael);
    v(yael);
  }
}

5.  package c07;
      public interface Months {
        int
         JANUARY = 1, FEBRUARY = 2, MARCH = 3,
         APRIL = 4,  MAY = 5,  JUNE = 6,  JULY = 7,
         AUGUST = 8,  SEPTEMBER = 9, OCTOBER = 10,
         NOVEMBER = 11,  DECEMBER = 12;
    }

6. import java.util.*;
interface RandVals {
  int rint = (int) (Math.random() * 10);
  long rlong = (long) (Math.random() * 10);
  float rfloat = (float) (Math.random() * 10);
  double rdouble = Math.random() * 10; }
public class TestRandVals {
  public static void main(String args[]) {
    System.out.println(RandVals.rint);
    System.out.println(RandVals.rlong);
    System.out.println(RandVals.rfloat);
    System.out.println(RandVals.rdouble); }
}

7. public class Parcel {
  class Contents {
    private int i=11;
    public int value() { return i; }
  }
  class Destination {
    private String label;
    Destination (String whereTo) {
      label = whereTo;
    }
    String readLabel() { return label; }
  }
  public Destination to(String s) {
    return new Destination(s);
  }
  public Contents cont() { return new Contents(); }
  public void ship(String dest) {
    Contents c = cont();
    Destination d = to(dest);
  }
  public static void main(String args[]) {
    Parcel p = new Parcel();
    p.ship("Israel");
    Parcel q = new Parcel();
    Parcel.Contents c = q.cont();
    Parcel.Destination d = q.to("USA");  }
}

8.abstract class Contents {
  abstract public int value();
}
interface Destination {
  String readLabel();
}
public class Parcel2 {
  private class PContents extends Contents {
    private int i=11;
    public int value() { return i; }
  }
  protected class PDestination
    implements Destination {
   private String label;
   private PDestination(String whereTo) {
     label = whereTo;
   }
   public String readLabel() {return label; }
  }
  public Destination dest(String s) {
    return new PDestination(s);
  }
  public Contents cont() {
    return new PContents();
  }
  public static void main(String args[]) {
    Parcel2 p = new Parcel2();
    Contents c = p.cont();
    Destination d = p.dest("Israel");
    //  Parcel2.PContents c = p.new PContents();
  }
}

9. public class PassHandles {
  static void f(PassHandles h) {
    System.out.println('h inside f(): " + h);
  }
  public static void main(String args[]) {
    PassHandles p = new PassHandles();
    System.out.println("p inside main(): " + p);
    f(p);
  }
}

10. public class Alias1 {
  int i;
  Alias1(int ii) { i=ii; }
  public static void main(String args[]) {
    Alias1 x = new Alias1(7);
    Alias1 y = x;
    System.out.println("x: "+x.i);
    System.out.println("y: "+y.i);
    System.out.println("Incrementing x");
    x.i++;
    System.out.println("x: "+x.i);
    System.out.println("y: "+y.i);
  }
}

11. public class Alias2 {
  int I;
  Alias2(int ii) { i = ii; }
   static void f(Alias2 handle) {
    handle.i++;
  }
  public static void main(String args[]) {
    Alias2 x = new Alias2(7);
    System.out.println(x: " + x.i);
    System.out.println("Calling f(x)...");
    f(x);
    System.out.println("x: " + x.i);
  }
}

12. enumeration     dynamic data structure
import java.util.Enumeration;
// Class describing each element of a Datagram queue.
class DataGramHolder {
Datagram contents;
DataGramHolder link;
public DataGramHolder(Datagram d) { 
  link = null;
  contents = d;
}
public Datagram getContents() {
  return contents
}
public DataGramHolder getLink() {
  return link;
}
}
//
//  Class describing a DataGram queue.
//
class DataGramQ {
private DataGramHolder head, tail;
private int noInQ;
public DataGramQ() {
  head = null;
  tail = null;
  noInQ = 0;
}
public boolean emptyQ() {
  return noInQ == 0;
}
public int dataGramInQ() {
  return noInQ;
}
public void addDataGram(Datagram a) {
DataGramHolder di = new DataGramHolder(a);
  if (noInQ == 0) {
    head = di;
    tail = di; }
 else {
    tail.link = di;
    tail = di;  }
  noInQ++;
}
public Datagram removeDataGram()
throws EmptyQueueException {
  if (noInQ == 0)
    throw new EmptyQueueException();
 else {
    noInQ--;
    Datagram h = head.getContents();
    head = head.getLink();
    return h; }
}
public QueueEnumeration elements() {
  return new QueueEnumeration(head);
}
//
// Class describing the iterator for a DataGram queue.
//
class QueueEnumeration implements Enumeration {
private DataGramHolder cursor;
public QueueEnumeration(DataGramHolder d) {
  cursor = d; }
public boolean hasMoreElements() {
  return cursor != null;
}
public Object nextElement() {
  Object o = cursor.getContents();
  cursor = cursor.getLink();
  return o;
{

DataGramQ dq;
Object dGram;
dq = new DataGramQ();
  //                           DQ-קוד להוסיף איברים ל
QueueEnumeration qe = dq.elements();
while (qe.hasMoreElements()) {
  dGram = qe.nextElement();
  System.out.println(" Element = " +
        ((Datagram) dGram).getDatagramValue());
}

13.  encapsulation       dynamic binding
     multiple inheritance
        public class C extends B implements E; 
     package telNetCommsPackage;
     sutility.class                  package sutility;
                                 // קוד להגדרת החבילה  
     import projectLibrary.general.utilities.sutility;