Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
PROGRAMMING 2 CS112- LAB 2 JAVA TA: NOUF AL-HARBI WWW.ACADOX.COM/CLASS/ 14347 [email protected] LAB OBJECTIVES The String Class Applications: String. Methods of the String Class. Lab4_1, Programming 2 2 Exercises. STRING String is a sequence of characters enclosed in quotes. E.g. “Hello” Once a String object is created it cannot be changed. Stings are Immutable. To get changeable strings use the class called StringBuffer. Lab4_1, Programming 2 3 String and StringBuffer classes are declared final, so there cannot be subclasses of these classes. HOW TO CREATE A STRING String newString = new String(stringLiteral); String s1= new String("Welcome to Java"); Since strings are used frequently, Java provides a shorthand initializer for creating a string: Lab4_1, Programming 2 4 String s1= "Welcome to Java"; STRINGS ARE IMMUTABLE A String object is immutable: its content cannot be changed. Does the following code changes the content of the string? String s = "Java"; Lab4_1, Programming 2 5 s = "HTML"; TRACE CODE String s = "Java"; s = "HTML"; After executing s = "HTML"; After executing String s = "Java"; s : String String object for "Java" Contents cannot be changed s : String This string object is now unreferenced String object for "Java" : String Lab4_1, Programming 2 6 String object for "HTML" ANOTHER EXAMPLE OF IMMUTABILITY String str1 = “Hello”; String str2 = “Hello”; Lab4_1, Programming 2 √ 7 Χ INTERNED STRINGS Since strings are immutable and are frequently used, to improve efficiency and save memory, the JVM uses a unique instance for string literals with the same character sequence. Such an instance is called interned. Lab4_1, Programming 2 8 For example, the following statements: EXAMPLES String s1 = "Welcome to Java"; String s2 = new String("Welcome to Java"); s1 s3 : String Interned string object for "Welcome to Java" String s3 = "Welcome to Java"; System.out.println("s1 == s2 is " + (s1 == s2)); s2 System.out.println("s1 == s3 is " + (s1 == s3)); : String A string object for "Welcome to Java" • A new object is created if you use the Lab4_1, Programming 2 9 s1 == s2 is false s1 == s3 is true new operator. • If you use the string initializer, no new object is created if the interned object is already created. STRING/CHARACTER METHODS String str="I Love Java"; String str1= "I Love Java"; String str2=" Programm "; char charAt(int index) Lab4_1, Programming 2 Description Returns the character at the specified index. Example char x=str.charAt(2); 10 Method STRING/CHARACTER METHODS String str="I Love Java"; String str1= "I Love Java"; String str2=" Programm "; Description int compareTo(String s) int compareToIngoreCase (s) Compares this String to another Object or String. returns +ve number, 0, or -ve number if this String is greater than, equal to or less than s. Lab4_1, Programming 2 Example int comp=str.compareTo(str2); int comp2=str.compareToIngoreCase (str2); 11 Method STRING/CHARACTER METHODS String str="I Love Java"; String str1= "I Love Java"; String str2=" Programm "; String concat(String str) Lab4_1, Programming 2 Description Concatenates the specified string to the end of this string. Example String str2=" Programm "; String res=str.concat(str2); 12 Method STRING/CHARACTER METHODS String str="I Love Java"; String str1= "I Love Java"; String str2=" Programm "; Description Example boolean equals(Object anObject) Compares this string to the specified object. returns true if s the same as this String. boolean isEqual=str.equals(str); Lab4_1, Programming 2 13 Method STRING/CHARACTER METHODS String str="I Love Java"; String str1= "I Love Java"; String str2=" Programm "; int length() Lab4_1, Programming 2 Description Returns the length of this string. Example int len=str.length(); 14 Method STRING/CHARACTER METHODS String str="I Love Java"; String str1= "I Love Java"; String str2=" Programm "; String substring(int beginIndex) Lab4_1, Programming 2 Description Returns a new string that is a substring of this string. Example String res1=str.substring(2); 15 Method STRING/CHARACTER METHODS String str="I Love Java"; String str1= "I Love Java"; String str2=" Programm "; Description Example String substring(int beginIndex, int endIndex) Returns a new string that is a substring of this string. String res1=str.substring(2,6); String substring(int beginIndex, int endIndex) Returns a new string that is a substring of this string. String res1=str.substring(2,6); Lab4_1, Programming 2 16 Method STRING/CHARACTER METHODS String str="I Love Java"; String str1= "I Love Java"; String str2=" Programm "; String toUpperCase() String toLowerCase() Lab4_1, Programming 2 Description returns a new String, equivalent to the Upper/lower case of this String Example String upCase=str.toUpperCase(); String lowCase=str.toLowerCase(); 17 Method STRING/CHARACTER METHODS String str="I Love Java"; String str1= "I Love Java"; String str2=" Programm "; Description Character.isLetter(char determines if ch) the specified character is a letter. Lab4_1, Programming 2 Example boolean isLetter=Character.isLetter('r'); Ex2: boolean isLetter=Character.isLetter(str.charAt(5)); 18 Method Description char charAt(int index) Returns the character at the specified index. int compareTo(String s) int compareToIngoreCase(s) Compares this String to another Object or String. returns +ve number, 0, or -ve number if this String is greater than, equal to or less than s. String concat(String str) Concatenates the specified string to the end of this string. boolean equals(Object anObject) Compares this string to the specified object. returns true if s the same as this String. int length() Returns the length of this string. String substring(int beginIndex) Returns a new string that is a substring of this string. String substring(int beginIndex, int endIndex) Returns a new string that is a substring of this string. Character.isLetter(char ch) determines if the specified character is a letter. String toUpperCase() String toLowerCase() returns a new String, equivalent to the Upper/lower case of this String Lab4_1, Programming 2 19 Method Example of String Operations Design and implement a Java program that will do the following operations to this string s1=“Welcome to java” s2=“Welcome to Java” 1. Print out the length of the string. 2. Use concat method which concatenates s1 to s2. 3. Use CharAt method which returns the character of s1 at index 1 4. Convert all s1 alphabets to capital letters and print out the result. 5. Convert all s1 alphabets to lower-case letters and print out the result. Lab4_1, Programming 2 20 Using methods of the String Class. Example of String Operations Lab4_1, Programming 2 21 6. Use equals method which compare s1 to s2. 7.Use equalsIgnoreCase method which compare s1 to s2 ignoring case consideration. 8. Use CompareTo method which compare s1 to s2. 9. Use Substring method which extracting substring from s1. Lab4_1, Programming 2 22 Constructing two strings Lab4_1, Programming 2 23 Using Length method which returns the length of s1 Lab4_1, Programming 2 24 Using concat method which concatenates s1 to s2 Lab4_1, Programming 2 25 Using CharAt method which returns the character of s1 at index 1 Lab4_1, Programming 2 26 Using toUpperCase method which converts all the character of s1 to uppercase Lab4_1, Programming 2 27 Using toLowerCase method which converts all the character of s1 to lowercase Lab4_1, Programming 2 28 Using equals method which compare s1 to s2 Lab4_1, Programming 2 29 Using equalsIgnoreCase method which compare s1 to s2 ignoring case consideration Lab4_1, Programming 2 30 Using CompareTo method which compare s1 to s2 Lab4_1, Programming 2 31 Using Substring method which extracting substring from s1 Lab4_1, Programming 2 32 The Output EXERCISE1: PHONE KEYPADS Problem Description: Lab4_1, Programming 2 33 The international standard letter/number mapping found on the telephone is shown below: EXERCISE1: PHONE KEYPADS Write a method that returns a number, given an uppercase letter, as follows: public static int getNumber(char uppercaseLetter) Lab4_1, Programming 2 34 Write a test program that prompts the user to enter a phone number as a string. The input number may contain letters. The program translates a letter (upperor lowercase) to a digit and leaves all other characters intact. Lab4_1, Programming 2 35 OUTPUT Any question Lab4_1, Programming 2 36 THANK YOU