-
String :
Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects. The Java platform provides the String class to create and manipulate strings.
Classification of java:
#immutable
#String constant pool.
Program:
package demo;
public class newone {
public static void main(String[] args) { // TODO Auto-generated method stub String s="java"; String s1="java"; boolean R= true; if(s==s1) { System.out.println(R); } }}
output:
true
-
Pattern program :
Pattern programs are nothing but patterns consisting of numbers, alphabets or symbols in a particular form.
Program:
package demo;
public class Rowandcolumn {
public static void main(String[] args) { // TODO Auto-generated method stub num(); for(char r='A';r<='E';r++) { for(char c=r;c<='E';c++) { System.out.print(c+" "); } System.out.println(); } }static void num() {
int S=1;
for(int r=1;r<=5;r++) {
for(int c=1;c<=6-r;c++) {
System.out.print(S+” “);
S++;} System.out.println(); }}
}Output:
1 2 3 4 5
6 7 8 9
10 11 12
13 14
15
A B C D E
B C D E
C D E
D E
E -
Homework Program:
sir give a homework program to print our name in pattern.
program:
package demo;
public class patternS {
public static void main(String args[]) {
S();
O();
B();
H();
I();
K();
A();
}
static void S() {
for(int i=0;i<7;i++) {
for(int j=0;j<7;j++) {
if(i==0 || i==3 || i==6 || i==1&&j==0 || i==2&&j==0 || i==4&&j==6 || i==5&&j==6 ) {
if(i==0&&j==0 || i==4&&j==0 ||i==3&&j==6 ||i==3&&j==0|| i==6&&j==6){
System.out.print(” “);
}
else {
System.out.print(“* “);
}
}
else {
System.out.print(” “);
}} System.out.println(); } System.out.println("-------------------"); } static void O(){ for(int i=0;i<7;i++) { for(int j=0;j<7;j++) { if(i==0 || i==6 || j==0 || j==6) { if(i==0&&j==0 || i==6&&j==0 ||i==6&&j==6|| i==0&&j==6) { System.out.print(" "); } else { System.out.print("* "); } } else { System.out.print(" "); } } System.out.println(); } System.out.println("-------------------"); }static void B() {
for(int i=0;i<7;i++) {
for(int j=0;j<7;j++) {
if(j== 0|| i==0 || i==3 || i==6 || i==1&&j==6 || i==2&&j==6 || i==4&&j==6 || i==5&&j==6) {
if(i==0&&j==6 || i==3&&j==6 || i==6&&j==6) {
System.out.print(” “);
}
else {
System.out.print(“* “);
}
}
else {
System.out.print(” “);
}
}System.out.println();}
System.out.println(“——————-“);
}static void H() {
for(int i=0;i<7;i++) {
for(int j=0;j<7;j++) {
if(j==0 || j==6 || i==3) {
System.out.print(“* “);
}else {
System.out.print(” “);
}
}
System.out.println();
}
System.out.println(“——————-“);
}static void I() {
for(int i=0;i<7;i++) {
for(int j=0;j<7;j++) {
if(i==0 || i==6 || j==3) {
System.out.print(“* “);}else { System.out.print(" "); } } System.out.println(); } System.out.println("-------------------");}
static void K() {
for(int i=0;i<7;i++) {
for(int j=0;j<4;j++) {
if(j==0 || i==3&&j==0 || i==2&&j==1 || i==1&&j==2 || i==0&&j==3 || i==4&&j==1 || i==5&&j==2 || i==6&&j==3) {
System.out.print(“* “);
}else {
System.out.print(” “);
}
}
System.out.println();
}
System.out.println(“——————-“);
}static void A() {
for(int i=0;i<7;i++) {
for(int j=0;j<7;j++) {
if(j==0 || i==0 || j==6 || i==3) {
if(i==0&&j==0 || i==0&&j==6){
System.out.print(” “);
}
else{
System.out.print(“* “);
}
}
else {
System.out.print(” “);
}
}
System.out.println();
}
System.out.println(“———————“);}
}
output:
* * * * * * * * * * * * * * * * * * * * * ------------------- * * * * * * * * * * * * * * * * * * * * ------------------- * * * * * * * * * * * * * * * * * * * * * * * * * * ------------------- * * * * * * * * * * * * * * * * * * * ------------------- * * * * * * * * * * * * * * * * * * * ------------------- * * * * * * * * * * * * * ------------------- * * * * * * * * * * * * * * * * * * * * * * --------------------- -
Todays Program:
Diagonal matrix adding Program:
Today we did program in Diagonal matrix adding it means adding the centre matrix.
Example={(0,0),(1,1),(2,2)}
Program:
package demo;
public class Digonalsadding {
public static void main(String[] args) { // TODO Auto-generated method stub int[][]arr= { {1,2,3}, {4,5,6}, {7,8,9} }; int dtotal=0; for(int i=0;i<arr.length;i++) { for(int j=0;j<arr[i].length;j++) { if(i==j){ dtotal=dtotal+arr[i][j]; } } } System.out.println(dtotal); }}
output:
15
Transposing the given number:
Transfering the given number
Progtam:
package demo;
//import java.util.Arrays;
public class Trans {
public static void main(String[] args) { // TODO Auto-generated method stub int[][]Trans= { {1,2,3}, {4,5,6}, {7,8,9} }; for(int i=0;i<Trans.length;i++) { for(int j=0;j<Trans[i].length;j++) { //Trans[i][j]=Trans[i][j]; } } for(int i=0;i<Trans.length;i++) { for(int j=0;j<Trans[i].length;j++) { System.out.print(Trans[j][i]+" "); } System.out.println(); } }}
output:
1 4 7
2 5 8
3 6 9 -
Bubble sort:
Bubble sort is a simple sorting algorithm that compares adjacent elements of an array and swaps them if the element on the right is smaller than the one on the left.
Program:
package demo;
import java.util.Arrays;
public class Bubblesort {
public static void main(String[] args) { // TODO Auto-generated method stub int arr[]= {30,20,10,60,40}; for(int j=1;j<arr.length;j++) { for(int i=0;i<arr.length-j;i++) { if(arr[i]>arr[j]) { int temp=arr[i+1]; arr[i+1]=arr[i]; arr[i]=temp; } } } System.out.println(Arrays.toString(arr)); } }output:
[30, 10, 20, 40, 60]
Jagged array:
Jagged Arrays are special types of Multidimensional arrays which have variable number of columns. It is an array of arrays where each element is an array and can be of a different size.
program:
package demo;
import java.util.Scanner;
public class Jaggedarray {
public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc=new Scanner(System.in); System.out.println("Enter the row"); int row=sc.nextInt(); int a[][]=new int[row][]; for(int i=0;i<a.length;i++) { a[i]=new int[sc.nextInt()]; } for(int i=0;i<a.length;i++) { for(int j=0;j<a[i].length;j++) { a[i][j]=sc.nextInt(); } } for(int i=0;i<a.length;i++) { for(int j=0;j<a[i].length;j++) { System.out.print(a[i][j]+" "); }System.out.println(); } } }output:
Enter the row
3
3
2
1
1
1
1
1
1
1
1 1 1
1 1
1 -
Two Dimensional array:
In Java, a multi-dimensional array is nothing but an array of arrays. 2D array − A two-dimensional array in Java is represented as an array of one-dimensional arrays of the same type. Mostly, it is used to represent a table of values with rows and columns − Int[][] myArray = {{10, 20, 30}, {11, 21, 31}, {12, 22, 32} }
Program 1:
package demo;
public class Twodimensional1 {
public static void main(String[] args) { // TODO Auto-generated method stub int[][] arr=new int[3][3]; for(int row=0;row<arr.length;row++) { for(int col=0;col<arr.length;col++) { arr[row][col]=1; } } for(int row=0;row<arr.length;row++) { for(int col=0;col<arr.length;col++) { System.out.print(arr[row][col]+" "); } System.out.println(); } } }output:
1 1 1
1 1 1
1 1 1Program 2:
package demo;
public class twodimensional2 {
public static void main(String[] args) { // TODO Auto-generated method stub int[][] arr=new int[3][3]; int value=1; for(int row=0;row<arr.length;row++) { for(int col=0;col<arr.length;col++) { arr[row][col]=value; value++; } } for(int row=0;row<arr.length;row++) { for(int col=0;col<arr.length;col++) { System.out.print(arr[row][col]+" "); } System.out.println(); } } }output:
1 2 3
4 5 6
7 8 9Program 3:
package demo;
import java.util.Scanner;
public class twodimensional3 {
public static void main(String[] args) { // TODO Auto-generated method stub int value=1; Scanner sc=new Scanner(System.in); System.out.println("Enter the number"); int row=sc.nextInt(); int col=sc.nextInt(); int[][] arr=new int[row][col]; for(int r=0;r<arr.length;r++) { for(int c=0;c<arr.length;c++) { arr[r][c]=value; value++; } } for(int r=0;r<arr.length;r++) { for(int c=0;c<arr.length;c++) { System.out.print(arr[r][c]+" "); } System.out.println(); } } }output:
3
3
1 2 3
4 5 6
7 8 9 -
Todays Program:
Binary Search:
Binary search is an efficient algorithm for finding an item from a sorted list of items. It works by repeatedly dividing in half the portion of the list that could contain the item, until you’ve narrowed down the possible locations to just one.
Why it is called binary search?
It is called binary because it splits the array into two halves as part of the algorithm.
Program:
package demo;
public class Binarysearch {
public static void main(String[] args) { // TODO Auto-generated method stub int []bookarr= {0,1,2,3,4,5,6,7}; int firstpage=0; int lastpage=bookarr.length; int midpage=(firstpage+lastpage)/2; int tofind=6; boolean found=false; while(firstpage<lastpage) { if(bookarr[midpage]<tofind) { firstpage=midpage+1; } else if(bookarr[midpage]==tofind) { System.out.println("the number is found on arrindex "+bookarr[midpage]); found=true; break; } else { lastpage=midpage-1; } midpage=(firstpage+lastpage)/2; } if(found==false) { System.out.println("not found"); } } }Output:
the number is found on arrindex 6
Program for finding odd number:
package demo;
import java.util.Arrays;
public class oddindexsaving {
public static void main(String[] args) { // TODO Auto-generated method stub int[]ar= {10,245,30,460,50}; int oddsize=ar.length/2; int []oddar=new int[oddsize]; int j=0; for(int i=1;i<ar.length;i=i+2) { oddar[j]=ar[i]; j++; } System.out.println(Arrays.toString(oddar)); }}
Output:
[245, 460]
Program for finding method:
package demo;
import java.util.Arrays;
public class Method {
public static void main(String[] args) { // TODO Auto-generated method stub int[]ar1= {10,20,30,40}; int[]ar2= {5,10}; int bigIndex=ar1.length>ar2.length?ar1.length:ar2.length; //terinary operator int[]ar3=new int[bigIndex]; for(int i=0;i<bigIndex;i++) { if(i<ar1.length) { ar3[i]=ar1[i]; } if(i<ar2.length) { ar3[i]=ar3[i]+ar2[i]; } } System.out.println(Arrays.toString(ar3)); }}
Output:
[15, 30, 30, 40]
-
Todays Program:
Left Shifting the given numbers:
package demo;
//left swift
import java.util.Arrays;public class singleReverse {
public static void main(String[] args) { // TODO Auto-generated method stub int[]ar= {10,20,30,40,50}; int temp=ar[0]; for(int i=0;i<ar.length-1;i++) { ar[i]=ar[i+1]; } ar[ar.length-1]=temp; System.out.println(Arrays.toString(ar)); }}
output:
[20, 30, 40, 50, 10]
right shifting the given numbers:
package demo;
//right swift
import java.util.Arrays;public class rightshift {
public static void main(String[] args) { // TODO Auto-generated method stub int[] ar= {10,20,30,40,50}; int temp=ar[ar.length-1]; for(int i=ar.length-1;i>0;i--) { ar[i]=ar[i-1]; } ar[0]=temp; System.out.println(Arrays.toString(ar)); }}
output:
[50, 10, 20, 30, 40]
Righting twice the given numbers:
package demo;
import java.util.Arrays;
//right twice shift
public class rightswift {public static void main(String args[]) {
int[] ar= {10,20,30,40,50}; int temp1=ar[ar.length-2]; int temp2=ar[ar.length-1]; for(int i=ar.length-1;i>1;i--) { ar[i]=ar[i-2]; } ar[0]=temp1; ar[1]=temp2; System.out.println(Arrays.toString(ar));}
}
output:
[40, 50, 10, 20, 30]
left shifting the given two number:
package demo;
import java.util.Arrays;
public class lefttwiceshift {
public static void main(String[] args) { // TODO Auto-generated method stub int[] ar= {10,20,30,40,50}; int temp1=ar[0]; int temp2=ar[1]; for(int i=0;i<ar.length-2;i++) { ar[i]=ar[i+2]; } ar[ar.length-2]=temp1; ar[ar.length-1]=temp2; System.out.println(Arrays.toString(ar)); }}
output:
[30, 40, 50, 10, 20]
-
Removing the given number:
package demo;
import java.util.Arrays;
public class Remove {
public static void main(String args[]) {
int[] ar= {10,19,18,17,16};
int index=3;
int size=ar.length-1;
int[]removear=new int[size];
int j=0;
for(int i=0;i<ar.length;i++) {
if(i!=3) {
removear[j]=ar[i];
j++;
}} System.out.println(Arrays.toString(removear)); }}
output:
[10, 19, 18, 16]
Program for reversing the given number:
package demo;
import java.util.Arrays;
public class Reversearray {
public static void main(String args[]) {
int[]ar= {11,12,13,14,15};
int i=0;
int j=ar.length-1;
for( i=0;i<j;i++) {
int temp=ar[i];
ar[i]=ar[j];
ar[j]=temp;
j–;
}
System.out.println(Arrays.toString(ar));
}}
output:
[15, 14, 13, 12, 11]
-
Array:
Today we learnt new things called array. finally we seen Array.
An array is a collection of similar types of data. For example, if we want to store the names of 100 people then we can create an array of the string type that can store 100 names. String[] array = new String[100]; Here, the above array cannot store more than 100 names.
Program for printing the same number using while and for loop.
package demo;
public class Array {
public static void main(String[] args) { int[]age= {12,13,14,15,16}; // int i=0; System.out.print("{"); /*while(i<age.length) { System.out.print(age[i]+","); i++; }*/ // System.out.print("}"); for(int i=0;i<age.length;i++) { System.out.print(age[i]+","); } System.out.print("}"); }}
output:
{12,13,14,15,16,}
program for adding the given numbers;
package demo;
public class arraysum {
public static void main(String arg[]) {
int[]age= {11,12,14,15,17};
int b=0;
for(int i=0;i<age.length;i++) {b=b+age[i]; } System.out.print(b);}
}output:
69
Program for displaying the sum of given odd number:
package demo;
public class Arrayodd {
public static void main(String[] args) { // TODO Auto-generated method stub int[]age= {11,12,13,14,15}; int b=0; for(int i=0;i<age.length;i++) { if(i%2!=0) { b=b+age[i]; } } System.out.println("odd"+b); }}
output;
odd26
program for finding the single number in the given number:
package demo;
public class Array2 {
public static void main(String args[]) {
int[]age= {11,12,13,14,15};
for(int i=0;i<age.length;i++) {
if(age[i]==13) {
System.out.println(“yes”);
}
else {System.out.println("no"); } }}
}output:
no
no
yes
no
no