Home
Site Map
Useful Tools
Graphing Calc

Scientific Calculator

Introduction to String Projects

These methods are suitable for someone who is just beginning to work with strings and require understanding of concatenation, as well as the String's indexOf() and equals() methods.
Write the body for the methods described below.

Methods that involve substring() , .equals() and 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
isThere("foo","f") false
isThere("foo","foo") true
isThere("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 boolen sameFirst2Letters(String a, String a)
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

Methods that indexOf()

public boolean isAThere(String str)
Description: This method returns true if the letter "a" is anywhere inside of str . Note: You should use indexOf(). Remember, indexOf() return -1 if it is searching for something that is not there.
Method Call return value/output
isThere("foo") false
isThere("food" ) false
isThere("xay", "a") true
public boolean isThere(String str,String letter)
Description: This method returns true if letter is anywhere inside of str . Note: You should use indexOf().
Method Call return value/output
isThere("foo","f") true
isThere("foo","z") false
isThere("xy", "y") true

Challenge Methods

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"
Sitemap