Exam example 8 Stam Davar Test 5 public
Exam example 8. Stam - ו Davar , Test 5 : מחלקות שלוש של חלקי מימוש לפניך public class Stam { private char x; public Stam() { this. x = '*'; } public Stam (char c) { this. x = c; } public Stam get. Stam() { return this; } public String to. String() { return "x = " + this. x; } public boolean is. Stam 1 (Stam other) { return this. x == other. x ; } // is. Stam 1 public boolean is. Stam 2 (Stam other) { return this. equals(other); } // is. Stam 2 public void same (Stam other) { if (this. Stam 1(other)) System. out. println( this + " same 1 as " + other); else System. out. println( this + " not same 1 as " + other); if (this. Stam 2(other)) System. out. println( this + " same 2 as " + other); else System. out. println( this + " not same 2 as " + other); } // same public void print() { System. out. println(this. to. String()); } public void print (Stam other) { this. same(other); } } //class Stam 2 public class Davar extends Stam { private int y; public int get. Y() { return y; } public void set. Y(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 to. String() { return "Davar: " + super. to. String(); } } // class Davar
Exam example 8, cont. public class Test 5 { 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 המערך את הצג . א s[3] = new Davar( 'b‘ ); : הבא הקטע s[4] = new Davar( 'a', 0 ); s[5]=s[2]. get. Stam( ); 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
Exam example 8 - solution, cont. : FOR לולאת פלט . ב : הוא הקטע פלט . ג Davar: x = * same 1 as x = * Davar: x = * not same 2 as x = * x = b same 1 as x = b same 2 as x = b Davar: x = b not same 1 as Davar: x = a Davar: x = b not same 2 as Davar: x = a 5
{a, b, c}n bbb bbc bca bcb bcc caa cab cac cba cbb cbc cca ccb aaa aab aac aba abb abc aca acb acc baa bab bac bba ccc
פתרון 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; }
- Slides: 13