LIBRARY CHAPTER 6 - A set of ready-made software routines (class definitions) that can be reused in new programs, is called a Library. - Some commonly used java libraries are : Math Library String Library IO(Input/output) Library WORKING WITH STRINGS Java offers two classes to work with string data: 1. String Class Whose instances or object can hold unchanging string (immutable string) i.e. once initialized its contents cannot be modified. 2. StringBuffer Class Whose instance or objects can hold (mutable) strings that can be changed or modified. CREATING STRINGS String <object/variable> = <value of object/variable> ; e.g. String name = I am a Student ; CREATING STRINGBUFFERS Java provide three ways to create Stringbuffer object: 1. StringBuffer s = new StringBuffer( ); This method creates an empty StringBuffer object namely s. 2. StringBuffer str = new StringBuffer( First ); This method creates a StringBuffer object namely str and initializes it with string value First. 3. int n =15; StringBuffer str2 = new StringBuffer(n); This method creates the StringBuffer with an initial capacity equal to n (=15 here) number of characters. METHODS OF STRING CLASS 1. char charat( int index) - Returns the character at the specified index. e.g. String a = INFORMATICS PRACTICE ; char ch=a.charat(0); System.out.println( +ch ); The value in ch will be I as the index of this character is 0. 2. int capacity( ) - Returns maximum no. of characters that can be entered in the string object. Page 1 of 8
e.g. CHAPTER 6 String a = INFORMATICS PRACTICES ; int cap =a.capacity( ); System.out.println( +cap ); The output is 21. 3. int compareto( String1 ) - Compares two strings lexicographically. 0 (Zero) is returned if the strings are identical. e.g.1 String a = ABC ; String b = ABC ; int c = a.compareto(b); System.out.println( +c ); The output is 0. e.g.2 String a = ABC ; String b = DBC ; int c = a.compareto(b); System.out.println( +c ); The output is -3. 4. String concat(string str) - Concatenates the specified string to the end of this string (current string object). e.g. String a = INFORMATICS ; String b = PRACTICES ; String c = a.concat (b); OR String c = a + b ; System.out.println(c) ; 6. boolean equals(string) The output is INFORMATICSPRACTICES - compares this string (current string object) to the specified string. e.g. e.g. 2. String a = IBCM ; String b = IBCM ; boolean c = a. equals(b); The output is true String a = IBCM ; String b = ibcm ; boolean c = a. equals(b); The output is false Page 2 of 8
7. boolean equalsignorecase(string) - compares this string (current string object) to the specified string, ignoring case consideration. e.g. String p= INFORM ; String q= inform ; boolean r = p.equalsignorecase(q); System.out.println( +r); The output is true 8. int indexof(char) - Returns the index within the this string (current string object) of the first occurrence of the specified character. 9. int lastindexof(char) String b = INTIMATION ; int z = b.indexof( I ); System.out.println( +z); The output is 0 - Returns the character within this string of the last occurrence of the specified character. 10. int length( ) String b = INTIMATION ; int z = b.lastindexof( I ); System.out.println( +z); The output is 7 - Returns the length of this string. String a= INFORMATICS ; int b = a.length( ); System.out.println( +b); The output is 11 11. String replace(char oldchar, char newchar) - Returns a new string resulting from replacing all occurrence of old char in this string with new char. String a = INFORMATION ; String c = a.replace( I, P ); System.out.println( +b); 12. boolean endswith(string) The output is PNFORMATPON - Tests if this string (current String object) ends with the specified suffix. e.g. String a = INFORMATICS ; boolean s = a.endswith( MATICS ); Page 3 of 8
13. boolean startswith(string) CHAPTER 6 System.out.println( s); The output is true for above code. - Tests if this string (current String object) starts with specified suffix. e.g. String a = JAVAPROGRAM ; boolean s = a.startswith( JAVA ); System.out.println( s); The output is true for above code. 14. String substring(int, int ) - Returns a new string that is a substring of the this string. e.g.1. String a = INFORMATICS PRACTICES ; String c = a.substring(0,5); System.out.println( +c); The output is INFOR e.g.2. String a = INFORMATICS PRACTICES ; String c = a.substring(8,13); System.out.println( +c); The output is ICS P 15. String tolowercase( ) - Converts all the characters in this String to lower case. e.g. String z = PRACTICES ; String n = z.tolowercase( ); System.out.println( +n); The output is practices 16. String touppercase( ) - Converts all the characters in this String to upper case. e.g. String z = Informatics ; String n = z.touppercase( ); System.out.println( +n); The output is INFORMATICS 17. String trim( ) - Removes white spaces from both sides of this String. e.g. String a = INFORMATION ; String c = a. trim ( ) ; The output is INFORMATION Page 4 of 8
18. String valueof(all types) - Returns the String representation of the passed argument. e.g. int a = 123 ; valueof(a) ; The value retuned by above statement is 123 STRING BUFFER METHODS 1. String append (String) - adds the specified string in parameter at the end of current String object. e.g. StringBuffer s = new StringBuffer( INFORMATICS ); String c = s.append( PRACTICES ); The Output is INFORMATICSPRACTICES 2. String insert(int, String) - adds the string specified in parameter, at the index specified in first parameter. e.g. StringBuffer s = new StringBuffer( IFORM ); String c = s.insert(1, N ); The output is INFORM 3. String setcharat (int, char ) - Alter just one character. It replaces character specified in second parameter at the index specified in first parameter. e.g. StringBuffer b = new StringBuffer( POGRAM ); String c = b.setcharat(1, R ); The output is PROGRAM 4. String delete (int beginindex, int endindex) -delete the characters from begin to end index. e.g. StringBuffer s = new StringBuffer( INFORMATICS ); String c = s.delete(6,10); The output is INFORM 5. String reverse( ) - Reverse the content of String Buffer. e.g. StringBuffer str = new StringBuffer( POWDER ); String a = str.reverse( ); System.out.println( +a); The output is REDWOP Page 5 of 8
MATH CLASS METHODS 1. pow ( ) - This method returns the first argument raised to the power of the second argument. Syntax is : double pow(double, double) OR int pow(int, int) e.g. Math.pow(2, 3) = 2 3 = 8. Math.pow(2.0, 2.0 ) = 2.0 2.0 = 4.0 2. round( ) - Round off the number to the nearest integer. Syntax is : double round( double) OR float round(float) e.g. Math.round(1.5) = 2 Math.round(1.568, 2) = 1.57 Math.round(2.68, 0) = 3 PROBLEMS Q1. Write the output that will be displayed in the textfields by the following Java code : String x ; String stream = Commerce ; jtextfield1.settext((stream.length( ) + 9) + ); x = stream.concat( Humainities ); jtextfield2.settext(x); jtextfield3.settext( +x. substring(2,5)); jtextfield4.settext(stream.tolowercase( )); Q2. Predict the output of the following code : String s = COMPUTER ; int L = s.length(); for(int i =L ; i >=0 ; i--) System.out.println(s.substring(0, i)); Q3. What will be the content of jtextarea1 after the execution of the following statement? String message = All the Best ; jtextfield1.settext((message.length() - 6 )+ ); Q4. What will be the value of X1 after the execution of the following code? String X1= Graduate, X2= Post ; X1=X2.concat(X1); Page 6 of 8
Q5. Give the output of the following java code: String name = Sid Nagar ; int T = name.length( ), N ; N = 150 T ; jtextfield2.settext(integer.tostring(t)); jtextfield3.settext(integer.tostring(n)); Q6. What will be the contents of jtextfield1 and jtextfield2 after executing the following code : String s = Best ; jtextfield1.settext(s.length( ) + ); jtextfield2.settext(s.touppercase( )); Q7. What will be the contents of str1 and str2 after the following code is executed? String Str2, Str1 ; Str1 = Dear Friend ; Str2 = Hello ; Str1 = Str2.concat(Str1); Q8. What will be the contents of jtextfield1 and jtextfield2 after executing the following : String name1 = Hello World ; Name1 = name.tolowercase( ) ; String name2 = name1.replace(,! ); jtextfield1.settext(name1); jtextfield2.settext(name2); Q9. Given an int variable K with value 253. It is converted into equivalent string i.e. 253. What are two ways of doing this? Q10. Write the output : System.out.println(Math.pow(4.0,2.0)); System.out.println(Math.pow(6.459)); Q11.What will be the contents of jtextfield1 and jtextfield2 after executing the following code: String s = ABC MicroSystems ; jtextfield1.settext(s.length( ) + ); jtextfield2.settext(s.tolowercase( )); Q12. Given a String object named code having value as 908 stored in it. What will be result of the following? JOptionPane.showMessageDialog(null, +(code.length( ) +Integer.parseInt(code)); Q13. Assuming String age = 17 ; Write the Java expression to store value of age in a variable of double type(say double d). Q14. What will be the value of X1 after execution of the following code : String X1= Spread, X2= PEACE ; X1 = X2.concat(X1); Q15. What will be the content of jtextfield1 and jtextfield2 after executing the following code: String st = New to Information Technology ; jtextfield1.settext(st.replace( Technology, Practices )); jtextfield2.settext(st.substring(7)); Page 7 of 8
Page 8 of 8