W M1
W M1
W M1
1. LOGICAL PROGRAMS: -
A. OPERATORS
B. DATATYPES
C. LOOPS
D. VARIABLES
E. DMS(DECISIOPN MAKING STATEMENTS)
F. METHODS
G. TYPE CASTING
--------------------------------------------------------------------------
Variables: -
-> container to store values or data
-> named block memory
types of variables
1. local variable:-
->
2. global variable:-
-> the variables which are declared inside the class block
-> types of global variables:-
1. static variable
2. non static variable
a. static variable:- the variable which are declared inside the class block
and prefixed with static keyword
Ex: -
class A
{
static double d=34.5;
}
---------------------------------------------------------------------------------
b. non static variable:- the variables which are declared inside the class block
and not prefixed with static keyword
Ex: -
class A
{
int a=10;
}
----------------------------------------------------------------------------------
static -> keyword or modifier [constant]
------------------------------------------------------------------
10 student details
-> name
-> id
-> contact number
-> class
-> email-d
-> school name
Note: - for school name use static keyword while declaraing
----------------------------------------------------------------------
Student: -
physical characteristics-> name,id,gender,age
behviour-> read,write,sleep,eat,play,walk
----------------------------------------------------------------------
Laptop: -
physical characteristics-> battery,brand,price,weight,storage,color
behaviour:- gaming,coding,editing
-----------------------------------------------------------------------
physical properties of an object:- we represent in java in terms of variables
behaviours of an object:- we represent in java in terms of methods
class Student
{
String name;
int id;
long cno;
a. static variable:- the variable which are declared inside the class block
and prefixed with static keyword.
class A
{
static byte b=20;//static variable
}
----------------------------------------------------------------------------
b. non static variable: - the variable which are declared inside the class
block and not prefixed with static keyword
class B
{
long l=10;//non static variable
}
----------------------------------------------------------------------------
Object:- any real time entity which can be seen ,touched
physical properties -> variables
behaviour -> methods
Car:-
-> color, price, doors,wheels,brand
-> drive,park,stop
class Car
{
String color;
double price;
int noOfDoors;
Arithmetic operators:-
+ , - , * ,% ,/
% -> to find remainder
double c=10/7;
S.o.pln(c);//1.0
a) 1
b) 1.0
c) 1.42
d) 3
10+20+"abc"+10+20 ->30abc1020
'a'+10 -> 97
"a"+'a'+10 -> "aa10"
"10"+20+"abc"+10+20 -> 1020abc1020
----------------------------------------------------------------------------------
Modulus operator: -(%)
m%n
10%23 -> 10
12%35 -> 12
34%234 -> 34
---------------------------------------------------------------------------------
1. WAJP to print the last digit of a user entered number
testcases: -
i/p -> 12345
o/p -> 5
i.p -> 987653432
o/p -> 2
----------------------------------------------------------------------------------
User input :-
Step 1:-
import Scanner class from java.util package
"import java.util.Scanner;"
before the class declaration
Step 2:-
create an object for Scanner class
Step 3:-
With the help of variable call below methods
String
single word next();
multi words nextLine();
-----------------------------------------------------------------
Loops: -
-> to perform some task repeatedly or multiple times
types of loops:-
1. for loop
2. while loop
3. do-while loop
4. for each loop or advance for loop
initialization
condition
updation(inc and dec)
1. for loop
for(initilaization;condition;updation)
{
}
------------------------------------------------------------------
2. while loop
initialization
while(condition)
{
updation
}
-----------------------------------------------------------------
3. do-while loop
initialization
do
{
updation
}
while(condition);
------------------------------------------------------------------
while loop and do-while loop
while loop:-
-> entry contolled loop
-> minimum iteration of while loop is zero
do-while loop:-
-> exit controlled loop
-> minimum iteration of do-while loop is one.
-----------------------------------------------------------------
int i=1;
while(i<1)
{
S.o.pln(i);
i++;
}-> output:- no output and minimum iteration count is zero
-----------------------------------------------------------
int i=1;
do
{
S.o.pln(i);
i++;
}
while(i<1);
-> output:- 1 and minimum iteration count is 1
int a=12367;
if(a%2==0)
{
System.out.println("even number");
}
else
{
System.out.println("odd number");
}
------------------------------OR--------------------------------
int a=1236;
switch(a%2)
{
case 0:
{
System.out.println("even number");
}break;
case 1:
{
System.out.println("odd number");
}
}
----------------------------------------------------------------
2. WAJP to check the number is odd or even without using the modulus operator
int a=1;
int b=a/2;
int c=b*2;
if(a==c)
{
System.out.println("even number");
}
else
{
System.out.println("odd number");
}
-------------------------------------------------------------------------------
3. WAJP to print the even numbers between 0-50.
for(int i=0;i<=50;i++)
{
if(i%2==0)
{
System.out.println(i);
}
}
--------------------------------------------------------------------------------
4. WAJP to print the sum of natural numbers till 20.
1-20
int sum=0;
for(int i=1;i<=20;i++)
{
sum=sum+i;
}
System.out.println(sum);
----------------------------------------------------------------------------------
5. WAJP to print the factors of given number.
i/p:- 6
o/p:- 1,2,3,6
i/p:- 15
o/p:- 1,3,5,15
int n=100;
for(int i=1;i<=n;i++)
{
if(n%i==0)
{
System.out.println(i);
}
}
-------------------------------------------------------------------------------
6. WAJP to count the numbers which are divisible by 3 and 7 from 0-100
int count=0;
for(int i=0;i<=100;i++)
{
if(i%3==0 && i%7==0)
{
count++;
}
}
System.out.println(count);
-----------------------------------------------------------------------------------
WAJP to print the factors of the numbers from 1-10
for loop:- 2
for(int i=1;i<=10;i++)
for(int j=1;j<=i;j++)
dms-> if(i%j==0)
for(int i=1;i<=10;i++)
{
S.o.p(i+":-");
for(int j=1;j<=i;j++)
{
S.o.p(j);
}
S.o.pln();
}
------------------------------------------------------------------------
WAJP to print multiplication table of 5.
class Factor
{
public static void main(String[] args)
{
for(int i=1;i<=10;i++)
{
int mul=5*i;
System.out.println("5 * "+i+" = "+mul);
}
}
}
-----------------------------------------------------------------------
for(int i=1;i<=5;i++)
{
for(int j=1;j<=3;j++)
{
S.o.p(j);
}
S.o.pln();
}
----------------------------------------------------------------------------
for(int i=0;i<=5;i++)
{
for(int j=0;j<=5;j++)
{
}
}
i-> 0,1,2,3,4,5
i=2-> j-> 0,1,2,3,4,5
------------------------------------------------------------
WAJP to swap 2 variables.
class Swapping
{
public static void main(String[] args)
{
int a=10;
int b=20;
System.out.println("before swapping");
System.out.println("a:- "+a);//10
System.out.println("b:- "+b);//20
System.out.println("after swapping");
int c=a;
a=b;
b=c;
System.out.println("a:- "+a);//20
System.out.println("b:- "+b);//10
}
}
----------------------------------------------------------------------
WAJP to swap the variables without using third variable.
class Swapping1
{
public static void main(String[] args)
{
int a=10;
int b=20;
System.out.println("before swapping");
System.out.println("a:- "+a);//10
System.out.println("b:- "+b);//20
System.out.println("after swapping");
a=a+b;
b=a-b;
a=a-b;
System.out.println("a:- "+a);//20
System.out.println("b:- "+b);//10
}
}
-----------------------------------------------------------------------------
WAJP to print the factorial of a given number.
class Factorial
{
public static void main(String[] args)
{
int n=5;
int fact=1;
for(int i=1;i<=n;i++)
{
fact=fact*i;
}
System.out.println(fact);
}
}
-----------------------------------------------------------------------------------
WAJP to count the number of digits in given number.
loops,/
1 st iteration :-
n=123 ->> n=n/10; n=>12
2 nd iteration :-
n=12 ->> n=n/10; n=1
3 rd iteration :-
n=1 ->> n=n/10; n=0
int n=12345;
int count=0;
while(n>0)//n!=0
{
count++;
n=n/10;
}
System.out.println("no of digits:- "+count);
___________________________________OR____________________________
int n=12345;
int c=0;
for(int i=n;i!=0;i=i/10)
{
c++;
}
System.out.println("no of digits:- "+c);
----------------------------------------------------------------------------------
int n=4798;
while(n>0)//n!=0
{
int a=n%10;
System.out.println(a);
n=n/10;
}
o/p:-
8
9
7
4
-----------------------------------------------------------------------------------
--------
WAJP to print the even digits of a given number.
n=234789
even digits:- 2 4 8
n=157294
even digits:- 2 4
class Program5
{
public static void main(String[] args)
{
int n=12345684;
while(n>0)
{
int a=n%10;
if(a%2==0)
{
System.out.println(a);
}
n=n/10;
}
}
}
---------------------------------------------------------------
WAJP to count the odd digits in a given number.
n-> 12345
count-> 3
n-> 356791
count-> 5
class Program5
{
public static void main(String[] args)
{
int n=12345684;
int count=0;
while(n>0)
{
int a=n%10;
if(a%2!=0)
{
count++;
}
n=n/10;
}
System.out.println(count);
}
}
---------------------------------------------------------------------
WAJP to print the digits of a given number which are divisible by 3
n-> 123697
o/p:- 3 6 9
n-> 2469312
o/p:- 6 9 3
class Program5
{
public static void main(String[] args)
{
int n=12345684;
while(n>0)
{
int a=n%10;
if(a%3==0)
{
System.out.println(a);
}
n=n/10;
}
}
}
------------------------------------------------------------------------
features of java:-
1. high level language:-
-> it is more readable and writable
-> it is simialar to english
-> easy to learn
--------------------------------------------------------------
2. robust:-
-> strong memory management
---------------------------------------------------------
3. secure:-
-> beacuse of byte code
--------------------------------------------------------
4. it supports packages:-
-> we can create user defined package
----------------------------------------------------------
5. platform independent:-
-> because we can execute bytecode file in any other platform
-> java is platform independent but JRE dependent
----------------------------------------------------------------
6. rich in library:-
-> because inbuilt classes are more
-------------------------------------------------------------
7. object oriented:-
-> java supports all features or priniciples or pillars of OOPs
1. Encapsulation
2. Inheritance
3. Polymorphism
4. Abstarction
------------------------------------------------------------------------
Whether java is 100% object oriented?
-> No,java is not 100% object oriented because we use 8 primitive datatypes
------------------------------------------------------------------------------
Architecture of JDK:-
->
JDK :-
-> java development kit
-> it is a software which provides components for compiling and executing a java
program
-----------------------------------------------------------------------------------
----------
javac:-
-> java compiler
-> it converts from java file into byte code file(HLL -> MLL)
-----------------------------------------------------------------------------------
----------
development tools:-
-> it has tools like keywords which are required for developing java program
-----------------------------------------------------------------------------------
---------
JRE:-
-> java run time environment
-> it is a software which provides components for executing a java program
-----------------------------------------------------------------------------------
---------
JVM:-
-> java virtual machine
-> it is a software which is not physically present but behaves or works like a
physical machine.
----------------------------------------------------------------------------
Interpreter:-
-> it converts byte code into binary language or low level language
-----------------------------------------------------------------------------------
-------------
JIT compiler:-
-> just in time compiler
-> it helps interpreter for converting from byte code to low level language.
i/p:- 123
p/p:- 321
i/p:- 7248
o/p:- 8427
class Reverse
{
public static void main(String[] args)
{
int n=1253;
int c=0;
while(n>0)
{
int a=n%10;
c=c*10+a;
n=n/10;
}
System.out.println(c);
}
}
-----------------------------------------------------------------------------------
----------
WAJP to check whether number is palindrome number.
class Reverse
{
public static void main(String[] args)
{
int n=121;
int temp=n;
int c=0;
while(n>0)
{
int a=n%10;
c=c*10+a;
n=n/10;
}
if(c==temp)
{
System.out.println("palindrome");
}
else
{
System.out.println("not a palindrome");
}
}
}
-----------------------------------------------------------------------------------
--------------
components of java or elements of java:-
-> class
-> method
-> variables
-> interface
-> packages
----------------------------------------------------------------
members of the class:-
-> variables
-> methods
-> initializer/blocks
variables:-
-> static variables
-> non static variables
-> local variables
methods:-
-> static methods
-> non static methods
initializers/blocks:-
-> static initializer
-- single line static initializer
-- multi line static initializer
-> non static initializer
-- single line non-static initializer
-- multi line non-static initializer
---------------------------------------------------------------
structure of a java program:-
//package declaration
//import section
class declaration
{
//methods
------------------------------------------------
int a=20;
a=a-- - --a;
S.o.pln(a);
a=20 - 18 -> 2
int a; ->> declaring the variable
a=20; ->> initialising the variable
a=30; ->> reinitialising the variable
-------------------------------------------------
int a=20; --> declaring and initialising a variable
---------------------------------------------------------------------
WAJP to print the fibonacci series.
0 and 1
0+1-> 1
0 1 1 2 3 5 8 13 21 34 55 89 ....................
class Fibonacci
{
public static void main(String[] args)
{
int a=0;
int b=1;
System.out.print(a+" ");//0
System.out.print(b+" ");//1
int count=0;
while(count<=10)
{
int c=a+b;
System.out.print(c+" ");
a=b;
b=c;
count++;
}
}
}
--------------------------------------------------------------
WAJP to find the power without using inbuilt function.
class Power
{
public static void main(String[] args)
{
int base=2;
int exp=6;
int pow=1;
for(int i=1;i<=exp;i++)
{
pow=pow*base;
}
System.out.println(pow);
}
}
==========================================================================
WAJP to find the power with using inbuilt function.
class Power1
{
public static void main(String[] args)
{
int base=5;
int exp=3;
int power=(int)Math.pow(base,exp);
System.out.println(power);
}
}
==========================================================================
WAJP to check whether the given number is Prime number or not.
class PrimeNumber
{
public static void main(String[] args)
{
int n=3;
int flag=0;
if(n==0 || n==1)
{
flag=1;
}
else
{
for(int i=2;i<=n/2;i++)
{
if(n%i==0)
{
flag++;
}
}
}
if(flag==0)
{
System.out.println("prime number");
}
else
{
System.out.println("not a prime number");
}
}
}
-----------------------------------------------------------------------------------
-----
WAJP to find the perfect number.
class PerfectNumber
{
public static void main(String[] args)
{
int n=20;
int sum=0;
for(int i=1;i<n;i++)
{
if(n%i==0)
{
sum=sum+i;
}
}
if(sum==n)
{
System.out.println("perfect number");
}
else
{
System.out.println("not a perfect number");
}
}
}
-----------------------------------------------------------------------------------
--
WAJP to print the perfect numbers between 1 and 100.
class Perfect1
{
public static void main(String[] args)
{
for(int i=1;i<=10;i++)
{
check(i);
}
}
public static void check(int n)
{
int sum=0;
for(int i=1;i<n;i++)
{
if(n%i==0)
{
sum=sum+i;
}
}
if(sum==n)
{
System.out.println(n+" is a perfect number");
}
}
}
-----------------------------------------------------------------------------------
-------
WAJP to check whether the year is leap year or not.
===================================================================================
1. WAJP to find prime number.
2. WAJP to find the factors of a given number.
3. WAJP to find the factorial of a given number.
4. WAJP to the perfect number.
5. WAJP to reverse the given number.
6. WAJP to check the palindrome number.
7. WAJP to check the Strong number.
8. WAJP to check the Amstrong number.
9. WAJP to check number is Xylem or Phloem.
10. WAJP to find the GCD of given numbers.
11. WAJP to find the LCM of given numbers.
12. WAJP to find fibonacci series.
13. WAJP to print the even digits in a given number.
14. WAJP to find the sum of first and last digit in a given number.
15. WAJP to swap the variables.
16. WAJP to swap the variables without using the third variable.
17. WAJP to generate random number.(OTP logic)
18. WAJP to find the largest of 2 numbers.
19. WAJP to find the largest of 3 numbers.
20. WAJP to find the smallest of 2 numbers.
21. WAJP to find the smallest of 3 numbers.
22. WAJP to print the first 10 factors of given numbers.
23. WAJP to check whether the number is HAPPY number or not.
24. WAJP to check leap year.
25. WAJP to convert binary into decimal.
26. WAJP to convert decimal into binary.
--------------------------------------------------------------------
if(true)
{
S.o.pln();
}
------------------------------------
if(true)
S.o.pln();
-------------------------------------------------
for(;;)
{
}
-----------------------------------------------------------
WAJP to design a method which accepts 2 integers and returns average of 2 numbers
parameterised
return type :- int/double
class Average
{
public static double avg(int a,int b)
{
int sum=a+b;
double c=sum/2;
return c;
}
}
-----------------------------------------------------------------------------------
-
WAJP to check whether the number is Strong number or not
2-> 2!-> 2
1-> 1!-> 1
class StrongNumber
{
public static void main(String[] args)
{
int n=3;
int temp=n;
int sum=0;
while(n>0)
{
int a=n%10;
sum=sum+factorial(a);
n=n/10;
}
if(sum==temp)
{
System.out.println(temp+" is a strong number");
}
else
{
System.out.println(temp+" is not a strong number");
}
}
public static int factorial(int a)
{
int fact=1;
for(int i=1;i<=a;i++)
{
fact=fact*i;
}
return fact;
}
}
-----------------------------------------------------------------------------------
-----------
WAJP to print the Strong numbers between 1 till 1000;
class StrongNumber
{
public static void main(String[] args)
{
for(int i=1;i<=10000;i++)
{
checkStrong(i);
}
}
public static void checkStrong(int n)
{
int temp=n;
int sum=0;
while(n>0)
{
int a=n%10;
sum=sum+factorial(a);
n=n/10;
}
if(sum==temp)
{
System.out.println(temp+" is a strong number");
}
}
public static int factorial(int a)
{
int fact=1;
for(int i=1;i<=a;i++)
{
fact=fact*i;
}
return fact;
}
}
-----------------------------------------------------------------------------------
-------
WAJP to print the factorial of numbers between 1 to 10.
1 factorial is -> 1
2 factorial is -> 2
3 factorial is -> 6
4 factorial is -> 24
-----------------------------------------------------------------------------------
--
WAJP to check whether the number is Amstrong or not.
int n=153;
int temp=n;
int m=n;
int count=0;
while(n>0)
{
count++;
n=n/10;
}
int sum=0;
while(temp>0)
{
int a=temp%10;
sum=sum+(int)Math.pow(a,count);
temp=temp/10;
}
if(sum==m)
{
System.out.println("amstrong number");
}
else
{
System.out.println("not a amstrong number");
}
-----------------------------------------------------------------------------------
---
WAJP to print the Amstrong number from 1 till 1000.
class Amstrong
{
public static void main(String[] args)
{
for(int i=1;i<=10000;i++)
{
checkAmstrong(i);
}
}
public static void checkAmstrong(int n)
{
int temp=n;
int m=n;
int count=0;
while(n>0)
{
count++;
n=n/10;
}
int sum=0;
while(temp>0)
{
int a=temp%10;
sum=sum+(int)Math.pow(a,count);
temp=temp/10;
}
if(sum==m)
{
System.out.println(m+" is amstrong number");
}
}
}
--------------------------------------------------------------------------------
WAJP to xylem and phloem
n-> 1234
sum of extreme digits(first and last digits) -> 1 and 4 -> 1+4 = 5
sum of middle digits-> 2 and 3 -> 2+3 = 5
-----------------------------------------------------------------------------------
-------
class Xylem
{
public static void main(String[] args)
{
int n=1237;
int temp=n;
int extremeSum=0;
int middleSum=0;
while(n>0)
{
if(n==temp || n<10)
{
extremeSum=extremeSum+n%10;
}
else
{
middleSum=middleSum+n%10;
}
n=n/10;
}
if(extremeSum==middleSum)
{
System.out.println("xylem number");
}
else
{
System.out.println("phloem number");
}
}
}
-----------------------------------------------------------------------------------
-------------
WAJP to print the GCD of 2 numbers
class GCD
{
public static void main(String[] args)
{
int m=10;
int n=4;
int gcd=0;
for(int i=1;i<=m && i<=n;i++)
{
if(m%i==0 && n%i==0)
{
gcd=i;
}
}
System.out.println(gcd+" is the gcd of "+m+" and "+n);
}
}
-----------------------------------------------------------------------------------
-------------
WAJP to find the LCM of 2 numbers.
1.
public static int square(int n)
{
int a=n*n;
return a;
}
OR
public static int square(int n)
{
return n*n;
}
OR
public static int square(int n)
{
return Math.pow(n,2);
}
-----------------------------------------------------------------------------------
-------
2.
public static boolean check(int n)
{
if(n%3==0)
{
return true;
}
else
{
return false;
}
}
OR
public static boolean check(int n)
{
boolean b=false;
if(n%3==0)
{
b=true;
}
return b;
}
OR
public static boolean check(int n)
{
return n%3==0;
}
------------------------------------------------------------------------------
class A
{
static int a=10;
public static void m1()
{
}
}
class B
{
static int b=20;
}
-----------------------------------------------------------------------------------
----
WAJP to print random number
int min=100000;
int max=999999;
int a=(int)(Math.random()*(max-min+1)+min);
System.out.println(a);
-----------------------------------------------------------------------------------
import java.util.Scanner;
class Instagram
{
public static void login()
{
System.out.println("welcome to login page");
}
public static void signUp()
{
Scanner m=new Scanner(System.in);
System.out.println("enter the name");
String name=m.nextLine();
System.out.println("enter the email-d");
String email=m.next();
System.out.println("enter the mobile number");
long number=m.nextLong();
System.out.println("enter the password");
String password=m.next();
System.out.println("re-enter the password");
String password1=m.next();
if(password.equals(password1))
{
System.out.println("Successfully account created");
login();
}
else
{
System.out.println("password invalid try after some time");
}
}
public static void main(String [] args)
{
signUp();
}
}
----------------------------------------------------------------------------------
200 -> 1 ,2 ,4, 5,8,10,20,25,40,50,100,200
for(int i=1;i<=5;i++)
{
for(int j=1;j<=3;j++)
{
System.out.print(j+" ");
}
System.out.println();
}
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
for(int i=1;i<=rows;i++)
{
for(int j=1;j<=columns;j++)
{
}
System.out.println();
}
-----------------------------------------------------------------------------------
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5
class A3
{
public static void main(String[] args)
{
for(int i=1;i<=5;i++)
{
for(int j=1;j<=5;j++)
{
System.out.print(i+" ");
}
System.out.println();
}
}
}
---------------------------------------------------------------------------------
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
class A3
{
public static void main(String[] args)
{
for(int i=1;i<=5;i++)
{
for(int j=1;j<=5;j++)
{
System.out.print(j+" ");
}
System.out.println();
}
}
}
---------------------------------------------------------------------------
A A A A A
B B B B B
C C C C C
D D D D D
E E E E E
for(char c='A';c<='E';c++)
{
for(char d='A';d<='E';d++)
{
System.out.println(c)
}
}
-----------------------------OR-----------------------------------
for(int c='A';c<='E';c++)
{
for(int d='A';d<='E';d++)
{
System.out.print((char)c+" ");
}
System.out.println();
}
----------------------------OR-----------------------------------------
for(int i=1;i<=5;i++)
{
for(int j=1;j<=5;j++)
{
System.out.print(((char)(i+64))+" ");
}
System.out.println();
}
--------------------------------------------------------------------------
char ch='A';
for(int i=1;i<=5;i++)
{
for(int j=1;j<=5;j++)
{
System.out.print(ch+" ");
}
System.out.println();
ch++;
}
-------------------------------------------------------------------------------
A B C D E
A B C D E
A B C D E
A B C D E
A B C D E
for(int i=1;i<=5;i++)
{
for(int j=1;j<=5;j++)
{
System.out.print((char)(j+64));
}
System.out.println();
}
-------------------------OR---------------------------
for(int i=1;i<=5;i++)
{
char ch='A';
for(int j=1;j<=5;j++)
{
System.out.print(ch++);
}
System.out.println();
}
--------------------------------------------------------------------------------
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9
for(int i=1;i<=5;i++)
{
int n=i;
for(int j=1;j<=5;j++)
{
System.out.print(n++ +" ");
}
System.out.println();
}
----------------------------------------------------------------
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
int n=1;
for(int i=1;i<=5;i++)
{
for(int j=1;j<=5;j++)
{
S.o.p(n++ +" ");
}
S.o.pln();
}
-----------------------------------------------------------------------------------
----
A B C D E
F G H I J
K L M N O
P Q R S T
U V W X Y
char n='A';
for(int i=1;i<=5;i++)
{
for(int j=1;j<=5;j++)
{
S.o.p(n++ +" ");
}
S.o.pln();
}
--------------------------------------------------------------------------------
5 5 5 5 5
4 4 4 4 4
3 3 3 3 3
2 2 2 2 2
1 1 1 1 1
int n=5;
for(int i=1;i<=5;i++)
{
for(int j=1;j<=5;j++)
{
S.o.p(n + " ");
}
n--;
S.o.pln();
}
-----------------------------------------------------------------------------
5 4 3 2 1
5 4 3 2 1
5 4 3 2 1
5 4 3 2 1
5 4 3 2 1
for(int i=1;i<=5;i++)
{
int n=5;
for(int j=1;j<=5;j++)
{
System.out.print(n-- +" ");
}
System.out.println();
}
--------------------------------------------------------------------------------
1 1 1 1 1
0 0 0 0 0
1 1 1 1 1
0 0 0 0 0
1 1 1 1 1
for(int i=1;i<=5;i++)
{
for(int j=1;j<=5;j++)
{
System.out.print(i%2+" ");
}
System.out.println();
}
-----------------------------------------------------------------------------------
-
1 0 1 0 1
1 0 1 0 1
1 0 1 0 1
1 0 1 0 1
1 0 1 0 1
for(int i=1;i<=5;i++)
{
for(int j=1;j<=5;j++)
{
System.out.print(j%2+" ");
}
System.out.println();
}
------------------------------------------------------------------------------
1 * 1 * 1
0 * 0 * 0
1 * 1 * 1
0 * 0 * 0
1 * 1 * 1
for(int i=1;i<=5;i++)
{
for(int j=1;j<=5;j++)
{
if(i%2==1)
{
if(j%2==1)
{
System.out.print("1 ");
}
else
{
System.out.print("* ");
}
}
else
{
if(j%2==1)
{
System.out.print("0 ");
}
else
{
System.out.print("* ");
}
}
}
System.out.println();
}
-----------------------------------------------------------------------------------
-
1 0 0 0 0
0 2 0 0 0
0 0 3 0 0
0 0 0 4 0
0 0 0 0 5
for(int i=1;i<=5;i++)
{
for(int j=1;j<=5;j++)
{
if(i==j)
{
System.out.print(i+" ");
}
else
{
System.out.print(0+" ");
}
}
System.out.println();
}
-----------------------------------------------------------------------------------
1 0 1 0 1
0 1 0 1 0
1 0 1 0 1
0 1 0 1 0
1 0 1 0 1
for(int i=0;i<=4;i++)
{
for(int j=1;j<=5;j++)
{
System.out.print((i+j)%2+" ");
}
System.out.println();
}
-----------------------------OR-----------------------------
int n=1;
for(int i=1;i<=5;i++)
{
for(int j=1;j<=5;j++)
{
System.out.print(n%2+" ");
n++;
}
System.out.println();
}
1 * 2 * 3
1 * 2 * 3
1 * 2 * 3
1 * 2 * 3
1 * 2 * 3
for(int i=1;i<=5;i++)
{
int n=1;
for(int j=1;j<=5;j++)
{
if(j%2==0)
{
System.out.print("* ");
}
else
{
System.out.print(n++ +" ");
}
}
System.out.println();
}
--------------------------------------------------------------
*
* *
* * *
* * * *
* * * * *
for(int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print("* ");
}
System.out.println();
}
----------------------------------------------------------------
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
for(int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(j+" ");
}
System.out.println();
}
i-> 1 ;j -> 1
i-> 2 ;j -> 1,2
i-> 3 ;j -> 1,2,3
i-> 4 ;j -> 1,2,3,4
i-> 5 ;j -> 1,2,3,4,5
------------------------------------------------------------------
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
for(int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(i+" ");
}
System.out.println();
}
--------------------------------------------------------------
5
5 4
5 4 3
5 4 3 2
5 4 3 2 1
for(int i=1;i<=5;i++)
{
int n=5;
for(int j=1;j<=i;j++)
{
S.o.p(n-- +" ");
}
S.o.pl();
}
------------------------------------------
A
B C
D E F
G H I J
K L M N O
char c='A';
for(int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
{
S.o.p(c++ +" ");
}
S.o.pln();
}
------------------------------------------
A
A B
A B C
A B C D
A B C D E
for(int i=1;i<=5;i++)
{
char c='A';
for(int j=1;j<=i;j++)
{
S.o.p(c++ +" ");
}
S.o.pln();
}
-------------------------------------------
1
1 0
1 0 1
1 0 1 0
1 0 1 0 1
for(int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
{
S.o.p(j%2);
}
S.o.pln();
}
-----------------------------------------------------------------------------------
------------
* * * * *
* * * *
* * *
* *
*
for(int i=1;i<=5;i++)
{
for(int j=i;j<=5;j++)
{
System.out.print("* ");
}
System.out.println();
}
-----------------------------------------------------------------------------------
----------
1 2 3 4 5
2 3 4 5 1
3 4 5 1 2
4 5 1 2 3
5 1 2 3 4
int a=1;
for(int i=1;i<=5;i++)
{
for(int j=i;j<=5;j++)
{
System.out.print(j+" ");
}
if(i>1)
{
for(int k=1;k<=a;k++)
{
System.out.print(k +" ");
}
a++;
}
System.out.println();
}
-----------------------------------------------------------------------------------
------------
1 3 5 7 9
3 5 7 9 1
5 7 9 1 3
7 9 1 3 5
9 1 3 5 7
int a=1;
for(int i=1;i<=9;i=i+2)
{
for(int j=i;j<=9;j=j+2)
{
System.out.print(j+" ");
}
if(i>1)
{
for(int k=1;k<=a;k=k+2)
{
System.out.print(k +" ");
}
a=a+2;
}
System.out.println();
}
-----------------------------------------------------------------------------------
-----------
1 0 1 0 1
1 0 1 0
1 0 1
1 0
1
A B C D E
A B C D
A B C
A B
A
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
1 1 1 1 1
2 2 2 2
3 3 3
4 4
5
E D C B A
E D C B
E D C
E D
C
E E E E E
D D D D
C C C
B B
A
5 4 3 2 1
5 4 3 2
5 4 3
5 4
5
*
* *
* * *
* * * *
* * * * *
for(int i=1;i<=5;i++)
{
for(int j=i;j<=4;j++)
{
System.out.print(" ");
}
for(int k=1;k<=i;k++)
{
System.out.print("* ");
}
System.out.println();
}
---------------------------------------------------------------------------------
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
for(int i=1;i<=5;i++)
{
for(int j=i;j<=4;j++)
{
System.out.print(" ");
}
for(int k=1;k<=i;k++)
{
System.out.print(k+" ");
}
System.out.println();
}
-------------------------------------------------------------------------------
-------------------------------------------------------------------------
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
for(int i=1;i<=5;i++)
{
for(int j=i;j<=4;j++)
{
System.out.print(" ");
}
for(int k=1;k<=i;k++)
{
System.out.print(i+" ");
}
System.out.println();
}
---------------------------------------------------------------------------------
* * * * *
* * * *
* * *
* *
*
for(int i=1;i<=5;i++)
{
for(int j=1;j<i;j++)
{
System.out.print(" ");
}
for(int k=i;k<=5;k++)
{
System.out.print("* ");
}
System.out.println();
}
----------------------------------------------------------------------------
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
for(int i=1;i<=4;i++)
{
for(int j=i;j<=4;j++)
{
System.out.print(" ");
}
for(int k=1;k<=i;k++)
{
System.out.print("* ");
}
System.out.println();
}
for(int i=1;i<=5;i++)
{
for(int j=1;j<i;j++)
{
System.out.print(" ");
}
for(int k=i;k<=5;k++)
{
System.out.print("* ");
}
System.out.println();
}
--------------------------------------------------------------------
*
* *
* * *
* * * *
* * *
* *
*
---------------------------------------------------------
*
**
***
****
*****
****
***
**
*
-------------------------------------------------------------
A B 1 C D
E F 2 G H
I J 3 K M
L N 4 O P
Q R 5 S T
----------------------------------------------------------------
* * * * *
* * * *
* * *
* *
*
* *
* * *
* * * *
* * * * *
----------------------------------------------------------------------
*
*
* * * * *
*
*
for(int i=1;i<=5;i++)
{
for(int j=1;j<=5;j++)
{
if(i==3 || j==3)
{
S.o.p("*");
}
else
{
S.o.p(" ");
}
}
S.o.pln();
}
-----------------------------------------------------------------------------
A B 1 C D
E F 2 G H
I J 3 K L
M N 4 O P
Q R 5 S T
A -> T
ALL THE ROWS IN 3RD COLUMN WE NEED TO PRINT NUMBERS
char n='A';
for(int i=1;i<=5;i++)
{
for(int j=1;j<=5;j++)
{
if(j==3)
{
S.o.p(i);
}
else
{
S.o.p(n);
n++;
}
}
S.o.pln();
}
-----------------------------------------------------------------------------------
--------
1
2 7
3 8 13
4 9 14 19
5 10 15 20 25
for(int i=1;i<=5;i++)
{
int n=i;
for(int j=1;j<=i;j++)
{
System.out.print(n+" ");
n=n+5;
}
System.out.println();
}
-----------------------------------------------------------------------------
1
2 5
3 6 8
4 7 9 10
5 8 10 11 11
for(int i=1;i<=5;i++)
{
int n=i;
int a=3;
for(int j=1;j<=i;j++)
{
S.o.p(n);
n=n+a;
a--;
}
S.o.pln();
}
------------------------------------------------------------------------------
1
2 6
3 7 10
4 8 11 13
5 9 12 14 15
new keyword:-
-> create an object in heap area
constructor:-
-> to load all non-static members into the object
note :-
-> all blocks(method block,constuctor block,static and non static block)
except class blocks they are stored inside the method area
class A
{
int a=10;
static int b=20;
static
{
}
A()
{
}
{
}
public static void m1()
{
}
public void m2()
{
}
}
------------------------------------------------------------------
*
* *
* *
* *
* * * * *
for(int i=1;i<=5;i++)
{
for(int j=1;j<=5;j++)
{
if(i==j || j==1 || i==5)
{
System.out.print("* ");
}
else
{
System.out.print(" ");
}
}
System.out.println();
}
------------------------------------------------------------------
* * * * *
* *
* *
* *
* * * * *
for(int i=1;i<=5;i++)
{
for(int j=1;j<=5;j++)
{
if(i==1 || j==1 || i==5 || j==5)
{
System.out.print("* ");
}
else
{
System.out.print(" ");
}
}
System.out.println();
}
--------------------------------------------------------------
* * * * *
* 1 2 3 *
* 1 2 3 *
* 1 2 3 *
* * * * *
*
* *
* *
* *
* * * * *
*
* *
* * *
* * * *
* * * * *
for(int i=1;i<=5;i++)
{
for(int j=i;j<5;j++)
{
System.out.print(" ");
}
for(int k=1;k<=i;k++)
{
System.out.print("*");
}
System.out.println();
}
--------------------------------------------------------------------
Array :-
-> it is a continuous block of memory
-> in array we can store similar type of data or homogeneous type of data
-> to access the elements from an array we have to take the help of index
-> index starts from 0.
1,2,3,4,5 ->>>>
int [] j ={1,2,3,4,5};
String [] s1={"i","am","from","spiders"};
a,b,c,d,e
Step 1:-
import the Scanner class
----->>>> import java.util.Scanner;
Step 2:-
create an object for scanner class
Step 3:-
import java.util.Scanner;
class A2
{
public static void main(String [] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("enter the numbers");
int [] a1=new int[5];
for(int i=0;i<a1.length;i++)
{
System.out.println("enter "+(i+1)+" number");
a1[i]=sc.nextInt();
}
System.out.println("even numbers are");
for(int j=0;j<a1.length;j++)
{
if(a1[j]%2==0)
{
System.out.println(a1[j]);
}
}
}
}
-----------------------------------------------------------------------------
WAJP to accept 6 number from the user and print the sum of odd numbers in it.
i/p:- [1,2,3,4,5,6]
o/p:- 9
i/p:- [10,34,57,11,24,9]
o/p:- 77
import java.util.Scanner;
class A2
{
public static void main(String [] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("enter the numbers");
int [] a1=new int[6];
for(int i=0;i<a1.length;i++)
{
System.out.println("enter "+(i+1)+" number");
a1[i]=sc.nextInt();
}
int sum=0;
System.out.println("even numbers are");
for(int j=0;j<a1.length;j++)
{
if(a1[j]%2==1)
{
sum=sum+i;
}
}
System.out.println(sum);
}
}
-----------------------------------------------------------------------------------
--------------------------------
WAJP to store 5 city names in an array then take input from the user any city name
and check
whether it is in array or not . If it is present print the index else -1
String [] s={"pune","bengaluru","mumbai","chennai","delhi"};
i/p:- "hyderabad"
o/p:- -1
i/p:- "chennai"
o/p:- 3
i/p:- "pune"
o/p:- 0
String [] s={"pune","bengaluru","mumbai","chennai","delhi"};
String key=sc.next();
for(int i=0;i<s.length;i++)
{
if(s[i].equals(key))
{
System.out.println(s[i]);
break;
}
else
{
System.out.println(-1);
}
}
-----------------------------------------------------------------------------------
---------
Structure of java program:-
package declrataion
import section
class ClassName
{
public static void main(String [] args)
{
//instructions
}
}
-----------------------------------------------------------------------------------
---------
Abstraction:-
->
import java.util.Scanner;
class A4
{
public static void main(String[] args)
{
System.out.println("enter binary digits");
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int m=0;
int sum=0;
while(true)
{
if(n==0)
{
break;
}
else
{
int temp=n%10;
sum=sum+temp*(int)Math.pow(2,m);
m++;
n=n/10;
}
}
System.out.println("the decimal representation for given binary is :-
"+sum);
}
}
-----------------------------------------------------------------------------------
-----
WAJP to find the sum of all the elements in an array.
a1={1,2,3,4,5}
o/p:- 15
{4,2,8}
o/p:- 14
------------------------------------------------------------------------
Sorting an array
import java.util.Arrays;
class A6
{
public static void main(String[] args)
{
int [] a1={50,96,23,10,67};
System.out.println("before sorting");
for(int i=0;i<a1.length;i++)
{
System.out.println(a1[i]);
}
Arrays.sort(a1);
System.out.println("after sorting");
for(int i=0;i<a1.length;i++)
{
System.out.println(a1[i]);
}
}
}
-----------------------------------------------------------------
Bubble sort:-
-> the current element is compared with next element
-> if current element is greater than next element then we need to swap them
import java.util.Arrays;
class BubbleSort
{
public static void main(String[] args)
{
int [] a1={12,7,15,56,0,5};
System.out.println("before sorting");
System.out.println(Arrays.toString(a1));
for(int i=0;i<a1.length;i++)
{
for(int j=i+1;j<a1.length;j++)
{
if(a1[i]>a1[j])
{
int temp=a1[i];
a1[i]=a1[j];
a1[j]=temp;
}
}
}
System.out.println("after sorting");
System.out.println(Arrays.toString(a1));
}
}
------------------------------------------------------------------
WAJP to print the 2nd largest element in an array.
import java.util.Arrays;
class BubbleSort
{
public static void main(String[] args)
{
int [] a1={12,7,15,56,0,5};
System.out.println("before sorting");
System.out.println(Arrays.toString(a1));
for(int i=0;i<a1.length;i++)
{
for(int j=i+1;j<a1.length;j++)
{
if(a1[i]>a1[j])
{
int temp=a1[i];
a1[i]=a1[j];
a1[j]=temp;
}
}
}
System.out.println(a1[a1.length-2]);
}
}
---------------------------------------------------------------
class A7
{
public static void main(String[] args)
{
int [] a={1,2,3,4,60};
int count=0;
int sum=0;
for(int i=0;i<a.length;i++)
{
if(a[i]>59)
{
count++;
}
}
if(count==0)
{
for(int i=0;i<a.length;i++)
{
if(a[i]%2==0)
{
sum=sum+a[i];
}
}
}
System.out.println(sum);
}
}
--------------------------------------------------------------------------
import java.util.Arrays;
class A8
{
public static void main(String[] args)
{
int a[] ={1,92,3,4,5};
Arrays.sort(a);
int sum=0;
if(a[a.length-1]<59)
{
for(int i=0;i<a.length;i++)
{
if(a[i]%2==0)
{
sum=sum+a[i];
}
}
}
System.out.println(sum);
}
}
------------------------------------------------------------------------
WAJP to accept 10 numbers from the user and check the count of prime numbers in it
and print the count of prime numbers.
Test case 1:-
a=[23,46,76,34,89,10,15,39,97,75]
o/p:- count of prime numbers are 3
String literal:-
-> anything enclosed in double quotes
-> all the string literals are stored inside the String constant pool
-> String pool are present in heap area
class A
{
p s v m(String [] args)
{
String s1="abc";
String s2="ABC";
String s3=new String("Abc");
String s4="Abc";
String s5=new String("abc");
}
}
SCP:- 3 objects
heap area:- 2 objects
class B
{
p s v main(S [] args)
{
String s="qpsiders";
s.toUpperCase();
System.out.println(s);//qpsiders
}
}
-----------------------------------------------------------------
Important methods of String class:-
1. charAt(int index) -> char
2. concat(String s) -> String
3. equals(String s) -> boolean
4. equalsIgnoreCase(String s) -> boolean
5. split(String s) -> String array
import java.util.Scanner;
class ArrayPrime
{
public static void main(String[] args)
{
String s="pune";
String s1="";
for(int i=s.length()-1;i>=0;i--)
{
s1=s1+s.charAt(i);
}
System.out.println(s1);
}
}
------------------------------------------------------------------------
WAJP to check whether the given String is palindrome or not.
import java.util.Scanner;
class ArrayPrime
{
public static void main(String[] args)
{
String s="pune";
String s1="";
for(int i=s.length()-1;i>=0;i--)
{
s1=s1+s.charAt(i);
}
if (s.equals(s1))
{
System.out.println("palindrome");
}
else
{
System.out.println("not a palindrome");
}
}
}
-------------------------------------------------------------------------------
workspace -> project -> package -> class
Package:-
-> it is used to store a group of related classes and interfaces.
Advantage :-
-> it is used to avoid name conflicts
WorkSpace:-
-> it means where we save all the projects
Shortcuts :-
1. ctrl + space -> suggestions
2. ctrl + f11 -> to run the program
3. ctrl + shift + O -> importing the packages
---------------------------------------------------------------------
Access modifier:-
-> it is used to give accessibility for members of the class
private:-
-> within the class
default:-
-> within the package
protected:-
-> within the class
-> outside the class(within the package)
-> outside the package possible only by the child class
public:-
-> everywhere
private<default<protected<public
-----------------------------------------------------------------------------
Method overriding :-
-> non static methods
-> run time polymorphism or dynamic binding or late binding
-> method overriding depends upon the object creation or Type of the object created
--------------------------------------------------------------------------------
CTP:-(Compile time polymorphism)
1. method overloading
2. constructor overloading
3. method shadowing
4. variable shadowing
Method shadowing:-
-> in both parent class and child class the method having same name and same
arguments but if the
method is static then we call it as method shadowing
-> method shadowing depends upon the type of the address storing or type of the
object reference variable
class A
{
public static void m1()
{
S.o.pln("from a");
}
}
class B extends A
{
public static void m1()
{
S.o.pln("from b");
}
}
A a1=new A();
a1.m1();//from a
B a2=new B();
a2.m1();//from b
A a3=new B();
a3.m1();//from a
-----------------------------------------------------------------------------------
----------------
class A
{
public void m1()
{
S.o.pln("from a");
}
}
class B extends A
{
public void m1()
{
S.o.pln("from b");
}
}
A a1=new A();
a1.m1();//-->>from a
B a2=new B();
a2.m1();//-->>from b
A a3=new B();
a3.m1();//-->>from b
-----------------------------------------------------------------------------------
-----------------
class Bike extends Vehicle
{
public void noOfWheels()
{
System.out.println("bike has 2 wheels");
{
}
-------------------------------------------------
abstract class Fruit
{
abstract public void tasteOfFruit();
}
class Mango extends Fruit
{
public void tasteOfFruit()
{
System.out.println("mango is sweet");
}
}
Vehicle and Fruit we dont know the proper implementation