KFUPM, ICS Department 2/2 Fall 2007 (Term 071)

Total Page:16

File Type:pdf, Size:1020Kb

KFUPM, ICS Department 2/2 Fall 2007 (Term 071)

King Fahd University of Petroleum & Minerals ICS102: Introduction to Computing

Information and Computer Science Department Fall 2007 (Term 071)

Lab04: Java Strings

Objectives Designing and implementing Java programs that deal with: 1. String Class Objects. 2. String Concatenation (+). 3. 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. 4. String Indices 5. Escape Sequences (\”, \’, \\, \n, \r, \t). 6. 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 = "Ahmed";

String lastName = "Al-Ghamdi";

int age = 20; KFUPM, ICS Department 2/3 Fall 2007 (Term 071) ICS102: Introduction to Computing Lab04: Java Strings

// 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: (StringWelcome.java)

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.  print out the index of Course.

Exercise #02: (PasswordMaker2.java)

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: (FamilyName.java)

We have two student names Ali Al-Ali and Ahmed Al-Ahmed. Design and implement a Java program that will exchange the last names 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. KFUPM, ICS Department 3/3 Fall 2007 (Term 071) ICS102: Introduction to Computing Lab04: Java Strings Exercise #04: (Swap.java)

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.

Exercise #05: (Optional) (Replace.java)

Assume you have the following string: “This is ICS102 Lab”. Write a program that searches for ICS102 and replaces it with COE-202.

Recommended publications