KFUPM, CCSE, ICS Department 2006 – 2007 Fall Semester (Term 061)
ICS102: Introduction to ComputingLab03: Strings
Objectives
Designing and implementing Java programs that deal with:
- String Class Objects.
- String Concatenation (+).
- String Class Methods (length, equals, equalsIgnoreCase, toLowerCase, toUpperCase, trim, charAt, substring, indexOf, lastIndexOf, compareTo, compareToIgnoreCase). Refer to the String class (page 1022) on Appendix 4 of the book, or to the JDKv1.5 Documentation online or offline for more methods belonging to the String class.
- String Indices
- Escape Sequences (\”, \’, \\, \n, \r, \t).
- The Imputable Characteristic of the String Object.
Example
The following example generates a password for a student using his initials and age. (note: do not use this for your actual passwords).
/*
* generates a password for a student using his initials and age
*/
public class PasswordMaker {
public static void main(String[] args) {
String firstName = "Amr";
String middleName = "Samir";
String lastName = "Al-Ibrahim";
int age = 20;
//extract initials
String initials = firstName.substring(0,1)+
middleName.substring(0,1)+
lastName.substring(0,1);
//append age after changing the initials to lower case
String password = initials.toLowerCase()+age;
System.out.println("Your Password ="+password);
}
}
Exercises
Exercise #01
Design and implement a Java program that will do the following operations to this string “Welcome! This is ICS102 Course”:
convert all alphabets to capital letters and print out the result;
convert all alphabets to lower-case letters and print out the result; and
print out the length of the string.
Exercise #02
Modify the example shown above in such a way that instead of taking initials of the name, the new program will take the middle letter from the first, middle and last name. The obtained letters will be concatenated along with age which is multiply by 100.
NOTE:your program should work for any names NOT ONLY those names specified in the example.
Exercise #03
We have two student names Ali Al-Ali and Ahmed Al-Ahmed. Design and implement a Java program that will exchange the last name of the two students in such a way that the new names are going to be Ali Al-Ahmed and Ahmed Al-Ali.
NOTE:your program should work for any names NOT ONLY these given names in the exercise.
Exercise #04
We have the student name ali al-ali. Desing and implement a Java program that will change the first letter with the last letter and it will change the last letter with the first letter and the new name is going to be ili al-ala.
NOTE:your program should work for any name NOT ONLY this given name in the exercise.
Page 1 of 2