slide1
Download
Skip this Video
Download Presentation
תרגול 13 חזרה

Loading in 2 Seconds...

play fullscreen
1 / 13

תרגול 13 חזרה - PowerPoint PPT Presentation


  • 86 Views
  • Uploaded on

תרגול 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;}

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' תרגול 13 חזרה' - september-dunlap


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
exam example 8
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

exam example 8 cont
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 אחרי ביצוע הקטע הבא:

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

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

exam example 8 solution
Exam example 8- solution

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

exam example 8 solution cont
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

a b c n
{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

slide7
פתרון

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);

{

slide13
פתרון
  • 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;
  • {
ad