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” |