1 / 24

תוכנה 1 סמסטר א' תשע"ב

תוכנה 1 סמסטר א' תשע"ב. תרגול מס' 7 * מנשקים, דיאגרמות וביטים. == vs equals. Point p1 = new Point(1,2) Point p 2 = new Point(1,2) p 1 == p 2 p 1.equals(p2 ) מתי נכון להשתמש בכל אחד מהם ? שימו לב , במחלקה שכתבתם בעצמכם יש לכתוב מתודת equals על מנת להשתמש בה .

kimn
Download Presentation

תוכנה 1 סמסטר א' תשע"ב

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. תוכנה 1סמסטר א' תשע"ב תרגול מס' 7 *מנשקים, דיאגרמות וביטים * לא בהכרח בסדר הזה

  2. == vs equals Point p1 = new Point(1,2) Point p2 = new Point(1,2) p1 == p2 p1.equals(p2) • מתי נכון להשתמש בכל אחד מהם ? • שימו לב, במחלקה שכתבתם בעצמכם יש לכתוב מתודת equalsעל מנת להשתמש בה. • אין להשתמש במתודת "ברירת מחדל" (יוסבר בהמשך הקורס)

  3. המערכת הבנקאית • נתאר את מערכת התוכנה שלנו בעזרת דיאגרמות • דיאגרמות סטטיות: • תיאור היחסים בין המחלקות השונות במערכת • דיאגרמות דינאמיות: • תיאור ההתנהגות של המערכת בזמן ריצה • מצב האוביקטים • תיאור של תרחיש

  4. Customer Bank public Customer(String name, String id) public String getName() public String getID() main … Class Diagram BankAccount public BankAccount(Customer customer, long id) public void withdraw(double amount) public void deposit(double amount) public void transferTo(double amount, BankAccount otherAccount) public double getBalance() public Customer getOwner() public double getNumber() * Aggregation(has-a) Association 1

  5. המחלקה Customer public class Customer { public Customer(String name, String id) { this.name = name; this.id = id; } public String getName() { return name; } public String getID() { return id; } private String name; private String id; }

  6. Toy Bank Program public class Bank { public static void main(String[] args) { Customer customer1 = new Customer("Avi Cohen", "025285244"); Customer customer2 = new Customer("Rita Stein", "024847638"); BankAccount account1 = new BankAccount(customer1, 1234); BankAccount account2 = new BankAccount(customer2, 5678); BankAccount account3 = new BankAccount(customer1, 2984); account1.deposit(1000); account2.deposit(500); account1.transferTo(100, account3); account2.withdraw(300); System.out.println("account1 has " + account1.getBalance()); System.out.println("account2 has " + account2.getBalance()); } }

  7. Object Diagram Customer: String name: String id: String: "Avi Cohen" String: "025285244" Bank main (…) { customer1: }

  8. Toy Bank Program public class Bank { public static void main(String[] args) { Customer customer1 = new Customer("Avi Cohen", "025285244"); Customer customer2 = new Customer("Rita Stein", "024847638"); BankAccount account1 = new BankAccount(customer1, 1234); BankAccount account2 = new BankAccount(customer2, 5678); BankAccount account3 = new BankAccount(customer1, 2984); account1.deposit(1000); account2.deposit(500); account1.transferTo(100, account3); account2.withdraw(300); System.out.println("account1 has " + account1.getBalance()); System.out.println("account2 has " + account2.getBalance()); } }

  9. Object Diagram Customer: String name: String id: String: "Avi Cohen" String: "025285244" Bank main (…) { customer1: customer2: } Customer: String name: String id: String: "Rita Stein" String: "024847638"

  10. Toy Bank Program public class Bank { public static void main(String[] args) { Customer customer1 = new Customer("Avi Cohen", "025285244"); Customer customer2 = new Customer("Rita Stein", "024847638"); BankAccount account1 = new BankAccount(customer1, 1234); BankAccount account2 = new BankAccount(customer2, 5678); BankAccount account3 = new BankAccount(customer1, 2984); account1.deposit(1000); account2.deposit(500); account1.transferTo(100, account3); account2.withdraw(300); System.out.println("account1 has " + account1.getBalance()); System.out.println("account2 has " + account2.getBalance()); } }

  11. Object Diagram BankAccount: owner: balance: 0 number: 1234 Customer: String name: String id: String: "Avi Cohen" BankAccount: owner: balance: 0 number: 2984 String: "025285244" Bank main (…) { customer1: customer2: account1: account2: account3: } Customer: String name: String id: String: "Rita Stein" BankAccount: owner: balance: 0 number: 5678 String: "024847638"

  12. Message Sequence Chart public class Bank { public static void main(String[] args) { Customer customer1 = new Customer("Avi Cohen", "025285244"); Customer customer2 = new Customer("Rita Stein", "024847638"); BankAccount account1 = new BankAccount(customer1, 1234); BankAccount account2 = new BankAccount(customer2, 5678); BankAccount account3 = new BankAccount(customer1, 2984); account1.deposit(1000); account2.deposit(500); account1.transferTo(100, account3); account2.withdraw(300); System.out.println("account1 has " + account1.getBalance()); System.out.println("account2 has " + account2.getBalance()); } }

  13. Message Sequence Chart account1 account2 account3 main time owner: balance: 900 number: 1234 owner: balance: 200 number: 5678 owner: balance: 100 number: 2984 deposit 1000 void deposit 500 void transferTo 100, account3 deposit 100 void void withdraw 300 void

  14. Output public class Bank { public static void main(String[] args) { Customer customer1 = new Customer("Avi Cohen", "025285244"); Customer customer2 = new Customer("Rita Stein", "024847638"); BankAccount account1 = new BankAccount(customer1, 1234); BankAccount account2 = new BankAccount(customer2, 5678); BankAccount account3 = new BankAccount(customer1, 2984); account1.deposit(1000); account2.deposit(500); account1.transferTo(100, account3); account2.withdraw(300); System.out.println("account1 has " + account1.getBalance()); System.out.println("account2 has " + account2.getBalance()); } } output: account1 has 900.0 account2 has 200.0

  15. מנשקים • מנשק (interface) הוא מבנה תחבירי ב Java המאפשר לחסוך בקוד לקוח • קוד אשר משתמש במנשק יוכל בזמן ריצה לעבוד עם מגוון מחלקות המממשות את המנשק הזה (ללא צורך בשכפול הקוד עבור כל מחלקה) • דוגמא: נגן מוזיקה אשר מותאם לעבוד עם קובצי מוזיקה (mp3) ועם קובצי וידאו (mp4)

  16. Playing Mp3 publicclass MP3Song { publicvoid play(){ // audio codec calculations, // play the song... { // does complicated stuff // related to MP3 format... { publicclass Player { privatebooleanrepeat; privatebooleanshuffle; publicvoidplaySongs(MP3Song[] songs) { do { if (shuffle) Collections.shuffle(Arrays.asList(songs)); for (MP3Song song : songs) song.play(); } while (repeat); } {

  17. Playing VideoClips publicclassVideoClip { publicvoid play(){ // video codec calculations, // play the clip ... { // does complicated stuff // related to MP4 format ... { publicclass Player { // same as before... publicvoidplayVideos(VideoClip[] clips) { do { if (shuffle) Collections.shuffle(Arrays.asList(clips)); for (VideoClipvideoClip : clips) videoClip.play(); } while (repeat); { {

  18. שכפול קוד publicvoidplaySongs(MP3Song[] songs) { do { if (shuffle) Collections.shuffle(Arrays.asList(songs)); for (MP3Song song : songs) song.play(); } while (repeat); } publicvoidplayVideos(VideoClip[] clips) { do { if (shuffle) Collections.shuffle(Arrays.asList(clips)); for (VideoClipvideoClip : clips) videoClip.play(); } while (repeat); } למרות ששני השרותים נקראים play() אלו פונקציות שונות! נרצה למזג את שני קטעי הקוד

  19. שימוש במנשק public void play (Playable[] items) { do { if (shuffle) Collections.shuffle(Arrays.asList(items)); for (Playable item : items) item.play(); } while (repeat); } public interfacePlayable { public void play(); }

  20. מימוש המנשק ע"י הספקים • public class VideoClipimplementsPlayable { • @Override • public void play() { • // render video, play the clip on screen... • } • // does complicated stuff related to video formats... • } • public class MP3Song implementsPlayable { • @Override • public void play(){ • // audio codec calculations, play the song... • } • // does complicated stuff related to MP3 format... • }

  21. מערכים פולימורפים Playable[] playables = new Playable[3]; playables[0] = new MP3Song(); playables[1] = newVideoClip(); playables[2] = new MP4Song(); // new Playable class Player player = new Player(); // init player... player.play(playables); public void play (Playable [] items) { do { if (shuffle) Collections.shuffle(Arrays.asList(items)); for (Playable item : items) item.play(); } while (repeat); } עבור כל איבר במערך יקרא ה play() המתאים

  22. Battle Ship Game

  23. פעולות על סיביות • אופרטורים לביצוע פעולות על ביטים • רק על טיפוסים איטנגרליים (int, short, byte, char)

  24. פעולות על סיביות - דוגמאות • int 32 ביטים • מה נקבל מ i & 3? • שני הביטים הימניים של i • ומה נקבל מ i & 0xF0?

More Related