תרגול 13 חזרה 1
Exam example 8 public class Stam { private char x; public Stam() { this.x = '*'; } public Stam (char c) { this.x = c; } public Stam getStam() { return this; } public String toString() { return "x = " + this.x; } public boolean isStam1 (Stam other) { return this.x == other.x ; } // isStam1 public boolean isStam2 (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 2 לפניך מימוש חלקי של שלוש מחלקות : Davar,Test5 ו -.Stam public class Davar extends Stam { private int y; public int getY() { return y; } public void setY(int y) { this.y = y;} public Davar() { super(); this.y = 0; } public Davar(char c) { super(c); this.y = 0; } // Davar public Davar(char c, int num) { super(c); this.y = num; } // Davar public String toString() { return "Davar: " + super.toString(); } } // class Davar
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 3 א. הצג את המערך s אחרי ביצוע הקטע הבא : ב. רשום את פלט הלולאה. ג. מהו פלט הקטע ?
Exam example 8- solution 4 א. תוכן מערך S לאחר ביצוע קטע של פעולה ראשית :
Exam example 8- solution,cont. 5 ב. פלט לולאת 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 ג. פלט הקטע הוא :
{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
פתרון public static void abc(int n) { abc(n, ""); } public static void abc(int n, String s) { if (n == 0) System.out.println(s); else for (char c = 'a'; c <= 'c'; c++) abc(n-1, s+c); } public static void main(String[] args) { abc(3); {
תרגיל ממבחן
פתרון
תרגיל ממבחן
פתרון
תרגיל ממבחן
פתרון public static int change(int sum, int[] coins) } return change(sum, coins, 0); { public static int change(int sum, int[] coins, int first) } if (sum == 0) return 1; else if (sum>0 && first < coins.length) return change(sum-coins[first], coins, first) + change(sum, coins, first+1); else return 0; {