0% found this document useful (0 votes)
10 views

Library Classes

The document provides an overview of Java library classes, specifically focusing on the String class and its methods, including examples of usage. It covers various methods such as length(), toLowerCase(), toUpperCase(), and others, along with their descriptions and sample code. Additionally, it introduces StringBuffer and outlines several class designs for string manipulation tasks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Library Classes

The document provides an overview of Java library classes, specifically focusing on the String class and its methods, including examples of usage. It covers various methods such as length(), toLowerCase(), toUpperCase(), and others, along with their descriptions and sample code. Additionally, it introduces StringBuffer and outlines several class designs for string manipulation tasks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Library Classes

Library classes:- Pre-defined classes of java which get includes in an application


program itself are called the library classes.

i) String class:- A ‘String’ is term that refers to a set of characters. Java


provides a class String in java.lang package whose objects can be treated as
variable of type String.
e.g. String str=”Hello”;

Methods in String Class:-


Method Return type Arguement Description
length() int nil Returns the number of character of the calling
string which include white space
toLowerCase() String nil The calling string gets converted in to lower case.
toUpperCase() String nil The calling string gets converted in to upper case.
trim() String nil Removes extra white space before and after the
string, but does not remove while space within the
string.
class StringDemo1
{
public void test()
{
String str=” String Class in Java “;
str= str.toUpperCase();
System.out.println(str);
str= str.toLowerCase();
System.out.println(str);
str = str.trim();
System.out.println(str);
int n= str.length();
System.out.println(n);
}
}
Output:- STRING CLASS IN JAVA
string class in java
string class in java
20
Method Return Arguement Description
type
charAt() char int Returns the character at the specified index. Where index
starts from 0.
class StringDemo2
{
public void test()
{
String str=”Working with Java”;
char ch = str.charAt(3);
System.out.println(ch);
}
}
Output :- K

Method Return Arguement Description


type
indexOf() int char Returns index position of the character passed as
argument starting from zero index.
int char, int Returns index position of the character passed as
argument starting from the index number specified as
second parameter.
int String Returns index position of the String passed as arguments
starting from zero index no.
int String, int Returns index position of the String passed as argument
starting from the index no. specified as second
parameter.
class StringDemo3
{
public void test()
{
String str=”work work work”;
int p1=str.indexOf(‘w’);
int p2=str.indexOf(‘w’,2);
int p3=str.indexOf(“work”);
int p4=str.indexOf(“work”, 3);
System.out.println(p1); // 0
System.out.println(p2); // 5
System.out.println(p3); // 0
System.out.println(p4); // 5
}
}

Method Return Arguement Description


type
equals() boolean String When the calling string and the string within
arguments are same, function returns true else
false
equalsIgnoreCase() boolean String When the calling string and the string within
argument are same ignoring the case whether
it is capital or small case, function returns true
else false.
compareTo() int String a) The calling string is less than string in
the argument then the function returns
any negative value
b) The calling string is greater than string in
the argument then the function returns
any positive value.
c) The calling string is equal to the string in
the argument then the function returns 0.
class StringDemo4
{
public void test()
{
String str1=”INDIA”;
String str2=”india”;
System.out.println(str1.equals(str2));
System.out.println(str1.equalsIgnoreCase(str2));
String str3 = ”hello”;
String str4 = “hello”;
String str5 = “Hello”;
System.out.println(str3.compareTo(str4));
System.out.println(str3.compareTo(str5));
System.out.println(str5.compareTo(str3));
}
}
Method Return Arguement Description
type
substring() String int Return part of String from the specified start index
position.
String int, int Returns parts of String from the specified start index
( first parameter) excluding character at the end index.
class StringDemo5
{
public void test()
{
String str1=”Working with String class in java”;
String str2= str.substring(13);
String str3=str.substring(13, 20);
System.out.println(str2);
System.out.println(str3);
}
}
Output:- String class in java
String
Method Return Arguement Description
type
valueOf() String int / float Returns String after converting the numeric values which
are passed as argument to the double String. As
valueOf() is static function it is called using class name
and dot operator.
345.62
String.valueOf(345.62);
“345.62”

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.

3) A class Modify has been defined with the following details:


Class name Modify
Data members
St Stores a string.
len To store the length of the string
Member function:
void read( ) To accept the string in Uppercase
alphabets
void putin(int, char) To insert a character at the specified
position in the string and display the
changed string.
void takeout(int) To remove character from the
specified position in the string and
display the changed string
void change( ) To replace each character in the
original strin by the character which
is at adistance of 2 moves ahead.
For example “ABCD” becomes
“CDEF”
“XYZ” becomes “ZAB”.
4) A class SortWord has been defined with the following details:
Class name SortWord
Data members
txt Store the word
len Store the length of the word
Member functions:
SortWord( ) Default constructor
void readTxt( ) To accept the word in lower case.
void sortTxt() To sort the word in alphabetical
order of characters using bubble
sort technique and display it
void changeTxt( ) To change the case of vowels in th
word to Upper case.
void display( ) To display the changed 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.

6) Design a class VowelWord to accept a sentence and calculate the frequency


of words that begin with a vowel. The words in the input string are separated
by a single blank space and terminated by a full stop. The description of the
class is given below:
Class name VowelWord
Data members
str To store a sentence.
freq Store the frequency of the words beginning with a vowel.
Member function
VowelWord( ) Constructor to initialize data members legal initial value.
void readstr() To accept a sentence.
void freq_vowel( ) Counts the frequency of the word that begin with a
vowel.
void display( ) To display the original string and the frequency of the
words that begin with vowels.

You might also like