Lecture 4 String.ppt
Lecture 4 String.ppt
•Strings
String
1
1. Java Class Library
2
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
3
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;
• To import all classes in a particular package, you can use the *
wildcard character
import java.util.*; // wildcard
4
import Declaration
5
2. Class Math
• PI constant
• E (base of natural logarithms) constant
• Trigonometric Methods
• Exponent Methods
• Rounding Methods
• min, max, abs, and random Methods
6
Example
import java.util.Scanner;
public class Quadratic
{
public static void main (String[] args)
{
int a, b, c; // ax^2 + bx + c
double discriminant, root1, root2;
Scanner scan = new Scanner (System.in);
7
Example
Output:
8
Trigonometric Methods
Examples:
• sin(double a)
• cos(double a)
Math.sin(0) returns 0.0
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
9
Exponent Methods
• exp(double a) Examples:
Returns e raised to the power of a.
• log(double a) Math.exp(1) returns 2.71
Returns the natural logarithm of a. Math.log(2.71) returns 1.0
Math.pow(2,3) returns 8.0
• log10(double a)
Returns the 10-based logarithm of a. Math.pow(3,2) returns 9.0
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.
10
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)
11
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
12
Min(), max(), and abs()
Examples:
• max(a,b)and min(a,b)
Returns the maximum or minimum
of two parameters. Math.max(2,3) returns 3
• abs(a) Math.max(2.5,3) returns 3.0
Returns the absolute value of the Math.min(2.5,3.6) returns 2.5
parameter. Math.abs(-2) returns 2
Math.abs(-2.1) returns 2.1
13
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:
In general,
14
Generating Random Characters
Note:
Since 0.0 <= Math.random() <1.0, you have to add 1 to 65535
15
Generating Random Characters
Lowercase letter: The Unicode for lowercase letters are consecutive integers starting
from the Unicode for 'a', 'b', 'c', ..., and 'z'.
To generalize, a random character between any two characters ch1 and ch2 with ch1 <
ch2 can be generated as follows:
17
Character Type - Revisited
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
18
ASCII Code in Decimal
19
Casting char Type
20
Comparing char Type
21
Class Character Methods
22
Class Character Methods
Character ch1 = new Character('b'); //object NOT char type
Character ch2 = new Character(‘9'); //object NOT char type
23
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
25
4. Class String
26
String Methods
0 1 2 3 4 5 6
C S 2 3 0 1
27
String Index Values
28
Getting Characters from a String
29
String Concatenation
// Three strings are concatenated
String message = "Welcome " + "to " + "Java";
30
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;
31
Example
Output:
32
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
33
Reading Strings
Note: If we use
String s1 = input.nextLine();
s1 contains all typed characters until we press the "Enter" key.
34
Reading Characters
35
Comparing Strings
36
Obtaining Substrings
37
indexOf() method
38
Conversion of Strings/Numbers
39
5. printf() Statement
System.out.printf(format, items);
40
Frequently-Used Specifiers
Specifier Output Example .
%b a boolean value true or false
%c a character 'a'
%d a decimal integer 200
%f a floating-point number 45.4600000
%e a standard scientific notation 4.556000e+01
%s a string "Java is cool"
Homework: Type and run program FormatDemo, listing 4.6, page 148. It shows
how to display tabulated outputs using printf() statement.
41