תרגול 13 חזרה 1. exam example 8 public class stam { private char x; public stam() { this.x...

13
ללללל13 לללל1

Upload: elmer-fox

Post on 01-Jan-2016

235 views

Category:

Documents


13 download

TRANSCRIPT

Page 1: תרגול 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()

13תרגול

חזרה

1

Page 2: תרגול 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()

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

Page 3: תרגול 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()

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 הצגהבא : הקטע ביצוע

פלט. ב את רשום.הלולאה

הקטע. ?ג פלט מהו

Page 4: תרגול 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()

4

Exam example 8- solution

מערך. א ראשית :Sתוכן פעולה של קטע ביצוע לאחר

Page 5: תרגול 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()

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

הוא. :ג הקטע פלט

Page 6: תרגול 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()

{a,b,c}n

הרקורסיבית הפונקציה את מספר abc(int n)כתבו מקבלת ומדפיסה nאשרהאותיות } מעל המילים כל את .nבאורך{ a,b,cלמסך

. הצורך: במידת רקורסיבית עזר פונקציית לכתוב ניתן הערה

לדוגמא:: נקבל עבור

aaaaabaacabaabbabcacaacbaccbaababbacbbaccc

bbbbbcbcabcbbcccaacabcaccbacbbcbcccaccb

Page 7: תרגול 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()

פתרון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);

{

Page 8: תרגול 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()

ממבחן תרגיל

Page 9: תרגול 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()

פתרון

Page 10: תרגול 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()

ממבחן תרגיל

Page 11: תרגול 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()

פתרון

Page 12: תרגול 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()

ממבחן תרגיל

Page 13: תרגול 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()

פתרון

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;

{