Computer Science  Regular Session  Summer Session  The Site
 CoursesContestsComputersLanguagesInternetActivitiesLibraryQuotations

Exercises

 

 

 

 

 

Exercise 1

 

¢ Create a string named String1 with the following contents: This is the first string.

¢ Create a second string named String2 with the following contents: This is the second string.

¢ Create a third string named CombinedString that contains String1 concatenated with String2.

¢ Print all three strings, appropriately labeled.

 

Exercise 2

 

Notes for this lab

¢ Use the technique discussed in class (using the Scanner class) to prompt the user for input.

¢ When printing output, use the System.out.println() method.

 

Requirements

Prompt the user to enter 2 separate strings. Name the strings FirstString and SecondString.

¢ When prompted, enter

      A roach is shorter than a mouse for FirstString

      A Chicken is a dumb animal for SecondString.

¢ Print the two strings, appropriately labeled.

¢ Convert FirstString to upper case and print the result, appropriately labeled.

Find and print, appropriately labeled, the index of the first occurrence of the character c in FirstString.

 

Exercise 3

 

¢ Prompt the user to enter a sentence that states the user's name (My name is George, for example).

¢ Prompt the user to enter a character shift from 1 to 4.

¢ Print the sentence and the amount of the shift, appropriately labeled.

¢ Encrypt the sentence using Caesar's shift.

¢ Print the encrypted sentence, appropriately labeled.

¢ Recover and print the original sentence, appropriately labeled.

 

Exercise 4 (Optional)

 

Another version of Caesar's shift and one which is harder to crack requires the use of 2 shifts.

Same requirements as above except prompt the user to enter 2 shifts. The code will then alternate use

of the shifts provided.

For example, if the numbers entered are 2 and 3, then the first character will be shifted 2, the second will

be shifted 3, the next will be shifted 2, etc.