public interface My Interface int i 0 public
הגדרת מנשק לדוגמא public interface My. Interface{ int i = 0; public static final int j = 0; void func 1(); public abstract void func 2(); static void print. I(){ System. out. println(i); } } Java בשפת 1 תוכנה אוניברסיטת תל אביב ניתן להגדיר שירותי מחלקה הנראות של. ( )פונקציות סטטיות שירותים אלה היא תמיד . public 11
IPoint package il. ac. tau. cs. software 1. shapes; y public interface IPoint { /** returns the x coordinate of the current point*/ public double x(); o rh theta /** returns the y coordinate of the current point*/ public double y(); x /** 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(); Java בשפת 1 תוכנה אוניברסיטת תל אביב 13
/** returns the distance between the current point and other */ public double distance(IPoint other); /** returns a point that is symmetrical to the current point * with respect to X axis */ public IPoint symmetrical. X(); /** returns a point that is symmetrical to the current point * with respect to Y axis */ public IPoint symmetrical. Y(); /** returns a string representation of the current point */ public String to. String(); /** 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); } Java בשפת 1 תוכנה אוניברסיטת תל אביב 14
/** @abst */ y OR this x OR y y this OR x this OR this x x x public interface IPoint { /** returns the x coordinate of the current point */ public double x(); @abst y this $ret x y /** returns the x coordinate of the current point @abst */ public double y(); $ret this x y this /** returns the distance between the current point and (0, 0) @abst */ public double rho(); $ret x y this $ret /** returns the angle between the current point and the abscissa @abst */ Java בשפת 1 תוכנה public double theta(); אוניברסיטת תל אביב x 16
/** returns a point that is symmetrical to the current point * with respect to X axis */ y $ret public IPoint symmetrical. X(); x this /** returns a point that is symmetrical to the current point * with respect to Y axis */ public IPoint symmetrical. Y(); מפיקות /** move the current point by dx and dy */ public void translate(double dx, double dy); y $ret this x this y dy $prev(this) x dx /** rotate the current point by angle degrees with respect * to (0, 0) */ y public void rotate(double angle); פקודות Java בשפת 1 תוכנה אוניברסיטת תל אביב this angle $prev(this) x 19
top. Right public class Rectangle { private IPoint top. Right; private IPoint bottom. Left; bottom. Left /** constructor using points */ public Rectangle(IPoint bottom. Left, IPoint top. Right) { this. bottom. Left = bottom. Left; this. top. Right = top. Right; } /** constructor using coordinates */ public Rectangle(double x 1, double y 1, double x 2, double y 2) { top. Right = ? ? ? ; bottom. Left = ? ? ? ; } Java בשפת 1 תוכנה אוניברסיטת תל אביב 22
/** returns a point representing the bottom-right corner of the rectangle*/ public IPoint bottom. Right() { return ? ? ? ; } /** returns a point representing the top-left corner of the rectangle*/ public IPoint top. Left() { return ? ? ? ; } /** returns a point representing the top-right corner of the rectangle*/ public IPoint top. Right() { return top. Right; } /** returns a point representing the bottom-left corner of the rectangle*/ public IPoint bottom. Left() { return bottom. Left; } Java בשפת 1 תוכנה אוניברסיטת תל אביב 23
שאילתות /** returns the horizontal length of the current rectangle */ public double width(){ return top. Right. x() - bottom. Left. x(); } /** returns the vertical length of the current rectangle */ public double height(){ return top. Right. y() - bottom. Left. y(); } /** returns the length of the diagonal of the current rectangle */ public double diagonal(){ return top. Right. distance(bottom. Left); } Java בשפת 1 תוכנה אוניברסיטת תל אביב 24
Rectangle מימוש פקודות /** move the current rectangle by dx and dy */ public void translate(double dx, double dy){ top. Right. translate(dx, dy); bottom. Left. translate(dx, dy); } Java בשפת 1 תוכנה אוניברסיטת תל אביב 25
/** constructor using points */ public Rectangle(IPoint bottom. Left, IPoint top. Right) { this. bottom. Left = bottom. Left; this. top. Right = top. Right; } ? מה הבעייתיות במימוש הזה /** returns a point representing the top-right corner of the rectangle*/ public IPoint top. Right() { return top. Right; } /** returns a point representing the bottom-left corner of the rectangle*/ public IPoint bottom. Left() { return bottom. Left; ? ובזה } Java בשפת 1 תוכנה אוניברסיטת תל אביב 27
". . . "ופרצת Rectangle r = new Rectangle(…); IPoint bl = r. bottom. Left(); bl. translate(10. 0, 0. 0); r bl מלבן Java בשפת 1 תוכנה אוניברסיטת תל אביב 28
Cartesian. Point
public class Cartesian. Point implements IPoint { private double x; private double y; public Cartesian. Point(double x, double y) { this. x = x; this. y = y; } public double x() { return x; } public double y() { return y; } public double rho() (tradeoff) קיים מאזן : בין מקום וזמן • תכונה שנשמרת כשדה תופסת מקום בזכרון אך חוסכת זמן גישה • תכונה שממומשת כפונקציה חוסכת מקום אך דורשת זמן חישוב בכל גישה { return Math. sqrt(x*x + y*y); public double theta() { return Math. atan 2(y, x); Java בשפת 1 תוכנה אוניברסיטת תל אביב } } 38
// this works also if other is not Cartesian. Point! public double distance(IPoint other) { return Math. sqrt((x-other. x()) * (x-other. x()) + (y-other. y())*(y-other. y())); } public IPoint symmetrical. X() { return new Cartesian. Point(x, -y); } public IPoint symmetrical. Y() { return new Cartesian. Point(-x, y); } public void translate(double dx, double dy) { x += dx; y += dy; } Java בשפת 1 תוכנה אוניברסיטת תל אביב 39
public String to. Cartesian. String(){ return "(x=" + x + ", y=" + y + ")"; } (? )מדוע IPoint אינה חלק מהמנשק public String to. String(){ return "(x=" + x + ", y=" + y + ", r=" + rho() + ", theta=" + theta() + ")"; } IPoint חלק מהמנשק public void rotate(double angle) { double current. Theta = theta(); double current. Rho = rho(); x = current. Rho * Math. cos(current. Theta+angle); y = current. Rho * Math. sin(current. Theta+angle); } } Java בשפת 1 תוכנה אוניברסיטת תל אביב 40
Polar. Point
public class Polar. Point implements IPoint { private double r; private double theta; זמן הפוך - המאזן מקום במקרה זה בעקבות בחירת השדות public Polar. Point(double r, double theta) { this. r = r; this. theta = theta; } public double x() { return r * Math. cos(theta); } public double y() { return r * Math. sin(theta); } public double rho() { return r; public double theta() { return theta; } } Java בשפת 1 תוכנה אוניברסיטת תל אביב 42
public double distance(IPoint other) { double delta. X = x()-other. x(); double delta. Y = y()-other. y(); return Math. sqrt(delta. X*delta. X + delta. Y*delta. Y); } public IPoint symmetrical. X() { return new Polar. Point(r, -theta); } public IPoint symmetrical. Y() { return new Polar. Point(r, Math. PI-theta); } Java בשפת 1 תוכנה אוניברסיטת תל אביב 43
public void translate(double dx, double dy) { double new. X = x() + dx; double new. Y = y() + dy; r = Math. sqrt(new. X*new. X + new. Y*new. Y); theta = Math. atan 2(new. Y, new. X); } public void rotate(double angle) { theta += angle; } Java בשפת 1 תוכנה אוניברסיטת תל אביב 44
public String to. Radian. String() { return "theta=" + theta ; } public String to. Degree. String() { return "theta=" + theta*180. 0/Math. PI; } IPoint אינה חלק מהמנשק public String to. String() { return "(x=" + x() + ", y=" + y() + ", r=" + r + ", theta=" + theta + ")"; } IPoint חלק מהמנשק } Java בשפת 1 תוכנה אוניברסיטת תל אביב 45
Smart. Point
/** @imp_inv polar||cartesian , “at least one of the representations is valid” * * @imp_inv polar && cartesian $implies * x == r * Math. cos(theta) && y == r * Math. sin(theta) */ public class Smart. Point implements IPoint { private double x; private double y; private double r; private double theta; private boolean cartesian; private boolean polar; /** Constructor using cartesian coordinates */ public Smart. Point(double x, double y) { this. x = x; this. y = y; cartesian = true; } Java בשפת 1 תוכנה אוניברסיטת תל אביב 47
/** make x, y consistent */ private void set. Cartesian(){ if (!cartesian){ x = r * Math. cos(theta); y = r * Math. sin(theta); cartesian = true; } } /** make r, theta consistent */ private void set. Polar(){ if (!polar){ r = Math. sqrt(x*x + y*y); theta = Math. atan 2(y, x); polar = true; } } Java בשפת 1 תוכנה אוניברסיטת תל אביב 48
לרקוד על שתי החתונות public double x() { set. Cartesian(); return x; } public double rho() { set. Polar(); return r; } public double y() { set. Cartesian(); return y; } public double theta() { set. Polar(); return theta; } Java בשפת 1 תוכנה אוניברסיטת תל אביב 49
דוגמאות שימוש בנקודות Polar. Point polar = new Polar. Point(Math. sqrt(2. 0), (1. 0/6. 0)*Math. PI); // theta now is 30 degrees polar. rotate((1. 0/12. 0)*Math. PI); // rotate 15 degrees polar. translate(1. 0, 1. 0); System. out. println(polar. to. Degree. String()); Cartesian. Point cartesian = new Cartesian. Point(1. 0, 1. 0); cartesian. rotate((1. 0/2. 0)*Math. PI); cartesian. translate(-1. 0, 1. 0); System. out. println(cartesian. to. Cartesian. String()); Java בשפת 1 תוכנה אוניברסיטת תל אביב 52
שימוש במנשקים IPoint polar = new Polar. Point(Math. sqrt(2. 0), (1. 0/6. 0)*Math. PI); // theta now is 30 degrees polar. rotate((1. 0/12. 0)*Math. PI); // rotate 15 degrees polar. translate(1. 0, 1. 0); System. out. println(polar. to. Degree. String()); // Compilation Error IPoint cartesian = new Cartesian. Point(1. 0, 1. 0); cartesian. rotate((1. 0/2. 0)*Math. PI); cartesian. translate(-1. 0, 1. 0); System. out. println(cartesian. to. Cartesian. String()); // Compilation Error Java בשפת 1 תוכנה אוניברסיטת תל אביב 54
שימוש במנשקים IPoint polar = new Polar. Point(Math. sqrt(2. 0), (1. 0/6. 0)*Math. PI); // theta now is 30 degrees polar. rotate((1. 0/12. 0)*Math. PI); // rotate 15 degrees polar. translate(1. 0, 1. 0); System. out. println(polar. to. String()); // Now OK! IPoint cartesian = new Cartesian. Point(1. 0, 1. 0); cartesian. rotate((1. 0/2. 0)*Math. PI); cartesian. translate(-1. 0, 1. 0); System. out. println(cartesian. to. String()); // Now OK! IPoint point = new IPoint (1. 0, 1. 0); // Compilation Error Java בשפת 1 תוכנה אוניברסיטת תל אביב 55
העברת ארגומנטים לפונקציות void expect. Point(IPoint p); void expect. Cartesian(Cartesian. Point c); void bar() { IPoint p = new Cartesian. Point(. . . ); Cartesian. Point c = new Cartesian. Point(. . . ); p = c; expect. Cartesian(c); expect. Point(p); expect. Cartesian(p); } Java בשפת 1 תוכנה אוניברסיטת תל אביב 59
ארגומנטים והשמות void foo(IPoint p, Smart. Point smart, Cartesian. Point c) { IPoint local. P; Smart. Point local. S; Cartesian. Point local. C; local. P = p; local. P = smart; local. P = c; // ERROR local. S = p; local. S = smart; // ERROR local. S = c; local. C = p; // ERROR local. C = smart; local. C = c; } Java בשפת 1 תוכנה אוניברסיטת תל אביב 60
שימוש במלבן IPoint tr = new Polar. Point(3. 0, (1. 0/4. 0)*Math. PI); // theta now is 45 degrees IPoint bl = new Cartesian. Point(1. 0, 1. 0); Rectangle rect = new Rectangle(bl, tr); System. out. println("Diagonal of rect is: " + rect. diagonal()); rect. translate(1, -2); System. out. println("Diagonal of rect stayed: " + rect. diagonal()); Java בשפת 1 תוכנה אוניברסיטת תל אביב 64
מבני נתונים פולימורפים point. Arr Smart. Point Cartesian. Point Polar. Point Simple. Point Smart. Point Java בשפת 1 תוכנה אוניברסיטת תל אביב 66
מבני נתונים פולימורפים IPoint [] point. Arr = new IPoint[3]; point. Arr[0] = new Smart. Point(1, 2); point. Arr[1] = new Cartesian. Point(1, 3); point. Arr[2] = new Polar. Point(1, 0. 5*Math. PI); for (IPoint point : point. Arr) { point. translate(1. 0, 2. 0); } Java בשפת 1 תוכנה אוניברסיטת תל אביב עבור כל נקודה תורץ גירסת ה " ה"נכונה translate 67
ריבוי מנשקים public interface I 1 { public void method. From. I 1(); } public interface I 2 { public void method. From. I 2(); } public class C implements I 1, I 2, I 3 { public void method. From. I 1() {…} public void method. From. I 2() {…} public void method. From. I 3() {…} public void another. Method() {…} } public interface I 3 { public void method. From. I 3(); } Java בשפת 1 תוכנה אוניברסיטת תל אביב 69
המפעל : תבנית עיצוב (factory design pattern)
public class Point. Factory { public Point. Factory(boolean using. Cartesian, boolean using. Polar) { this. using. Cartesian = using. Cartesian; this. using. Polar = using. Polar; } public Point. Factory() { this(false, false); } public IPoint create. Point(double x, double y) { if (using. Cartesian && !using. Polar) return new Cartesian. Point(x, y); if (using. Polar && !using. Cartesian) return new Polar. Point(Math. sqrt(x*x + y*y), Math. atan 2(y, x)); return new Smart. Point(x, y); } private boolean using. Cartesian; private boolean using. Polar; } Java בשפת 1 תוכנה אוניברסיטת תל אביב 79
public class Rectangle { private Point. Factory factory; private IPoint top. Right; private IPoint bottom. Left; Rectangle כעת אין למחלקה תלות במחלקת הנקודה כלל /** constructor using points */ public Rectangle(IPoint bottom. Left, IPoint top. Right, Point. Factory factory) { this. bottom. Left = bottom. Left; this. top. Right = top. Right; this. factory = factory; } /** constructor using coordinates */ public Rectangle(double x 1, double y 1, double x 2, double y 2 , Point. Factory factory) { this. factory = factory; top. Right = factory. create. Point(x 1, y 1); bottom. Left = factory. create. Point(x 2, y 2); } Java בשפת 1 תוכנה אוניברסיטת תל אביב 80
/** Default Constructor for private use */ private Smart. Point(){ } מה היה קורה אם היינו מסירים את הבנאי ? הפרטי מהמימוש public static Smart. Point create. Polar(double r, double theta) { Smart. Point result = new Smart. Point(); result. r = r; result. theta = theta; result. polar = true; return result; } public static Smart. Point create. Cartesian(double x, double y) { Smart. Point result = new Smart. Point(); result. x = x; result. y = y; result. cartesian = true; return result; } Java בשפת 1 תוכנה אוניברסיטת תל אביב 84
- Slides: 86