Author Archives: Mr. M

Java Strings

Java  Strings

substrings1

climb-the-stairs-[python]

Climb The Stairs

For all these maps:

  • Start your jeroo in the bottom left , at 23,0
  • use 1 and only 1 while loop
  • you may only hop() 1 space at a time
  • pick the flower at the end.

 

Download Island Files

climb-the-stairs__1.jev

 

 

climb-the-stairs__2.jev

  

climb-the-stairs__3.jev

   

 

climb-the-stairs__4.jev

 

 

climb-the-stairs__5.jev

climb_the_stairs_7

 

climb-the-stairs__6.jev

climb_the_stairs_8

 

climb-the-stairs__7.jev  (GRADE BOOK)

climb_the_stairs_7

 

climb-the-stairs__8.jev (GRADE BOOK)

 

climb_the_stairs_8

 

climb-the-stairs__9.jev

**( Only if you want to get that ‘A’)

 

 

 

Pascal’s Triangle Recursion example

Pascal’s Triangle

Pascals Triangle presents a simple formula for expanding binomials. If you think a little bit about how Pascal’s Triangle determines each term, you should see that a recursive method is used!

The animation below demonstrates the recursive nature of Pascal’s Triangle.

Part I

Objective: to employ recursion to be able to print out an entire line of Pascal’s Triangle . Look at the animation, getting a particular term in a line is inherently recursive. This class should throw an ArithmeticException if you attempt to calculate an invalid Term. (quick example of how to throw an Exception)

Hint: You only need to use recusion to get the term of the expansion.

Part II

Now that you can print out an entire line of Pascal’s Triangle. Print out the entire triangle up to some degree ‘n’.

java-recursion-excercises-1

void countDown(int num)

Description: This method prints the numbers from num down to and including on new lines

Method Call output
countDown(5) 5
4
3
2
1
0
countDown(3) 3
2
1
0
void printRev(String s)

Description: This method prints each letter of String s on new lines in reverse order.

Method Call output
printRev(“abcdef”) a
b
c
d
e
f
printRev(“abc”) a
b
c

 

int sumLessthan(int n)

Description: This method returns the sum of all positive integers less than or equal to n

Method Call output
sumLessThan(4)   10 (4+3+2+1)
sumLessThan( 5 )  15 ( 5 + 4 + 3 + 2 + 1)

 

int sumLessthan(int n)

Description: This method returns the sum of all positive integers less than or equal to n

Method Call output
sumLessThan(4)   10 (4+3+2+1)
sumLessThan( 5 )  15 ( 5 + 4 + 3 + 2 + 1)

 

double average( int[] nums, int index)

Description: This method returns the average of all the elements in nums. The parameter ‘idnex’ is used to keep track of how far along you have gone in the array.

Method Call output
average({3,2,1}, 0)   2
average({3,2,2}, 0)    2.333333

Recursion in Java

Recur

bunnyEars2() (try to figure out what goes where the question marks are)

Recursion problems (bunny ears is a good, straight forward one to start with)

 

Two Friends Meet

Two Friends Meet

  • Create two different Jeroos
  • one Jeroo should start at (0,0) and be facing EAST
  • the other should start at  (0,23) and be facing west
  • they should move at “the same time” and plant flowers in the pattern shown below
  • use a single while loop
  • the isJeroo( relative_diretion) method  might be helpful
  • Note: To get 100%
    • Your loop MUST end (not infinite)
    • You can not use hasFlower()

 

 

jeroo-2-friends-meet-done

Traversing the Rim

In the prior assignment (#2b), you were asked to go around the inner lake  as shown  below. We are going to add 1 new requirement to this

  • with a flower in 6,6
  • using while loop(s) traverse around the border of the inner lake
  • For each map, you should pick the flower, after the loop is over

What is new this time?

  • You are forced to use 1 and only 1 while loop .
  • you can only use hop() (ie you can’t hop more than 1 space)
  • after the loop is done , then you can   pick()  the flower


Download the map files

recangular-lake.jev

walk-the-lake-annotated

 

 

 


 

recangular-lake-if2.jev 

Scroll down for a hint about how to handle if test #1 for this map.

rectangular_lake_if2_overview


 

A closerlook at if test #1 (how to get around that first if test)

rectangular_lake_if2_get_around_net

 


 

rectangular-lake-bumps1.jev


recangular-lake-bump3A.jev

rectangular_lake_3A2


recangular-lake-bump3B.jev ( NOT BEING GRADED, SKIP THIS)

rectangular_lake_3A2_annotated


walk-the-lake-jacks-map.jev

Write an infinite loop (1 while loop). You may NOT use isWater()

 

Cross The bridge Custom Method 1

Objective: Your goal is get your jeroo onto the right side of the island. Note that the red areas are nets. You must toss() a flower onto the net before you can go on the net.. You will need the following island file .

 

The first time that we did this our code looked like this .

jeroo pickall1

#1)However, we want to create the following custom method custom_method .

 

jeroo pickall2

 

#2) Then our “main Section” code should be changed to:

 

jeroo pickall4