Monthly Archives: February 2014

Systematically Visit [python]

 

Parameters

  • 1 while loop
  • visit all cells that you can
  • pick up all flowers
  • your loop must terminate (ie your Jeroo cannot keep going around the field forever)

Map #1

Grade : 2/10

  • start in (0,0) with 625 flowers!
  • row by row, plant flowers until you to position (23,0) as shown in animation below.


 

Grade : 3/10

 

Download all the island files

The Maps


 Grade : 5/10

Grade : 8/10 

Note: Do not be fooled by how similar the next map is. You will probably have to rewrite some code to complete the next one.


 Grade : 9/10  (THE FIRST MAP THAT *MUST* BE SHOWN)

Grade : 10/10 
Want an “A”, you must do the following map

Extra credit:

Java String Assignments -intro-1

Java String Assignments

These exercises are introductory methods that require use of basic String methods including

  • substring()
  • length()
    • “abc”.length() – >3
  • concatenation

 

public boolean sameStrings(String string1 ,String string2)

Description: This method returns true if string1 and string2 are the same. Use the ‘.equals()’ method.

Method Call return value/output
sameStrings(“foo”,”f”) false
sameStrings(“foo”,”foo”) true
sameStrings(“abc”, “cba”) false

public boolean any2Same(String a,String b, String c)

Description: This method returns true if any 2 of the strings are the same. Remember: Use the ‘.equals()’ method.

Method Call return value/output
any2Same(“xz”,”f”, “xz”) true
any2Same(“xz”,”f”, “xt”) false
any2Same(“xz”,”xz”, “fff”) true
any2Same(“xtz”,”abc”, “abc”) true
any2Same(“xtz”,”a^c”, “a!c”) false

public String firstThirdLettters(String str)

Description: This method returns the first and third letters of str concatenated together.

Method Call return value/output
firstThirdLettters(“foo“) “fo”
firstThirdLettters(“abcdefg”) “ac”
firstThirdLettters(“ad!kjkj”) “a!”

public boolean sameFirst2Letters(String a, String b)

Description: This method returns the first 2 letters of a and of b are the same .

Method Call return value/output
sameFirst2Letters(“axt”, “axjjj”) true
sameFirst2Letters(“1%3″ , “3$1″) false
sameFirst2Letters(“a~dd” ,”~adt” ) false

public String concatTwice(String str)

Description: This method returns str concatenated with itself .

Method Call return value/output
concatTwice(“foo”) “foofoo”
concatTwice(“a”) “aa”
concatTwice(“abcdd”) “abcddabcdd”

public String concatWithComma(String str)

Description: This method returns str concatenated with itself and with a comma in between

Method Call return value/output
concatWithComma(“foo”) “foo,foo”
concatWithComma(“a”) “a,a”
concatWithComma(“abcdd”) “abcdd,abcdd”

public String sandwich(String bread, String meat)

Description: This method is easiest to understand by looking at the sample calls below

Method Call return value/output
sandwich(“a“,”b“) aba
sandwich(“xy“,”ab“) xyabxy
sandwich(“hi“,”bye“) hibyehi

public int lengthTimesTwo(String str)

Description: This method returns the length of str times 2.

Method Call return value/output
lengthTimesTwo(“foo”) 6
lengthTimesTwo(“a”) 2
lengthTimesTwo(“abcdd”) 10

 String prePendFoo(String str)

Description: prepend “foo ” to the input and return the concatenation.

Method Call return value/output
prePendFoo(“abc”) “foo abc”
prePendFoo(“x”) “foo x”
prePendFoo(“abcdd”) foo abcdd”

public int sumOfLengths(String a, String a)

Description: This method returns the sum of the lengths of String a and String b .

Method Call return value/output
sumOfLengths(“ab”, “jk1”) 5  ie ( 2 +3)
sumOfLengths(“jj”, “”) 2 (ie 2 + 0)
sumOfLengths(“a~dd” ,”6″ ) 5  ie ( 4  + 1)


**public String concat5Times(String str)

Description: This method returns str concatenated with itself 5 times (Do this with a loop)

Method Call return value/output
concat5Times(“foo”) “foofoofoofoofoo”
concat5Times(“a”) “aaaaa”
concat5Times(“abcdd”) “abcddabcddabcddabcdd”

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)