CSE215 Chapter 4 Math Functions Chars and Strings
CSE215 Chapter 4 Math Functions Chars and Strings
Mathematical Functions,
Characters, and Strings
1
Outline
1. Java Class Library
2. Class Math
3. Character Data Type
4. Class String
5. printf Statement
2
1. Java Class Library
• A class library is a collection of classes that we use
when developing programs
• The Java standard class library is part of any Java
development environment
• The library classes are not part of the Java language
per se, but we rely on them heavily
• Various library classes we've already used in our
programs, such as System, Scanner, and Random
• Other class libraries can be obtained through third
party vendors, or you can create them yourself
• Classes must be imported into the program
3
Packages
• The classes of the Java standard class library are
organized into packages
• Some of the packages in the standard class library are:
Package Purpose
4
import Declaration
• When you want to use a class from a package, you
could use its fully qualified name
java.util.Scanner
• Or you can import the class, and then use just the
class name
import java.util.Scanner;
5
import Declaration
• All classes of the java.lang package are
imported automatically into all programs
• It's as if all programs contain the following line:
import java.lang.*;
6
2. Class Math
• The Math class is part of the java.lang package
• The Math class contains methods (called class methods)
that perform various mathematical functions:
PI constant
E (base of natural logarithms) constant
Trigonometric Methods
Exponent Methods
Rounding Methods
min, max, abs, and random Methods
7
Trigonometric Methods
Examples:
• sin(double a)
Math.sin(0) returns 0.0
• cos(double a)
Math.sin(Math.PI/6) returns 0.5
• tan(double a)
Math.sin(Math.PI/2) returns 1.0
• acos(double a) Math.cos(0) returns 1.0
• asin(double a) Math.cos(Math.PI/2) returns 0
• atan(double a) Math.cos(Math.PI/6) returns 0.866
8
Exponent Methods
• exp(double a) Examples:
Returns e raised to the power of a.
• Math.exp(1) returns 2.71
log(double a)
Math.log(2.71) returns 1.0
Returns the natural logarithm of a.
Math.pow(2,3) returns 8.0
• log10(double a)
Math.pow(3,2) returns 9.0
Returns the 10-based logarithm of a.
Math.pow(3.5,2.5) returns
• pow(double a, double b) 22.91765
Returns a raised to the power of b. Math.sqrt(4) returns 2.0
• sqrt(double a) Math.sqrt(10.5) returns 3.24
Returns the square root of a.
9
Rounding Methods
• double ceil(double x)
x is rounded up to its nearest integer. This integer is returned as
a double value.
• double floor(double x)
x is rounded down to its nearest integer. This integer is returned
as a double value.
• double rint(double x)
x is rounded to its nearest integer. If x is equally close to two
integers, the even one is returned as a double.
• int round(float x)
returns (int)Math.floor(x+0.5)
• long round(double x)
returns (long)Math.floor(x+0.5)
10
Rounding Methods Examples
Math.ceil(2.1) returns 3.0
Math.ceil(2.0) returns 2.0
Math.ceil(-2.0) returns –2.0
Math.ceil(-2.1) returns -2.0
Math.floor(2.1) returns 2.0
Math.floor(2.0) returns 2.0
Math.floor(-2.0) returns –2.0
Math.floor(-2.1) returns -3.0
Math.rint(2.1) returns 2.0
Math.rint(2.0) returns 2.0
Math.rint(-2.0) returns –2.0
Math.rint(-2.1) returns -2.0
Math.rint(2.5) returns 2.0 //returns even value as double
Math.rint(-2.5) returns -2.0
Math.round(2.6f) returns 3 //round returns integers
Math.round(2.0) returns 2
Math.round(-2.0f) returns -2
Math.round(-2.6) returns -3
11
Min(), max(), and abs()
12
Method random()
Generates a random double value greater than or equal to 0.0 and
less than 1.0 (0.0 <= Math.random() < 1.0)
Examples:
Returns a random integer
(int)(Math.random() * 10)
between 0 and 9.
In general,
13
3. Character Data Type
A char variable stores a single character.
Character literals are delimited by single quotes:
'a' 'X' '7' '$' ',' '\n' '\t'
Example declarations:
char topGrade = 'A';
char terminator = ';', separator = ' ';
14
Character Type - Revisited
char letter = 'A'; Four hexadecimal digits.
NOTE: The increment and decrement operators can also be used on char variables
to get the next or preceding Unicode character. For example, the following statements
display character b.
char ch = 'c';
ch = ch + 1;
System.out.println(ch); //prints character d
ch = ch - 2;
System.out.println(ch); //prints character b
15
ASCII Code in Decimal
17
Comparing char Type
18
Class Character Methods
Method Description
19
Class Character Methods
Character ch1 = new Character('b'); //object NOT char type
Character ch2 = new Character(‘9'); //object NOT char type
20
Class Character Test
// Class Character Test
import java.util.Scanner;
public class CharacterTest
{
public static void main (String[] args)
{
Character ch1 = new Character('b'); //object NOT char type
Character ch2 = new Character('9'); //object NOT char type
}
} 21
Escape Sequences
Description Escape Sequence Unicode
Backspace \b \u0008
Tab \t \u0009
Linefeed \n \u000A
Carriage return \r \u000D
------------------------------------------------------------------------
Backslash \\ \u005C
Single Quote \' \u0027
Double Quote \" \u0022
22
4. Class String
• To create a String object, we need to declare a variables
of type String:
String title = "Java Software Solutions";
23
String Methods
• However, several methods of the String class return
new String objects that are modified versions of the
original string
• A String object is a sequence of characters (known as
Single-Dimensional Array).
String courseName = "CS 2301";
0 1 2 3 4 5 6
C S 2 3 0 1
24
String Index Values
• It is occasionally helpful to refer to a particular character
within a string
• This can be done by specifying the character's numeric
index (position)
• The indexes begin at zero in each string
• In the string "Hello", the character 'H' is at index 0
and the 'o' is at index 4
25
Getting Characters from a String
26
String Concatenation
// Three strings are concatenated
String message = "Welcome " + "to " + "Java";
27
Example
public class StringMutation
{
// Prints a string and various mutations of it.
public static void main (String[] args)
{
String phrase = "Change is inevitable";
String mutation1, mutation2, mutation3, mutation4;
29
Other String Methods
String S1 = "Welcome";
String S2 = new String(char[]);
S2 = " Hello! ";
char ch = S1.charAt(index);
int length = S1.length();
int index = S1.indexOf(ch);
int index = S1.lastIndexOf(ch);
boolean b = S1.equals(S2);
boolean b = S1.equalsIgnoreCase(S2);
boolean b = S1.startsWith(S2);
Boolean b = S1.endsWith(S2);
String S = S1.toUpperCase();
String S = S2.toLowerCase();
String S = S2.substring(i); //from position i to last position
String S = S2.substring(i,j); //excluding j position
String S = S2.replace(ch1,ch2);
String S = S2.trim(); //returns "Hello!", no spaces
30
Reading Strings
Scanner input = new Scanner(System.in);
System.out.print("Enter three words separated by spaces: ");
String s1 = input.next();
String s2 = input.next();
String s3 = input.next();
System.out.println(“First word is " + s1);
System.out.println(“Second word is " + s2);
System.out.println(“Third word is " + s3);
Note: If we use
String s1 = input.nextLine();
s1 contains all typed characters until we press the "Enter" key.
• next() can read the input only till the space while nextLine() reads
input including space between the words until the new line.
31
Reading Characters
32
Comparing Strings
Method Description
33
Obtaining Substrings
Method Description
substring(beginIndex) Returns this string’s substring that begins with the character at the specified
beginIndex and extends to the end of the string, as shown in Figure 4.2.
substring(beginIndex, Returns this string’s substring that begins at the specified beginIndex and
endIndex) extends to the character at index endIndex – 1, as shown in Figure 9.6.
Note that the character at endIndex is not part of the substring.
34
indexOf() method
Method Description
indexOf(ch) Returns the index of the first occurrence of ch in the string. Returns -1 if not
matched.
indexOf(ch, fromIndex) Returns the index of the first occurrence of ch after fromIndex in the string.
Returns -1 if not matched.
indexOf(s) Returns the index of the first occurrence of string s in this string. Returns -1 if
not matched.
indexOf(s, fromIndex) Returns the index of the first occurrence of string s in this string after
fromIndex. Returns -1 if not matched.
lastIndexOf(ch) Returns the index of the last occurrence of ch in the string. Returns -1 if not
matched.
lastIndexOf(ch, Returns the index of the last occurrence of ch before fromIndex in this
fromIndex) string. Returns -1 if not matched.
lastIndexOf(s) Returns the index of the last occurrence of string s. Returns -1 if not matched.
lastIndexOf(s, Returns the index of the last occurrence of string s before fromIndex.
fromIndex) Returns -1 if not matched.
35
Conversion of
Strings/Numbers
You can convert strings of digits to numbers:
String intString = “123”;
int intValue = Integer.parseInt(intString);
36
End of Chapter 4
37