Professional Documents
Culture Documents
12/10/2019
String Length Method- length()
2
12/10/2019
Concatenating Methods-concat()
3
12/10/2019
charAt(index)
4
12/10/2019
indexOf(substring/char)
5
Example
public class StringDemo
public static void main(String[] args) {
String str = “Java is Programming Language";
int A=str.indexOf(“is",4 );
int B=str. indexOf('F');
System.out.println(" is Starts at index "+ A);
System.out.println(" F occured at index "+ B);
}}
World Starts at index 5
F occured at index -1
12/10/2019
Substring Methods
7
12/10/2019
8
12/10/2019
Example
9
public class Example {
public static void main(String[] args) {
T e x t P r o c e s s i n g i s H a r d
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
P R O C E S S I N G I S E A S Y
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
E A S Y p r o g r a m m i n g
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
12/10/2019
Data type Conversion
11
12/10/2019
12
Syntax is:
variable = (data-type) value;
Here, data-type specifies the desired type to convert the specified value to.
Example:-
Conversion of int to byte.
byte b; i and b 257 1
int i = 257;
double d = 323.142;
Conversion of double to int.
System.out.println("\nConversion of int to byte.");
d and i 323.142 323
b = (byte) i;
System.out.println("i and b " + i + " " + b);
System.out.println("\nConversion of double to int."); Conversion of double to
i = (int) d; byte.
System.out.println("d and i " + d + " " + i); d and b 323.142 67
System.out.println("\nConversion of double to byte.");
b = (byte) d;
System.out.println("d and b " + d + " " + b);
12/10/2019
string to a primitive type Conversion
13
12/10/2019
14
Example
int a=1234;
String str=Integer.toString(a);
12/10/2019
Control Structure
15
12/10/2019
if Statement
16
12/10/2019
17
Example:
Public class IfStatement {
public static void main(String[] args) {
int number = 10; if (number > 0) {
System.out.println("Number is positive."); }
System.out.println("This statement is always executed."); }
}
12/10/2019
18
12/10/2019
Example
19
12/10/2019
if..else if..else Statement
20
if (expression1)
{
// codes
}
else if(expression2)
{
// codes
}
.
.
else
{
// codes
}
12/10/2019
Example
21
else if (b< 0) {
System.out.println("Number is negative.");
}
else {
System.out.println("Number is 0.");
}
}
12/10/2019
Nested if..else Statement
22
12/10/2019
Solution
23
12/10/2019
Exercise
24
Sample:
Enter three integers
45
32
-1
The numbers in creasing order is:- -1,32,45
12/10/2019
Switch
25
12/10/2019
26
12/10/2019
27
12/10/2019
Loops
28
12/10/2019
for loop
29
12/10/2019
How for loop works?
30
12/10/2019
for loop flow chart
31
12/10/2019
Example:
32
12/10/2019
infinite for Loop
33
Infinite
for ( ; ; ) { }
12/10/2019
while loop
34
12/10/2019
35
12/10/2019
Flow Chart of do..while()
37
Example
Write calculates the sum of numbers entered by the
user until user enters 0.
12/10/2019
Solution
38
import java.util.Scanner;
public class Sum {
public static void main(String[] args) {
Double number, sum = 0.0;
Scanner input = new Scanner(System.in);
do {
System.out.print("Enter a number: ");
number = input.nextDouble();
sum += number;
} while (number != 0.0);
System.out.println("Sum = " + sum);
}
}
12/10/2019
39
Example
Enter Your Name: Abebe
The reversed name is: ebebA
12/10/2019
Solution
40
import java.util.Scanner;
public class Reverse {
12/10/2019
break Statement
41
12/10/2019
continue Statement
42
12/10/2019
Example
43
12/10/2019
45
12/10/2019
Accessing Elements of Array
46
12/10/2019
One Dimensional Array
47
And here’s a loop that fills an array of player names with strings entered by
the user:
String scientist[] = new String[10];
Scanner sc=new Scanner(new System.in)
for (int i = 0; i < scientist.length; i++) {
System.out.print(“Enter scientist’s name: “);
scientist[i] = sc.next(); // sc is a Scanner
}
12/10/2019
Multidimensional Arrays
48
12/10/2019
49
12/10/2019
50
12/10/2019
51
12/10/2019
52
12/10/2019
53
12/10/2019
54
Question?
12/10/2019