1 / 13

תרגול 13 חזרה

תרגול 13 חזרה. Exam example 8. לפניך מימוש חלקי של שלוש מחלקות: Davar ,Test5 ו - . Stam. public class Davar extends Stam { private int y; public int getY () { return y; } public void setY ( int y) { this .y = y;}

jhayes
Download Presentation

תרגול 13 חזרה

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. תרגול 13 חזרה

  2. Exam example 8 לפניך מימוש חלקי של שלוש מחלקות:Davar ,Test5 ו - .Stam public class DavarextendsStam { private inty; public intgetY() { return y; } public void setY(int y) { this.y = y;} publicDavar() { super(); this.y = 0; } publicDavar(char c) { super(c); this.y = 0; } // Davar publicDavar(char c, intnum) { super(c); this.y = num; } // Davar public String toString() { return"Davar: " + super.toString(); } } // class Davar public class Stam { private char x; public Stam() { this.x = '*'; } publicStam (char c) { this.x = c; } publicStamgetStam() { returnthis; } public String toString() { return"x = "+ this.x; } publicboolean isStam1 (Stam other) { returnthis.x == other.x ; } // isStam1 public booleanisStam2 (Stam other) { return this.equals(other); } // isStam2 public void same (Stam other) { if (this.isStam1(other)) System.out.println(this + " same1 as " + other); else System.out.println(this + " not same1 as " + other); if (this.isStam2(other)) System.out.println(this + " same2 as " + other); else System.out.println(this + " not same2 as " + other); } // same public void print() { System.out.println(this.toString()); } public void print (Stam other) { this.same(other); } } //class Stam

  3. Exam example 8, cont. public class Test5 { public static void main(String[ ] args) { Stam[ ] s = new Stam[6]; s[0] = new Stam(); s[1] = new Davar(); s[2] = new Stam( 'b‘ ); s[3] = new Davar( 'b‘ ); s[4] = new Davar( 'a', 0); s[5]=s[2].getStam(); for(int i=0; i< 6; i++) s[i].print(); s[1].print(s[0]); s[2].print(s[5]); s[3].print(s[4]); } // main } // class Test א. הצג את המערך s אחרי ביצוע הקטע הבא: ב. רשום את פלט הלולאה. ג. מהו פלט הקטע ?

  4. Exam example 8- solution א. תוכן מערך S לאחר ביצוע קטע של פעולה ראשית :

  5. Exam example 8- solution,cont. ב. פלט לולאת FOR : ג. פלט הקטע הוא : Davar: x = * same1 as x = * Davar: x = * not same2 as x = * x = b same1 as x = b x = b same2 as x = b Davar: x = b not same1 as Davar: x = a Davar: x = b not same2 as Davar: x = a

  6. {a,b,c}n כתבו את הפונקציה הרקורסיבית abc(int n) אשר מקבלת מספר n ומדפיסה למסך את כל המילים מעל האותיות {a,b,c} באורך n. הערה: ניתן לכתוב פונקציית עזר רקורסיבית במידת הצורך. לדוגמא: עבור נקבל: aaa aab aac aba abb abc aca acb acc baa bab bac bba ccc bbb bbc bca bcb bcc caa cab cac cba cbb cbc cca ccb

  7. פתרון publicstaticvoidabc(int n) { abc(n, ""); } publicstaticvoidabc(int n, String s) { if (n == 0) System.out.println(s); else for (char c = 'a'; c <= 'c'; c++) abc(n-1, s+c); } publicstaticvoid main(String[] args) { abc(3); {

  8. תרגיל ממבחן

  9. פתרון

  10. תרגיל ממבחן

  11. פתרון

  12. תרגיל ממבחן

  13. פתרון • publicstaticint change(int sum, int[] coins) • } • returnchange(sum, coins, 0); • { • publicstaticint change(int sum, int[] coins, int first) • } • if(sum == 0) • return1; • elseif (sum>0 && first < coins.length) • return change(sum-coins[first], coins, first) • + change(sum, coins, first+1); • else • return0; • {

More Related