Custom Search

Monday 27 August 2012

Java Program that explain the Gregorian calendar class and display current year, current month, current date and also test if is a leap year or not.

Java Program that explain the Gregorian calendar class and display current year, current month, current date and also test if is a leap year or not.

Program

import java.util.*;
class grCalander
{
public static void main(String args[])
{
GregorianCalendar gc = new GregorianCalendar();
int year= gc.get(Calendar.YEAR);
System.out.println(year);
System.out.println(gc.isLeapYear(year));
System.out.println("Month:"+gc.get(Calendar.MONTH));
System.out.println("Week of Year:"+gc.get(Calendar.WEEK_OF_YEAR)); System.out.println("Week of Month:"+gc.get(Calendar.WEEK_OF_MONTH));
System.out.println("Day of year:"+gc.get(Calendar.DAY_OF_YEAR));
System.out.println("Day of week:"+gc.get(Calendar.DAY_OF_WEEK));
}
}

Java Program that add n or 100 number of days to the current date & display new date & time

Java Program that add n or 100 number of days to the current date & display new date & time

Program

import java.util.*;
class Add100days
{
public static void main(String args[])
{
Date date=new Date();
long msec=date.getTime();
msec+=100*24*60*60*1000L;
date.setTime(msec);
System.out.println(date)
}
}

Java Program that generate 30 random numbers, Write java code to generate random numbers


Java Program that generate 30 random numbers, Write java code to generate random numbers

Program
import java.util.*;
class randomnos
{
public static void main(String args[])
{
Random R = new Random();
for(int i=0;i<30;i++)
System.out.println(R.nextInt()%30);
}
}

Java Program that generates custom exception or user define exception, Write a java code to illustrate custom exception or user define exception


Java Program that generates custom exception or user define exception, Write a java code to illustrate custom exception or user define exception


Program
class negativeArgExc
{
public static void main(String args[])throws NegativeArgumentException
{
int i;
i=Integer.parseInt(args[0]);
if (i<0)
throw new NegativeArgumentException("Negative Commandline Argument");
}
}
class NegativeArgumentException extends Exception
{
public NegativeArgumentException(String str)
{
super(str);
}
}

Java Program to explain different methods of catch class


Java Program to explain different methods of catch class


Program
class catchMethod
{
public static void main(String args[])
{
int i,j,ans;
i=1;
j=0;
try
{
ans=i/j;
System.out.println("Ans is:"+ans);
}
catch(ArithmeticException e)
{
System.out.println("filIn: " + e.fillInStackTrace());
System.out.println("cause: " + e.getCause());
System.out.print("trace: "); 
e.printStackTrace();
System.out.println("messa: " + e.getMessage());
}

}
}


Java Program to explain the concept of throw clause


Java Program to explain the concept of throw clause


Program
class throwClause
{
public static void main(String args[]) throws Exception
{
int no=24,i,quot;
for(i=0;i<=5;i++)
{
try
{
quot=no/(i-2);
System.out.println("Quotient:"+quot);
}
catch(ArithmeticException e)
{
System.out.println("Exception Occure at i:"+i);
}
}
}
}

Java Program to explain Super Keyword, write a program in java using super keyword through extend class of class inheritance


Java Program to explain Super Keyword, write a program in java using super keyword through extend class of class inheritance

Program
class base
{
int i;
base(int a)
{
i=a;
}
void show()
{
System.out.println("Base:"+i);
}

}
class derived extends base
{
int i,j;
derived(int a,int b,int c)
{
super(a);
i=b;
j=c;
}
void show()
{
super.show();
System.out.println("Base i:"+super.i);
System.out.println("Derived i:"+i);
System.out.println("Derived j:"+i);
}

}
class superEx
{
public static void main(String args[])
{
derived d = new derived(10,20,30);
d.show();
}
}

Java Program to explain Method Modifier using Extend Class & Abstract Class


Java Program to explain Method Modifier using Extend Class & Abstract Class

Program
abstract class A
{
int a;
public abstract void show();
}
class B extends A
{
int b,c;
B(int i,int j,int k)
{
a=i;
b=j;
c=k;
}
public void show()
{
System.out.println("a="+a);
System.out.println("b="+b);
System.out.println("c="+c);
}
final void disp()
{
System.out.println("Inside B Class Final Method");
}
}
class C extends B
{
static int i=10;
C(int i,int j,int k)
{
super(i,j,k);
}
public static void prt()
{
i=i*100;
System.out.println("Static Method:"+i);
}
private void display()
{
System.out.println("Private Method");
}
protected void msg()
{
System.out.println("Proctected Method");
}
/* void disp()
{
System.out.println("Final");
} Not Possible bcoz it is declared as final in the base class*/
}
class Methodmodi
{
public static void main(String args[])
{
B b = new B(10,20,30);
C c= new C(10,20,30);
c.show();
C.prt();
//c.display();Not possible bcoz it is private
c.msg();
c.disp();
}
}

Java Program to illustrate Variable modifier like Public, Private, Protected, Java code to explain any three modifier


Java Program to illustrate Variable modifier like Public, Private, Protected, Java code to explain any three modifier


Program
class var
{
public int a;
final int b=24;
static int c;
private int d;
protected int e;

public void show()
{
System.out.println("Public:"+a);
System.out.println("Private:"+d);
System.out.println("Protected"+e);
System.out.println("Final:"+b);
System.out.println("Static:"+c);
}
}
class variablemodi
{
public static void main(String args[])
{
var v=new var();

v.a=10;
//v.d=20; bcoz it is private variable cant access outside the class
v.e=30;
//v.b=40; bcoz it is final variable so it is constant
var.c=50;
v.show();
}
}

Java Program to find area of circle using Final Class, Write a java program to find area of circle using final keyword


Java Program to find area of circle using Final Class, Write a java program to find area of circle using final keyword


Program
final class Fincircle
{
double area;
double radius;
final double PHI=22.0/7.0;
Fincircle(double r)
{
radius =r;
}
double Area()
{
area=PHI*radius*radius;
return area;
}
}
class finalcls
{
public static void main(String args[])
{
double ar;
Fincircle F=new Fincircle(3.5);
ar=F.Area();
System.out.println("Area of Circle is:"+ar);
}
}

Java Program to find area of Rectangle & circle using Abstract Class, Write a program in java explaining program Abstract Class, class modifiers


Java Program to find area of Rectangle & circle using Abstract Class, Write a program in java explaining program Abstract Class, class modifiers 


Program
/*  This is a example of Abstract  Class */
abstract class shape
{
double area;
final double PHI=22.0/7.0;
abstract double Area();
}
class Rectangle extends shape
{
double length,breadth;
Rectangle(double x,double y)
{
length=x;
breadth=y;
}
double Area()
{
area=length*breadth;
return area;
}
}
class Circle extends shape
{
double radious;
Circle(double r)
{
radious=r;
}
double Area()
{
area=PHI*radious*radious;
return area;
}
}
class Abscls
{
public static void main(String args[])
{
Rectangle rec=new Rectangle (6.0,4.0);
double ar;
ar=rec.Area();
System.out.println("Area of Ractangle is:"+ar);
Circle cir = new Circle(5.25);
ar=cir.Area();
System.out.println("Area of Circle is:"+ar);
}
}

Saturday 25 August 2012

Java Program for Matrix Multiplication, Write a program in java language for Matrix Multiplication


Java Program for  Matrix Multiplication, Write a program in java language for Matrix Multiplication

Program
class multi
{
public static void main(String args[])
{
int k=1;
int arr[][],arr2[][],arr3[][];

arr= new int[3][3];
arr2=new int[3][3];
arr3=new int[3][3];

for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
arr[i][j]=k;
arr2[i][j]=++k;
k++;
}

}
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
for(int p=0;p<3;p++)
{
arr3[i][j]+=arr[i][p]*arr2[p][j];
}

}
}

for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(" "+arr[i][j]);
}
System.out.println();

}
System.out.println();
System.out.println();

for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(" "+arr2[i][j]);
}
System.out.println();
}
System.out.println();
System.out.println();
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(" "+arr3[i][j]);
}
System.out.println();
}
}
}

Java Program for printing pyramid of 10 numbers, Write a java program that display pyramid of 10 numbers


Java Program for printing pyramid of 10 numbers, Write a java program that display pyramid of 10 numbers


Program
class pyramid
{
public static void main(String args[])
{
for(int i=1;i<=10;i++)
{
for(int s=20;s>=i;s--)
{
System.out.print(" ");
}
for(int j=1;j<=i;j++)
{
System.out.print(" "+j+" ");
}
System.out.println();
}
}
}

Java Program to find square root and power, Write a java program that finds square root and power


Java Program to find square root and power, Write a java program that finds square root and power


Program
class powsq
{
public static void main(String args[])
{
int i=Integer.parseInt(args[0]);
int j=Integer.parseInt(args[1]);

double sq=Math.sqrt(i);
double pw=Math.pow(i,j);

System.out.println("Square root is:"+sq);
System.out.println("Power is:"+pw);
}
}

Java Program for wrapper class. Write a java program for using any five methods for wrapper class.


Java Program for wrapper class. Write a java program for using any five methods for wrapper class.


Program
class Wrappercls
{
public static void main(String args[])
{
double no1=0;
double no2=0;
double no3=no1/no2;
System.out.println("no3:"+no3);
System.out.println("isInfinite:"+Double.isInfinite(no3));
System.out.println("isNaN:"+Double.isNaN(no3));
System.out.println("Max Value:"+Double.MAX_VALUE);
System.out.println("Min Value:"+Double.MIN_VALUE);
System.out.println("Positive Infinity:" + Double.POSITIVE_INFINITY);
System.out.println("Negative Infinity: " + Double.NEGATIVE_INFINITY);
}
}

Java Program to find Radius of a circle, Write a java Program that finds a radius using this variable


Java Program to find Radius of a circle, Write a java Program that finds a radius using this variable


Program
class radius
{
double pie,area,r;
radious(double p,double a)
{
this.pie=p;
this.area=a;
}
void calculate()
{
r=(area/(2*pie));
System.out.println("Radius of circle is: "+r);
}
}
class Test
{
public static void main(String args[])
{
radius rd=new radius(3.14,22.24);
rd.calculate();
}
}

Java Program to perform Stack operations like Push, Pop, Peep, Change, Write a java Program for stack using constructor, define methods with parameter etc


Java Program to perform Stack operations like Push, Pop, Peep, Change, Write a java Program for stack using constructor, define methods with parameter etc

Program
class stack
{
int stck[] = new int[10];
int top;

stack() //Default Constructor
{
top=-1;
}
void push(int item)
{
if(top==9)
System.out.println("Stack is Overflow");
else
top++;
stck[top]=item;
}
int pop()
{
if(top<0)
{
System.out.println("Stack is Underflow");
return 0;
}
else
{
return stck[top--];
}
}
void peep(int l)
{
if(top-l+1<=0)
System.out.println("Stack Underflow");
else
System.out.println(" The value of "+l+" location in stack::"+stck[top-l+1]);
}
void change(int l,int x)
{
if(top-l+1<=0)
System.out.println("Stack Underflow");
else
stck[top-l+1]=x;
}
}

class MyStack
{
public static void main(String args[])
{
stack Mystack1 = new stack();
stack Mystack2 = new stack();
//Push
for(int i=0;i<10;i++)
Mystack1.push(i);
for(int i=10;i<20;i++)
Mystack2.push(i);

//Peep Operation
Mystack1.peep(5);
Mystack2.peep(4);

//Change Operation
Mystack1.change(5,25);
Mystack2.change(4,50);

//Peep Operation
Mystack1.peep(5);
Mystack2.peep(4);

//Pop
System.out.println("Stack in Mystack1");
for(int i=0;i<10;i++)
System.out.println(Mystack1.pop());

System.out.println("Stack in Mystack2");
for(int i=0;i<10;i++)
System.out.println(Mystack2.pop());
}

Java Program that illustrate StringBuffer class & using different method like append, insert, capacity, lenth, charat etc.


Java Program that illustrate StringBuffer class & using different method like append, insert, capacity, lenth, charat etc.


Program
class stBuffercls
{
public static void main(String args[])
{
StringBuffer sb = new StringBuffer("Delhi");

//Length
System.out.println("Length of Buffer String:"+sb.length());
//Capacity
System.out.println("Capacity of Buffer String:"+sb.capacity());
//charAt
System.out.println("CharAt(3) from Buffer String:"+sb.charAt(3));
//Append
System.out.println("String After Append Operation:"+ sb.append(" Hai Meri Jaan"));
//Insert
sb.insert(0,"Yeh ");
System.out.println("String After insert operation:"+ sb);
}
}

Java Program that illustrate String class using different methods like equal, compareTo, replace, lowercase, uppercase, concat, valueof, indedof etc.


Java Program that illustrate String class using different methods like equal, compareTo, replace, lowercase, uppercase, concat, valueof, indedof etc.


Program
class StCls
{
public static void main(String args[])
{
String str1="I Love My India";
String str2=new String("I Love My India");
String str3="I Love My India";
String str4="Teach";
String str5="YourSelf";
StringBuffer sb=new StringBuffer("I Love My India");
System.out.println("str1==str2:"+ (str1==str2));
System.out.println("str1==str3:"+ (str1==str3));
        int result = str1.compareTo(str2);
      if (result < 0)
           System.out.println(str1 + " is less than "  + str2);
        else if (result == 0)
System.out.println(str1 +" is equal to " + str2);
            else 
        System.out.println(str1 +" is greater than " + str2);
        //Concat
String str6=str4.concat(str5);
System.out.println("Concated String is: "+ str6);
    //Replace
System.out.println("String After Replace:"+ sb.replace(10,16,"Country"));
//Lowercase
String lower=str4.toLowerCase();
System.out.println("String After Conversion:"+ lower);
//Uppercase
String upper=str4.toUpperCase();
System.out.println("String After Conversion:"+ upper);
int i=str4.indexOf("c");
System.out.println("The Index of c is :"+i);
}
}

Java Program that perform type conversion like int to byte, double to int, double to byte etc.( other possible types)


Java Program that perform type conversion like int to byte, double to int, double to byte etc.( other possible types)


Program
class conversion
{
public static void main(String[] args)
{
byte b;
int i=123;
double d=121.212;
System.out.println("\nConversion of int to byte");
b=(byte)i;
System.out.println("i= " + i + " b= " + b);

System.out.println("\nConversion of double to int");
i=(int)d;
System.out.println("d= " + d +" i= "+i);

System.out.println("\nConversion of double to byte");
b=(byte)d;
System.out.println("d= " + d + " b= " + b);

//The type promotion rules.....

byte b1=11;
char c1='a';
short s1=256;
int i1=4000;
float f1=1.24f;
double d1=0.123;

double result=(f1*b1)+(i1/c1)-(d1*s1);
System.out.println((f1*b1)+ " + " + (i1/c1) + " - " + (d1*s1));
System.out.println("Result-->" + result);
}
}

Java Program to find Prime Number, Write a program in Java Language to find a twin prime numbers.


Java Program to find Prime Number, Write a program in Java Language to find a twin prime numbers.


Program
class prime
{
public static void main(String args[])
{
Logic L=new Logic(); // Create instance of Logic class
L.findprime();
}
}
class Logic
{
public void findprime()
{
int i,j,flag;
flag=0;
System.out.println("Twin Prime Numbers are.......");
for(i=1;i<100;i=i+2) // Logic to find First prime number
{
flag=0;

for(j=2;j<i-1;j++)
{
if( i % j == 0 )
{
flag = 1;
break;
}
}
if (flag == 0) // to find number is twin prime or not
{
for(j=2;j<(i+2)-1;j++)
{
if((i+2) % j == 0 )
{
flag = 1;
break;
}
}
if (flag == 0)
{
System.out.println(i + " " + (i+2));
}
}
}
}
}

Java Program to swap 2 numbers without using third variable, Write a program in Java for swapping values of two variable without using temporary variable


Java Program to swap 2 numbers without using third variable, Write a program in Java  for swapping values of two variable without using temporary variable


Program
class swapping
{
public static void main(String args[])
{
int no1,no2;
no1 = Integer.parseInt(args[0]);
no2 = Integer.parseInt(args[1]);
Logic L=new Logic();
L.swapping(no1,no2);
}
}
class Logic
{
public void swapping(int n1,int n2)
{
System.out.println("Before Swapping......");
System.out.println("No1 is: " + n1);
System.out.println("No2 is: " + n2);

n1 = n1+ n2; // Logic
n2 = n1 - n2;
n1 = n1 - n2;

System.out.println("After Swapping......");
System.out.println("No1 is: " + n1);
System.out.println("No2 is: " + n2);


}

}

Java Program to swap 2 numbers using third variable, Write an application for swapping values of two variable using third variable


Java Program to swap 2 numbers using third variable, Write an application for swapping values of two variable using third variable


Program
class swap
{
public static void main(String args[])
{
int no1,no2;
no1 = Integer.parseInt(args[0]);
no2 = Integer.parseInt(args[1]);
Logic L=new Logic();
L.swapping(no1,no2);
}
}
class Logic
{
public void swapping(int n1,int n2)
{
int t; //Third Variable

System.out.println("No1 is : " + n1);
System.out.println("No2 is: " + n2);

t =  n1;
n1 = n2;
n2 = t;

System.out.println("After Swapping..........");
System.out.println("No1 is : " + n1);
System.out.println("No2 is: " + n2);
}
}

Java Program to find Max & Min number, Write a program in Java Language to find maximum & minimum numbers


Java Program to find Max & Min number, Write a program in Java Language to find maximum & minimum numbers

Program
class maxmin
{
public static void main(String args[])
{
int no1,no2,ans;
no1 = Integer.parseInt(args[0]);
no2 = Integer.parseInt(args[1]);
Logic L=new Logic();
ans=L.find(no1,no2);
if (ans == no1)
System.out.println("No1 Is Greater Than No2...." );
else
System.out.println("No2 Is Greater Than No1...." );

}
}
class Logic
{
public int find(int n1, int n2)
{
return( n1 > n2 ? n1 : n2); // Conditional Operator
}
}

Java Program to find Factorial Using Recursion, Write a program in Java language for an an application to find factorial using recursion


Java Program to find Factorial Using Recursion, Write a program in Java language for an an application to find factorial using recursion

Program
class facto
{
public static void main(String args[])
{
int no,f;
no = Integer.parseInt(args[0]);
Logic L=new Logic();
f=L.fact(no);
System.out.println("Factorial of " + no + " is " + f);
}
}
class Logic
{
public int fact(int n)
{
if(n==1)
return 1;
else
{
return n*fact(n-1); //Recursion
}

}
}

Java Program to find Factorial, Write a Java Program for an application to find factorial


Java Program to find Factorial, Write a Java Program for an application to find factorial


Program:-
class factorial
{
public static void main(String args[])
{
int no;
no = Integer.parseInt(args[0]);
Logic L=new Logic();
L.fact(no);
}
}
class Logic
{
public void fact(int n)
{
int f,no;
no = n;
f=1;
while(no>0)
{
f = f * no;
no = no - 1;
}
System.out.println("Factorial of " + n + " is " + f);
}
}

Java Program of Fibonacci series, Write a Java program for application for Fibonacci series


Java Program of Fibonacci series, Write a Java program for application for Fibonacci series


Program:-
class fibonacci
{
public static void main(String args[])
{
int no;
no = Integer.parseInt(args[0]);
Logic L=new Logic();
L.fibo(no);
}
}
class Logic
{
public void fibo(int n)
{
int a,b,c;
a=0;
b=1;
System.out.print(a +" ");
System.out.print(b +" ");
c=1;
do //Using Do....While Loop
{
c=a+b;
System.out.print(c +" ");
a=b;
b=c;
}while(c<n);
}
}

Laptops