4 class B protected int a protected int
4 שימוש בשירות המקורי מתוך השירות הדורס class B { protected int a; protected int b; public String to. String(){ return "a: " + this. a + " b: " + this. b; } } class C extends B{ private int c; public String to. String(){ return super. to. String() + " c: " + this. c; } }
7 IPoint הממשק public interface IPoint { /** returns the x coordinate of the current point*/ public double get. X(); /** returns the y coordinate of the current point*/ public double get. Y(); /** returns the distance between the current point and (0, 0) */ public double rho(); /** returns the angle between the current point and the abscissa */ public double theta(); /** move the current point by dx and dy */ public void translate(double dx, double dy); /** rotate the current point by angle degrees with respect to (0, 0) */ public void rotate(double angle); … }
10 דוגמא - מחלקות מופשטות public abstract class A { public void f() { System. out. println(“A. f!!”); } abstract public void g(); } A a = new A(); X public class B extends A { public void g() { System. out. println(“B. g!!”); } } A a = new B();
11 Cartesian. Point Polar. Point private double x; private double y; private double r; private double theta; public Cartesian. Point(double x, double y) { this. x = x; this. y = y; } public Polar. Point(double r, double theta) { this. r = r; this. theta = theta; } public double get. X() { return x; } public double get. X() { return r * Math. cos(theta); } public double get. Y() { return y; } public double get. Y() { return r * Math. sin(theta); } public double rho() { return Math. sqrt(x*x + y*y); } public double rho() { return r; } public double theta() { return Math. atan 2(y, x); } public double theta() { return theta; . קשה לראות דמיון בין מימושי המתודות במקרה זה המתודות בסיסיות ויש להן קשר הדוק לייצוג שנבחר לשדות 4 כל }
13 Cartesian. Point public double distance(IPoint other) { return Math. sqrt((x-other. get. X()) * (x-other. get. X()) + (y-other. get. Y())*(y-other. get. Y())); } Polar. Point public double distance(IPoint other) { double delta. X = get. X()-other. get. X(); double delta. Y = get. Y()-other. get. Y(); return Math. sqrt(delta. X * delta. X + delta. Y * delta. Y); } . . . נראה מה ניתן לעשות , הקוד דומה אבל לא זהה delta. Y - ו delta. X ע"י הוספת משתני העזר Cartesian. Point ננסה לשכתב את
14 Cartesian. Point public double distance(IPoint other) { double delta. X = x-other. get. X(); double delta. Y = y-other. get. Y(); Polar. Point public double distance(IPoint other) { double delta. X = get. X()-other. get. X(); double delta. Y = get. Y()-other. get. Y(); return Math. sqrt(delta. X * delta. X + delta. Y * delta. Y); return Math. sqrt(delta. X * delta. X + (delta. Y * delta. Y ); } } : נשאר הבדל אחד – get. X() להיות x נחליף את במאזן ביצועים לעומת כלליות נעדיף תמיד את הכלליות
15 Cartesian. Point public double distance(IPoint other) { double delta. X = get. X()-other. get. X(); double delta. Y = get. Y()-other. get. Y(); Polar. Point public double distance(IPoint other) { double delta. X = get. X()-other. get. X(); double delta. Y = get. Y()-other. get. Y(); return Math. sqrt(delta. X * delta. X + delta. Y * delta. Y ); } } ! שתי המתודות זהות לחלוטין Abst. Point עתה ניתן להעביר את המתודה למחלקה Polar. Point - ו Cartesian. Point ולמחוק אותה מהמחלקות
16 Cartesian. Point public String to. String(){ return "(x=" + x + ", y=" + y + ", r=" + rho() + ", theta=" + theta() + ")"; } Polar. Point public String to. String() { return "(x=" + get. X() + ", y=" + get. Y() + ", r=" + r + ", theta=" + theta + ")"; } to. String תהליך דומה ניתן גם לבצע עבור
17 מימוש המחלקה האבסטרקטית public abstract class Abstract. Point implements IPoint{ public double distance(IPoint other) { double delta. X = get. X()-other. get. X(); double delta. Y = get. Y()-other. get. Y(); return Math. sqrt(delta. X * delta. X delta. Y ); + delta. Y * } public String to. String() { return "(x=" + get. X() + ", y=" + get. Y() + ", r=" + rho() + ", theta=" + theta() + ")"; } }
18 ירושה מהמחלקה האבסטרקטית public class Polar. Point extends Abstract. Point{ private double r; private double theta; public Polar. Point(double r, double theta) { this. r = r; this. theta = theta; } @Override public double get. X() { return r * Math. cos(theta); } @Override public void rotate(double angle) { theta += angle; } … }
19 חריגים . • נממש שירות המחשב ממוצע הרמוני על אוסף של מספרים public static double harmonic. Mean(Collection<Integer> numbers){ if (numbers. is. Empty()){ return 0; } double denominator = 0; for (int i : numbers){ denominator += 1. 0/i; } return numbers. size()/ denominator; } ממוצע הרמוני מוגדר רק על מספרים : שאלה חיובי - מה נעשה אם נקבל מספר אי. חיוביים ? ברשימה
21 חריגים public static double harmonic. Mean(Collection<Integer> numbers) throws Exception{ if (numbers. is. Empty()){ return 0; } double denominator = 0; for (int i : numbers){ if (i <= 0){ throw new Exception("wrong value in list: " + i); } denominator += 1. 0/i; } return numbers. size()/denominator; } עלינו לייצר אובייקט חדש מטיפוס ולהשתמש במילה Exception בשביל לזרוק את throw השמורה השגיאה מצהירים על שגיאה שנזרקת בשירות
22 חריגים משם קובץ לאוסף : • נוסיף שירות נוסף – השירות מקבל מפה . ומדפיס ממוצע הרמוני עבור כל קובץ , המספרים שהוא מכיל public static void print. Means. By. Files(Map<String, Collection<Integer>> numbers) { for (Map. Entry<String, Collection<Integer>> map. Entry: numbers. entry. Set()){ double h. Mean. For. File = harmonic. Mean(map. Entry. get. Value()); System. out. println("for file: " + map. Entry. get. Key() + " h. Mean is: " + h. Mean. For. File); } } Exception בקוד הזה יש שגיאת קומפילציה בגלל שגיאה שלא הצהרנו עליה אך גם לא טיפלנו בה
23 חריגים ורק נצהיר עליו , לא נטפל בחריג : • אפשרות ראשונה מי שיצטרך להתמודד עם הטיפול בחריג הוא השירות , • במקרה הזה . print. Means. By. Files שיקרא ל public static void print. Means. By. Files(Map<String, Collection<Integer>> files. Info) throws Exception{ for (Map. Entry<String, Collection<Integer>> map. Entry: files. Info. entry. Set()){ double h. Mean. For. File = harmonic. Mean(map. Entry. get. Value()); System. out. println("for file: " + map. Entry. get. Key() + " h. Mean is: " + h. Mean. For. File); } }
24 חריגים ! נטפל בחריג : • אפשרות שניה public static void print. Means. By. Files(Map<String, Collection<Integer>> files. Info) { for (Map. Entry<String, Collection<Integer>> map. Entry: files. Info. entry. Set()){ try{ double h. Mean. For. File = harmonic. Mean(map. Entry. get. Value()); System. out. println("for file: " + map. Entry. get. Key() + " h. Mean is: " + h. Mean. For. File); } catch (Exception e){ System. out. println("cannot calculate h. Mean for file " + map. Entry. get. Key()); } } }
25 חריגים ? • איך זה עובד public static void main(String[] args){ Map<String, Collection<Integer>> files = new Linked. Hash. Map<>(); files. put("file 1", Arrays. as. List(1, 2, 3)); files. put("file 2", Arrays. as. List(1, 2, -4)); files. put("file 3", Arrays. as. List(15, 17, 30)); print. Means. By. Files(files); } : • תוכנית זו מייצרת את הפלט
27 חריגים public static double harmonic. Mean(Collection<Integer> numbers) throws Exception{ if (numbers. is. Empty()) … } Null. Pointer. Exception public static void print. Means. By. Files(Map<String, Collection<Integer>> files. Info) … catch (Exception e){ System. out. println("cannot calculate h. Mean for file " + map. Entry. get. Key()); } … }
29 יצירת טיפוס חריג חדש Exception ירושה מ class HMean. Exception extends Exception{ public HMean. Exception(String message) { super("Harmonic Mean calculation error! " + message); } } קריאה לבנאי של מחלקת האב – קריאה זו תמיד תהיה הפקודה הראשונה של הבנאי
30 שימוש בטיפוס החריג החדש public static double harmonic. Mean(Collection<Integer> numbers) throws HMean. Exception { if (numbers. is. Empty()){ return 0; } double denominator = 0; for (int i : numbers){ if (i <= 0){ throw new HMean. Exception("wrong value in list: " + i); } denominator+ = 1. 0/i; } return numbers. size()/denominator; }
31 שימוש בטיפוס החריג החדש public static void print. Means. By. Files(Map<String, Collection<Integer>> files. Info) … catch (HMean. Exception e){ System. out. println("cannot calculate h. Mean for file " + map. Entry. get. Key()); } … } הבלוק הזה יטפל רק בשגיאה שזרקנו מתוך חריגים , harmonic. Mean. אחרים יזרקו הלאה
32 שימוש בשגיאות – פורמט הודעת השגיאה public static void print. Means. By. Files(Map<String, Collection<Integer>> files. Info) … catch (HMean. Exception e){ System. out. println("cannot calculate h. Mean for file " + map. Entry. get. Key()); e. print. Stack. Trace(); } } public static void main(String[] args){ Map<String, Collection<Integer>> files = new Linked. Hash. Map<>(); files. put("file 2", Arrays. as. List(1, 2, -4)); files. put("file 3", Arrays. as. List(15, 17, 30)); print. Means. By. Files(files); } : • עבור תוכנית זו נקבל את הפלט
33 שימוש בשגיאות : • הדפסת פורמט שגיאה מצומצם יותר public static void print. Means. By. Files(Map<String, Collection<Integer>> files. Info) … catch (HMean. Exception e){ System. out. println("cannot calculate h. Mean for file " + map. Entry. get. Key()); System. out. println(e. get. Message()) } } : • פלט התוכנית יהיה class HMean. Exception extends Exception{ public HMean. Exception(String message) { super("Harmonic Mean calculation error! " + message); }
- Slides: 32