Tag Archives: Strings

String Lab Assignment [loops]

In this lab, you will work with a GUI to validate the input in each text field . Below is a screenshot of the running program.

validate-user-input

 

  1. download the code: Validate Form Loop Version Starter Code 
  2.  If you’re using bluej, you want to run “main” as shown herevalidate-form-bluej
  3. Find the code section shown below. This is the one and only 1 area that you should be editingscreen-shot-code
  4. Validate the user input as described below:
  1. Name
    1. length of 3 >=
    2. only letters (spaces is ok)
  2.  Email
    1. Must include an “@”
    2. Must include a “.”
    3. the “.” must come after the “@”
  3. Phone
    1. XXX-XXX-XXXX
      1. where each”X” must be an integer value
      2. each “-” must be in the correct location
  4. Date of Birth
    1. To convert a String to an integer use Integer.valueOf(String str)
    2. Max age is 110
    3. Minimum accepted age is 18
      1. If the user is less than 18, display the message. “Sorry you must at least 18 years of age for this service”
  5. Password
    1. Length must be at least 5
    2. cannot include the name anywhere inside the password
  6. Zip Code
    1. Each character must be an integer
    2. Length between 3 and 5 , inclusive

 

If the user has no errors, then please show the message “Thanks for submitting your information ” , as shown below

validate-form_thx_for_submitting_V2

 

A  file that does a few test cases (be careful not to overwrite your work)

String Assignments SUPA 2015-16

Unit 3 Strings

substrings1

Java String Loop Assingments 1

 

public void printTwos(String str)

Description: This method prints two letters from the String starting at with the first two letters and ending with the last two.

Method Call return value/output
printTwos(“abcdef”) “ab”
“bc”
“de”
“ef”

 

 

public void printReverse(String str)

Description: This method print every letter in reverse and on a new line.

Method Call return value/output
printReverse(“abcd”) “d”
“c”
“b”
“a”
printReverse(“thefoo”) “o”
“o”
“f”
“e”
“h”
“t”
public void printTwoReverse(String str)

Description: This method print every 2 letter in reverse and on a new line.

Method Call return value/output
printTwoReverse(“abcd”) “dc”
“cb”
“ba”
printTwoReverse(“thefoo”) “oo”
“of”
“fe”
“eh”
“ht”

 

public void printTwoReverseAgain(String str)

Description: This method print every 2 letter in reverse and on a new line and with no overlap of letters

Method Call return value/output
printTwoReverseAgain(“abcd”) “dc”
“ba”
printTwoReverseAgain(“thefoo”) “oo”
“fe”
“ht”

 

 

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”