People.engr.tamu.edu



Strings Lab A

1. Fix the program so it displays each letter of “hello” on a separate line:

public static void main(String [] args)

{

System.out.println(“Your name”);

System.out.println(“Your Class and Section”);

String message = “Hello”;

for(int i = 0; i < 5; i++)

{

Print message; // CHANGE ONLY THIS LINE!!!

}

} //(s.c. and code)

2. Write a program so the user will input ONLY two words. You will add up all the characters of each word and compare THE LENGTH of the two words. Print the length of each word and, if the first word is greater in length than the second, print “First word is greater in length than the second.” Vice versa for second greater than first. If the first and second words are equal, print “First word is equal in length to the second word.” (s.c. x 2 and code)

Hint: Use the String helper functions length

3. A new user is creating an account on a website. To make sure the email is legitimate, use the String helper functions (check API) that will determine if the email entered is legitimate by:

a. determine if an @ is present

b. determine if the email address ENDS with an “.edu” or “.com”

c. confirm the email does NOT contain a space

(yes, I know there are others out there)

Both conditions above must be true in order for the email to be verified. After receiving input from the user (using Scanner), check the input and use an if/else statement to display if the email was legitimate.

(s.c. x 3 and code)

Hint: Use the String helper function contains (x2) and endsWith.

4. A new user is creating an account on a website. The website (much like the toolkit) will create a lowercase username based on the users FIRST LETTER and ENTIRE last name. For example:

Shawn Lupoli => username => slupoli

(s.c. x 2 and code)

Hint: Use the String helper functions charAt and toLowerCase.

5. Random is another complex object and a class that has helper files. Using the java website:

a. identify the URL for the Random class, start here

b. list three helper functions

(just write down answers, no code)

6. Using the Random class you just identified. Create a program that prints a random phone number in the form XXX-YYY-ZZZZ, AS A STRING with dashes included. Again, the FINAL answer must be a string concatenated with the numbers. Use the code below to help you. Please note the comments on the code given.

(s.c. x 2 and code)

|import java.util.Random; |

|import java.util.Scanner; |

| |

|public class test { |

| |

| |

|public static void main(String[] args) |

|{ |

|int xxx, yyy, zzzz; |

| |

|Random r = new Random(); // creates an INSTANCE of Random |

| |

|xxx = r.????? // p |

|yyy = r.????? // p |

|zzzz = r.????? // p |

| |

|// Do rest of work here |

| |

|} |

| |

|} |

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download