♠ Posted by
Java Tutorials
at
1:56 AM
Page 1|2|3|4
19.
Reverse string using StringBuffer class
class InvertString
{
public static void main(String args[])
{
StringBuffer a = new StringBuffer("Java
programming is fun");
System.out.println(a.reverse());
}
}
StringBuffer class contains
a method reverse which can be used to reverse or invert an object of this
class.
20.
Java program to check palindrome
Java palindrome program:
Java program to check if a string is a palindrome or not. Remember a string is
a palindrome if it remains unchanged when reversed, for example "dad"
is a palindrome as reverse of "dad" is "dad" whereas
"program" is not a palindrome.
import java.util.*;
class Palindrome
{
public static void main(String args[])
{
String original, reverse="";
Scanner in = new Scanner(System.in);
System.out.println("Enter a string
to check if it is a palindrome");
original = in.nextLine();
int length = original.length();
for ( int i = length - 1 ; i >= 0 ; i-- )
reverse = reverse + original.charAt(i);
if (original.equals(reverse))
System.out.println("Entered string is a palindrome.");
else
System.out.println("Entered
string is not a palindrome.");
}
}
Output of program:
Another method to check
palindrome:
import java.util.*;
class Palindrome
{
public static void main(String args[])
{
String inputString;
Scanner in = new Scanner(System.in);
System.out.println("Input a
string");
inputString = in.nextLine();
int length = inputString.length();
int i, begin, end, middle;
begin
= 0;
end
= length - 1;
middle = (begin + end)/2;
for (i = begin; i <= middle; i++) {
if (inputString.charAt(begin) == inputString.charAt(end)) {
begin++;
end--;
}
else {
break;
}
}
if (i == middle + 1) {
System.out.println("Palindrome");
}
else {
System.out.println("Not a palindrome");
}
}
}
Both the above code
consider a string as case sensitive, you can modify them so that they are not
case sensitive.
21.
Java program to compare two strings
This program compare
strings i.e test whether two strings are equal or not, compareTo method of
String class is used to test equality of two String class objects. compareTo
method is case sensitive i.e "java" and "Java" are two
different strings if you use compareTo method. If you wish to compare strings
but ignoring the case then use compareToIgnoreCase method.
import java.util.Scanner;
class CompareStrings
{
public static void main(String args[])
{
String s1, s2;
Scanner in = new Scanner(System.in);
System.out.println("Enter the
first string");
s1 = in.nextLine();
System.out.println("Enter the
second string");
s2 = in.nextLine();
if ( s1.compareTo(s2) > 0 )
System.out.println("First
string is greater than second.");
else if ( s1.compareTo(s2) < 0 )
System.out.println("First
string is smaller than second.");
else
System.out.println("Both
strings are equal.");
}
}
Output of program:
String 'hello' is greater
than 'Hello' as ASCII value of 'h' is greater than 'H'. To check two strings
for equality you can use equals method which returns true if strings are equal
otherwise false.
22.
Java program for linear search
Java program for linear
search: Linear search is very simple, To check if an element is present in the
given list we compare search element with every element in the list. If the
number is found then success occurs otherwise the list doesn't contain the
element we are searching.
import java.util.Scanner;
class LinearSearch
{
public static void main(String args[])
{
int c, 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 = 0; c < n; c++)
array[c] = in.nextInt();
System.out.println("Enter
value to find");
search = in.nextInt();
for (c = 0; c < n; c++)
{
if (array[c] == search) /* Searching element is present */
{
System.out.println(search + " is present at location "
+ (c + 1) + ".");
break;
}
}
if (c == n) /* Searching element is absent */
System.out.println(search + " is not present in array.");
}
}
Output of program:
Above code locate first
instance of element to found, you can modify it for multiple occurrence of same
element and count how many times it occur in the list. Similarly you can find
if an alphabet is present in a string.
23.
Java program for binary search
Java program for binary
search: This code implements binary search algorithm. Please note input numbers
must be in ascending order.
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 = 0; c < n; c++)
array[c] = in.nextInt();
System.out.println("Enter
value to find");
search = in.nextInt();
first
= 0;
last
= n - 1;
middle = (first + last)/2;
while( first <= last )
{
if ( array[middle] < search )
first = middle + 1;
else if ( array[middle] == search )
{
System.out.println(search + " found at location " + (middle + 1) + ".");
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if ( first > last )
System.out.println(search + " is not present in the list.\n");
}
}
Output of program:
Other methods of searching
are Linear search and Hashing. There is a binarySearch method in Arrays class
which 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.binarySearch(characters, 'a'));
System.out.println(Arrays.binarySearch(characters, 'p'));
}
}
binarySearch method returns
the location if a match occurs otherwise -(x+1) where x is the no. of elements
in the array, For example in the second case above when p is not present in
characters array the returned value will be -6.
24.
Java program to find all substrings of a
string
Java program to find
substrings of a string :- This program find all substrings of a string and the
prints them. For example substrings of "fun" are :- "f",
"fu", "fun", "u", "un" and "n".
substring method of String class is used to find substring. Java code to print
substrings of a string is given below.
import java.util.Scanner;
class SubstringsOfAString
{
public static void main(String args[])
{
String string, sub;
int i, c, length;
Scanner in = new Scanner(System.in);
System.out.println("Enter a
string to print it's all substrings");
string
= in.nextLine();
length = string.length();
System.out.println("Substrings
of \""+string+"\" are :-");
for( c = 0 ; c < length ; c++ )
{
for( i = 1 ; i <= length - c ; i++ )
{
sub = string.substring(c, c+i);
System.out.println(sub);
}
}
}
}
Output of program:
For a string of length n
there will be (n(n+1))/2 non empty substrings and one more which is empty
string. Empty string is considered to be substring of every string also known
as NULL string.
25.
Java program to display date and time, print
date and time using java program
Java date and time program
:- Java code to print or display current system date and time. This program
prints current date and time. We are using GregorianCalendar class in our
program. Java code to print date and time is given below :-
import java.util.*;
class GetCurrentDateAndTime
{
public static void main(String args[])
{
int day, month, year;
int second, minute, hour;
GregorianCalendar date = new GregorianCalendar();
day = date.get(Calendar.DAY_OF_MONTH);
month = date.get(Calendar.MONTH);
year = date.get(Calendar.YEAR);
second = date.get(Calendar.SECOND);
minute = date.get(Calendar.MINUTE);
hour = date.get(Calendar.HOUR);
System.out.println("Current
date is "+day+"/"+(month+1)+"/"+year);
System.out.println("Current
time is "+hour+" : "+minute+" : "+second);
}
}
Output of program:
Don't use Date and Time
class of java.util package as their methods are deprecated means they may not
be supported in future versions of JDK. As an alternative of GregorianCalendar
class you can use Calendar class.
26.
Java program to generate random numbers
Java program to generate
random numbers: This code generates random numbers in range 0 to 100 (both
inclusive).
import java.util.*;
class RandomNumbers {
public static void main(String[] args) {
int c;
Random t = new Random();
// random
integers in [0, 100]
for (c = 1; c <= 10; c++) {
System.out.println(t.nextInt(100));
}
}
}
Output of program:
nextInt(c) method returns
next integer in 0 to c (both inclusive), c must be positive. To generate random
float's use nextFloat which returns float between 0.0 to 1.0.
0 comments :
Post a Comment