Category Archives: Computer Science

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

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

 

While Loops #2

#2 A

Create the map below

  • with a flower in 0,0
  • Jeroo to its right (0,1)
  • using while loop(s) traverse around the border of the island
  • at the end pick the flower
  • you can only use hop() (ie you can’t hop more than 1 space)

 

jeroo_walk_exterior1 jeroo_walk_exterior2 jeroo_walk_exterior3 jeroo_walk_exterior4 jeroo_walk_exterior5 jeroo_walk_exterior6 jeroo_walk_exterior7jeroo_walk_exterior8

 


#2 B

Create the map below

  • Jeroo to its right (6,6)
  • use multiple while loops
  • at the end pick the flower
  • you can only use hop() (ie you can’t hop more than 1 space)


Download Island Files

rectangular-lake.jev

1) Starting
<

2) Go around lake, until you are in front of the flower

3) Then pick the flower

finished program state1

Sorting Algorithm Assignment

Sorting Algorithm

 

Good Links

 

You will be given a sorting algorithm to research and to present to the class in a powerpoint. Your powerpoint should

  1. in general terms explain how the algo works
  2. show Java code for the algorithm
  3. have an animation demonstrating how the algorithm works (step by step)
  4.  clearly explain how the code (in each line) relates to the code (most important and easy to forget step)
  5. It’s important that you can explain each line of the code
  6. state best,worst, average case for the data and, in particular, state how the original state of data affects run time performance
    1. in order
    2. random
    3. in reverse order

Grading Rubric Algorithm Project


 

 

 

4

3

2

1

  Big Oh

(double weight)

Addresses best/worst/ave cases with clear connection to all aspects of code

 

Addresses best/worst/ave cases  but does not relate to all relevant code Addresses best/worst/ave cases  but does not connect to code/is too general Does not address all 3 cases Big Oh or does not connect to code in meaningful way
 Animation of algorithm(double weight)

 

 

 

Animation is clearly connected to all aspects of code Animation is connected to critical areas of code Animation is not clearly connected to actual code Animation and code are not related to one another
 Overview(double weight)

 

Gives a clear and enlightening overview of how the algo works in general. Overview of algorithm is technically correct and discusses most of the relevant features. Algorithm is vaguely summarized or is not well explicated. Algorithm is not explained in a meaningful way.