תרגול 13 חזרה 1. exam example 8 public class stam { private char x; public stam() { this.x...
TRANSCRIPT
13תרגול
חזרה
1
2
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
: מחלקות שלוש של חלקי מימוש Stamו - .Davar ,Test5 לפניך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
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 = bx = b same2 as x = bDavar: x = b not same1 as Davar: x = aDavar: x = b not same2 as Davar: x = a
הוא. :ג הקטע פלט
{a,b,c}n
הרקורסיבית הפונקציה את מספר abc(int n)כתבו מקבלת ומדפיסה nאשרהאותיות } מעל המילים כל את .nבאורך{ a,b,cלמסך
. הצורך: במידת רקורסיבית עזר פונקציית לכתוב ניתן הערה
לדוגמא:: נקבל עבור
aaaaabaacabaabbabcacaacbaccbaababbacbbaccc
bbbbbcbcabcbbcccaacabcaccbacbbcbcccaccb
פתרוןpublic static void abc(int n) {
abc(n, "");}public static void abc(int n, String s) {
if (n == 0)System.out.println(s);
elsefor (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;
{