1 / 18

נתבונן בפונקציה הבאה

נתבונן בפונקציה הבאה. public static int min(int[] a,int n) { int min = a[0]; for (int i = 1; (i < n ) && (i < a.length) ; i++) if (min > a[i]) min = a[i]; return min; }. הפונקציה מחזירה לכל מערך של מספרים שלמים את המספר הנמוך ביותר בו.

Télécharger la présentation

נתבונן בפונקציה הבאה

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. נתבונן בפונקציה הבאה public static int min(int[] a,int n) { int min = a[0]; for (int i = 1; (i < n ) && (i < a.length) ; i++) if (min > a[i]) min = a[i]; return min; } הפונקציה מחזירה לכל מערך של מספרים שלמים את המספר הנמוך ביותר בו. ניתן כמובן לכתוב פונקציה כזו עבור טיפוסי עצמים רבים ושונים זה מזה: מספרים, מחרוזות, תלמידים וכו'. היינו מעדיפים לכתוב את הקוד פעם אחת.

  2. הכלי המאפשר לנו זאת הוא הממשק Comparable (interface) . המוגדר על ידי // public int compareTo(Object obj) returns: // a positive integer if this > obj // zero if this = obj // a negative integer if this < obj Public interface Comparable { public int compareTo(Object obj); }

  3. כעת ניתן לכתוב את הפונקציה מחדש בתוך המחלקה Util: public class Util { public static Comparable min(Comparable[] a,int n) { Comparable min = a[0]; for (int i = 1; (i < a.length) && (a[i] != null) && (i < n); i++) if (min.compareTo(a[i]) > 0) min = a[i]; return min; }

  4. נותר רק ליישם את הממשק ע"י המחלקות השונות לדוגמה public class Integer implements Comparable { : : public int compareTo(Object other) { if (! (other instanceof Integer)) { System.out.println(“Cannot compare Integer to “+other); return 10000; } if (intValue() >((Integer) other).intValue()) return 1; if (intValue() <((Integer) other).intValue()) return -1; return 0; } : }

  5. Public class String implements Comparable { : : public int compareTo(String other) public int compareTo(Object other) { if (! (other instanceof Integer)) { System.out.println(“Cannot compare Integer to “+other); return 10000; } for (int i = 0; (i < size() )& (i <other.size()); i++) { if (charAt(i) > other.charAt(i)) return 1; if (charAt(i) < other.charAt(i)) return -1; } if (size() >other.size()) return 1; if (size() < other.size()) return –1; return 0; } או

  6. וגם public class TwoDvector implements Comparable{ private double x,y; public TwoDvector(double x,double y) { this.x = x; this.y = y; } public String toString() { return "( "+x+" , "+y+" )"; } public double x() { return x;} public double y() { return y;}

  7. public int compareTo(Object compare_me) { if (! (compare_me instanceof TwoDvector)) { System.out.println("cannot compare a "+ "twoDvector to "+compare_me); return 100000; } TwoDvector other = (TwoDvector) compare_me; double my_length = Math.sqrt(x*x+y*y); double other_length = Math.sqrt(other.x()*other.x()+ other.y()*other.y()); if (my_length > other_length) return 1; if (my_length < other_length) return -1; return 0; } }

  8. public class TwoDvector_driver { public static void main(String[] args) { TwoDvector[] vectors = new TwoDvector[100]; vectors[0] = new TwoDvector(1,2); vectors[1] = new TwoDvector(0.5,2); vectors[2] = new TwoDvector(0.5,1); System.out.println(Tools.min(vectors,3)); } } הפלט ( 0.5 , 1.0 )

  9. ניתבונן כעת בממשק Set public interface Set { public boolean add(Object addMe); public Iterator iterator(); public boolean contains(Object obj); public boolean equals(Set other); public int size(); }

  10. ובממשק Iterator public interface Iterator { public boolean hasNext(); public Object next(); }

  11. נציג ישום אחד שלהם במחלקה SimpleSet public class SimpleSet implements Set{ public static final int DEFAULT_CAPACITY = 100; private int capacity; private int n_members; private Object[] members; public SimpleSet() { this(DEFAULT_CAPACITY); } public SimpleSet(int capacity) { this.capacity = capacity; members = new Object[capacity]; }

  12. public boolean add(Object add_me) { if (contains(add_me)) return false; if (n_members == capacity) { capacity = capacity + DEFAULT_CAPACITY ; Object[] new_members = new Object[capacity]; for (int i = 0; i < n_members; i++) new_members[i] = members[i]; members = new_members; } members[n_members] = add_me; n_members++; return true; }

  13. public boolean contains(Object other) { for (int i = 0; i < n_members; i++) if (members[i].equals(other)) return true; return false; } public int size() { return n_members; } public Iterator iterator() { return (Iterator) new SimpleSetIterator(); }

  14. public class SimpleSetIterator implements Iterator { private int current; public SimpleSetIterator() { current = 0; } public boolean hasNext() { return current < n_members; } public Object next() { current = current+1; return members[current - 1]; } }

  15. public boolean equals (Object test_me) { if (! (test_me instanceof Set)) return false; Set other = (Set) test_me; if (size() != other.size()) return false; Iterator it = iterator(); while (it.hasNext()) if (! other.contains(it.next())) return false; return true; } } // class SimpleSet

  16. public class Set_driver { public static void main(String[] args) { SimpleSet my_set = new SimpleSet(2); my_set.add(new Integer(1)); my_set.add(new Double(50)); my_set.add("Happy new year"); Iterator it = my_set.iterator(); while (it.hasNext()) System.out.println(it.next());

  17. SimpleSet your_set = new SimpleSet(); your_set.add(new Double(50)); your_set.add(new Integer(1)); your_set.add("Happy new year"); it = your_set.iterator(); while (it.hasNext()) System.out.println(it.next()); System.out.println(my_set.equals(your_set)); System.out.println(my_set == your_set); } }

  18. פלט 1 50.0 Happy new year 50.0 1 Happy new year true false

More Related