Object Oriented Programming
By
S.V.V.D.Jagadeesh
Sr. Assistant Professor
Dept of Artificial Intelligence & Data Science
LAKIREDDY BALI REDDY COLLEGE OF ENGINEERING
S.V.V.D.Jagadeesh
Friday, September 1, 2023
Previous Discussion
LBRCE
OOP
At the end of this session, Student will be able to:
S.V.V.D.Jagadeesh
Friday, September 1, 2023
Session Outcomes
LBRCE
OOP
S.V.V.D.Jagadeesh
Friday, September 1, 2023
If Statement
LBRCE
OOP
S.V.V.D.Jagadeesh
Friday, September 1, 2023
If Statement
LBRCE
OOP
S.V.V.D.Jagadeesh
Friday, September 1, 2023
If Statement
import java.util.Scanner;
class larger1
{
public static void main(String args[])
{
int x1,x2,x3;
int large;
Scanner s = new Scanner(System.in);
System.out.println("Enter value for x1 : ");
x1=s.nextInt();
System.out.println("Enter value for x2 : ");
x2=s.nextInt();
System.out.println("Enter value for x3 : ");
x3=s.nextInt();
large = x1;
if (x2 > large)
large = x2;
if (x3 > large)
large = x3;
System.out.println("\n\n\tLargest number = " + large);
}
}
LBRCE
OOP
S.V.V.D.Jagadeesh
Friday, September 1, 2023
if-else statement
Syntax:
if (condition)
{
statement1;
}
else
{
statement2;
}
Write a java program to check whether the given mark is pass or fail using if-else statement?
import java.util.Scanner;
class result
{
public static void main(String args[])
{
Scanner s = new Scanner(System.in);
System.out.println("Enter marks : ");
int marks = s.nextInt();
if (marks<40)
System.out.println("\nThe student has failed .. ");
else
System.out.println("\nThe student has Passed .. ");
}
}
LBRCE
OOP
S.V.V.D.Jagadeesh
Friday, September 1, 2023
if-else-if ladder
Syntax :
if(condition)
statements;
else if(condition)
statemenst;
else if(condition)
statements;
...
...
else
statements;
Write a java program to accept the day between 0 to 6 based on that it has to print day between Sunday to Saturday using if-else-if Ladder.
LBRCE
OOP
S.V.V.D.Jagadeesh
Friday, September 1, 2023
if-else-if ladder
else if (day == 3)
{
System.out.println("\n Wednesday");
}
else if (day == 4)
{
System.out.println("\n Thursday");
}
else if (day == 5)
{
System.out.println("\n Friday");
}
else
{
System.out.println("\n Saturday");
}
}
}
import java.util.Scanner;
class Day
{
public static void main(String args[])
{
Scanner s = new Scanner(System.in);
System.out.println("Enet day between 0 to 6 Day = ");
int day = s.nextInt();
if (day == 0)
{
System.out.println("\n Sunday");
}
else if (day == 1)
{
System.out.println("\n Monday");
}
else if (day == 2)
{
System.out.println("\n Tuesday");
}
LBRCE
OOP
S.V.V.D.Jagadeesh
Friday, September 1, 2023
Nested if
Syntax :
if(condition)
{
if(condition)
statements....
else
statements....
}
else
{
if(condition)
statements....
else
statements....
}
Write a java program to accept three values a, b, c from the user and find out the maxvalue using Nested-if.
LBRCE
OOP
S.V.V.D.Jagadeesh
Friday, September 1, 2023
Nested if
import java.util.Scanner;
class MaxValue
{
public static void main(String args[])
{
int a,b,c;
int max=0;
Scanner s = new Scanner(System.in);
System.out.println("Enter value for a : ");
a=s.nextInt();
System.out.println("Enter value for b : ");
b=s.nextInt();
System.out.println("Enter value for c : ");
c=s.nextInt();
if (a>b)
{
if(a>c)
max=a;
else //This else is associate with this if(a>c)
max=c;
}else //This else is associate with this if(a>b)
{
if(b>c)
max=b;
else //This else is associate with this if(b>c)
max=c;
}
System.out.println("\n max value = " +max);
}
}
LBRCE
OOP
S.V.V.D.Jagadeesh
Friday, September 1, 2023
Switch Statement
switch (expression)
{
case value 1 :
statement 1 ; break;
case value 2 :
statement 2 ; break;
...
...
case value N :
statement N ; break;
default :
statements ; break;
}
LBRCE
OOP
S.V.V.D.Jagadeesh
Friday, September 1, 2023
Switch Statement
import java.util.Scanner;
public class Calci
{
public static void main(String[] args)
{
int a,b,ch;
double ans;
Scanner s = new Scanner(System.in);
System.out.print("Enter a : ");
a=s.nextInt();
System.out.print("Enter b : ");
b=s.nextInt();
System.out.println("Enter 1 for addition");
System.out.println("Enter 2 for subtraction");
System.out.println("Enter 3 for multiplication");
System.out.println("Enter 4 for division");
System.out.print("Enter your choice : ");
ch=s.nextInt();
switch(ch)
{
case 1:
ans=a+b;
System.out.println("a + b = " + ans);
break;
case 2:
ans=a-b;
System.out.println("a - b = " + ans);
break;
case 3:
ans=a*b;
System.out.println("a * b = " + ans);
break;
case 4:
ans=a/b;
System.out.println("a / b = " + ans);
break;
default:
System.out.println("Enter correct choice");
}
}
}
LBRCE
OOP
S.V.V.D.Jagadeesh
Friday, September 1, 2023
Looping statements-while
while (condition)
{
body(statements) of the loop
}
Write a java program to print the natural number from 1 to 10 using while loop?
class while1
{
public static void main(String args[])
{
int i=1;
while(i<=10)
{
System.out.println("\n" + i);
i++;
}
}
}
LBRCE
OOP
S.V.V.D.Jagadeesh
Friday, September 1, 2023
Do-While
do
{
body of the loop
} while (condition);
Write a java program to print the natural number from 10 to 1 using do while loop?
class dowhile1
{
public static void main(String args[])
{
int i=10;
do
{
System.out.println("\n" + i);
i--;
}
while(i>=1);
}
}
LBRCE
OOP
S.V.V.D.Jagadeesh
Friday, September 1, 2023
for loop
for (initialization; condition; expression)
{
Set of statements;
}
Write a java program to print the numbers from 1 to 9 using for loop?
class forLoop1
{
public static void main(String args[])
{
int i;
for (i=0;i<10;i++)
{
System.out.println("\n"+i);
}
}
}
LBRCE
OOP
S.V.V.D.Jagadeesh
Friday, September 1, 2023
for-each
for (datatype variable:array|collection)
{
Set of statements;
}
Write a java program to print the numbers from 1 to 9 using for-each loop?
class forLoop1
{
public static void main(String args[])
{
int a[]={1,2,3,4,5};
for (int i:a)
{
System.out.println(i);
}
}
}
LBRCE
OOP
S.V.V.D.Jagadeesh
Friday, September 1, 2023
Break Statement
LBRCE
OOP
S.V.V.D.Jagadeesh
Friday, September 1, 2023
Break Statement
class break1
{
public static void main(String args[])
{
int i = 1;
while (i<=10)
{
System.out.println("\n" + i);
i++;
if (i==5)
{
break;
}
}
}
}
LBRCE
OOP
S.V.V.D.Jagadeesh
Friday, September 1, 2023
Continue Statement
LBRCE
OOP
S.V.V.D.Jagadeesh
Friday, September 1, 2023
Continue Statement
class continue1
{
public static void main(String args[])
{
for (int i=1; i<=10; i++)
{
if (i%2 == 0)
continue;
System.out.println("\n" + i);
}
}
}
LBRCE
OOP
S.V.V.D.Jagadeesh
Friday, September 1, 2023
Summary
LBRCE
OOP