By : Aniket Pataskar
INDEX
1. Hello World example3
2. Add two matrices 4
3. Armstrong number7
4. Binary Search11
5. Bubble sort14
6. Command line arguments17
11. Java Programs part 1 28
1. Hello World example
class HelloWorld {
public static void main ( String args []) {
System . out . println ( Hello World" ) ; }
}
"Hello World" is passed as an argument to printlnmethod, you can print whatever you want. There is alsoa print method which doesn't takes the cursor tobeginning of next line as println does. System is a class,out is object of PrintStream class and println is themethod.
Output of program:
2. Add Two Matrices
import java.util.Scanner ;
class AddTwoMatrix {
public static void main ( String args []) {
Scanner in = new Scanner ( System . in ) ;
System . out . println ( "Enter
the number of rows and
columns of matrix" ) ;
m = in. nextInt () ;
n = in. nextInt () ;
int first [][] =
new int [ m ][ n ] ;
int second [][] = new int [ m ][ n ] ; int sum [][] = new int [ m ][ n ] ;
System . out . println ( "Enter
the elements of first
matrix" ) ;
for ( c = ; c < m ; c ++ )
for ( d = ; d < n ; d ++ )
int m, n, c, d ;
first [ c ][ d ] = in.nextInt () ;
System . out . println ( Enter the elements of second matrix" ) ;
for ( c = ; c < m ; c ++ )
for ( d = ; d < n ; d ++ )
second [ c ][ d ] = in.nextInt () ;
for ( c = ; c < m ; c ++ )
for ( d = ; d < n ; d ++ )
sum [ c ][ d ] = first [ c ][ d ] + second [ c ]
[ d ] ; //replace '+' with '-' to subtract matrices
System . out . println ( "Sum of entered matrices:-" ) ;
for ( c = ; c < m ; c ++ )
{
for ( d = ; d < n ; d ++ )
System . out . print ( sum [ c ][ d ] + " \t " ) ;
System . out . println () ;
}
}
}
Output of program:
This code adds two matrix, you can modify it to add anynumber of matrices. You can create a Matrix class andcreate it's objects and then create an add method which sum the objects, then you can add any number ofmatrices by repeatedly calling the method using a loop.
3. Binary Search
import java.util.Scanner ;
class BinarySearch {
public static void main ( String args []) {
int c, first, last, middle, n, search, array [] ;
Scanner in =
new Scanner ( System . in ) ; System . out . println ( "Enter
number of elements" ) ;
n = in. nextInt () ;
array =
new int [ n ] ;
System . out . println ( "Enter "
+ n + " integers" ) ;
for ( c = ; c < n ; c ++ ) array [ c ] =
in. nextInt () ;
System . out . println ( "Enter
value to find" ) ;
search = in. nextInt () ;
first = ; last = n - ;
middle = ( first + last ) /
;
if ( array [ middle ] < search ) first = middle + ; else if
( array [ middle ] == search ){
System . out . println ( search +
" found at location " +
( middle + ) + "." ) ;
break ; }
elselast = middle - ;
middle = ( first +
last ) / ;
}
if ( first > last )
System . out . println ( search + " is not present in the list. \n " ) ;
}
}
while ( first <= last ) {
Output of program:
Other methods of searching are Linear search andHashing. There is a binarySearch method in Arrays classwhich can also be used.
import java.util.Arrays ;
class BS {
public static void main ( String args [])
{
char characters [] =
{ 'a' , 'b' , 'c' , 'd' , 'e' } ;
System . out . println ( Arrays . bi
narySearch ( characters,
'a' )) ;
System . out . println ( Arrays . bi
narySearch ( characters,
'p' )) ;
}
}
binarySearch method returns the location if a match occurs otherwise - (x+1) where x is the no. of elements inthe array, For example in the second case above when pis not present in characters array the returned value willbe -6.
4. Armstrong number
This java program checks if a number is Armstrong ornot. Armstrong number is a number which is equal tosum of digits raise to the power total number of digits inthe number. Some Armstrong numbers are: 0, 1, 4, 5, 9,153, 371, 407, 8208 etc.
Java programming code
import java.util.Scanner ;
class ArmstrongNumber {
public static void main ( String args []) {
int n, sum = , temp, remainder, digits = ;
Scanner in =
new Scanner ( System . in ) ;
System . out . println ( "Input a number to check if it is an Armstrong number" ) ; n = in. nextInt () ; temp = n ;
// Count number of digits
while ( temp != ) {
% ;
digits ++;
temp = temp / ;
}
temp = n ;
while ( temp != ) {
remainder = temp
sum = sum + power ( remainder, digits ) ;
temp = temp / ;
}
if ( n == sum )
System . out . println ( n + " is an Armstrong number." ) ;
else
System . out . println ( n + " is not an Armstrong number." ) ;
}
static int power ( int n, int r ) { int c, p = ;
for ( c = ; c <= r ; c +
+ )
return p ;
p = p * n ; }
}
Output of program:
Using one more loop in the above code you can generateArmstrong numbers from 1 to n(say) or between twointegers (a to b).
5.Bubble sort
Java program to bubble sort: This code sorts numbersinputted by user using Bubble sort algorithm.
Java programming code
import java.util.Scanner ;
class BubbleSort { public static void
main ( String [] args ) { int n, c, d, swap ; Scanner in =
newScanner ( System . in ) ;
System . out . println ( "Input
number of integers to
sort" ) ;
n = in. nextInt () ; int array [] = new
int [ n ] ; System . out . println ( "Enter " + n + " integers" ) ;
for ( c = ; c < n ; c ++ ) array [ c ] = in. nextInt () ;
for ( c = ; c < ( n - ) ; c ++ ) {
for ( d = ; d < n - c
- ; d ++ ) {
if ( array [ d ] >
array [ d + ]) /* For descending order use < */
{
array [ d ] ;
swap =
array [ d ] =
array [ d + ] ;
array [ d + ] = swap ;
}
} }
System . out . println ( "Sorted
list of numbers" ) ;
for ( c = ; c < n ; c ++ )
System . out . println ( array [ c ])
;
}
}
Complexity of bubble sort is O(n2) which makes it a lessfrequent option for arranging in sorted order whenquantity of numbers is high.
Output of program:
Next page