1 / 35

public class PrintSquares { public static void main(String [] args ) {

public class PrintSquares { public static void main(String [] args ) { System.out.println ("****"); System.out.println ("* *"); System.out.println ("* *"); System.out.println ("****"); System.out.println (); System.out.println ("****"); System.out.println ("* *");

kenny
Download Presentation

public class PrintSquares { public static void main(String [] args ) {

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. public class PrintSquares { public static void main(String [] args) { System.out.println("****"); System.out.println("* *"); System.out.println("* *"); System.out.println("****"); System.out.println(); System.out.println("****"); System.out.println("* *"); System.out.println("* *"); System.out.println("****"); } } public class PrintSteps { public static void main(String [] args) { System.out.println("-----"); System.out.println(" |"); System.out.println(" |"); System.out.println(" -----"); System.out.println(" |"); System.out.println(" |"); } }

  2. public static void pos(int n, String str){ for(inti=0;i<n;i++)System.out.print(str); } public static void square(intlen){ pos(len, “*”); System.out.println(); for(inti=0;i<len-2;i++){ pos(1, “*”); pos(len-2, “*”); pos(1, “*”); System.out.println(); } pos(len, “*”); System.out.println(); } public static void squares (int n, int length){ square(length); for(inti=1;i<n;i++){ System.out.print square(length); } }

  3. public static void pos(int n, String str){ for(inti=0;i<n;i++)System.out.print(str); } public static void step(int start, int width, int depth){ space(start, “ “); pos(width, “-”); System.out.println(); for(inti=0;i<depth;i++){ pos(start+width, “ “); pos(1, “|”); System.out.println(); } } public static void steps (int n, int width, int depth){ for(inti=0;i<n;i++){ step((width+1)*i, width, depth); } }

  4. Calculate the result 1+3+5+7+9+11+13+…+99 when f1(99) is called. job division: 1+3+5+…+97+99 => 1+…+97 f1(99) => f1(97) results collection: f1(97)+99 result reporting: return

  5. job division: 1+3+5+…+97 => 1+…+95 f1(97) => f1(95) results collection: f1(95)+97 result reporting: return General format:: job division: f1(x) => f1(x-2) results collection: f1(x-2)+x result reporting: return

  6. job division: f1(x) => f1(x-2) f1(3) => f1(1) => none! Terminating condition: x <=1 Why x<=2 ok? 7, 5, 3 go false!

  7. job division: f1(x) => f1(x-2) results collection: f1(x-2)+x result reporting: return Terminating condition: x <=1 public static int f1 ( ) int x { if (x <=1) return 1; else return f1(x-2)+x; }

  8. Calculate the result 1+2+4+8+16+32+…+4096 when f2(4096) is called. job division: 1+2+…+2048+4096 => 1+…+2048 f2(4096) => f2(2048) results collection: f2(2048)+4096 result reporting: return

  9. job division: f2(x) => f2(x/2) results collection: f2(x/2)+x result reporting: return

  10. f2(x) => f2(x/2) f2(2) => f2(1) => none! Terminating condition: x <=1

  11. job division: f2(x) => f2(x/2) results collection: f2(x/2)+x result reporting: return Terminating condition: x <=1 public static int f2 ( ) int x { if (x <=1) return 1; else return f2(x/2)+x; }

  12. Calculate the result 1/i+2/(i-1)+3/(i-2)+…+(i-1)/2+i/1 when f3(1, i). job division: 1/i+2/(i-1)+…+i/1 => 2/(i-1)+…+i/1 => f3(2,i-1) results collection: f3(2, i-1) + (double)1/i result reporting: return f3(1,i) f3(1,i)

  13. job division: f3(x, y) => f3(x+1, y-1) results collection: f3(x+1, y-1)+(double)x/y result reporting: return

  14. f3(x, y) => f3(x+1, y-1) f3(i-1, 2) => f3(i, 1) => none! Terminating condition: x >=i Terminating condition: y <=1 Where is “I” in “f3”?

  15. job division: f3(x, y) => f3(x+1, y-1) results collection: f3(x+1, y-1)+(double)x/y result reporting: return Terminating condition: y <=1 public static double f3 ( ) int X, int y { if (y<=1) return (double)x/y; else return f3(x+1, y-1)+(double)x/y; }

  16. Calculate the result 1/i+2/(i-1)+3/(i-2)+…+(i-1)/2+i/1. job division: 1/i+…+(i-1)/2+i/1 => 1/i+…. (i-1)/2 f3(1,i) => f3(2,i-1) results collection: f3(2, i-1) + (double)i/1 result reporting: return

  17. job division: f3(x, y) => f3(x+1, y-1) results collection: f3(x+1, y-1)+(double)y/x result reporting: return

  18. f3(x, y) => f3(x+1, y-1) f3(i-1, 2), i.e., 2/i-1 => f3(i, 1), i.e., 1/i => none! Terminating condition: y<=1

  19. job division: f3(x, y) => f3(x+1, y-1) results collection: f3(x+1, y-1)+(double)y/x result reporting: return Terminating condition: y<=1 public static double f3 ( ) int X, int y { if (y<=1) return (double)y/x; else return f3(x+1, y-1)+(double)y/x; }

  20. The world population reached 6 billion people in 1999 and was growing at the rate of 1.4 percent each year. Write a recursive function f to return/determine when the population will exceed 10 Billion when f4(6, 10, 1999) is called. job division: from 1999 (p=6) to target year (p>10)  from 2000 (p=6*1.014) to target year results collection: f4(6*1.014, 10, 2000) result reporting: return

  21. job division: f4(cp, tp, y) => f4(cp *1.014, tp, y+1) results collection: f4(cp *1.014, tp, y+1) result reporting: return

  22. General Format: => f4(cp, tp, yr) … => f4(>tp, tp, yr) => none! Terminating condition: cp>tp

  23. job division: f4(cp, tp, y) => f4(cp *1.014, tp, y+1) results collection: f4(cp *1.014, tp, y+1) result reporting: return Terminating condition: cp>tp public static int f4 ( ) double cp, double tp, int x { if (cp >tp) return x; else return f4(cp*1.014, tp, x+1); }

  24. A TV set is purchased with a loan of $563 to be paid off with monthly payment (no more than $116). The interest rate is 1 percent per month. Write a recursive function f to return the amount of last bill when f5(563, 116) is called. job division: from 563 until <=116  from (563-116 )*1.01 (i.e., next month) until <=116 results collection: f5((563-116)*1.01, 116) result reporting: return

  25. job division: f5(x, y) => f5((x-y)*1.01, y) results collection: f5((x-y)*1.01,y) result reporting: return

  26. f5(x, y) => f5((x-y)*1.01, y) … => f5(<=116, 116) => none! Terminating condition: x <=116, i.e., y

  27. job division: f5(x, y) => f5((x-y)*1.01, y) results collection: f5((x-y)*1.01,y) result reporting: return Terminating condition: x <=y public static double f5 ( ) double X, int y { if (x<=y) return x; else return f5((x-y)*1.01, y); }

  28. Develop a recursive function f6 to display the sequence 1 2 3 4 5 … i in a text field when f6(i) is called. job division: display 1, 2, …, i-1, i  display 1, 2, …, i-1 results collection: f6(i-1) on the screen, and System.out.print(i) result reporting: results are on the screen, nothing to report!

  29. job division: f6(x) => f6(x-1) results collection: f6(x-1); System.out.print(x); result reporting: void

  30. f6(x) => f6(x-1) … => f6(1) => none! Terminating condition: x <=1

  31. job division: f6(x) => f6(x-1) results collection: f6(x-1); System.out.print(x); result reporting: none Terminating condition: x <=y public static void f6 ( ) int x { if (x <=1) System.out.print(x); else { f6(x-1); System.out.print(x); } }

  32. Develop a recursive function f7 to display the sequence 1 2 3 4 5 … i (i-1) (i-2) … 2 1 in a text field when f7(1,i) is called. job division: display 1, 2, …, i-1, i, i-1, …, 2, 1  display 2, …, i-1, i, i-1, …, 2 results collection: f7(2,i) on the screen, and System.out.print(1) twice! result reporting: results are on the screen, nothing to report!

  33. job division: f7(x, y) => f7(x+1, y) results collection: System.out.print(x) f7(x+1,y); System.out.print(x); result reporting: void

  34. f7(x,y) => f7(x+1,y) … => f7(i-1, i) //two 8’s => none! Terminating condition: x >=y

  35. job division: f7(x,y) => f7(x+1,y) results collection: System.out.print(x); f7(x+1,y); System.out.print(x); result reporting: none Terminating condition: x >=y public static void f7 ( ) int X, int y { if (x>=y) System.out.print(x); else { System.out.print(x); f7(x+1,y); System.out.print(x); } }

More Related