Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Thursday, June 16, 2011

PROGRAM TO DEMONSTRATE THE USE OF STRING CLASS AND EMPLOYING AT LEAST 10 STRING HANDLING METHODS


class StringMethods
{    public static void main(String args[ ])
{    String string1 = new String ("WELCOME TO");
String string2 = new String (" JAVA PROGRAMMING");
String s3,s4,s5,s6,s7,s8;
boolean b;
char ch;
int a,c;
System.out.println("\n\nSTRING1 is  : "+string1);
s3 = string1.toLowerCase();
System.out.println("\n\nSTRING1 is Converted into Lower Case       : "+s3);
s4 = string1.toUpperCase();
System.out.println("\n\nSTRING1 is Converted into Upper Case       : "+s4);
s5 = string1.replace ('O','A');
System.out.println("\n\nSTRING1's character 'O' is Replaced by 'A'  : "+s5);
a = string1.length();
System.out.println("\n\nLength of The STRING1  is                  : "+a);
ch = string1.charAt(5);
System.out.println("\n\nThe Character Present at Index value = 5 in the STRING1 is : "+ch);
s7 = string1.substring(5);
System.out.println("\n\nThe Substring  Starting from index value = 5 of STRING1 is :"+s7);
s6 = string1.trim();
System.out.println("\n\nAfter Remove White Spaces at the Beg. and End of the STRING1 is : "+s6);
System.out.println("\n\nSTRING2 is  : "+string2);
b  = string1.equals(string2);
System.out.println("\n\nAre Both Strings Equal? True  or  False    : "+b);
s6 = string1.concat (string2);
System.out.println("\n\nThe Concatenates of  STRING1 &  STRING2 is : "+s6);
s8 = string2.substring(5,11);
System.out.println("\n\nThe Substring Start from index value = 5 to 11 of STRING2 : "+s8); //Not including 11th index value
}
}

Thursday, June 02, 2011

Program to Find the Sum of the Digits of a Number

import java.io.*;
class Add
{
void number()
{
try
{
int num,sum=0;
System.out.println("Enter any number:");
DataInputStream dts=new DataInputStream(System.in);
int n=Integer.parseInt(dts.readLine());
while ( n > 0 )
{
num = n % 10;
n = n / 10;
sum = sum + num;
}
System.out.println("The sum of the digits is:"
+sum);
}
catch(Exception s)
{
System.out.println(s.getMessage());
}
}
}
class Digit_add
{
public static void main(String args[])
{
Add a = new Add();
a.number();
}
}

Sunday, May 08, 2011

Whether a Number is Palindrome or Not?

import java.io.*;
class rev
{
public static void main(String a[ ])
{
int n, m, rev = 0;
int k = Integer.parseInt(a[0]);
n = k;
while( n > 0 )
{
m = n % 10;
rev = ( rev * 10 ) + m;
n = n / 10;
}
if( rev == k )
System.out.println("palindrome" );
else
System.out.println(" not palindrome" );
}
}

Wednesday, May 04, 2011

Add two matrices

import java.lang.*;
import java.io.*;
class addmatrix
{
static void add()
{
int a[][], b[][], c[][];
a = new int[2][2];
b = new int[2][2];
c = new int[2][2];
DataInputStream dts=new DataInputStream(System.in);
try
{
System.out.println("enter first matrix
2*2 order :");
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
a[i][j]=Integer.parseInt(dts.readLine());
}
}
System.out.println("enter second matrix
2*2 order :");
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
b[i][j]=Integer.parseInt(dts.readLine());
}
}
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
System.out.print(c[i][j]+" ");
}
System.out.println("\t");
}
}
catch(Exception x)
{
System.out.println("error");
}
}
public static void main(String arg[])
{
add();
}
}