Library Classes
Library Classes
double d = Double.parseDouble(“345.62”);
class StringDemo6
{
public void test()
{
double a = 5.5;
String str = String.valueOf(a);
System.out.println(str);
}
}
Method Return type Arguement Description
replace() String char, char Returns String after replacing the character in the
first parameter by the character in second
parameter.
startsWith() boolean String It checks the given String that begin with a specified
string returns either true or false.
endsWith() boolean String It checks the given String that end with a specified
string returns either true or false.
concat() String String Returns a string by joining the calling string with the
string passed as argument.
class StringDemo7
{
public void test()
{
String str1=”OOPS”
str1.replace(‘O’,’A’);
System.out.println(str1);
String str2 = “Life is my college”;
String str3=”Lif”;
String str4=”ege”;
System.out.println(str2.startsWith(str3));
System.out.println(str2.endsWith(str4);
String s = ”NEW”;
String s1 = “DELHI”;
s.concat(s1)
System.out.println(s);
}
}
AAPS
true
true
NEWDELHI
String Buffer Method
String represent fixed – length, immutable Character sequences. Immutable means that
a string object, itself cannot change. Any change in the string needs to be stored in a
new string.
String Buffer:- It represent growable and mutable Character sequences.
1) StringBuffer() – reserves space for 16 characters.
StringBuffer(int size) – sets the size of StringBuffer.
StringBuffer(String s)- initialize the initial string and reserves space for 16 more
characters.
2) length() – calculate length of a StringBuffer.
StringBuffer str = new StringBuffer(“ India”);
Str.length(); -> 5
3) capacity() ->returns total allocation space of a StringBuffer.
str.capacity(); ->21
4) charAt() -> Extract single character.
str.charAt(0) -> I
5) setCharAt() – set the value of a character within a StringBuffer.
str.setCharAt(2,’t’);
System.out.println(str); - >Intia
6) append() – add a new string to the end of StringBuffer.
str.setCharAt(2, ‘d’);
str.append(“Today”);
System.out.println(str); - > IndiaToday
7) insert() – insert one string inside another StringBuffer.
str.insert(5,”-“);
System.out.println(str); India-Today
8) reverse() – reverse the characters of StringBuffer.
StringBuffer s= new StringBuffer(“hello”);
s.reverse();
System.out.println(s); -> olleh
9) delete() – to delete one or more characters of a StringBuffer.
StringBuffer s1= new StringBuffer(“computer”);
s1.delete(3,6); // delete characters from 3rd position to 5th
System.out.println(s1); -> comer
10) deleteCharAt() – to delete one character of a StringBuffer.
StringBuffer s2= new StringBuffer(“Dehradun”);
s2.deleteCharAt(0);
System.out.print(s2); -> ehradun
String Based Question
1) Design a class Alpha which enables a word to be arranged in ascending
order according to its alphabets. The details of the members of the class are
given below:
Class name Alpha
Data members / instance variable
str To store a word
Member functions
Alpha( ) Default constructor
void readword( ) To accept the inputted word
void arrange( ) To arrange the word in alphabetical
order using any standard sorting
technique.
void display( ) Displays the word.
2) A class Mystring has been defined for the following methods/ functions:
Class name Mystring
Data members / instance variable
str To store a string.
len Length of the given string
Member function
Mystring( ) Constructor
void readstring( ) Reads the given string from the input
Int code(int index) Returns ASCII code for the character
at position index .
void word( ) Displays longest word in the string.
5) Input a sentence form the user and count the number of times, the words
“an” and “and” are present in the sentence. Design a class Frequency using
the description given below:
Class name Frequency
Data members
text Store the sentence.
countand To store the frequency of the word “and”.
countan To store the frequency of the word “an”.
len Stores the length of the string.
Member function
Frequency( ) Constructor to initialize the instance
variables.
void accept(String n) To assign n to text, where the value of the
parameter n should be in lower case.
void checkandfreq( ) To count the frequency of “and”.
void checkanfreq() To count the frequency of “an”.
void display( ) To display the number of “and” and “an”
with appropriate messages.