W M1

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 53

--------------------------------------------------------------

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;

public void play()


{
}
public void read()
{
}
public void write()
{
}
public void sleep()
{
}
}
-------------------------------------------------------------------
Variables:-
-> it is a container to store value or data
-> it is a named memory location
1. local variable
2. global variable: - the variable which are declared inside the class block
* global variable is of 2 types:- static variable and non static variable

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

adjective -> describing -> variables


verb-> action words -> methods

class Car
{
String color;
double price;
int noOfDoors;

public void drive()


{
}
public void park()
{
}
public void stop()
{
}
}
---------------------------------------------------------------------------------
Operator: -
-> it is predefined symbol which peforms some special task

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

int +double -> double


double + int +double+int -> double
char +int -> int
char +double -> double
String +double -> String
String +char -> String

10+20+"abc"+10+20 ->30abc1020
'a'+10 -> 97
"a"+'a'+10 -> "aa10"
"10"+20+"abc"+10+20 -> 1020abc1020
----------------------------------------------------------------------------------
Modulus operator: -(%)
m%n

case 1 :- if m and n are equal -> output is 0


case 2 :- if m is less than n -> output is m
case 3 :- if m is greater than n -> output is from 0 to n-1

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

"Scanner variable = new Scanner(System.in);"

Step 3:-
With the help of variable call below methods

datatype method signature


byte nextByte();
short nextShort();
int nextInt();
long nextLong();
float nextFloat();
double nextDouble();
char next().charAt(0);
boolean nextBoolean();

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

iteration:- how many times loop body gets executed


----------------------------------------------------------------------------
Type casting: -
-> the process of converting one datatype into another datatype.
-> type casting happens based on ranges not on size
1. primitive type casting:-
a. widening:-
-> converting smaller range of primitive datatype into larger range of primitive
larger datatype
-> implicit type casting
-> no data loss, so compiler will take responsibility of converting
-----------------------------------------------------------------------------------
--------
b. narrowing:-
-> converting larger range of datatype into smaller range of datatype.
-> explicit type casting
-> data loss is there
-> instead of compiler programmer should take the responsibility of converting by
using the type cast operator.
-----------------------------------------------------------------------------------
--------
Ex: -
byte a=(byte)127;
byte b=(byte)128;
byte c=(byte)129;
byte d=(byte)130;
System.out.println(a);//127
System.out.println(b);//-128
System.out.println(c);//-127
System.out.println(d);//-126
---------------------------------------------------------------------------------
Decision making statement:-
1. if statement
2. if-else
3. else if ladder
4. nested if
5. switch
---------------------------------------------------------------------------------
1.WAJP to check whether the number is odd or even.

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

the factors of 1 are :- 1


the factors of 2 are :- 1 2
the factors of 3 are :- 1 3
the factors of 4 are :- 1 2 4
the factors of 5 are :- 1 5
the factors of 6 are :- 1 2 3 6

the factors of 10 are :- 1 2 5 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.

n=5467 ->>>o/p:- 4 digits


n=56790 ->>>o/p:- 5 digits

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.

low level language:-


-> the language which is not readable and instructable

EX:- binary language

middle level language:-


-> the language which is partially readable and instructable

EX:- byte code, micro processor language

high level language:-


-> the language which is readable and instructable

Ex:- java, python


-----------------------------------------------------------------------------------
--------------
WAJP to reverse a given number.

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

static context->> static methods and static multi line initializer


non-static context->> non-static methods and non-static multi line initializer

can we execute a java program without main method??


-> no, we cannot execute

---------------------------------------------------------------
structure of a java program:-

//package declaration
//import section
class declaration
{

//methods

class is a blueprint of an object.

------------------------------------------------
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.

0,1 -> are not prime numbers


2,3,5,7,11,13,17.......-> prime numbers

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

145 -> 1!+4!+5! ->> 1 + 24 + 120 -> 145

145 -> strong number

254 -> 2! + 5! + 4! -> 2 + 120 + 24 -> 146


254 -> not a strong number

2-> 2!-> 2
1-> 1!-> 1

1,2,145 -> are the strong numbers

sum of the factorial of all the digits is equals to given number

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

1234 is xylem number


-----------------------------------------------------------------------------------
n-> 3456

extreme digits-> 3 and 6 -> 3+6 = 9


middle digits-> 4 and 5 -> 4+5 = 9

3456 is xylem number


-----------------------------------------------------------------------------------
--
n-> 784945

sum of extreme digits -> 7 + 5 -> 12


sum of middle digits -> 8 + 4 + 9 + 4 -> 25
784945 is phloem number
-----------------------------------------------------------------------------------
----
n-> 123
sum of extreme digits -> 1+3 -> 4
sum of middle digits -> 2 -> 2

123 is a phloem number

-----------------------------------------------------------------------------------
-------
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.

LCM:- lowest common multiple


LCM(a,b)= (a*b)/GCD(a,b)
-------------------------------------------------------------------------
1. WAJP to design a method which accepts number and returns the square of that
number
2. WAJP to design a method which accepts a number and returns true if it is
divisible by
3 else return false
3. WAJP to design a method which accepts character and returns it ASCII value

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

WAJP to count the number of prime numbers between 1-50.


--------------------------------------------------------------------------------

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

outer loop (i) -> rows


inner loop (j) -> columns
------------------------------------------------------------------------------
Square pattern:-

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

i=1 ; int n=i ; n=1; S.o.p(n++);


i=2 ; int n=i ; n=2; S.o.p(n++);

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

where static variables are stored???


-> static area or class static area or static pool

what is the use of method??


-> code reusability
where methods(static and non-static)are stored?
-> method area

where constructors are stored??


-> constructors are stored inside the method area

note :-
-> all blocks(method block,constuctor block,static and non static block)
except class blocks they are stored inside the method area

constructor and method :-

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.

syntax to declare an array:-

datatype [] variable = new datatype[size];


100,150,300,500,700 -> int

int [] a1 = new int[5];

syntax for declaring and initializing an array

datatype [] variable={val1 , val2 , val3 , val4 ,........};

1,2,3,4,5 ->>>>

int [] j ={1,2,3,4,5};

String [] s1={"i","am","from","spiders"};

a,b,c,d,e

char [] n=new char[5];


n[0]='a';
n[1]='b';
n[2]='c';
n[3]='d';
n[4]='e';
-------------------------------------------------------
char [] d={'a','b','c','d','e'};

Step 1:-
import the Scanner class
----->>>> import java.util.Scanner;

Step 2:-
create an object for scanner class

----->>>> Scanner variable=new Scanner(System.in);

Step 3:-

inside the Scanner class all methods are non static


-------------------------------------------------------------------------------
WAJP to ask the user to enter 5 numbers and print only the even numbers
i/p:- [1,4,8,34,50]
o/p:- even numbers are :- 4,8,34,50

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:-
->

abstract class Vehicle


{
int c;
abstract public void noOfWheels();
public void m1()
{
System.out.println("abc");
}
}
-----------------------------------------------------------------------------------
----------------------------
WAJP to convert decimal into binary.
import java.util.Scanner;
class A3
{
public static void main(String [] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("enter the number");
int n=sc.nextInt();
int [] a=new int[20];
int i=0;
while(n>0)
{
a[i]=n%2;
n=n/2;
i++;
}
for(int j=i-1;j>=0;j--)
{
System.out.print(a[j]);
}
}
}
---------------------------------------------------------------
WAJP to convert binary into decimal

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

Test case 2:-


a=[4,6,12,35,78,99,105,135,160,256]
o/p:- count of prime numbers are 0
-------------------------------------------------------------------------------
String:-
-> it is a non primitive datatype
-> it is predefined class in java
-> it is present in java.lang package
-> all string literals are stored inside the string constant pool
-> SCP(String constant pool) it is present inside the heap area
-> Strings are immutable in java because once String object is created
we cannot modify the String object but if we try modify a new object will
be created
-> immutable means not able to change
-> muttable means able to change

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

WAJP to demonstrate String immutable?

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

WAJP to reverse a given String


i/p:- pune
o/p:- enup

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

In java we have 2 types of packages:-


1. in-built package(pre-defined packages)
2. user-defined package
1. in-built package
-> we can use inbuilt packages with the help of import keyword
Ex:- java.util, java.lang , java.sql, java.io.

2. user defined package:-


-> syntax to create package is
package package_name;
-> we need to declare package before the class declaration

workspace -> project -> package -> class

WorkSpace:-
-> it means where we save all the projects

src -> where all java files are stored


bin -> where all class files are stored.

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

You might also like