OOP’s Using JAVA Module 2

Содержание

Слайд 2

Topics Covered

Data Types
Operators
Control Statements
Variables & Arrays
String Handling
The String Constructors
String Length
Special String Operations
Character

Topics Covered Data Types Operators Control Statements Variables & Arrays String Handling
Extraction
String Comparison
Modifying a String
String Buffer

Слайд 3

Data Types

Data types specify the different sizes and values that can be

Data Types Data types specify the different sizes and values that can
stored in the variable. There are two types of data types in Java
Primitive data types: The primitive data types include Boolean, char, byte, short, int, long, float and double.
Non-primitive data types: The non-primitive data types include Classes, Interfaces, and Arrays,Strings.
In Java language, primitive data types are the building blocks of data manipulation. These are the most basic data types available in Java language.

Слайд 5

Operators in Java

Operator in Java is a symbol that is used to perform operations.
Java

Operators in Java Operator in Java is a symbol that is used
divides the operators into the following groups:
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Conditional operators

Слайд 6

Arithmetic Operator
Arithmetic operators are used to perform common mathematical operations.

Arithmetic Operator Arithmetic operators are used to perform common mathematical operations.

Слайд 7

Comparison operators

comparison operators are used to compare two values

Comparison operators comparison operators are used to compare two values

Слайд 8

Assignment operators

Assignment operators are used to assign values to variables
For example,

Assignment operators Assignment operators are used to assign values to variables For
we use the assignment operator (=) to assign the value to variable x:
in x=10

Слайд 9

Logical operators

Logical operators are used to determine the logic between variables or

Logical operators Logical operators are used to determine the logic between variables or values
values

Слайд 10

Conditional operators

Consider this example
Int a=30;
Int b=40;
In x=(a>b)?a:b;

Conditional operators Consider this example Int a=30; Int b=40; In x=(a>b)?a:b;

Слайд 11

Control Statements

There are three types of control flow statements.
Conditional statements: based

Control Statements There are three types of control flow statements. Conditional statements:
on particular condition the selected block will be executed
if
if –else
else-if
Switch
2. Loop statements: The same code will be Repeated multiple times
for
while
do-while
3. Transfer statements: The control is transfer from one location to another location is called transfer statements
break
continue

Слайд 12

Control Statement Conditional statements If Statement

To take only one option
Syntax of if statement

Control Statement Conditional statements If Statement To take only one option Syntax
is given below.
if(condition) {    
True body; //executes when condition is true   
}    

Слайд 13

Example of “if” statement Program 1

class Student {    
public static void main(String[] args) {    
int x = 20;    
int y = 10;    
if(x>y) {    //if Condition
System.out.println("x  is greater than y");    
}    
}      

Example of “if” statement Program 1 class Student { public static void

Слайд 14

Control Statement Decision-Making statements “if-else” statement

Two option available.
The if-else statement is an extension to

Control Statement Decision-Making statements “if-else” statement Two option available. The if-else statement
the if-statement, which uses another block of code, i.e., else block.
The else block is executed if the condition of the if-block is evaluated as false.
Syntax:
if(condition) {    
True body; //executes when condition is true   
}  
else{  
False body; //executes when condition is false   
}  

Слайд 15

Example of “if else” statement Program 2

class Student {  
public static void main(String[] args) {  
int x = 20;    
if(x> 18) {  
System.out.println(“Eligible for marriage….");  
}   else {  
System.out.println(“Not eligible for marriage try

Example of “if else” statement Program 2 class Student { public static
after some time….");  
}  
}  
}  

Слайд 16

Control Statement Decision-Making statements “if-else-if” statement

The if-else-if statement contains the if-statement followed by

Control Statement Decision-Making statements “if-else-if” statement The if-else-if statement contains the if-statement
multiple else-if statements.
In other words, we can say that it is the chain of if-else statements that create a decision tree where the program may enter in the block of code where the condition is true.
Syntax
if(condition 1) {    
statement 1; //executes when condition 1 is true   
}  
else if(condition 2) {  
statement 2; //executes when condition 2 is true   
}  
else {  
statement 2; //executes when all the conditions are false   
}  

Слайд 17

Example of “if else if” statement Program 3

class Positive {    
public static void main(String[] args) {    
    int number=-13;    
    if(number>0){  
    System.out.println("POSITIVE");  
    }else if(number<0){  
    System.out.println("NEGATIVE");  
    }else{  
    System.out.println("ZERO");  
   }  
}    
}    

Example of “if else if” statement Program 3 class Positive { public

Слайд 18

Example of “if else if” statement Program 3

class Sleep {  
public static void main(String[] args) {  
    int a=10;  
      if(a==10){  
        System.out.println(“Aslam don’t sleep");  
    }  
    else if(a==20){  
        System.out.println(" Amar don’t sleep

Example of “if else if” statement Program 3 class Sleep { public
");  
    }  
    else if(a==30){  
        System.out.println(“Umar don’t sleep");  
    }  
     else{  
        System.out.println(" Bharath don’t sleep ");  
    }  
}  
}  

Слайд 19

Control Statement Decision-Making statements switch statement

The Java switch statement executes one statement from multiple conditions.

Control Statement Decision-Making statements switch statement The Java switch statement executes one
It is like if-else-if ladder statement. The switch statement works with byte, short, int.
Points to be noted about switch statement:
The case variables can be int, short, byte, char. String type is also supported since version 7 of Java
Cases cannot be duplicate
Default statement is executed when any of the case doesn't match the value of expression. It is optional.

Слайд 20

The syntax to use the switch
switch(argument){    
case value1:   Statements;    
 break;  //optional  
case value2:   Statements;    
 break;  //optional  
......    
default:     
  code to be executed if all cases are not matched;  
}    

The syntax to use the switch switch(argument){ case value1: Statements; break; //optional

Слайд 21

Example of “Switch” statement Program 5

EX:
class SwitchExample {  
public static void main(String[] args) {  
    //Declaring a variable for switch expression  
    int  a=20;  
    //Switch expression  
    switch(a){  
    //Case statements  
    case 10: System.out.println("10");  
    break;  
    case 20: System.out.println("20");  
    break;  
    case 30: System.out.println("30");  
    break;  
    //Default case statement  
    default:System.out.println("Not in 10, 20 or 30");  
    }  
}  
}  
EX:
class SwitchExample2 {  
public static void main(String[] args) {  
    int  a=20;  
    //switch expression with int value  
    switch(a){  
    //switch cases without break statements  
    case 10: System.out.println("10");  
    case 20: System.out.println("20");  
    case 30: System.out.println("30");  
    default:System.out.println("Not in 10, 20 or 30");  
    }  
}  
}  

Example of “Switch” statement Program 5 EX: class SwitchExample { public static

Слайд 22

Loop Statements

Loop statements: The same code will be Repeated multiple times
for

Loop Statements Loop statements: The same code will be Repeated multiple times for while do-while
while
do-while

Слайд 23

Loop Statements for

In Java, for loop is similar to C and C++
It enables us to initialize

Loop Statements for In Java, for loop is similar to C and
the loop variable, check the condition, and increment/decrement in a single line of code.
We use the for loop only when we exactly know the number of times, we want to execute the block of code.
Syntax
for(initialization, condition, increment/decrement) {    
//block of statements    
}    

Слайд 24

Example of “for loop” Program 5

class Test {  
public static void main(String[] args) {      
for(int i= 0; i<=10; i++) {  
System.out.println(“Good Morning…." );
}    
}  
}  

Example of “for loop” Program 5 class Test { public static void

Слайд 25

Loop Statements While

The while loop is also used to iterate over the number of

Loop Statements While The while loop is also used to iterate over
statements multiple times.
For loop is recommended when we have :(start;end_cond;incre_decre)
while loop is recommended when we have the only condition check.
syntax :
while(condition){    
body    
}
The body will be executed until the condition is fail

Слайд 26

Example of “while” Program 6

class Test {    
public static void main(String[] args) {    
// for loop to printing the data 10 times    
for(int j=0;j<10;j++)  
{
System.out.println(“Good

Example of “while” Program 6 class Test { public static void main(String[]
Morning..”); 
}
// while loop to printing the data 10 times   
Int i=0;
while(i<10) {    
System.out.println(“Good Evening..”);  
i++;     
}    
}    
}    
//To print the data 10-time it is recommended to use for loop.

Слайд 27

Loop Statements do While

If you want execute the body first without condition check

Loop Statements do While If you want execute the body first without
then use do while. 
The syntax
do     
{    
//body    
} while (condition); 

Слайд 28

Loop Statements do While

Ex:
class Test {    
public static void main(String[] args) {       
do {    
System.out.println(“Good Morning”);    
}while(20<10);    
}    
}    
EX:
class Test {    
public static void main(String[] args) {       
Int i=0;
do {    
System.out.println(“Good Morning”);  
i++;  
}while(i<10);   
}    
}    

Loop Statements do While Ex: class Test { public static void main(String[]

Слайд 29

Jump Statement

Jump statements are used to transfer the control of the program

Jump Statement Jump statements are used to transfer the control of the
to the specific statements.
There are two types of jump statements in Java, i.e., break and continue.
break statement- As the name suggests, the break statement is used to break the current flow of the program and transfer the control to the next statement outside a loop or switch statement
continue statement –The continue statement break one iteration(in the loop),if a specified condition occurs and continues next iteration in the loop.

Слайд 30

Jump Statement

Jump Statement

Слайд 31

Variables in JAVA

A variables are used to store the values by using

Variables in JAVA A variables are used to store the values by
that value we are achieving the project requirements.
A variable is assigned with a data type.
There are three types of variables in java:
local
instance
static

Слайд 32

Variables in JAVA

1) Local Variable
A variable declared inside the method is called

Variables in JAVA 1) Local Variable A variable declared inside the method
local variable.
You can use this variable only within that method
other methods in the class aren't even aware that the variable exists.
Void m1()
{
Int a=10; // Local variables
System.out.println(a); //Possible
}
Void m2()
{
System.out.println(a); //Not Possible
}

Слайд 33

2) Instance Variable
A variable declared inside the class but outside the method.
Accessing

2) Instance Variable A variable declared inside the class but outside the
done through object .
Direct access.
EX:
class  variable 
{  
    int a=10;//instance variable    
    public static void main(String args[])  
    {  
variable obj=new variable(); /* object creation*/
             System.out.println(obj.a); ------//10
    }  
}   

Слайд 34

3) Static variable
A variable that is declared as static is called a

3) Static variable A variable that is declared as static is called
static variable.
Memory allocation for static variables happens only once.
Direct access.
EX:
class  variable 
{  
    static int a=10;//static variable    
    public static void main(String args[])  
    {  
             System.out.println(a); ------//10
    }  
}   

Слайд 35

Example of “Variables” Program 9

EX:
class  variable 
{  
    int a=10;//instance variable 
static int b=10;//static variable    
    public static void main(String args[])  
    {  
int c=10;//Local variable

Example of “Variables” Program 9 EX: class variable { int a=10;//instance variable
System.out.println(b);
System.out.println(c);
variable obj=new variable(); /* object creation*/
             System.out.println(obj.a); ------//10
    }  
}   

Слайд 36

ARRAY in JAVA

Arrays are used to store the group of elements and

ARRAY in JAVA Arrays are used to store the group of elements
these elements are homogenous & fixed number
int[] a={10,20,30,40};
double [] d={10.2,20.6,2.5};
Arrays are indexed based, index start from zero.

Слайд 37

EX:
class  Test 
{  
    int[] a={10,20,30,40};   
    public static void main(String args[])  
    {  
System.out.println(a[0]);
System.out.println(b[1]);
System.out.println(c[2]);
System.out.println(d[3]);
for(int i=0;i {
System.out.println(a[i]);
}
      }  
}   

EX: class Test { int[] a={10,20,30,40}; public static void main(String args[]) {

Слайд 38

Advantages
Code Optimization: It makes the code optimized, we can retrieve or sort the

Advantages Code Optimization: It makes the code optimized, we can retrieve or
data efficiently.
Random access: We can get any data located at an index position.
Disadvantages
Size Limit: We can store only the fixed size of elements in the array. It doesn't grow its size at runtime.
There are two types of array.
Single Dimensional Array
Multidimensional Array

Слайд 39

String in JAVA

Generally, String is a sequence of characters.
But in Java,

String in JAVA Generally, String is a sequence of characters. But in
string is an object that represents a sequence of characters.
The java.lang.String class is used to create a string object.
There are two ways to create String object:
By string literal (String s="welcome"; )
By new keyword (String s=new String("Welcome");

Слайд 40

Example of “String” Program 10

public class StringExample{    
public static void main(String args[]){    
String s1="java";//creating string by Java string literal    
char ch[]={'s','t','r','i','n','g','s'};    
String s2=new String(ch);//converting char array to string    
String s3=new String("example");//creating Java string by new keyword    
System.out.println(s1);    
System.out.println(s2);    
System.out.println(s3);    
}}    

Example of “String” Program 10 public class StringExample{ public static void main(String

Слайд 41

String Handling

The Java String class length() method finds the length of a string. The

String Handling The Java String class length() method finds the length of
length of the Java string is the same as the Unicode code units of the string.
Syntax
public int length()  
public class LengthExample{  
public static void main(String args[]){  
String s1="javatpoint";  
String s2="python";  
System.out.println("string length is: "+s1.length());//10 is the length of javatpoint string  
System.out.println("string length is: "+s2.length());//6 is the length of python string  
}}  

Example of “String Length” Program 11

Слайд 42

Special String Operations

There are 4 types of special string Operations
String Literals
String Concatenation
String

Special String Operations There are 4 types of special string Operations String
Concatenation with Other Data Types
String Conversion and toString( )
String literal is created by using double quotes.
For Example:
String s="javaguides";
String Concatenation can be done using + operator, which concatenates two strings, producing a String object.
For Example:
String age = "9";
String s = "He is " + age + " years old.";
System.out.println(s);

Слайд 43

You can concatenate strings with other types of data.
For example
int age

You can concatenate strings with other types of data. For example int
= 9;
String s = "He is " + age + " years old.";
System.out.println(s);
String Conversion and toString( ) : valueOf() method
For Example
valueOf( )(The valueOf( ) method converts data from its internal format into a human-readable form.)

public static String valueOf(boolean b)
public static String valueOf(char c)
public static String valueOf(char[] c)
public static String valueOf(int i)
public static String valueOf(long l)

Слайд 44

String Handling Character Extraction

String is treated as an object in Java so

String Handling Character Extraction String is treated as an object in Java
we can’t directly access the characters that comprise a string.
There are several ways by which characters can be extracted from String class object.
charAt() -- method is used to extract a single character at an index.
char charAt(int index)
getChars() -- used to extract more than one character.
void getChars(int stringStart, int stringEnd, char arr[], int arrStart)
getBytes() -- extract characters from String object and then convert the characters in a byte array.
byte [] getBytes()
toCharArray() -- alternative of getChars() method.
char [] toCharArray()

Слайд 45

Example of “Character Extraction” Program 11

class temp
{
public static void main(String...s)
{
String str="Hello";
char ch=str.charAt(2);
System.out.println(ch);
}
}

class temp
{
public

Example of “Character Extraction” Program 11 class temp { public static void
static void main(String...s)
{
String str="Hello World";
char ch[]=new char[4];
str.getChars(1,5,ch,0);
System.out.println(ch);
}
}

class temp
{
public static void main(String...s)
{
String str="Hello World";
char ch[]=str.toCharArray();
System.out.println(ch);
}
}

Слайд 46

String Comparison

We can compare String in Java on the basis of content

String Comparison We can compare String in Java on the basis of
a
It is used in 
authentication (by equals() method), 
sorting (by compareTo() method), 
reference matching (by == operator).
There are three ways to compare String in Java:
By Using equals() Method
By Using == Operator
By compareTo() Method

Слайд 47

Example of “String Comparision” Program 12

class Teststringcomparison1{  
 public static void main(String args[]){  
   String s1="Sachin";  
   String s2="Sachin";  
   String s3=new String("Sachin");  
   String s4="Saurav";  
   System.out.println(s1.equals(s2));//true  
   System.out.println(s1.equals(s3));//true  
   System.out.println(s1.equals(s4));//false  
 }  
}  

class Teststringcomparison3{  
 public static void main(String args[]){  
   String s1="Sachin";  
   String s2="Sachin";  
   String s3=new String("Sachin");  
   System.out.println(s1==s2);//true (because both refer to same instance)  
   System.out.println(s1==s3);//false(because s3 refers to instance created in nonpool)  
 }  
}  

class Teststringcomparison4{  
 public static void main(String args[]){  
   String s1="Sachin";  
   String s2="Sachin";  
   String s3="Ratan";  
   System.out.println(s1.compareTo(s2));//0  
   System.out.println(s1.compareTo(s3));//1(because s1>s3)  
   System.out.println(s3.compareTo(s1));//-1(because s3 < s1 )  
 }  
}  

Example of “String Comparision” Program 12 class Teststringcomparison1{ public static void main(String

Слайд 48

Modifying a String

 Methods for modifying a String objects.
substring()
susubstring(int beginIndex)
bstring(int beginIndex, int endIndex) 
concat()

Modifying a String Methods for modifying a String objects. substring() susubstring(int beginIndex)
-- We can concatenate two strings using concat( )
replace()
String replace(char original, char replacement)
String replace(CharSequence original, CharSequence replacement)
replaceAll() – Replaces each substring of this string that matches the given regular expression with the given replacement.
replaceAll(String regex, String replacement)
replaceFirst() -- Replaces the first substring of this string that matches the given regular expression with the given replacement.
replaceFirst(String regex, String replacement)
trim() -- The trim( ) method returns a copy of the invoking string from which any leading and trailing whitespace has been removed.
String trim( )

Слайд 49

StringBuffer

A string buffer is like a String, but can be modified
append()
insert()
replace()
delete()
reverse()
capacity()

StringBuffer A string buffer is like a String, but can be modified

Слайд 50

Example of “String Buffer” Program 13

StringBuffer Class append() Method
class StringBufferExample1{  
public static void main(String args[]){  
StringBuffer sb=new StringBuffer("Hello ");  
sb.append("Java");   
System.out.println(sb);   
}  }  

Example of “String Buffer” Program 13 StringBuffer Class append() Method class StringBufferExample1{

Слайд 51

StringBuffer insert() Method

class StringBufferExample2{  
public static void main(String args[]){  
StringBuffer sb=new StringBuffer("Hello ");  
sb.insert(1,"Java");//now original string is changed  
System.out.println(sb);//prints HJavaello  
}  
}  

StringBuffer insert() Method class StringBufferExample2{ public static void main(String args[]){ StringBuffer sb=new

Слайд 52

StringBuffer replace() Method

class StringBufferExample3{  
public static void main(String args[]){  
StringBuffer sb=new StringBuffer("Hello");  
sb.replace(1,3,"Java");  
System.out.println(sb);//prints HJavalo  
}  
}  

StringBuffer replace() Method class StringBufferExample3{ public static void main(String args[]){ StringBuffer sb=new

Слайд 53

StringBuffer delete() Method

class StringBufferExample4{  
public static void main(String args[]){  
StringBuffer sb=new StringBuffer("Hello");  
sb.delete(1,3);  
System.out.println(sb);//prints Hlo  
}  
}  

StringBuffer delete() Method class StringBufferExample4{ public static void main(String args[]){ StringBuffer sb=new

Слайд 54

StringBuffer capacity() Method

class StringBufferExample5{  
public static void main(String args[]){  
StringBuffer sb=new StringBuffer();  
System.out.println(sb.capacity());//default 16  
sb.append("Hello");  
System.out.println(sb.capacity());//now 16  
sb.append("java is my favourite language");  
System.out.println(sb.capacity());//now (16*2)+2=34 i.e (oldcapacity*2)+2  
}  
}  

StringBuffer capacity() Method class StringBufferExample5{ public static void main(String args[]){ StringBuffer sb=new

Слайд 55

StringBuffer reverse() Method

class StringBufferExample6{  
public static void main(String args[]){  
StringBuffer sb=new StringBuffer("Hello");  
sb.reverse();  
System.out.println(sb);//prints olleH  
}  
}  

StringBuffer reverse() Method class StringBufferExample6{ public static void main(String args[]){ StringBuffer sb=new
Имя файла: OOP’s-Using-JAVA-Module-2.pptx
Количество просмотров: 32
Количество скачиваний: 0