Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 19

Fields of Math class:

 Math defines the following two constants fields of type double :

 static double E :


 This is the double value that is closer than any other to e (approximately
2.72).
 Represents the base of the natural logarithms.
 static double PI :
 This is the double value that is closer than any other to pi (approximately
3.14).
 the ratio of the circumference of a circle to its diameter.

CS-3130 Advance Object Oriented Programming (2013) 1


Methods of a Math Class:
 The major functions defined within the built in Math Class are divided
into the following categories:

 Transcendental Functions:
 Exponential Functions:
 Rounding Functions:

CS-3130 Advance Object Oriented Programming (2013) 2


Transcendental Functions:

CS-3130 Advance Object Oriented Programming (2013) 3


Exponential Functions:

CS-3130 Advance Object Oriented Programming (2013) 4


Rounding Functions:

CS-3130 Advance Object Oriented Programming (2013) 5


Miscellaneous Math Methods :

CS-3130 Advance Object Oriented Programming (2013) 6


Math Methods (Programming Example):
 // Demonstrate toDegrees() and toRadians().
 class Angles {
 public static void main(String args[]) {
 double theta = 120.0;
 System.out.println(theta + " degrees is " +
 Math.toRadians(theta) + " radians.");
 theta = 1.312;
 System.out.println(theta + " radians is " +
 Math.toDegrees(theta) + " degrees.");
 }
 } PROGRAM OUTPUT:

120.0 degrees is 2.0943951023931953 radians.


1.312 radians is 75.17206272116401 degrees.

CS-3130 Advance Object Oriented Programming (2013) 7


String class:
 Most commonly used class in JAVA library.
 Defined under java.lang package.
 In java, every string is actually an object of class String.
 String objects are immutable, means it cannot be changed once it’s being
created.

CS-3130 Advance Object Oriented Programming (2013) 8


String class (Contd):
 Creating a String object:
 String object can be declared in the following ways:

a) String Literal:
 Simple string enclosed in double quotes “ ”.

 Its being treated as a string object.

 String str = “HELLO”;

b) Using another String object & new Keyword:


 String str = “HELLO”;

 String str1 = new String(str);

CS-3130 Advance Object Oriented Programming (2013) 9


Object creation & storage mechanism:
 Each time a string object is created by using a String Literal, the string
object is being stored in a special memory area name string constant pool.

 Consider the following example:


 String str = “HELLO”; //string object using a string literal

CS-3130 Advance Object Oriented Programming (2013) 10


Object creation & storage mechanism (Contd):
 If we Create & initialize another string object with the same string, than it would
be returned the reference of the existing string.

 For instance:
 String str = “HELLO”; //string object using a string literal
 String str2 = str;

CS-3130 Advance Object Oriented Programming (2013) 11


String Concatenation:
 There are two methods to concatenate two or more strings:

 a) concat method
 b) + operator

 String str1 = “HELLO”; // using a string literal


 String str2 = “WORLD”; // using a string literal

 String str3 = str1.concat(str2);


OR
 String str3 = str1 + str2;

CS-3130 Advance Object Oriented Programming (2013) 12


String Comparison:
 There are three methods to compare two strings:

a) equals() method
 Compares the content of two strings for equality.

 Syntax :

 Boolean equals(Object s1);

 Returns TRUE if string matches otherwise FALSE.

b) == operator
 Compares two object references whether they refer to the same instance.

 Returns TRUE if string matches otherwise FALSE.

CS-3130 Advance Object Oriented Programming (2013) 13


String Comparison (Contd):

c) CompareTo() method

 Compare strings & return an int value.


 Returns -1 if string compared is less than the other string.
 Returns 0 if string compared is equal to the other string.
 Returns 1 if string compared is greater than the other string.

CS-3130 Advance Object Oriented Programming (2013) 14


String class functions:
Most commonly used methods of string class are mentioned below:
METHOD DESCRIPTION

charat() Returns the character located at the specific index.


Equalsignorecase() Determines whether the two strings are equal or not (ignoring
their case)
length() Returns the number of characters in a string
replace() Replace a specific character in a string with a new character.

substring(int begin) Returns a part of a string from the specified starting point to the
end point of the original string.
substring(int begin, int end) Returns a part of a string from the specified starting point to the
specified end point of the original string.
tolowercase() Converts all the characters of a string to lowercase.
touppercase() Converts all the characters of a string to uppercase.

trim() Returns a string by removing any whitespaces.

CS-3130 Advance Object Oriented Programming (2013) 15


String Buffer class:
 Used to create mutable string objects.
 Since string class objects are immutable, therefore making changes in the
string objects will end up with memory leakage.
 String buffer enable us to make considerable modifications in our string
objects.

 String buffer contains 4 constructors:

 String buffer(): //creates an empty string & reserves space for 16


characters
 String buffer(int size) //creates an empty string & takes an integer
argument to set the capacity of the buffer.
 String buffer(String str)
 String buffer(charsequence []ch)

CS-3130 Advance Object Oriented Programming (2013) 16


String Buffer class (METHODS):
 Most common methods of StringBuffer class are mentioned below:
 append():
 Used to concatenate the string representation of any type of data with the
called stringbuffer object.
 append() method is being overloaded and contains the following three
versions:

 append(string str)
 append(int r)
 append(object obj)

CS-3130 Advance Object Oriented Programming (2013) 17


String Buffer class (METHODS):
 insert():
 Used to insert one string into another.
 insert() method is being overloaded and contains the following three
versions:

 insert(int index, string str)


 insert(int index, int r)
 insert(int index, object obj)

 First parameter mentions the index from which position the string would
be inserted in the stringbuffer object.
 Second parameter indicates the string representation to be inserted in the
string buffer object.

CS-3130 Advance Object Oriented Programming (2013) 18


String Buffer class (METHODS):
 reverse():
 Reverse the order of characters in a string buffer object.

 replace():
 Replaces the string from the specified start index to the end index.

 capacity():
 Returns the current capacity of a string buffer object.

 ensurecapacity():
 Used to ensure minimum capacity of a string buffer object.

CS-3130 Advance Object Oriented Programming (2013) 19

You might also like