Java Coding Book
Java Coding Book
{Java}
SRINIVAS GARAPATI
Address : Near Satyam theatre, Opposite HDFC Bank, 3rd Floor, Ameerpet, Hyderabad-500016
Contact : 9119556789 , 7306021113
[Link]
Contents
S No Topic Page Num
01 Understanding Operators Briefly 02
02 Program to understand variables behavior 09
03 Formatting Output 11
04 Programs on arithmetic operators 17
05 Scanner class – Reading input 20
06 If , If-else, If-else-if, Nested if 23
07 Loops Introduction 36
08 For Loop Programs 37
09 While Loop Programs 44
10 Break & Continue 60
11 Switch Case 61
12 Do While Loop 63
13 Nested For Loop 64
14 Range Based Programs 65
15 Pattern Programs 68
16 Object oriented programming 97
17 Static and Instance methods 99
18 Recursion 111
19 Menu Driven Programs 113
20 Setters and Getters 117
21 Object Initialization 123
22 Arryas 126
23 Two dimensional arrays 156
24 Strings 165
sum=sum+n%10; rev=rev*10+n%10;
n=n/10; n=n/10;
sum=sum+n%10; rev=rev*10+n%10;
n=n/10; n=n/10;
sum=sum+n%10; rev=rev*10+n%10;
n=n/10; n=n/10;
Condition to check the multiplication of first 2 numbers equal to square of 3rd number
T
Condition to check average of 4 subjects marks greater than 60 E
C
H
N
Condition to check the sum of First 2 numbers equals to last digit of 3rd num
O
L
O
G
Condition to check average of 3 numbers equals to first number I
E
S
11. Condition to check the multiplication of 2 numbers not equals to 3 rd number or not:
13. Condition to check the sum of First 2 numbers equals to last digit of 3 rd num or not:
15. Condition to check the given quantity of fruits exactly in dozens or not:
17. Condition to check First Num is greater than both Second & Third Nums or Not:
A
20. Condition to check the number is 3 digits number or not: M
E
E
21. Condition to check the student passed in all 5 subjects or Not: R
P
E
22. Condition to check the character is Vowel or Not: T
T
E
23. Condition to check the character is Upper case alphabet or not: C
H
N
24. Condition to check the character is digit or not: O
L
O
25. Condition to check the character is Alphabet or not: G
I
E
26. Condition to check the character is Symbol or not: S
28. Condition to check any 2 numbers equal or not among the 3 numbers:
We need to specify the datatype only once in variable creation(not everytime in use):
public class Code
{
public static void main(String[] args)
{
int a=10;
int a=20;
[Link](a);
}
}
Formatting Output
• An output without format doesn’t make sense to the end user.
• We always display results in string format.
• We need to create formatted string with output values as follows
Syntax Example
int + int = int 10 + 20 => 30
String + String = String "Java" + "Book" => JavaBook
"10" + "20" => 1020
String + int = String "Book" + 5 => Book5
"123" + 456 => 123456
String + int + int = String "Sum = " + 10 + 20 => Sum = 1020
String + (int + int) = String "Sum = " + (10 + 20) => Sum = 30
String + double = String "Value = " + 23.45 => Value = 23.45
String – int = Error
Scanner Class:
• Using java library class Scanner, we can read input like integers, characters, strings,
double values from the user.
• Different methods to read different values such as nextInt(), next(), nextDouble()…
• We need to specify the Keyboard([Link]) while creating Scanner class object.
Scanner scan = new Scanner([Link]);
• We access all the methods using object reference name.
Reading integer value: nextInt() method read and returns an integer value
import [Link];
public class Code
{
public static void main(String[] args)
{
Scanner scan = new Scanner([Link]);
[Link]("Enter integer : ");
int n = [Link]();
[Link]("Input value is : " + n);
}
}
Output: Enter integer : 10
Input value is : 10
Reading Boolean value: nextBoolean() method returns Boolean value that we entered
import [Link];
public class Code
{
public static void main(String[] args)
{
Scanner scan = new Scanner([Link]);
[Link]("Enter boolean value : ");
boolean b = [Link]();
[Link]("Input value is : " + b);
}
}
Reading String: using next() method we can read single word string from the user
import [Link];
public class Code
{
public static void main(String[] args) {
Scanner scan = new Scanner([Link]);
[Link]("Enter your name : ");
String name = [Link]();
[Link]("Hello : " + name);
}
}
Output: Enter your name: Amar
Hello : Amar
Reading character:
import [Link];
class Code {
public static void main(String[] args) {
Scanner scan = new Scanner([Link]);
[Link]("Enter character : ");
char ch = [Link]().charAt(0);
[Link]("Input character is : " + ch);
}
}
Output: Enter character : A
Input character is : A
If Block Programs
if(Condition)
{
If-block-logic;
}
Program to give 15% discount on bill if the bill amount is greater than 5000
import [Link];
public class Code
{
public static void main(String[] args)
{
Scanner scan = new Scanner([Link]);
[Link]("Enter bill : ");
double bill = [Link]();
if(bill>=5000)
{
double discount = 0.15 * bill;
bill = bill-discount;
}
[Link]("Pay : " + bill);
}
}
Program to give 20% bonus on salary if the employee has more than 5 years of
experience:
import [Link];
class Code
{
public static void main(String[] args) {
Scanner scan = new Scanner([Link]);
[Link]("Enter salary : ");
double salary = [Link]();
[Link]("Enter experience : ");
int exp = [Link]();
if(exp>5){
double bonus = 0.2*salary;
salary = salary + bonus ;
}
[Link]("Salary to pay : " + salary);
}
}
Output: Enter salary : 30000
Enter experience : 7
Salary to pay : 36000
Else Block: It executes when the given condition of if block fails. Else block is optional for if
block. Else block cannot be defined without IF block.
Syntax Flow Chart
if(condition)
{
If-stats;
}
else
{
Else-stats;
}
else
[Link]("Not even number");
}
}
Output: Enter number: 7
Not even number
If Else If block
If else if: The if-else-if ladder statement executes only block among multiple we defined based
on valid condition.
Syntax Flow Chart
if(condition1){
Statement1;
}
else if(condition2){
Statement2;
}
else if(condition3){
Statement3;
}
else{
Statement4;
}
int c = [Link]();
if(a>b && a>c)
[Link]("a is big");
else if(b>c)
[Link]("b is big");
else
[Link]("c is big");
}
}
Output: Enter a, b, c values :
10
30
20
b is big
import [Link];
class Code
{
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of units : ");
int units = [Link]();
double bill=0.0;
if(units>=0 && units<=100)
bill = units*0.8;
else if(units>100 && units<=200)
bill = 80 + (units-100)*1.2;
else if(units>200 && units<=300)
bill = 200 + (units-200)*1.5;
else
bill = 350 + (units-300)*1.8;
[Link]("Total bill amount : " + bill);
}
}
Output: Enter number of units: 150
Total bill amount : 140.0
Nested If Block
Nested If: Defining if block inside another if block.
Syntax Flow Chart
if(condition){
if(condition){
if-if-Stat;
}
else{
if-else-stat;
}
}
else{
if(condition){
else-if-stat;
}
else{
else-else-
stat;
}
}
Nested If Programs
Check Even Number or Not only if it is Positive
import [Link];
class Code{
public static void main(String[] args) {
Scanner scan = new Scanner([Link]);
[Link]("Enter number : ");
int n = [Link]();
if(n>0){
if(n%2==0)
[Link]("Even number");
else
[Link]("Not even number");
}
else{
[Link]("Negative number given");
}
}
}
Output: Enter number: 5
Not even number
int b = [Link]();
int c = [Link]();
if(a==b && b==c)
[Link]("Equilateral");
else if(a==b ||a==c||b==c)
[Link]("Isosceles");
else
[Link]("Scalene");
}
}
Output: Enter 3 sides of triangle :
70
70
40
Isosceles
Introduction to Loops
Note: Block executes only once whereas Loop executes until condition become False
For Loop: We use for loop only when we know the number of repetitions. For example,
• Print 1 to 10 numbers
• Print Array elements
• Print Multiplication table
• Print String character by character in reverse order
While loop: We use while loop when we don’t know the number of repetitions.
• Display contents of File
• Display records of Database table
Do while Loop: Execute a block at least once and repeat based on the condition.
• ATM transaction: When we swipe the ATM card, it starts the first transaction. Once the
first transaction has been completed, it asks the customer to continue with another
transaction and quit.
For Loop
for loop: Execute a block of instructions repeatedly as long as the condition is valid. We use for
loop only when we know the number of iterations to do.
int n = [Link]();
int sum=0;
for (int i=1 ; i<n ; i++)
{
if(n%i==0)
sum=sum+i;
}
if(n==sum)
[Link]("Perfect Number");
else
[Link]("Not a Perfect Number");
}
}
While Loop
While loop: Execute a block of instructions repeatedly until the condition is false. We use while
loop only when don’t know the number of iterations to do.
while(condition)
{
statements;
}
if(num%2==0)
[Link](num + " is even.");
else
[Link](num + " is odd.");
int count=0;
for(int i=1 ; i<=n ; i++)
{
if(n%i==0)
{
count++;
}
}
if(count==2)
[Link](n + " is Prime");
else
[Link](n + " is not Prime");
{
[Link]("Enter 2 nums : ");
int a = [Link]();
int b = [Link]();
[Link]("Result = " + (a-b));
}
else if(ch==3)
{
[Link]("Enter 2 nums : ");
int a = [Link]();
int b = [Link]();
[Link]("Result = " + (a*b));
}
else if(ch==4)
{
[Link]("End of program");
break;
}
else
[Link]("Error : Invalid choice");
}
}
}
import [Link];
class Code
{
public static void main(String[] args)
{
Scanner scan = new Scanner([Link]);
[Link]("Enter Num : ");
int n = [Link]();
n = n/10;
[Link]("After removing last digit : " + n);
}
}
n = n/10;
}
[Link]("Smallest digit : " + small);
}
}
Program to find the sum of Largest and Smallest Digit in the given number:
import [Link];
public class Code
{
public static void main(String[] args)
{
Scanner scan = new Scanner([Link]);
[Link]("Enter Num : ");
int n = [Link]();
int large=0, small=9;;
int r;
while(n>0)
{
r = n%10;
if(r>large)
large = r;
if(r<small)
small = r;
n = n/10;
}
[Link]("Sum of large and small : " + (large+small));
}
}
Program to Find the Sum of First and Last digits of given number
import [Link];
class Code
{
public static void main(String[] args)
{
Scanner scan = new Scanner([Link]);
[Link]("Enter Num : ");
int n = [Link]();
int first = n%10;
n=n/10;
while(n>=10){
n = n/10;
}
int last = n%10;
[Link]("Sum of First & Last Digits : " + (first+last));
}
}
Output: Enter Num : 1234
Sum of First & Last Digits : 5
Palindrome Number: The number become same when we reverse is called Palindrome number
Examples: 121, 1001, 123321
import [Link];
class Code
{
public static void main(String[] args)
{
Scanner scan = new Scanner([Link]);
[Link]("Enter Num : ");
int n = [Link]();
int rev=0, r, temp=n;
while(n>0)
{
r = n%10;
rev = rev*10 + r;
n = n/10;
}
if(temp==rev)
[Link]("Palindrome Number");
else
[Link]("Not Palindrome Number");
}
}
Output: Enter Num : 1221
Palindrome Number
import [Link];
class Code
{
public static void main(String[] args)
{
int num, sum, dig;
Scanner scan = new Scanner([Link]);
[Link]("Enter Num : ");
num = [Link]();
[Link](num + "->");
while(num/10!=0)
{
sum = 0;
while(num!=0)
{
dig=num%10;
sum+=dig;
num/=10;
}
[Link](sum + "->");
num=sum;
}
}
}
Output: Enter Num: 9657
9657 -> 27 -> 9 ->
ADAM Number: Take a number then square it then reverse it then find its square root then
reverse. If the given number equals to the final number then it is called ADAM.
• Take the number (12)
• Square the number (144)
• Reverse the number(441)
• Square root of number (21)
• Reverse the number(12)
import [Link];
class Code
{
public static void main(String[] args)
{
break: A branching statement that terminates the execution flow of a Loop or Switch case.
class Code
{
public static void main(String[] args) {
for (int i=1 ; i<=10 ; i++){
if(i==5){
break;
}
[Link](i + " ");
}
}
}
Output: 1 2 3 4
Continue: A branching statement that terminates the current iteration of loop execution.
class Code
{
public static void main(String[] args) {
for (int i=1 ; i<=10 ; i++){
if(i==5){
continue;
}
[Link](i + " ");
}
}
}
Output: 1 2 3 4 5 6 7 8 9 10
Switch case
Switch: is a Conditional Statement executes based on given choice (case). Default case executes
if the user entered invalid choice. Case should terminate with break statement.
Syntax FlowChart
switch(choice)
{
case 1 : Statements ;
break
case 2 : Statements ;
break
......
case n : Statements ;
break
default: Statements ;
}
import [Link];
class Code
{
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter character(r, g, b) : ");
char ch = [Link]().charAt(0);
switch(ch)
{
case 'r' : [Link]("Red");
break;
case 'g' : [Link]("Green");
break;
case 'b' : [Link]("Blue");
break;
default : [Link]("Weird");
}
}
}
Output: Enter character(r, g, b): g
Green
Do-While Loop
do-while: Executes a block at least once and continue iteration until condition is false.
do
{
statements;
} while(condition);
Number of iterations in Nested loop is equals to “outer loop iterations multiplied by inner
loop iterations”.
class Code
{
public static void main(String[] args)
{
int count=0;
for (int i=1 ; i<=5 ; i++)
{
for (int j=1 ; j<=5 ; j++){
count++;
}
}
[Link]("Number of Iterations : " + count);
}
}
Output : Number of Iterations : 25
Pattern Programs
Pattern:
• Representation of data in two-dimensional format (rows and columns)
• We use nested loops to print patterns.
• We can print patterns with numbers, characters, starts, symbols
• Patterns can be in different shapes like triangle, rectangle, half triangle, pyramid and so
on.
Pattern Logic
for (int i=1 ; i<=5 ; i++)
{
11111
for (int j=1 ; j<=5 ; j++)
22222
33333 {
44444 [Link](i);
55555 }
[Link]();
}
Pattern Logic
for (int i=1 ; i<=5 ; i++)
{
*****
for (int j=1 ; j<=5 ; j++)
*****
***** {
***** [Link]("*");
***** }
[Link]();
}
Pattern Logic
for (int i=5 ; i>=1 ; i--)
54321 {
54321
for (int j=5 ; j>=1 ; j--){
54321
54321 [Link](j);
54321 }
[Link]();
}
}
Pattern Logic
for (int i=5 ; i>=1 ; i--)
55555 {
44444
for (int j=5 ; j>=1 ; j--)
33333
22222 {
11111 [Link](i);
}
[Link]();
}
Pattern Logic
for (int i=1 ; i<=5 ; i++)
10101 {
10101
for (int j=1 ; j<=5 ; j++)
10101
10101 {
10101 [Link](j%2);
}
[Link]();
}
Pattern Logic
for (int i=1 ; i<=5 ; i++)
11111 {
00000
for (int j=1 ; j<=5 ; j++)
11111
00000 {
11111 [Link](i%2);
}
[Link]();
}
Pattern Logic
for (int i=1 ; i<=5 ; i++){
for (int j=1 ; j<=5 ; j++){
$#$#$
if(j%2==0)
$#$#$
$#$#$ [Link]("#");
$#$#$ else
$#$#$ [Link]("$");
}
[Link]();
}
Pattern Logic
int k=1;
12345 for (int i=1 ; i<=5 ; i++){
67891
for (int j=1 ; j<=5 ; j++){
23456
78912 [Link](k++%10);
34567 }
[Link]();
}
Pattern Logic
for (int i=1 ; i<=5 ; i++)
{
11111
for (int j=1 ; j<=5 ; j++){
12345
33333 if(i%2==0)
12345 [Link](j);
55555 else
[Link](i);
}
[Link]();
}
Pattern Logic
for (int i=1 ; i<=5 ; i++)
{
55555
for (int j=1 ; j<=5 ; j++){
54321
33333 if(i%2==0)
54321 [Link](j);
11111 else
[Link](i);
}
[Link]();
}
Pattern Logic
for (int i=1 ; i<=7 ; i++){
+ for (int j=1 ; j<=7 ; j++){
+
if(i==4||j==4)
+
+++++++ [Link]("+");
+ else
+ [Link](" ");
+ }
[Link]();
}
Pattern Logic
for (int i=1 ; i<=7 ; i++){
+++++++ for (int j=1 ; j<=7 ; j++){
+ + +
if(i==1||i==4||i==7||j==1||j==4||j==7)
+ + +
+++++++ [Link]("+");
+ + + else
+ + + [Link](" ");
+++++++ }
[Link]();
}
Pattern Logic
for (int i=1 ; i<=7 ; i++){
+ for (int j=1 ; j<=7 ; j++){
+
if(i==j)
+
+ [Link]("+");
+ else
+ [Link](" ");
+ }
[Link]();
}
Pattern Logic
+ for (int i=1 ; i<=7 ; i++){
+ for (int j=1 ; j<=7 ; j++){
+
if(j==8-i)
+
+ [Link]("+");
+ else
+ [Link](" ");
}
[Link]();
}
Pattern Logic
for (int i=1 ; i<=7 ; i++){
+ + for (int j=1 ; j<=7 ; j++){
+ +
if(i==j || j==8-i)
+ +
+ [Link]("+");
+ + else
+ + [Link](" ");
+ + }
[Link]();
}
Pattern Logic
for (int i=1 ; i<=7 ; i++){
+ + + + + + + for (int j=1 ; j<=7 ; j++){
+ + + +
if(i==1||i==7||j==1||j==7||i==j||j==8-i)
+ + + +
+ + + [Link]("+");
+ + + + else
+ + + + [Link](" ");
+ + + + + + + }
[Link]();
}
Pattern Logic
for (int i=1 ; i<=7 ; i++){
++++ for (int j=1 ; j<=7 ; j++){
+
if((i==1 && j<=4) || j==4 || (i==7 && j>=4))
+
+ [Link]("+");
+ else
+ [Link](" ");
++++ }
[Link]();
}
Pattern Logic
+ for (int i=1 ; i<=7 ; i++){
+ for (int j=1 ; j<=7 ; j++){
+
if(i==4||(j==1 && i>=4) || (j==7 && i<=4))
+++++++
+ [Link]("+");
+ else
+ [Link](" ");
}
[Link]();
}
Pattern Logic
++++ + for (int i=1 ; i<=7 ; i++){
+ + for (int j=1 ; j<=7 ; j++){
+ +
if(i==4||(j==1 && i>=4) || (j==7 && i<=4) ||
+++++++
+ + (i==1 && j<=4) || j==4 || (i==7 && j>=4))
+ + [Link]("+");
+ ++++ else
[Link](" ");
}
[Link]();
}
Pattern Logic
for (int i=1 ; i<=7 ; i++){
+ for (int j=1 ; j<=5 ; j++){
+ +
if(j==4-i || j==3 || i==7)
+ +
+ [Link]("+");
+ else
+ [Link](" ");
+ + + + + + + }
[Link]();
Pattern Logic
for (int i=1 ; i<=7 ; i++){
+ + + + + for (int j=1 ; j<=5 ; j++){
+
if(i==1||i==4||i==7||(j==5&&i<=4)||(j==1&&i>=4))
+
+ + + + + [Link]("+");
+ else
+ [Link](" ");
+ + + + + }
[Link]();
}
Pattern Logic
for (int i=1 ; i<=7 ; i++){
+ + + + + for (int j=1 ; j<=5 ; j++){
+
if(i==1||i==4||i==7||j==5)
+
+ + + + + [Link]("+");
+ else
+ [Link](" ");
+ + + + + }
[Link]();
}
Pattern Logic
+ for (int i=1 ; i<=7 ; i++){
+ + for (int j=1 ; j<=7 ; j++){
+ +
if(j==5 || i==5 || j==6-i)
+ +
+ + + + + + + + [Link]("+");
+ else
+ [Link](" ");
}
[Link]();
}
Pattern Logic
for (int i=1 ; i<=7 ; i++)
+ + + + + {
+
for (int j=1 ; j<=5 ; j++){
+
+ + + + + if(i==1||i==4||i==7||(j==1&&i<=4)||(j==5&&i>=4))
+ [Link]("*");
+ else
+ + + + + [Link](" ");
}
[Link]();
}
Pattern Logic
for (int i=1 ; i<=7 ; i++){
+ + + + + for (int j=1 ; j<=5 ; j++){
+
if(i==1||j==1||i==4||i==7||(j==5&&i>=4))
+
+ + + + + [Link]("*");
+ + else
+ + [Link](" ");
+ + + + + }
[Link]();
}
Pattern Logic
for (int i=1 ; i<=7 ; i++){
for (int j=1 ; j<=5 ; j++){
+ + + + + +
if(i==1||j==7-i)
+
+ [Link]("*");
+ else
+ [Link](" ");
+ }
[Link]();
}
Pattern Logic
+ + + + + for (int i=1 ; i<=7 ; i++){
+ + for (int j=1 ; j<=5 ; j++){
+ +
if(i==1||i==4||i==7||j==1||j==5)
+ + + + +
+ + [Link]("*");
+ + else
+ + + + + [Link](" ");
}
[Link]();
}
Pattern Logic
+ + + + + for (int i=1 ; i<=7 ; i++){
+ + for (int j=1 ; j<=5 ; j++){
+ +
if(i==1||i==4||i==7||(j==1&&i<=4)||j==5)
+ + + + +
+ [Link]("*");
+ else
+ + + + + [Link](" ");
}
[Link]();
}
Pattern Logic
+ + + + for (int i=1 ; i<=7 ; i++){
+ + for (int j=1 ; j<=7 ; j++){
+ +
if(j==1||(i==1&&j<7)||(i==7&&j<7)||(j==7&&i>1&&i<
+ +
+ + 7))
+ + + + [Link]("+");
else
[Link](" ");
}
[Link]();
}
Pattern Logic
+ + + + + for (int i=1 ; i<=7 ; i++){
+ for (int j=1 ; j<=7 ; j++){
+
if(j==1||i==1||i==4||i==7)
+ + + + +
+ [Link]("+");
+ else
+ + + + + [Link](" ");
}
[Link]();
}
Pattern Logic
+ + + + + for (int i=1 ; i<=7 ; i++){
+ for (int j=1 ; j<=7 ; j++){
+
if(j==1||i==1||i==4)
+ + + + +
+ [Link]("+");
+ else
+ [Link](" ");
}
[Link]();
}
Pattern Logic
+ + + + + for (int i=1 ; i<=7 ; i++){
+ for (int j=1 ; j<=7 ; j++){
+
if(j==1||i==1||i==7||(j==7&&i>=4)||(i==4&&j>=4))
+ + +
+ + [Link]("+");
+ + else
+ + + + + [Link](" ");
}
[Link]();
}
Pattern Logic
for (int i=1 ; i<=7 ; i++){
+ + for (int j=1 ; j<=7 ; j++){
+ +
if(j==1||i==4||j==7)
+ +
+ + + + + [Link]("+");
+ + else
+ + [Link](" ");
+ + }
[Link]();
}
Pattern Logic
for (int i=1 ; i<=7 ; i++){
+ + + + + for (int j=1 ; j<=7 ; j++){
+
if(i==1||j==4||i==7)
+
+ [Link]("+");
+ else
+ [Link](" ");
+ + + + + }
[Link]();
}
Pattern Logic
for (int i=1 ; i<=7 ; i++){
+ + + + + for (int j=1 ; j<=7 ; j++){
+
if(i==1||j==4||i==j+3)
+
+ + [Link]("+");
+ + else
+ [Link](" ");
}
[Link]();
}
Pattern Logic
for (int i=1 ; i<=7 ; i++){
+ + for (int j=1 ; j<=7 ; j++){
+ +
if(j==1||(j==6-i)||(i==2+j))
+ +
+ [Link]("+");
+ + else
+ + [Link](" ");
+ + }
[Link]();
}
Pattern Logic
for (int i=1 ; i<=7 ; i++)
{
+
for (int j=1 ; j<=7 ; j++)
+
+ {
+ if(j==1||i==7)
+ [Link]("+");
+ + + + + else
[Link](" ");
}
[Link]();
}
Pattern Logic
for (int i=1 ; i<=7 ; i++)
{
+ +
for (int j=1 ; j<=7 ; j++)
+ + + +
+ + + + {
+ + + if(j==1||j==7||(i==j&&j<=4)||(j==8-i&&j>4))
+ + [Link]("+");
+ + else
+ + [Link](" ");
}
[Link]();
}
Pattern Logic
for (int i=1 ; i<=7 ; i++)
{
+ +
for (int j=1 ; j<=7 ; j++)
+ + +
+ + + {
+ + + if(j==1||j==7||(i==j))
+ + + [Link]("+");
+ + + else
+ + [Link](" ");
}
[Link]();
}
Pattern Logic
1 for (int i=1 ; i<=5 ; i++)
12 {
123
for (int j=1 ; j<=i ; j++){
1234
12345 [Link](j);
}
[Link]();
}
Pattern Logic
1 for (int i=1 ; i<=5 ; i++)
21 {
321
for (int j=i ; j>=1 ; j--){
4321
54321 [Link](j);
}
[Link]();
}
Pattern Logic
12345 for (int i=5 ; i>=1 ; i--)
1234 {
123
for (int j=1 ; j<=i ; j++){
12
1 [Link](j);
}
[Link]();
}
Pattern Logic
12345 for (int i=1 ; i<=5 ; i++)
2345 {
345
for (int j=i ; j<=5 ; j++){
45
5 [Link](j);
}
[Link]();
}
Pattern Logic
5 for (int i=5 ; i>=1 ; i--){
54 for (int j=5 ; j>=i ; j--){
543
[Link](j);
5432
54321 }
[Link]();
}
Pattern Logic
5 for (int i=5 ; i>=1 ; i--){
45 for (int j=i ; j<=5 ; j++){
345
[Link](j);
2345
12345 }
[Link]();
}
Pattern Logic
54321 for (int i=1 ; i<=5 ; i++)
5432 {
543
for (int j=5 ; j>=i ; j--){
54
5 [Link](j);
}
[Link]();
}
Pattern Logic
54321 for (int i=5 ; i>=1 ; i--)
4321 {
321
for (int j=i ; j>=1 ; j--){
21
1 [Link](j);
}
[Link]();
}
Pattern Logic
1 for (int i=1 ; i<=5 ; i++)
22 {
333
for (int j=1 ; j<=i ; j++){
4444
55555 [Link](i);
}
[Link]();
}
Pattern Logic
11111 for (int i=1 ; i<=5 ; i++)
2222 {
333
for (int j=i ; j<=5 ; j++){
44
5 [Link](i);
}
[Link]();
}
Pattern Logic
5 for (int i=5 ; i>=1 ; i--)
44 {
333
for (int j=i ; j<=5 ; j++){
2222
11111 [Link](i);
}
[Link]();
}
Pattern Logic
for (int i=5 ; i>=1 ; i--)
55555 {
4444
for (int j=1 ; j<=i ; j++)
333
22 {
1 [Link](i);
}
[Link]();
}
Pattern Logic
int k=1;
for (int i=1 ; i<=5 ; i++)
1
{
23
456 for (int j=1; j<=i ; j++)
7891 {
23456 [Link](k++);
if(k>9)
k=1;
}
[Link]();
}
Pattern Logic
int k=1;
for (int i=5 ; i>=1 ; i--)
12345
{
6789
123 for (int j=1; j<=i ; j++)
45 {
6 [Link](k++);
if(k>9)
k=1;
}
[Link]();
}
Pattern Logic
for (int i=1 ; i<=5 ; i++)
{
1
for (int j=i ; j<5 ; j++){
21
321 [Link](" ");
4321 }
54321 for (int k=i ; k>=1 ; k--){
[Link](k);
}
[Link]();
}
Pattern Logic
for (int i=5 ; i>=1 ; i--){
for (int j=i ; j<5 ; j++){
12345
[Link](" ");
1234
123 }
12 for (int k=1 ; k<=i ; k++){
1 [Link](k);
}
[Link]();
}
Pattern Logic
for (int i=1 ; i<=5 ; i++){
for (int j=1 ; j<i ; j++){
12345
[Link](" ");
2345
345 }
45 for (int k=i ; k<=5 ; k++){
5 [Link](k);
}
[Link]();
}
Pattern Logic
for (int i=5 ; i>=1 ; i--){
for (int j=1 ; j<i ; j++){
5
[Link](" ");
45
345 }
2345 for (int k=i ; k<=5 ; k++){
12345 [Link](k);
}
[Link]();
}
Pattern Logic
for (int i=5 ; i>=1 ; i--){
for (int j=1 ; j<i ; j++){
5
[Link](" ");
54
543 }
5432 for (int k=5 ; k>=i ; k--){
54321 [Link](k);
}
[Link]();
}
Pattern Logic
for (int i=5 ; i>=1 ; i--){
for (int j=i ; j<5 ; j++){
54321
[Link](" ");
4321
321 }
21 for (int k=i ; k>=1 ; k--){
1 [Link](k);
}
[Link]();
}
Pattern Logic
for (int i=1 ; i<=5 ; i++)
{
54321
for (int j=1 ; j<i ; j++){
5432
543 [Link](" ");
54 }
5 for (int k=5 ; k>=i ; k--){
[Link](k);
}
[Link]();
}
Pattern Logic
for (char i='A' ; i<='E' ; i++)
A {
BA
for (char j=i ; j>='A' ; j--){
CBA
DCBA [Link](j);
EDCBA }
[Link]();
}
Pattern Logic
for (char i='E' ; i>='A' ; i--)
ABCDE {
ABCD
for (char j='A' ; j<=i ; j++){
ABC
AB [Link](j);
A }
[Link]();
}
Pattern Logic
for (char i='A' ; i<='E' ; i++){
ABCDE for (char j=i ; j<='E' ; j++){
BCDE
[Link](j);
CDE
DE }
E [Link]();
}
Pattern Logic
E for (char i='E' ; i>='A' ; i--)
ED {
EDC
for (char j='E' ; j>=i ; j--){
EDCB
EDCBA [Link](j);
}
[Link]();
}
Pattern Logic
for (char i='E' ; i>='A' ; i--){
E for (char j=i ; j<='E' ; j++){
DE [Link](j);
CDE
}
BCDE
ABCDE [Link]();
}
attern Logic
EDCBA for (char i='A' ; i<='E' ; i++){
EDCB for (char j='E' ; j>=i ; j--){
EDC
[Link](j);
ED
E }
[Link]();
}
Pattern Logic
EDCBA for (char i='E' ; i>='A' ; i--){
DCBA for (char j=i ; j>='A' ; j--){
CBA
[Link](j);
BA
A }
[Link]();
}
Pattern Logic
for (int i=5 ; i>=1 ; i--){
***** for (int j=1 ; j<=i ; j++){
****
[Link]("*");
***
** }
* [Link]();
}
Pattern Logic
for (int i=1 ; i<=5 ; i++){
for (int j=i ; j<5 ; j++){
*
[Link](" ");
**
*** }
**** for (int k=1 ; k<=i ; k++){
***** [Link]("*");
}
[Link]();
}
Pattern Logic
for (int i=1 ; i<=5 ; i++){
for (int j=1 ; j<i ; j++){
*****
[Link](" ");
****
*** }
** for (int k=i ; k<=5 ; k++){
* [Link]("*");
}
[Link]();
}
Pattern Logic
for(int i=1 ; i<10 ; i++){
if(i<=5){
*
for(int j=1 ; j<=i ; j++){
**
*** [Link]("*");
**** }
***** [Link]();
**** }
*** else{
**
for(int k=i ; k<10 ; k++)
*
[Link]("*");
}
[Link]();
}
}
Pattern Logic
for(int i=1 ; i<10 ; i++){
if(i<=5){
for(int x=i ; x<=5 ; x++){
[Link](" ");
* }
** for(int j=1 ; j<=i ; j++)
*** [Link]("*");
**** [Link]();
*****
}
****
*** else{
** for(int x=i ; x>=5 ; x--)
* [Link](" ");
for(int k=i ; k<10 ; k++)
[Link]("*");
[Link]();
}
}
Pattern Logic
for(int i=1 ; i<10 ; i++)
{
if(i<=5){
*****
**** for(int j=i ; j<=5 ; j++){
*** [Link]("*");
** }
* [Link]();
** }
***
else{
****
***** for(int k=5 ; k<=i ; k++){
[Link]("*");
}
[Link]();
}
}
Pattern Logic
for(int i=1 ; i<10 ; i++){
if(i<=5){
for(int x=1 ; x<=i ; x++){
[Link](" ");
}
***** for(int j=i ; j<=5 ; j++){
**** [Link]("*");
*** }
**
[Link]();
*
** }
*** else{
**** for(int x=i ; x<10 ; x++){
***** [Link](" ");
}
for(int k=5 ; k<=i ; k++){
[Link]("*");
}
[Link]();
}
}
Pattern Logic
int k=1;
for (int i=5 ; i>=1 ; i--)
10101
{
0101
010 for (int j=1; j<=i ; j++){
10 [Link](k++%2);
1 }
[Link]();
}
Pattern Logic
for (int i=1 ; i<=5 ; i++){
1 for (int j=1; j<=i ; j++){
10
[Link](j%2);
101
1010 }
10101 [Link]();
}
Pattern Logic
for (int i=1 ; i<=5 ; i++){
1 for (int j=1; j<=i ; j++){
00
[Link](i%2);
111
0000 }
11111 [Link]();
}
Pattern Logic
for (int i=5 ; i>=1 ; i--){
11111 for (int j=1; j<=i ; j++){
0000
[Link](i%2);
111
00 }
1 [Link]();
}
Pattern Logic
int n=7;
* for(int i=1 ; i<=n ; i++)
**
{
* *
* * for(int j=1 ; j<=i ; j++)
* * {
* * if(i==1 || i==n || j==1 || j==i)
* * [Link]("*");
******** else
[Link](" ");
}
[Link]();
}
Pattern Logic
int n=7;
******** for(int i=n ; i>=1 ; i--)
* *
{
* *
* * for(int j=1 ; j<=i ; j++){
* * if(i==1 || i==n || j==1 || j==i)
* * [Link]("*");
** else
* [Link](" ");
}
[Link]();
}
Pattern Logic
for(int i=1 ; i<=7 ; i++)
{
********
for(int j=1 ; j<i ; j++){
* *
* * [Link](" ");
* * }
* * for(int k=i ; k<=7 ; k++){
* * if(i==1 || i==7 || k==i || k==7)
** [Link]("*");
*
else
[Link](" ");
}
[Link]();
}
Pattern Logic
for(int i=1 ; i<=7 ; i++)
{
*
for(int j=i ; j<7 ; j++){
**
* * [Link](" ");
* * }
* * for(int k=1 ; k<=i ; k++){
* * if(i==1 || i==7 || k==1 || k==i)
* * [Link]("*");
********
else
[Link](" ");
}
[Link]();
}
Pyramid Patterns
Pattern Logic
int n=7;
for(int i=1 ; i<=n ; i++)
*
{
***
***** for(int j=i ; j<n ; j++){
******* [Link](" ");
********* }
for(int k=1 ; k<=2*i-1 ; k++){
[Link]("*");
}
[Link]();
}
Pattern Logic
int n=7;
for(int i=1 ; i<=n ; i++){
for(int j=i ; j<n ; j++){
*
* * [Link](" ");
* * }
* * for(int k=1 ; k<=2*i-1 ; k++){
********* if(i==1 || i==n || k==1 || k==2*i-1)
[Link]("*");
else
[Link](" ");
}
[Link]();
}
Pattern Logic
1 int n=7;
222 for(int i=1 ; i<=n ; i++)
33333
{
4444444
555555555 for(int j=i ; j<n ; j++){
[Link](" ");
}
for(int k=1 ; k<=2*i-1 ; k++){
[Link](i);
}
[Link]();
}
A
M
Reverse Pyramid Patterns E
Pattern Logic E
int n=7; R
for(int i=n ; i>=1 ; i--) P
********* { E
*******
for(int j=i ; j<n ; j++){ T
*****
*** [Link](" ");
T
* }
E
for(int k=1 ; k<=2*i-1 ; k++){
C
[Link]("*");
H
}
N
[Link]();
O
}
L
O
Pattern Logic
G
int n=7; I
for(int i=n ; i>=1 ; i--) E
{ S
*********
* * for(int j=i ; j<n ; j++){
* * [Link](" ");
* * }
* for(int k=1 ; k<=2*i-1 ; k++){
if(i==1 || i==n || k==1 || k==2*i-1)
[Link]("*");
else
[Link](" ");
}
[Link]();
}
Pattern Logic
int n=7;
for(int i=n ; i>=1 ; i--)
{
555555555
4444444 for(int j=i ; j<n ; j++){
33333 [Link](" ");
222 }
1 for(int k=1 ; k<=2*i-1 ; k++){
[Link](i);
}
[Link]();
}
Pattern Logic
int r=6, c=1;
for(int i=0; i<r ; i++)
{
1
1 1 for(int s=1; s<r-i; s++) {
1 2 1 [Link](" ");
1 3 3 1 }
1 4 6 4 1 for(int j=0; j<=i; j++) {
1 5 10 10 5 1 if (j==0 || i==0)
c=1;
else
c=c*(i-j + 1)/j;
[Link]("%4d", c);
}
[Link]();
}
for (int i = 5; i >= 1; i--)
{
123454321
for (int j = 5 - i; j >= 1; j--){
1234321
12321 [Link](" ");
121 }
1 for (int j = 1; j <= i; j++){
[Link](j);
}
for (int j = i - 1; j >= 1; j--){
[Link](j);
}
[Link]();
}
Pattern Logic
int n=7;
for(int i=n ; i>=1 ; i--){
for(int j=i ; j<n ; j++){
543212345
4321234 [Link](" ");
32123 }
212 for(int k=i ; k>=1 ; k--){
1 [Link](k);
}
for(int l=1+1 ; l<=i ; l++){
[Link](l);
}
[Link]();
}
Pattern Logic
int n=7;
for(int i=1 ; i<=n ; i++){
for(int j=i ; j<n ; j++){
* [Link](" ");
*** }
***** for(int k=1 ; k<=2*i-1 ; k++){
******* [Link]("* ");
********* }
***********
[Link]();
*********
******* }
***** for(int i=n-1 ; i>=1 ; i--){
*** for(int j=i ; j<n ; j++){
* [Link](" ");
}
for(int k=1 ; k<=2*i-1 ; k++){
[Link]("* ");
}
[Link]();
}
}
Object Oriented Programming: is a concept of defining objects and establish the relation
between the objects.
Defining a class:
• In Java application, every statement belongs to class.
• Class has identity – to access.
Types of variables:
1. Static variables:
• A variable with static keyword inside the class outside to methods.
• Static variable stores common information of all objects.
• Static variable gets memory only once.
• Static variables can be accessed using class name.
2. Instance variables:
• A variable inside the class and outside to methods.
• Instance variables store specific information of object.
• Instance variables get separate memory inside every object.
• Instance variables can be accessed using object address.
3. Method parameters:
• Define variables inside method parenthesis.
• Parameters used to take input of method.
• Parameters get memory when method invokes.
• Parameters can be accessed directly and from the same method.
4. Local variables:
• A variable inside method.
• Local variables store processed information inside the object.
• Local variables get memory when we call the method.
• Local variables can be accessed directly.
A
M
E
E
R
P
E
T
T
E
C
H
N
O
L
O
G
I
E
Method: S
• Method is a block of instructions that performs a task.
• Method is called a sub program.
• Method takes input, process the input and returns the output.
Syntax Example
returntype identity(arguments) int add(int a, int b)
{ {
statements; int c=a+b;
} return c;
}
Static Method:
• Defining a method using static keyword.
• Static methods process static variables data.
• We can access static methods using class-name.
Instance Method:
• Defining a method without static keyword.
• Instance methods represents specific information of object.
• We can access instance methods using object-reference.
Classification of Methods: Based on taking input and returning output, methods are classified
into 4 types.
1. No arguments and No return values
2. With arguments and No return values
3. With arguments and with return values
4. No arguments and with return values
Biggest of 3 numbers:
import [Link];
public class Code {
public static void main(String[] args) {
Scanner scan = new Scanner([Link]);
[Link]("Enter 3 numbers : ");
int a = [Link]();
int b = [Link]();
int c = [Link]();
Display obj = new Display();
String res = [Link](a, b, c);
[Link](res);
}
}
class Display{
String big(int a, int b, int c)
{
if(a>b && a>c)
return "a is big";
else if(b>c)
return "b is big";
else
return "c is big";
}
}
Problem2:
1. Define class with name Code.
2. Define main() method in Code class.
3. Define class with name Wish and a static method toAll() inside the Wish class.
4. Invoke toAll() method from main() method.
Problem3:
1. Define class with name Code.
2. Define main() method in Code class.
3. Define a class with name Arithmetic and static method operations() inside the class.
4. Read 2 integers in main() method and pass to operations() method.
5. Perform all arithmetic operations and print results inside the method.
Problem4:
1. Define class with name Code.
2. Define main() method in Code class. A
3. Define class with name Person and a static method canVote() inside the class. M
4. canVote() method takes age as input and print message Can Vote or Not. E
5. Invoke canVote() method by passing input from main() method. E
R
Problem5: P
1. Define class with name Code. E
2. Define main() method and instance method abc() in Code class. T
3. Invoke abc() method from main() method.
T
E
Problem6:
C
1. Define class with name Code.
H
2. Define main() method and instance method isPerfect() method in Code class.
N
3. isPerfect() method takes integer input and display message Perfect or Not.
O
4. Read input using Scanner class and invoke the method isPerfect().
L
O
Problem7:
G
1. Define class with name Code and main() method..
I
2. Define a class with name Check and instance method isSymbol() inside that class. E
3. Read character from main() and pass to isSymbol() method. S
4. Display the character is Symbol or not inside the method.
Recursion
Recursion:
• Calling method itself is called Recursion.
• Invoking the method from the body of same method.
import [Link];
public class Code
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
while(true)
{
[Link]("1. Add");
[Link]("2. Subtract");
[Link]("3. Multiply");
[Link]("4. Divide");
[Link]("5. Quit");
[Link]("Enter your choice : ");
int ch = [Link]();
if(ch==1)
{
[Link]("Enter 2 numbers : ");
int a=[Link]();
int b=[Link]();
int c=a+b;
[Link](a + " + " + b + " = " + c);
}
else if(ch==2)
{
[Link]("Enter 2 numbers : ");
int a=[Link]();
int b=[Link]();
int c=a-b;
[Link](a + " - " + b + " = " + c);
}
else if(ch==3)
{
[Link]("Enter 2 numbers : ");
int a=[Link]();
int b=[Link]();
int c=a*b;
[Link](a + " * " + b + " = " + c);
}
else if(ch==4)
{
[Link]("Enter 2 numbers : ");
int a=[Link]();
int b=[Link]();
int c=a/b;
[Link](a + " / " + b + " = " + c);
}
else if(ch==5)
{
[Link]("End of Program");
[Link](1);
}
else
{
[Link]("Invalid Choice");
}
}
}
}
switch(ch){
case 1 : add(a,b);
break;
case 2 : subtract(a,b);
break;
case 3 : multiply(a,b);
break;
case 4 : divide(a,b);
break;
case 5 : [Link]("End of Program");
[Link](1);
default: [Link]("Invalid choice");
}
}
}
static void add(int a, int b)
{
[Link](a+" + "+b+" = "+(a+b));
}
static void subtract(int a, int b)
{
[Link](a+" - "+b+" = "+(a-b));
}
static void multiply(int a, int b)
{
[Link](a+" * "+b+" = "+(a*b));
}
static void divide(int a, int b)
{
[Link](a+" / "+b+" = "+(a/b));
}
}
Encapsulation(POJO) rules:
• We can protect the information of object in java by defining variables as private.
• One object cannot access the data of another object directly if it is private.
• Data sharing is possible using functionality in communication using setters and getters.
• Set method is used to send the values of object to another object.
• Get method is used to receive the values of object from another object.
import [Link];
public class Code
{
public static void main(String[] args)
{
Scanner scan = new Scanner([Link]);
[Link]("Enter x value : ");
int x = [Link]();
[Link]("Enter y value : ");
int y = [Link]();
[Link](x);
[Link](y);
[Link]("x value : " + [Link]());
[Link]("y value : " + [Link]());
}
}
class Test
{
private static int x, y;
public static int getX()
{
return Test.x;
}
public static void setX(int x)
{
Test.x = x;
}
public static int getY()
{
return Test.y;
}
public static void setY(int y)
{
Test.y = y;
}
}
Constructor:
• Define a method with the same name of class.
• Return types not allowed for constructor.
• We invoke constructor every time in object creation process
Parameterized constructor:
• A constructor with parameters.
• Parameterized constructors are used to set values to instance variables at the time of
Objet creation only.
@Override
public String toString()
{
return "Details : " + [Link] + " , " + [Link] + " , " + [Link];
}
}
Program2:
• Create Student class with private instance variables id, name, course and fee.
• Define parameterized constructor to initialize the object.
• Read input values from the user and create object of Student class.
• Display details by overriding toString() method
Java - Arrays
Array:
• Primitive variable can store only one value at a time.
• Array variable can store more than one element but of same type.
For example marks[ ] = {56, 45, 90, 67, 56};
• Array elements get side by side memory locations in a block.
• Array elements can be accessed using their index starts from 0 to length-1;
Memory representation:
Note: If we just declare the array variable instead of allocating the memory, we cannot find the
length. The following program raises error.
It is recommended to check the array contains elements or not before accessing elements:
class Code
{
public static void main(String[] args)
{
int[] arr = {4, 2, 8, 9, 1, 6, 7, 4};
if([Link]==0)
{
[Link]("Empty array");
}
else
{
int first = arr[0];
[Link]("First element is : " + first);
}
}
}
Java – Program to check first and last elements of Array are same or not
class Code {
public static void main(String[] args)
{
int[] arr = {4, 2, 8, 9, 1, 6, 7, 5};
if(arr[0]==arr[[Link]-1])
[Link]("Same");
else
[Link]("Not Same");
}
}
class Code
{
public static void main(String[] args) {
int[] arr = {6, 2, 8, 9, 1, 6, 7, 5};
int n = [Link];
if(n%2!=0)
[Link]("Mean : " + arr[n/2]);
else{
int x = arr[n/2-1];
int y = arr[n/2];
[Link]("Mean : " + ((x+y)/2));
}
}
}
class Code
{
public static void main(String[] args)
{
int[] arr = {4, 2, 8, 9, 1, 6, 7, 5};
[Link]("Array elements are : ");
for (int i=0 ; i<=[Link]-1 ; i++)
{
[Link](arr[i]);
}
}
}
Display default values: As soon as memory allocated to array, all the locations initialized with
default values based on type of array
class Code {
public static void main(String[] args)
{
int[] arr = {4, 2, 8, 9, 1, 6, 7, 5};
[Link]("Array elements using for-each loop : ");
for (int x : arr){
[Link](x);
}
}
}
Display Array element which are greater than average of all elements:
class Code {
public static void main(String[] args)
{
int[] arr = {6, 3, 9, 1, 2, 8, 4, 5};
double sum=0;
for (int x : arr){
sum = sum + x;
}
double avg = sum/[Link];
for(int x : arr){
if(x>avg)
[Link](x);
}
}
}
int n = arr[i];
int sum=0;
for (int j=1 ; j<n ; j++)
{
if(n%j==0)
sum=sum+j;
}
if(n==sum)
[Link](n + " is perfect");
}
}
}
Program to Swap the First even number and Last odd number in the array:
class Code{
public static void main(String[] args) {
int[] arr = {5, 2, 3, 8, 1, 4, 6};
int i, j;
for (i=0 ; i<[Link] ; i++){
if(arr[i]%2==0)
break;
}
for (j=[Link]-1 ; j>=0 ; j--){
if(arr[j]%2!=0)
break;
}
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
[Link]("Array after swap : ");
for (i=0 ; i<[Link] ; i++){
[Link](arr[i]);
}
}
}
}
}
import [Link] ;
class Demo
{
public static void main(String[] args)
{
Scanner scan = new Scanner([Link]);
[Link]("Enter array size : ");
int n = [Link]();
int arr[ ] = new int[n];
Arrays class:
• Java library class belongs to [Link] package.
• Arrays class providing set of method to process array elements.
• Some of the methods as follows.
static void sort(int[] a) Sorts the specified array into ascending numerical order.
static String toString(int[] a) Returns a string representation of the contents of the
specified array.
static <T> List<T> asList(T... a) Returns a fixed-size list backed by the specified array.
A
M
static int binarySearch(int[] a, int Searches the specified array of ints for the specified
E
key) value using the binary search algorithm
E
static int[] copyOfRange(int[] Copies the specified range of the specified array into a
R
original, int from, int to) new array.
P
static boolean equals(int[] a, int[] Returns true if the two specified arrays of ints are equal
E
a2) to one another.
T
static void fill(int[] a, int val) Assigns the specified int value to each element of the
specified array of ints. T
E
Sort array elements and display as String: C
import [Link]; H
import [Link]; N
class SortArray O
{ L
public static void main(String[] args) O
G
{
I
Random rand = new Random();
E
int arr[] = new int[5];
S
for(int i=0 ; i<5 ; i++)
{
arr[i] = [Link](100);
}
[Link]("Before sort : " + [Link](arr));
[Link](arr);
[Link]("After sort : " + [Link](arr));
}
}
import [Link] ;
import [Link] ;
class Search
{
public static void main(String[] args)
{
Scanner scan = new Scanner([Link]);
[Link]("Enter array size : ");
int n = [Link]();
int arr[ ] = new int[n];
boolean found=false;
int low=0;
int high=n-1;
while(low <= high)
{
int mid = (low+high)/2;
if(ele < arr[mid])
high = mid-1;
else if(ele > arr[mid])
low = mid+1;
else if(ele == arr[mid]){
[Link]("Element found at location : " + mid);
found = true;
break;
}
}
if(!found)
[Link]("Element not found");
}
}
Bubble sort:
• Sorting is the concept of arranging elements in the array either in ascending order or in
descending order.
• To sort the array, n number of algorithms is given such as Bubble Sort, Insertion Sort,
and Selection Sort and so on.
• Bubble sort is a simple sorting technique where elements can be sorted using their
index.
import [Link] ;
class Demo {
public static void main(String[] args) {
Scanner scan = new Scanner([Link]);
[Link]("Enter array size : ");
int n = [Link]();
int arr[ ] = new int[n];
Program to create Array with only even numbers from the given array:
class Code
{
public static void main(String[] args) {
int[] arr = {5, 2, 3, 8, 1, 4, 6, 7, 9};
int count=0;
for (int i=0 ; i<[Link] ; i++){
if(arr[i]%2==0)
count++;
}
int[] evens = new int[count];
int j=0;
for (int i=0 ; i<[Link] ; i++){
if(arr[i]%2==0){
evens[j]=arr[i];
j++;
} A
} M
E
[Link]("Even numbers array : ");
E
for (int i=0 ; i<[Link] ; i++){
R
[Link](evens[i]);
P
}
E
}
T
}
T
Program to divide the given array into 2 arrays with even numbers and odd numbers: E
class Code C
{ H
public static void main(String[] args) N
{ O
int[] arr = {5, 2, 3, 8, 1, 4, 6, 7, 9}; L
int count=0; O
for (int i=0 ; i<[Link] ; i++) G
{ I
if(arr[i]%2==0) E
count++; S
}
int[] evens = new int[count];
int[] odds = new int[[Link]-count];
int x=0, y=0;
for (int i=0 ; i<[Link] ; i++)
{
if(arr[i]%2==0)
{
evens[x]=arr[i];
x++;
}
else
{
odds[y]=arr[i];
y++;
}
}
[Link]("Even numbers array : ");
for (int i=0 ; i<[Link] ; i++)
{
[Link](evens[i]);
}
[Link]("Odd numbers array : ");
for (int i=0 ; i<[Link] ; i++)
{
[Link](odds[i]);
}
}
}
Program to arrange even numbers to left side and odd numbers to right side of Array:
import [Link];
class Code {
public static void main(String[] args) {
int[] arr = {7, 2, 9, 8, 4, 1, 2, 6, 5};
[Link]("Given array : " + [Link](arr));
int i=0;
int j=[Link]-1;
while(i<j){
while(true){
if(arr[i]%2!=0)
break;
else
i++;
}
while(true){
if(arr[j]%2==0)
break;
else
j--;
}
if(i<j){
int t=arr[i];
arr[i]=arr[j];
arr[j]=t;
}
i++; A
j--; M
} E
[Link]("Result array : " + [Link](arr)); E
} R
} P
E
Program to check 2 arrays are equal or not: T
import [Link];
T
class Code
E
{
C
public static void main(String[] args) {
H
int[] a1 = {3, 4, 5, 6, 7};
N
int[] a2 = {3, 4, 5, 6, 7};
O
String s1 = [Link](a1); L
String s2 = [Link](a2); O
if([Link](s2)) G
[Link]("Arrays are equal"); I
else E
[Link]("Arrays are not equal"); S
}
}
while(i<j)
{
while(true)
{
if(arr[i]==0)
break;
else
i++;
}
while(true)
{
if(arr[j]!=0)
break;
else
j--;
}
if(i<j)
{
int t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
i++;
j--;
}
[Link]("Resultant array: " + [Link](arr));
}
}
if(big<arr[i])
big=arr[i];
}
[Link]("Largest Difference between elements : " + (big-small));
}
}
Program to find the index difference between smallest and largest elements:
import [Link];
class Code
{
public static void main(String[] args)
{
int[] arr = {5, 7, 3, 8, 6, 9, 4};
[Link]("Given array : " + [Link](arr));
int small=arr[0];
int big=arr[0];
int x=0, y=0;
for (int i=1 ; i<[Link] ; i++)
{
if(small>arr[i])
{
small=arr[i];
x=i;
}
if(big<arr[i])
{
big=arr[i];
y=i;
}
}
[Link]("Index difference between Small and Large Elements : " +
[Link](x-y));
}
}
Two-dimensional Array:
• 2D arrays are also called Matrix in java.
• 2D arrays are used to store and process information of 2-dimensional format (rows and
columns).
In Java, you can create a two-dimensional array using the following syntax:
dataType[][] arrayName = new dataType[rows][columns];
You can also initialize the array with values right away:
int[][] matrix = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
}
}
int m = [Link]();
[Link]("Column size : ");
int n = [Link]();
int arr[][] = new int[m][n];
[Link]("Enter " + (m*n) + " elements : ");
for (int i=0 ; i<m ; i++){
for(int j=0 ; j<n ; j++){
arr[i][j] = [Link]();
}
}
[Link](m + "X" + n + " matrix is : ");
for (int i=0 ; i<m ; i++){
for(int j=0 ; j<n ; j++){
[Link](arr[i][j] + " ");
}
[Link]();
}
}
}
int sum = 0;
for (int i = 0; i < [Link]; i++) {
for (int j = 0; j < twoDimArray[i].length; j++) {
sum += twoDimArray[i][j];
}
}
[Link]("Sum of all elements: " + sum);
}
}
int maxRowSum = 0;
Addition of 2 matrixes:
import [Link];
public class Logic{
public static void main(String args[]) {
Scanner scan = new Scanner([Link]);
int A[][] = new int[2][2];
int B[][] = new int[2][2];
int C[][] = new int[2][2];
[Link]("Enter 4 elements into A : ");
for (int i=0 ; i<2 ; i++)
}
}
}
[Link]("Mutliplied matrix is : ");
for (int i=0 ; i<2 ; i++){
for(int j=0 ; j<2 ; j++){
[Link](C[i][j] + " ");
}
[Link]();
}
}
}
Transpose of a Matrix:
public class Logic{
public static void main(String args[]) {
int A[][] = {{10,20,30},{40,50,60},{70,80,90}};
[Link]("Input Matrix is : ");
for (int i=0 ; i<3 ; i++){
for(int j=0 ; j<3 ; j++){
[Link](A[i][j] + " ");
}
[Link]();
}
for (int i=0 ; i<3 ; i++){
for(int j=0 ; j<3 ; j++){
if(i<j){
int temp = A[i][j];
A[i][j] = A[j][i];
A[j][i] = temp;
}
}
}
[Link]("Transposed Matrix is : ");
for (int i=0 ; i<3 ; i++){
for(int j=0 ; j<3 ; j++){
[Link](A[i][j] + " ");
}
[Link]();
}
}
}
Strings in Java
What is Character?
Character is any symbol represent with single quotes (alphabets, digits, symbols);
Example: A-Z , a-z, 0-9, $, #, @, !, ^, &, ......
A
M
ASCII value table E
A-65 a-97 0-48 #-35 E
B-66 b-98 1-49 $-36 R
... .... .. ... P
... .... .. ... E
T
... .... .. ...
Z-90 z-122 9-57 ... T
E
Identify vowels from below? C
X A V I O U R H
N
O
L
O
Can we assign character to integer variable?
G
We can directly assign character to integer called “Implicit Cast”. The ASCII value will
I
store into integer variable.
E
S
Auto conversion Manually casting
char ch='a'; char ch='a';
int x=ch; int x=(int)ch;
print(x); print(x);
int alphabets = 0;
int digits = 0;
int symbols = 0;
Convert String object into character array: String class providing toCharArray() method for
this conversion.
class Code {
public static void main(String[] args) {
String str = "Coding";
char[] arr = [Link]();
[Link]("Elements :");
for(char ele : arr)
[Link](ele);
}
}
c1++;
else if(ch>='0'&&ch<='9')
c2++;
else
c3++;
}
[Link]("Alphabets : " + c1);
[Link]("Digits : " + c2);
[Link]("Symbols : " + c3);
}
}
}
[Link]("String is : " + s);
[Link]("Sum of Digits : " + sum);
}
}
if(ch>='0'&&ch<='9'){
int x=(int)(ch-48);
if(x>n)
n=x;
}
}
[Link]("Higher digit in String : " + n);
}
}
Program to display First and Last characters of each string in the array:
class Code
{
public static void main(String[] args)
{
String[] arr = {"java", "jsp", "servlets", "hibernate", "springBoot"};
for (int i=0 ; i<[Link] ; i++){
String s = arr[i];
[Link](s+" : "+[Link](0)+","+[Link]([Link]()-1));
}
}
}
Program to count number of words in the given string without split() method.
class Code { A
public static void main(String[] args) { M
String s = "This is core java test"; E
int spaces=0; E
R
for (int i=0 ; i<[Link]() ; i++){
P
if([Link](i)==' ') E
spaces++; T
}
T
[Link]("String is : " + s); E
[Link]("Words count : " + (spaces+1)); C
} H
} N
O
L
Program to check two strings are Anagrams or not: O
We can say if two strings are an anagram of each other if they contain the same G
characters but at different orders. For example, army & mary I
E
import [Link]; S
class Code
{
public static void main(String[] args) {
String s1 = "army";
String s2 = "mary";
[Link]("Check Anagram or not : "+ isAnagram(s1, s2));
}
public static boolean isAnagram(String s1, String s2){
char[] a1 = [Link]().toCharArray();
char[] a2 = [Link]().toCharArray();
[Link](a1);
[Link](a2);
return [Link](a1, a2);
}
}
}
[Link]("Given String : " + str);
[Link]("Resultant string : " + res);
}
}
permutations(n, nrem);
}
}
}
Program to check how many times the given sub string is present:
class Code
{
public static void main(String[] args) {
String s = "abcaabcaaabcabacabcaabcaabc";
Program to remove sub string in the given string without library method:
class Code
{
public static void main(String[] args)
{
String s1 = "This is Core Java String"; A
String s2 = ""; M
E
String sub = "Core";
E
int n=[Link](); R
for (int i=0 ; i<[Link]() ; i++) P
{ E
T
char ch = [Link](i);
if(ch != [Link](0)) T
E
{ C
s2 = s2 + ch; H
} N
O
else
L
{ O
String match = [Link](i, i+n); G
I
i=i+n;
E
} S
}
[Link]("Given String : " + s1);
[Link]("After removing Sub string : " + s2);
}
}