0% found this document useful (0 votes)
5 views37 pages

Program Ms

The document contains a comprehensive collection of Java programming examples covering various topics such as basic syntax, control structures, loops, and functions. It includes detailed code snippets for simple tasks like printing integers, swapping numbers, and calculating factorials, as well as more complex operations like matrix manipulation and string handling. Each section is organized by topic, making it a useful reference for learning and practicing Java programming.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views37 pages

Program Ms

The document contains a comprehensive collection of Java programming examples covering various topics such as basic syntax, control structures, loops, and functions. It includes detailed code snippets for simple tasks like printing integers, swapping numbers, and calculating factorials, as well as more complex operations like matrix manipulation and string handling. Each section is organized by topic, making it a useful reference for learning and practicing Java programming.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Contents

Java Programs ............................................................................................................................................................. 1


1. Simple Java Program .......................................................................................................................................... 4
2. Print Integer in java ............................................................................................................................................. 4
3. Command Line Argument ................................................................................................................................... 4
4. How to get Using input using Scanner Program in java ................................................................................ 5
5. How to convert Fahrenheit to Celsius Program in java ............................................................................... 5
rd
6. How to swap 2 no using 3 variable Program in java .................................................................................... 6
rd
7. How to swap 2 no without using 3 variable Program in java ...................................................................... 6
8. How to add two number Program in java ........................................................................................................ 7
9. Find Largest no in java Program ....................................................................................................................... 8
10. If Else clause in java ....................................................................................................................................... 8
11. If Else clause in java- Program 2 ................................................................................................................... 9
12. Nested If Else clause in java .......................................................................................................................... 9
13. How to check Odd and Even Number in java ............................................................................................. 10
14. Find factorial for given no Program in Java ............................................................................................... 10
15. How to complete 2 string in Java program ................................................................................................ 11
16. Simple For Loop Program in Java .............................................................................................................. 12
17. Print Star console using Loop ..................................................................................................................... 12
18. Print Star console using Loop ..................................................................................................................... 13
19. While loop Program in java .......................................................................................................................... 13
20. Print Reverse number in java program ...................................................................................................... 14
21. While loop using break Program in java .................................................................................................... 14
22. While loop using break and continue Program in java .............................................................................. 15
23. Print all alphabet using for loop Program in java....................................................................................... 15
24. Enhance loop in java Program .................................................................................................................... 16
25. Print Multiplication table Program in java .................................................................................................. 16
26. Print prime no Program in java ................................................................................................................... 17
27. Check no is Armstrong or not in java Program ........................................................................................ 18
28. Print Floyd’s Triangle in java Program ................................................................................................................ 19
29. Find All substring of string in java Program .............................................................................................. 19
30. Print reverse string in java Program .......................................................................................................... 20
31. Check Given No is palindrome or Not in java Program ............................................................................ 21
32. How to add two matrix in java Program ...................................................................................................... 22
33. How to multiply two matrix in java Program ............................................................................................... 23
34. How to get transpose of matrix in java Program ....................................................................................... 24
35. How to compare 2 string in java Program ................................................................................................. 25
36. How to string width with specific char in java Program ........................................................................... 25
37. How to use indesOf() in java Program ....................................................................................................... 25
38. How to replace string with another string in java Program ...................................................................... 26
39. How to split string in java Program ............................................................................................................. 26
40. How to remove space in string both end in java Program ........................................................................ 26
41. How to convert all char in string lower case in java Program ................................................................... 26
42. How to create method in java Program ...................................................................................................... 27
43. Find Length, Concatenate and Replace String in Java Program ............................................................. 27

44. How Static block working in java Program ................................................................................................. 28


45. Difference between Static and Instance method working in java Program ............................................. 28
46. How to create Multiple class in java Program ........................................................................................... 29
47. How to create constructor in java Program ............................................................................................... 29

48. How to create constructor overloading in java Program .......................................................................... 30


49. Linear search Program in java .................................................................................................................... 35
50. Binary search Program in java .................................................................................................................... 36
51. Bubble sort Program in java ....................................................................................................................... 37
52. How to create Array program in java ........................................................................................................... 50
53. How to create Multidimensional array program in java ............................................................................. 50
54. How to create Find Factorial No using Recursion Program in java ......................................................... 51
55. How to create Method Overriding program in java .................................................................................... 51
1. Simple Java Program
class HelloWorld
{
public static void main(String args[])
{
[Link]("Hello World by Technolamror");
}
}

2. Print Integer in java


class Integers {
public static void main(String[] arguments) {
int c; //declaring a variable

/* Using for loop to repeat instruction execution */

for (c = 1; c <= 10; c++) {


[Link](c);
}
}
}

3. Command Line Argument in java


class Arguments {
public static void main(String[] args) {
for (String t: args) {
[Link](t);
}
}
}
4. How to get Using input using Scanner
Program in java
import [Link];

class GetInputFromUser
{
public static void main(String args[])
{
int a;
float b;
String s;

Scanner in = new Scanner([Link]);

[Link]("Enter a string");
s = [Link]();
[Link]("You entered string "+s);

[Link]("Enter an integer");
a = [Link]();
[Link]("You entered integer "+a);

[Link]("Enter a float");
b = [Link]();
[Link]("You entered float "+b);
}
}

5. How to convert Fahrenheit to Celsius


Program in java
import [Link].*;

class FahrenheitToCelsius {
public static void main(String[] args) {
float temperatue;
Scanner in = new Scanner([Link]);

[Link]("Enter temperatue in Fahrenheit");


temperatue = [Link]();

temperatue = ((temperatue - 32)*5)/9;

[Link]("Temperatue in Celsius = " + temperatue);


}
}
6. How to swap 2 no using 3rd variable Program
in java
import [Link];

class SwapNumbers
{
public static void main(String args[])
{
int x, y, temp;
[Link]("Enter x and y");
Scanner in = new Scanner([Link]);

x = [Link]();
y = [Link]();

[Link]("Before Swapping\nx = "+x+"\ny = "+y);

temp = x;
x = y;
y = temp;

[Link]("After Swapping\nx = "+x+"\ny = "+y);


}
}

7. How to swap 2 no without using 3rd variable


Program in java
import [Link];

class SwapNumbers
{
public static void main(String args[])
{
int x, y;
[Link]("Enter x and y");
Scanner in = new Scanner([Link]);

x = [Link]();
y = [Link]();

[Link]("Before Swapping\nx = "+x+"\ny = "+y);


x = x + y;
y = x - y;
x = x - y;

[Link]("After Swapping\nx = "+x+"\ny = "+y);


}
}

8. How to add two number Program in java


import [Link];

class AddNumbers
{
public static void main(String args[])
{
int x, y, z;
[Link]("Enter two integers to calculate their sum ");
Scanner in = new Scanner([Link]);
x = [Link]();
y = [Link]();
z = x + y;
[Link]("Sum of entered integers = "+z);
}
}
//For Large Number
import [Link];
import [Link];

class AddingLargeNumbers {
public static void main(String[] args) {
String number1, number2;
Scanner in = new Scanner([Link]);

[Link]("Enter first large number");


number1 = [Link]();

[Link]("Enter second large number");


number2 = [Link]();

BigInteger first = new BigInteger(number1);


BigInteger second = new BigInteger(number2);
BigInteger sum;

sum = [Link](second);

[Link]("Result of addition = " + sum);


}
}

9. Find Largest no in java Program


import [Link];

class LargestOfThreeNumbers
{
public static void main(String args[])
{
int x, y, z;
[Link]("Enter three integers ");
Scanner in = new Scanner([Link]);

x = [Link]();
y = [Link]();
z = [Link]();

if ( x > y && x > z )


[Link]("First number is largest.");
else if ( y > x && y > z )
[Link]("Second number is largest.");
else if ( z > x && z > y )
[Link]("Third number is largest.");
else
[Link]("Entered numbers are not distinct.");
}
}

10. If Else clause in java


class Condition {
public static void main(String[] args) {
boolean learning = true;

if (learning) {
[Link]("Java programmer");
}
else {
[Link]("What are you doing here?");
}
}
}
11. If Else clause in java- Program 2
// If else in Java code
import [Link];

class IfElse {
public static void main(String[] args) {
int marksObtained, passingMarks;

passingMarks = 40;

Scanner input = new Scanner([Link]);

[Link]("Input marks scored by you");

marksObtained = [Link]();

if (marksObtained >= passingMarks) {


[Link]("You passed the exam.");
}
else {
[Link]("Unfortunately you failed to pass the exam.");
}
}
}

12. Nested If Else clause in java


import [Link];

class NestedIfElse {
public static void main(String[] args) {
int marksObtained, passingMarks;
char grade;

passingMarks = 40;

Scanner input = new Scanner([Link]);

[Link]("Input marks scored by you");

marksObtained = [Link]();

if (marksObtained >= passingMarks) {

if (marksObtained > 90)


grade = 'A';
else if (marksObtained > 75)
grade = 'B';
else if (marksObtained > 60)
grade = 'C';
else
grade = 'D';

[Link]("You passed the exam and your grade is " + grade);


}
else {
grade = 'F';
[Link]("You failed and your grade is " + grade);
}
}
}

13. How to check Odd and Even Number in java.


import [Link];

class OddOrEven
{
public static void main(String args[])
{
int x;
[Link]("Enter an integer to check if it is odd or even ");
Scanner in = new Scanner([Link]);
x = [Link]();

if ( x % 2 == 0 )
[Link]("You entered an even number.");
else
[Link]("You entered an odd number.");
}
}

14. Find factorial for given no Program in Java


import [Link];

class Factorial
{
public static void main(String args[])
{
int n, c, fact = 1;

[Link]("Enter an integer to calculate it's factorial");


Scanner in = new Scanner([Link]);

n = [Link]();
if ( n < 0 )
[Link]("Number should be non-negative.");
else
{
for ( c = 1 ; c <= n ; c++ )
fact = fact*c;

[Link]("Factorial of "+n+" is = "+fact);


}
}
}

//Calculate factorial for large No


import [Link];
import [Link];

class BigFactorial
{
public static void main(String args[])
{
int n, c;
BigInteger inc = new BigInteger("1");
BigInteger fact = new BigInteger("1");

Scanner input = new Scanner([Link]);

[Link]("Input an integer");
n = [Link]();

for (c = 1; c <= n; c++) {


fact = [Link](inc);
inc = [Link]([Link]);
}

[Link](n + "! = " + fact);


}
}

15. How to complete 2 string in Java program


import [Link];

class CompareStrings
{
public static void main(String args[])
{
String s1, s2;
Scanner in = new Scanner([Link]);

[Link]("Enter the first string");


s1 = [Link]();
[Link]("Enter the second string");
s2 = [Link]();

if ( [Link](s2) > 0 )
[Link]("First string is greater than second.");
else if ( [Link](s2) < 0 )
[Link]("First string is smaller than second.");
else
[Link]("Both strings are equal.");
}
}

16. Simple For Loop Program in Java


//Java for loop program
class ForLoop {
public static void main(String[] args) {
int c;

for (c = 1; c <= 10; c++) {


[Link](c);
}
}
}

17. Print Star console using Loop


class Stars {
public static void main(String[] args) {
int row, numberOfStars;

for (row = 1; row <= 10; row++) {


for(numberOfStars = 1; numberOfStars <= row; numberOfStars++) {
[Link]("*");
}
[Link](); // Go to next line
}
}
}
18. Print Star console using Loop
class Stars {
public static void main(String[] args) {
int row, numberOfStars;

for (row = 1; row <= 10; row++) {


for(numberOfStars = 1; numberOfStars <= row; numberOfStars++) {
[Link]("*");
}
[Link](); // Go to next line
}
}
}

19. While loop Program in java


import [Link];

class WhileLoop {
public static void main(String[] args) {
int n;

Scanner input = new Scanner([Link]);


[Link]("Input an integer");

while ((n = [Link]()) != 0) {


[Link]("You entered " + n);
[Link]("Input an integer");
}

[Link]("Out of loop");
}
}

20. Print Reverse number in java program


import [Link];

class ReverseNumber
{
public static void main(String args[])
{
int n, reverse = 0;

[Link]("Enter the number to reverse");


Scanner in = new Scanner([Link]);
n = [Link]();

while( n != 0 )
{
reverse = reverse * 10;
reverse = reverse + n%10;
n = n/10;
}

[Link]("Reverse of entered number is "+reverse);


}
}

21. While loop using break Program in java


import [Link];

class BreakWhileLoop {
public static void main(String[] args) {
int n;

Scanner input = new Scanner([Link]);

while (true) {
[Link]("Input an integer");
n = [Link]();

if (n == 0) {
break;
}
[Link]("You entered " + n);
}
}
}
22. While loop using break and continue
Program in java
import [Link];

class BreakContinueWhileLoop {
public static void main(String[] args) {
int n;

Scanner input = new Scanner([Link]);

while (true) {
[Link]("Input an integer");
n = [Link]();

if (n != 0) {
[Link]("You entered " + n);
continue;
}
else {
break;
}
}
}
}

23. Print all alphabet using for loop Program in


java
class Alphabets
{
public static void main(String args[])
{
char ch;

for( ch = 'a' ; ch <= 'z' ; ch++ )


[Link](ch);
}
}
24. Enhance loop in java Program
class EnhancedForLoop {
public static void main(String[] args) {
int primes[] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29};

for (int t: primes) {


[Link](t);
}
}
}
//For String
class EnhancedForLoop {
public static void main(String[] args) {
String languages[] = { "C", "C++", "Java", "Python", "Ruby"};

for (String sample: languages) {


[Link](sample);
}
}
}

25. Print Multiplication table Program in java


import [Link];

class MultiplicationTable
{
public static void main(String args[])
{
int n, c;
[Link]("Enter an integer to print it's multiplication
table");
Scanner in = new Scanner([Link]);
n = [Link]();
[Link]("Multiplication table of "+n+" is :-");

for ( c = 1 ; c <= 10 ; c++ )


[Link](n+"*"+c+" = "+(n*c));
}
}

//For Any Number


import [Link];

class Tables
{
public static void main(String args[])
{
int a, b, c, d;
[Link]("Enter range of numbers to print their multiplication
table");
Scanner in = new Scanner([Link]);

a = [Link]();
b = [Link]();

for (c = a; c <= b; c++) {


[Link]("Multiplication table of "+c);

for (d = 1; d <= 10; d++) {


[Link](c+"*"+d+" = "+(c*d));
}
}
}
}

26. Print prime no Program in java


import [Link].*;

class PrimeNumbers
{
public static void main(String args[])
{
int n, status = 1, num = 3;

Scanner in = new Scanner([Link]);


[Link]("Enter the number of prime numbers you want");
n = [Link]();

if (n >= 1)
{
[Link]("First "+n+" prime numbers are :-");
[Link](2);
}

for ( int count = 2 ; count <=n ; )


{
for ( int j = 2 ; j <= [Link](num) ; j++ )
{
if ( num%j == 0 )
{
status = 0;
break;
}
}
if ( status != 0 )
{
[Link](num);
count++;
}
status = 1;
num++;
}
}
}

27. Check no is Armstrong or not in java


Program
import [Link];

class ArmstrongNumber
{
public static void main(String args[])
{
int n, sum = 0, temp, remainder, digits = 0;

Scanner in = new Scanner([Link]);


[Link]("Input a number to check if it is an Armstrong
number");
n = [Link]();

temp = n;

// Count number of digits

while (temp != 0) {
digits++;
temp = temp/10;
}

temp = n;

while (temp != 0) {
remainder = temp%10;
sum = sum + power(remainder, digits);
temp = temp/10;
}

if (n == sum)
[Link](n + " is an Armstrong number.");
else
[Link](n + " is not an Armstrong number.");
}

static int power(int n, int r) {


int c, p = 1;

for (c = 1; c <= r; c++)


p = p*n;

return p;
}
}

28. Print Floyd’s Triangle in java Program


import [Link];

class FloydTriangle
{
public static void main(String args[])
{
int n, num = 1, c, d;
Scanner in = new Scanner([Link]);

[Link]("Enter the number of rows of floyd's triangle you


want");
n = [Link]();

[Link]("Floyd's triangle :-");

for ( c = 1 ; c <= n ; c++ )


{
for ( d = 1 ; d <= c ; d++ )
{
[Link](num+" ");
num++;
}

[Link]();
}
}
}

29. Find All substring of string in java Program


import [Link];

class SubstringsOfAString
{
public static void main(String args[])
{
String string, sub;
int i, c, length;

Scanner in = new Scanner([Link]);


[Link]("Enter a string to print it's all substrings");
string = [Link]();

length = [Link]();
[Link]("Substrings of \""+string+"\" are :-");

for( c = 0 ; c < length ; c++ )


{
for( i = 1 ; i <= length - c ; i++ )
{
sub = [Link](c, c+i);
[Link](sub);
}
}
}
}

30. Print reverse string in java Program


import [Link].*;

class ReverseString
{
public static void main(String args[])
{
String original, reverse = "";
Scanner in = new Scanner([Link]);

[Link]("Enter a string to reverse");


original = [Link]();

int length = [Link]();

for ( int i = length - 1 ; i >= 0 ; i-- )


reverse = reverse + [Link](i);

[Link]("Reverse of entered string is: "+reverse);


}
}
//Using Internal java Methog
class InvertString
{
public static void main(String args[])
{
StringBuffer a = new StringBuffer("Java programming is fun");
[Link]([Link]());
}
}
31. Check Given No is palindrome or Not in java
Program
import [Link].*;

class Palindrome
{
public static void main(String args[])
{
String original, reverse = "";
Scanner in = new Scanner([Link]);

[Link]("Enter a string to check if it is a palindrome");


original = [Link]();

int length = [Link]();

for ( int i = length - 1; i >= 0; i-- )


reverse = reverse + [Link](i);

if ([Link](reverse))
[Link]("Entered string is a palindrome.");
else
[Link]("Entered string is not a palindrome.");

}
}
//Another Method
import [Link].*;

class Palindrome
{
public static void main(String args[])
{
String inputString;
Scanner in = new Scanner([Link]);

[Link]("Input a string");
inputString = [Link]();

int length = [Link]();


int i, begin, end, middle;

begin = 0;
end = length - 1;
middle = (begin + end)/2;

for (i = begin; i <= middle; i++) {


if ([Link](begin) == [Link](end)) {
begin++;
end--;
}
else {
break;
}
}
if (i == middle + 1) {
[Link]("Palindrome");
}
else {
[Link]("Not a palindrome");
}
}
}

32. How to add two matrix in java Program


import [Link];

class AddTwoMatrix
{
public static void main(String args[])
{
int m, n, c, d;
Scanner in = new Scanner([Link]);

[Link]("Enter the number of rows and columns of matrix");


m = [Link]();
n = [Link]();

int first[][] = new int[m][n];


int second[][] = new int[m][n];
int sum[][] = new int[m][n];

[Link]("Enter the elements of first matrix");

for ( c = 0 ; c < m ; c++ )


for ( d = 0 ; d < n ; d++ )
first[c][d] = [Link]();

[Link]("Enter the elements of second matrix");

for ( c = 0 ; c < m ; c++ )


for ( d = 0 ; d < n ; d++ )
second[c][d] = [Link]();

for ( c = 0 ; c < m ; c++ )


for ( d = 0 ; d < n ; d++ )
sum[c][d] = first[c][d] + second[c][d]; //replace '+' with '-'
to subtract matrices

[Link]("Sum of entered matrices:-");

for ( c = 0 ; c < m ; c++ )


{
for ( d = 0 ; d < n ; d++ )
[Link](sum[c][d]+"\t");

[Link]();
}
}
}

33. How to multiply two matrix in java Program


import [Link];

class MatrixMultiplication
{
public static void main(String args[])
{
int m, n, p, q, sum = 0, c, d, k;

Scanner in = new Scanner([Link]);


[Link]("Enter the number of rows and columns of first
matrix");
m = [Link]();
n = [Link]();

int first[][] = new int[m][n];

[Link]("Enter the elements of first matrix");

for ( c = 0 ; c < m ; c++ )


for ( d = 0 ; d < n ; d++ )
first[c][d] = [Link]();

[Link]("Enter the number of rows and columns of second


matrix");
p = [Link]();
q = [Link]();

if ( n != p )
[Link]("Matrices with entered orders can't be multiplied
with each other.");
else
{
int second[][] = new int[p][q];
int multiply[][] = new int[m][q];

[Link]("Enter the elements of second matrix");

for ( c = 0 ; c < p ; c++ )


for ( d = 0 ; d < q ; d++ )
second[c][d] = [Link]();

for ( c = 0 ; c < m ; c++ )


{
for ( d = 0 ; d < q ; d++ )
{
for ( k = 0 ; k < p ; k++ )
{
sum = sum + first[c][k]*second[k][d];
}

multiply[c][d] = sum;
sum = 0;
}
}

[Link]("Product of entered matrices:-");

for ( c = 0 ; c < m ; c++ )


{
for ( d = 0 ; d < q ; d++ )
[Link](multiply[c][d]+"\t");

[Link]("\n");
}
}
}
}

34. How to get transpose of matrix in java


Program
import [Link];

class TransposeAMatrix
{
public static void main(String args[])
{
int m, n, c, d;

Scanner in = new Scanner([Link]);


[Link]("Enter the number of rows and columns of matrix");
m = [Link]();
n = [Link]();

int matrix[][] = new int[m][n];

[Link]("Enter the elements of matrix");

for ( c = 0 ; c < m ; c++ )


for ( d = 0 ; d < n ; d++ )
matrix[c][d] = [Link]();

int transpose[][] = new int[n][m];

for ( c = 0 ; c < m ; c++ )


{
for ( d = 0 ; d < n ; d++ )
transpose[d][c] = matrix[c][d];
}
[Link]("Transpose of entered matrix:-");

for ( c = 0 ; c < n ; c++ )


{
for ( d = 0 ; d < m ; d++ )
[Link](transpose[c][d]+"\t");

[Link]("\n");
}
}
}

35. How to compare 2 string in java Program


public class LastIndexOfExample{
public static void main(String args[]){
String s1="hello";
String s2="hello";
String s3="meklo";
String s4="hemlo";
[Link]([Link](s2));
[Link]([Link](s3));
[Link]([Link](s4));
}}

36. How to string width with specific char in


java Program
class StringEndwith{
public static void main(String args[]){
String s1="java by TechnoLamror";
[Link]([Link]("r")); //true
[Link]([Link]("Lamror")); //true
[Link]([Link]("lamror"));//false
}
}

37. How to use indesOf() in java Program


public class IndexOfExample{
public static void main(String args[]){
String s1="this is index of example";
//passing substring
int index1=[Link]("is");//returns the index of is substring
int index2=[Link]("index");//returns the index of index substring
[Link](index1+" "+index2);//2 8
//passing substring with from index
int index3=[Link]("is",4);//returns the index of is substring after 4th index
[Link](index3);//5 i.e. the index of another is

//passing char value


int index4=[Link]('s');//returns the index of s char value
[Link](index4);//3
}}

38. How to replace string with another string in


java Program
public class ReplaceAllExample2{
public static void main(String args[]){
String s1="My name is Rajendra. My name is lamror. My name is Technolamror.";
String replaceString=[Link]("is","was");//replaces all occurrences of "is" to
"was"
[Link](replaceString);
}}

39. How to split string in java Program


public class SplitExample{
public static void main(String args[]){
String s1="java string split method by Technolamror";
String[] words=[Link]("\\s");//splits the string based on whitespace
//using java foreach loop to print elements of string array
for(String w:words){
[Link](w);
}
}}

40. How to remove space in string both end in


java Program
public class StringTrimExample{
public static void main(String args[]){
String s1=" hello string ";
[Link](s1+"Technolamror");//without trim()
[Link]([Link]()+"Technolamror");//with trim()
}}

41. How to convert all char in string lower case


in java Program
public class StringLowerExample{
public static void main(String args[]){
String s1="TECHNOLAMROR by Rajendralamror HELLO stRIng";
String s1lower=[Link]();
[Link](s1lower);
}}

42. How to create method in java Program


class Methods {

// Constructor method

Methods() {
[Link]("Constructor method is called when an object of it's
class is created");
}

// Main method where program execution begins

public static void main(String[] args) {


staticMethod();
Methods object = new Methods();
[Link]();
}

// Static method

static void staticMethod() {


[Link]("Static method can be called without creating
object");
}

// Non static method

void nonStaticMethod() {
[Link]("Non static method must be called by creating an
object");
}
}

43. Find Length, Concatenate and Replace


String in Java Program
class StringMethods
{
public static void main(String args[])
{
int n;
String s = "Java programming", t = "", u = "";

[Link](s);

// Find length of string


n = [Link]();
[Link]("Number of characters = " + n);

// Replace characters in string

t = [Link]("Java", "C++");
[Link](s);
[Link](t);

// Concatenating string with another string

u = [Link](" is fun");
[Link](s);
[Link](u);
}
}

44. How Static block working in java Program


class StaticBlock {
public static void main(String[] args) {
[Link]("Main method is executed.");
}

static {
[Link]("Static block is executed before main method.");
}
}
//Static Block Application …. We need to open Program in speciif window
class StaticBlock {
public static void main(String[] args) {
[Link]("You are using Windows_NT operating system.");
}

static {
String os = [Link]("OS");
if ([Link]("Windows_NT") != true) {
[Link](1);
}
}
}

45. Difference between Static and Instance


method working in java Program
class Difference {

public static void main(String[] args) {


display(); //calling without object
Difference t = new Difference();
[Link](); //calling using object
}

static void display() {


[Link]("Programming is amazing.");
}

void show(){
[Link]("Java is awesome.");
}
}

46. How to create Multiple class in java Program


class Computer {
Computer() {
[Link]("Constructor of Computer class.");
}

void computer_method() {
[Link]("Power gone! Shut down your PC soon...");
}

public static void main(String[] args) {


Computer my = new Computer();
Laptop your = new Laptop();

my.computer_method();
your.laptop_method();
}
}

class Laptop {
Laptop() {
[Link]("Constructor of Laptop class.");
}

void laptop_method() {
[Link]("99% Battery available.");
}
}

47. How to create constructor in java Program


class Programming {
//constructor method
Programming() {
[Link]("Constructor method called.");
}
public static void main(String[] args) {
Programming object = new Programming(); //creating object
}
}

48. How to create constructor overloading in


java Program
class Language {
String name;

Language() {
[Link]("Constructor method called.");
}

Language(String t) {
name = t;
}

public static void main(String[] args) {


Language cpp = new Language();
Language java = new Language("Java");

[Link]("C++");

[Link]();
[Link]();
}

void setName(String t) {
name = t;
}

void getName() {
[Link]("Language name: " + name);
}
}

49. Linear search Program in java


import [Link];

class LinearSearch
{
public static void main(String args[])
{
int c, n, search, array[];

Scanner in = new Scanner([Link]);


[Link]("Enter number of elements");
n = [Link]();
array = new int[n];

[Link]("Enter " + n + " integers");

for (c = 0; c < n; c++)


array[c] = [Link]();

[Link]("Enter value to find");


search = [Link]();

for (c = 0; c < n; c++)


{
if (array[c] == search) /* Searching element is present */
{
[Link](search + " is present at location " + (c + 1) +
".");
break;
}
}
if (c == n) /* Searching element is absent */
[Link](search + " is not present in array.");
}
}

50. Binary search Program in java


import [Link];

class BinarySearch
{
public static void main(String args[])
{
int c, first, last, middle, n, search, array[];

Scanner in = new Scanner([Link]);


[Link]("Enter number of elements");
n = [Link]();
array = new int[n];

[Link]("Enter " + n + " integers");

for (c = 0; c < n; c++)


array[c] = [Link]();

[Link]("Enter value to find");


search = [Link]();

first = 0;
last = n - 1;
middle = (first + last)/2;

while( first <= last )


{
if ( array[middle] < search )
first = middle + 1;
else if ( array[middle] == search )
{
[Link](search + " found at location " + (middle + 1) +
".");
break;
}
else
last = middle - 1;

middle = (first + last)/2;


}
if ( first > last )
[Link](search + " is not present in the list.\n");
}
}
51. Bubble sort Program in java
import [Link];

class BubbleSort {
public static void main(String []args) {
int n, c, d, swap;
Scanner in = new Scanner([Link]);

[Link]("Input number of integers to sort");


n = [Link]();

int array[] = new int[n];

[Link]("Enter " + n + " integers");

for (c = 0; c < n; c++)


array[c] = [Link]();

for (c = 0; c < ( n - 1 ); c++) {


for (d = 0; d < n - c - 1; d++) {
if (array[d] > array[d+1]) /* For descending order use < */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}

[Link]("Sorted list of numbers");

for (c = 0; c < n; c++)


[Link](array[c]);
}
}

80. How to convert string to integer in java


program
public class StringToIntExample{
public static void main(String args[]){
String s="200";
int i=[Link](s);
[Link](s+100);//200100 because + is string concatenation operator
[Link](i+100);//300 because + is binary plus operator
}}
81. How to convert integer to string in java
program
public class IntToStringExample1{
public static void main(String args[]){
int i=200;
String s=[Link](i);
[Link](i+100);//300 because + is binary plus operator
[Link](s+100);//200100 because + is string concatenation operator
}}

82. How to convert string to long in java


public class StringToLongExample{
public static void main(String args[]){
String s="9990449935";
long l=[Link](s);
[Link](l);
}}

83. How to convert string to float in java


public class StringToFloatExample{
public static void main(String args[]){
String s="23.6";
float f=[Link]("23.6");
[Link](f);
}}

84. How to convert string to double in java


program
public class StringToDoubleExample{
public static void main(String args[]){
String s="23.6";
double d=[Link]("23.6");
[Link](d);
}}

85. How to convert string to date in java


program
import [Link];
import [Link];
public class StringToDateExample1 {
public static void main(String[] args)throws Exception {
String sDate1="31/12/1998";
String sDate2 = "31-Dec-1998";
String sDate3 = "12 31, 1998";
String sDate4 = "Thu, Dec 31 1998";
String sDate5 = "Thu, Dec 31 1998 23:37:50";
String sDate6 = "31-Dec-1998 23:37:50";
SimpleDateFormat formatter1=new SimpleDateFormat("dd/MM/yyyy");
SimpleDateFormat formatter2=new SimpleDateFormat("dd-MMM-yyyy");
SimpleDateFormat formatter3=new SimpleDateFormat("MM dd, yyyy");
SimpleDateFormat formatter4=new SimpleDateFormat("E, MMM dd yyyy");
SimpleDateFormat formatter5=new SimpleDateFormat("E, MMM dd yyyy HH:mm:ss");
SimpleDateFormat formatter6=new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
Date date1=[Link](sDate1);
Date date2=[Link](sDate2);
Date date3=[Link](sDate3);
Date date4=[Link](sDate4);
Date date5=[Link](sDate5);
Date date6=[Link](sDate6);
[Link]("String to Date converter by technolamror");
[Link](sDate1+"\t"+date1);
[Link](sDate2+"\t"+date2);
[Link](sDate3+"\t"+date3);
[Link](sDate4+"\t"+date4);
[Link](sDate5+"\t"+date5);
[Link](sDate6+"\t"+date6);
}
}

86. How to create Array program in java


class Array_Technolamror{
public static void main(String args[]){

int a[]=new int[5];//declaration and instantiation


a[0]=10;//initialization
a[1]=20;
a[2]=70;
a[3]=40;
a[4]=50;

//printing array
for(int i=0;i<[Link];i++)//length is the property of array
[Link](a[i]);

}}

87. How to create Multidimensional array


program in java
class Multi_Array{
public static void main(String args[]){

//declaring and initializing 2D array


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

//printing 2D array
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
[Link](arr[i][j]+" ");
}
[Link]();
}
}}

88. How to create Find Factorial No using


Recursion Program in java
public class Recursion_Technolamror {
static int factorial(int n){
if (n == 1)
return 1;
else
return(n * factorial(n-1));
}

public static void main(String[] args) {


[Link]("Factorial of 5 is: "+factorial(5));
}
}

89. How to create Method Overriding


program in java
class Bank{
int getRateOfInterest(){return 0;}
}

class SBI extends Bank{


int getRateOfInterest(){return 8;}
}

class ICICI extends Bank{


int getRateOfInterest(){return 7;}
}
class AXIS extends Bank{
int getRateOfInterest(){return 9;}
}

class Test2{
public static void main(String args[]){
SBI s=new SBI();
ICICI i=new ICICI();
AXIS a=new AXIS();
[Link]("SBI Rate of Interest: "+[Link]());
[Link]("ICICI Rate of Interest: "+[Link]());
[Link]("AXIS Rate of Interest: "+[Link]());
} }

You might also like