Wednesday, 19 December 2012

OOPS Lab programs


1. Experiment no .1:  Write a Program to find Simple Interest.

1. AIM:
                        Write a java program to find the simple interest.
2. Algorithm:

1.     Start the program.
2.     Declare the variables and assume values
3.     InterestTopay=principle*time*rate/100;
4.     print the values.
5.     End of class and main method.
6.     stop the program.

3. PROGRAM:
           
import java.io.*;
import java.lang.*;
class Simpleinterest
{
  public static void main(String args[])
  {
                  int  principle=25000;
                   float rate = 12.5f;
                   double interestToPay,time = 2.75;
           
                 interestToPay=principle*time*rate/100;

System.out.println("Principle amount is Rs."+principle+ "interest=Rs."+interestToPay);
System.out.println ("Total amount to pay to clear the loan = Rs."+(principle+interestToPay));
}
}


4. Output:

Principle amount is Rs.25000 interest = Rs.8593.75
Total amount to pay to clear the loan = Rs.33593.75


2. Experiment no .2 : Program To Find The Arthematic Operations

1. AIM:
                        Write a java program to find the given numbers of arithmetic operations.

2. Algorithm:
1.     Start the program.
2.     Declare the variables a and b.
3.     Read a string with inputstreamReader(System.in).
4.     convert the string into Integer.parseInt(stdin.readLine());
5.     Given variables addition, subtraction, multiplication, division.
6.     End of class and main method.
7.     stop the program.

3. PROGRAM:    
import java.io.*;
class arthematic
{
 public static void main(String s[]) throws IOException
  {
   int a,b,add,sub,mul,div;
   DataInputStream stdin=new DataInputStream(System.in);
   System.out.println("Enter First Number:");
   a=Integer.parseInt(stdin.readLine());
   System.out.println("Enter Second Number:");
   b=Integer.parseInt(stdin.readLine());
   add=a+b;
   sub=a-b;
   mul=a*b;
   div=a/b;
   System.out.println("Addition of a and b = "+add);
   System.out.println("Subtraction of a and b = "+sub);
   System.out.println("Multiplication of a and b = "+mul);
   System.out.println("Division of a and b = "+div);
  }
}
4. OUTPUT:
Enter first number: 5
Enter second number: 3
Addition of a and b = 8
Subtraction of a and b=2
Multiplication of a and b=15
Division of a and b=1.0

3. Experiment no .3 : Program To Find The Single Digit Number

1. AIM:
            Write a java program to find the given single digit number using switch case.

2. Algorithm:
1.     Start the program.
2.     Read a string with inputstreamReader(System.in).
3.     convert the string into Integer.parseInt(stdin.readLine());
4.     By using switch case ( multi way decision statement) when a match is found, that case is executed.
5.     Default it is a break statement exit the switch statement.
6.     Stop the program.

4. PROGRAM:    
import java.io.*;
class digit
{
 public static void main(String s[]) throws IOException
 {
   int n;
   DataInputStream stdin=new DataInputStream(System.in);
   System.out.println("Enter Any positive single digit number :");
   n=Integer.parseInt(stdin.readLine());
   switch(n)
    {
     case 0:
            System.out.println("Zero");
            break;
     case 1:
            System.out.println("One");
            break;
     case 2:
            System.out.println("Two");
            break;
     case 3:
            System.out.println("Three");
            break;
     case 4:
            System.out.println("Four");
            break;
     case 5:
            System.out.println("Five");
            break;
     case 6:
            System.out.println("Six");
            break;
     case 7:
            System.out.println("Seven");
            break;
     case 8:
            System.out.println("Eight");
            break;
     case 9:
            System.out.println("Nine");
            break;
     default:
             System.out.println("Invalid Number");
            break;
    }
  }
}

4. OUTPUT:
Enter any positive single digit number: 6
Six
Enter any positive single digit number: 5
Five

4. Experiment no .4 : Program To Find The Factorial Of A Number

1. AIM:
            Write a java program to find the given factorial numbers.
2. Algorithm:
1.     Start the program. Import the packages.
2.     Read a string with inputstreamReader(System.in).
3.     convert the string into Integer.parseInt(stdin.readLine());
4.     By using for loop rotating the integer value.
5.     Repeat enter the value until end of loop.
6.     End of class and main method.
7.     stop the program.

3. PROGRAM:    

import java.io.*;                                         //importing  io package
import java.lang.*;                                     //importing lang package
class Factorial
{
       public static void main(String args[])   throws IOException
         {
             int i,n,f=1;
             System.out.println("Enter the numbert you want to calculate the factorial");
             BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
             n=Integer.parseInt(stdin.readLine());
              for(i=1;i<=n;i++)
               {
                   f=f*i;
               }
             System.out.println("The  factorial of " + n + " is " +  f);
          }                                                                          //End of main
}                                                                                   //End of class Factorial         


4. Output
Enter the number for which you want the factorial  4
The factorial of 4 is 24
Enter the number for which you want the factorial  3
The factorial of 3 is 6
Enter the number for which you want the factorial  6
The factorial of 6 is 120

5. Experiment no. 5: Program to check whether the first number is a multiple of second number.

1. AIM:
    To check whether the first number is a multiple of second number.

2. Algorithm:
1.     Start the program, import the packages.
2.     Create a class and variables with data types.
3.     Read a string with inputstreamReader(System.in).
4.     convert the string into Integer.parseInt(stdin.readLine());
5.     By using if…else loop rotating the string.
6.     Print the concatenation of arrays.
7.     Stop the program.

3. PROGRAM:
import java.io.*;                         //Importing io package
class Multiple
{
    public static void main(String args[])   throws IOException
    {
      int m,n;
      BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));     
      System.out.println("Enter the first number" );
      m=Integer.parseInt(stdin.readLine());
      System.out.println("Enter the second number ");
      n=Integer.parseInt(stdin.readLine());
      if ( m%n==0)
      {
         System.out.println("The first number is  the multiple of  second number" );
      }
       else
       {
           System.out.println("The first number is not  the multiple of  second number" );
       }
     }                 //End of main
 }                     //End of class Multiple
4. Output
Enter the first number 10
Enter the second number 5
The first number is  the multiple of  second number
Enter the first number 2
Enter the second number 3
The first number is not the multiple of second number

6. Experiment no. 6: Program to check the given array is sorting order

1. AIM:
            Write a java program to check given numbers in a sorting order.

2. Algorithm:

1.     Start the program.
2.     Create a class and variables with data types.
3.     Read a string with DatainputstreamReader(System.in).
4.     convert the string into Integer.parseInt(stdin.readLine());
5.     By using for loop rotating the array.
6.     Print the concatenation of arrays.
7.     Stop the program.

3. PROGRAM:
import java.io.*;
class sorting
{
 public static void main(String s[]) throws IOException
      {
                int a[]=new int[10];
                int i,j;
                DataInputStream stdin=new DataInputStream(System.in);
                System.out.println("Enter 10 Elements into Array");
                for(i=0;i<10 i="i" p="p">
                       a[i]=Integer.parseInt(stdin.readLine());
                for(i=0;i<9 i="i" p="p">
                  for(j=0;j<9-i j="j" p="p">
                   {
                     if (a[j+1]
                        {
                         int temp=a[j+1];
                             a[j+1]=a[j];
                             a[j]=temp;
                        }
                    }
                System.out.println("Required Order  is ");
                for(i=0;i<10 i="i" p="p">
                   System.out.println(a[i]);
               }
}
4. OUTPUT:
Enter 10 elements into Array:10 9 8 7 6 5 4 3 2 1
Required order is:1 2 3 4 5 6  7 8 9 10


7. Experiment no. 7: Program To generate the ARMSTRONG number

1. AIM:
            Write a java program to generate the Armstrong number.

2. Algorithm:
1.     Start the program.
2.     Read a string with DatainputstreamReader(System.in).
3.     convert the string into Integer.parseInt(stdin.readLine());
4.     sum=sum+r*r*r formula.
5.     Using if else statement.
6.     Stop the program.

3. PROGRAM:
import java.io.*;
class armstrong
{
 public static void main(String s[]) throws IOException
 {
  int n,r,temp,sum;
  DataInputStream stdin=new DataInputStream(System.in);
  System.out.println("Enter any Positive Integer Number :");
  n=Integer.parseInt(stdin.readLine());
  temp=n;
  sum=0;
  while(temp>0)
   {
    r=temp  % 10;
    sum=sum+r*r*r;
    temp=temp/10;
   }
  if (n==sum)
     System.out.println("Given Number is Armstrong Number");
  else
     System.out.println("Given Number is not Armstrong Number");
 }
}
4. OUTPUT:
Enter any positive Integer number:  153
Given number is Armstrong number.
Enter any positive Integer number:  146
Given number is not Armstrong number.
8. Experiment no. 8: Program To generate the Quadratic equation

1. AIM:
            Write a java program to generate the quadratic equation.

2. Algorithm:
1.     Start the program. Import the packages.
2.     Create a class and variables with data types.
3.     Declaration of the main class.
4.     Read a string with inputstreamReader(System.in).
5.     convert the string into Integer.parseInt(stdin.readLine());
6.     By using if(d==0) “Roots are Equalent” loop rotating the integer value.
7.     if(d>0) “Roots are Real” otherwise ("Roots are Imaginary");
8.     Repeats enter the value until end of loop.
9.     End of class and main method.
10.       Stop the program.

3. PROGRAM:

import java.io.*;
import java.math.*;
class quadratic
{
  public static void main(String s[]) throws IOException
  {
   int a,b,c,d;
   double r1,r2;
   BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
   System.out.println("Enter value of a:");
   a=Integer.parseInt(stdin.readLine());
   System.out.println("Enter value of b:");
   b=Integer.parseInt(stdin.readLine());
   System.out.println("Enter value of c:");
   c=Integer.parseInt(stdin.readLine());
   d=b*b-4*a*c;
   if(d==0)
     {
      System.out.println("Roots are Equalent");
      r1=-b/(2*a);
      r2=-b/(2*a);
      System.out.println("Root1 = "+r1);
      System.out.println("Root2 = "+r2);
     }
 
else if (d>0)
       {
      System.out.println("Roots are Real");
      r1=(-b+Math.sqrt(d))/(2*a);
      r2=(-b-Math.sqrt(d))/(2*a);
      System.out.println("Root1 = "+r1);
      System.out.println("Root2 = "+r2);
       }
  else
     System.out.println("Roots are Imaginary");
 }

4. Output:
Enter  value of a:1
Enter value of b:2
Enter value of c:1
Roots are Equalent
Enter  value of a:2
Enter value of b:3
Enter value of c:2
Roots are Imaginary

9. Experiment no. 9: Program to Find The Primary Numbers

1. AIM:
            Write a java program to find the given primary  numbers.
2. Algorithm:
1.     Start the program. Import the packages.
2.     Read a string with inputstreamReader(System.in).
3.     convert the string into Integer.parseInt(stdin.readLine());
4.     By using Nested for() loop rotating the integer value.
5.     Repeat enters the value until end of loop.
6.     End of class and main method.
7.     stop the program.

3. PROGRAM:
import java.io.*;
class prime
{
  public static void main(String s[]) throws IOException
  {
   int i,j,c,n;
   BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
   System.out.println("Enter Positive value :");
   n=Integer.parseInt(stdin.readLine());
   for (i=1;i<=n;i++)
      {
       for(j=1,c=0;j<=i;j++)
          {
            if (i%j==0)
               c=c+1;
          }
       if(c==2)
          System.out.println(i);
      }
 }
}
4. Output:
Enter positive value:
10
1
2
3
5
7


10. Experiment no. 10: Program to generate the Fibonacci series

1. AIM:
Write a java program to generate the Fibonacci series, given number of n values.
2. Algorithm:
1.     Start the program. Import the packages.
2.     Create a class and variables with data types.
3.     Declaration of the main class.
4.     Read a string with inputstreamReader(System.in).
5.     convert the string into Integer.parseInt(stdin.readLine());
6.     By using for loop rotating the integer value.
7.     Repeats enter the value until end of loop.
8.     End of class and main method.
9.     Stop the program.

3. PROGRAM:
import java.io.*;                      //importing  io package           
import java.lang.*;                  //importing Lang package
class A
{
    int a,b,c;
   A( int f1,int f2 )
    {
        a=f1;
        b=f2;
    }                                 //End of constructor A
   void Feb()
    {
          c=a+b;
          System.out.print("\t" + c);
           a=b;
           b=c;
     }                                //End of method Feb
}                                     //End of  class A

class Febinocci
{
       public static void main(String args[])       throws IOException
       {
          int n,f3, i;
          A a=new A(0,1);
          System.out.println("Enter  how many numbers you want in febinoci series");
          BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
          n=Integer.parseInt(stdin.readLine());
          System.out.println("The febinocci series is as follows");
          System.out.print("\t" + 0);
          System.out.print("\t" + 1);
           for(i=0;i<(n-2);i++)
               {
                   a.Feb();
               }                //End of for loop
          }                     //End of main
}                               //End of class Febinocci

4.  Output
Enter  how many numbers you want in febinoci series 3
The febinocci series is as follows 0   1    1
Enter  how many numbers you want in febinoci series 6
The febinocci series is as follows 0   1    1    2    3   5
Enter  how many numbers you want in febinoci series 10
The febinocci series is as follows 0   1    1   2   3    5   8    13    21      34


11. Experiment no. 11: Program to generate the sorting order of array

1. AIM:
    Write a java program to generate the sorting order of a given number of n values.

2. Algorithm:
1.     Start the program. Import the packages.
2.     Create a class and variables with data types.
3.     Declaration of the main class.
4.     Read a string with inputstreamReader(System.in).
5.     convert the string into Integer.parseInt(stdin.readLine());
6.     By using for loop rotating the integer value.
7.     Swapping the values into a temp=a[i];
8.     Repeats enter the value until end of loop.
9.     End of class and main method.
10.      Stop the program.

3. PROGRAM:
import java.io.*;
class sorting
{
  public static void main(String s[]) throws IOException
  {
   int a[]=new int[20];
   int  i,j,temp;
   BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
   System.out.println("Enter 10 integers into array");
   for(i=0;i<10 i="i" p="p">
   a[i]=Integer.parseInt(stdin.readLine());
   for(i=0;i<10-1 i="i" p="p">
    {
     for(j=0;j<10-i-1 j="j" p="p">
        {
         if (a[j]>a[j+1])
            {
             temp=a[j];
             a[j]=a[j+1];
             a[j+1]=temp;
            }
         }
   }
   System.out.println("Required Order is ");
   for(i=0;i<10 i="i" p="p">
      System.out.println(a[i]);
 }
}

4. Output:
Enter 10 integers in to array:
9 8 7 6 5 4 3 2 1 0
Required order is: 0 1 2 3 4 5 6 7 8 9


































12. Experiment no. 12: Program to find the 2 largest numbers of a given array


1. AIM:
To find the largest number of given numbers, by using arrays.

2. Algorithm:

1.     Start the program, import the packages.
2.     Create a class and variables with data types.
3.     Read a string with inputstreamReader(System.in).
4.     convert the string into Integer.parseInt(stdin.readLine());
5.     By using for loop rotating the single dimensional arrays value.
6.     Repeats enter the value until end of loop.
7.     Print the concatenation of string.
8.     Stop the program.


3. PROGRAM:

import java.io.*;                                //Importing io package
import java.lang.*;                            //Importing lang package
class Largest
{
     public static void main(String args[])         throws IOException
      {
           int a[]= new int[10];
           int i,j,k;
           System.out.println("Enter the numbers of the array");
           BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
            for(i=0;i<10 i="i" p="p">
            {
                 a[i]=Integer.parseInt(stdin.readLine());
            }
            for(i=0;i<10 i="i" p="p">
            {
                 for(j=(i+1);j<10 j="j" p="p">
                  {
                        if(a[i]>a[j])
                {
                     k=a[i];
                             a[i]=a[j];
                             a[j]=k;
                         }                    //End of if
                   }                          //End of inner for loop
            }                                 //End of outer for loop
System.out.println("The first largest number in the array is " + a[9]);
System.out.println("The second largest number in the array is " + a[8]);
   }      //End of main
} //End of class Largest

4.  Output
Enter the numbers of the array 1  2   6   3  78   7   8   45   32  23
The first largest number in the array is 78
The second largest number in the array is 45
Enter the numbers of the array 12  23  34  45  56   67  68  78  89 100
The first largest number in the array is 100
The second largest number in the array is 89
Enter the numbers of the array 11  12   13  14   15    16   17   18   19  20
The first largest number in the array is 20
The second largest number in the array is 19

13. Experiment no. 13: Program to find product, sum and difference of 2 matices

1. AIM:
            Write a java program to find the sum of the matrices, product of the matrices and differences of matrices, by using two dimensional arrays.

2. Algorithm:
1.     Start the program, import the packages.
2.     Create a class and variables with two dimensional arrays.
3.     Read a string with inputstreamReader(System.in).
4.     convert the string into Integer.parseInt(stdin.readLine());
5.     By using for loop rotating the two dimensional arrays value.
6.     Column is i, rows is j declare in two dimensional matrices.
7.     Repeats enter the value until end of loop.
8.     Print the concatenation of arrays.
9.     Stop the program.

3. PROGRAM:
import java.io.*;                 //Importing io package
class Matrix
{
public static void main(String args[])                throws IOException
{
int a[][]=new int[3][3];
int b[][]=new int[3][3];
int s[][]=new int[3][3];
int d[][]=new int[3][3];
int p[][]=new int[3][3];
int i,j;
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the elements of 1st matrix");
for( i=0;i<3 eading="eading" elements="elements" first="first" i="i" matrix="matrix" nbsp="nbsp" of="of" p="p" the="the">
{
for(j=0;j<3 j="j" p="p">
{
a[i][j]=Integer.parseInt(stdin.readLine());
}
}
System.out.println("Enter the elements of the 2nd matrix");
for(i=0;i<3 eading="eading" elements="elements" i="i" matrix="matrix" nbsp="nbsp" of="of" p="p" second="second" the="the">
{
for(j=0;j<3 j="j" p="p">
{
b[i][j]=Integer.parseInt(stdin.readLine());
}
}
for(i=0;i<3 additon="additon" alculating="alculating" and="and" i="i" nbsp="nbsp" p="p" substraction="substraction">
{
for(j=0;j<3 j="j" p="p">
{
s[i][j]=a[i][j]+b[i][j];
d[i][j]=a[i][j]-b[i][j];
}
}
System.out.println("The sum of the matrices is");
for(i=0;i<3 alculating="alculating" i="i" nbsp="nbsp" p="p" product="product">
{
for(j=0;j<3 j="j" p="p">
{
System.out.print("  "+s[i][j]);
}
System.out.println();
}
System.out.println("The productof the matrices is");   
for(i=0;i<3 i="i" p="p">
{
for(j=0;j<3 j="j" p="p">
{
p[i][j]=0;
for(int k=0;k<3 k="k" p="p">
{
p[i][j]=p[i][j]+a[i][k]*b[k][j];
}
System.out.print("  "+p[i][j]);
}
System.out.println();
}
System.out.println("The difference of the matrices is");         
for(i=0;i<3 i="i" p="p">
{
for(j=0;j<3 j="j" p="p">
{
System.out.print("  "+d[i][j]);
}
System.out.println();
}
}
}
4. Output
Enter the elements of 1st matrix  1 2 3 4 1 2 3 4 1
Enter the elements of 2nd matrix 2 3 4 5 6 1 2 3 4
The sum of the matrices is
3    5    7
9    7    3
5    7    5
The productof the matrices is
18   24   18
17   24   25
28   36   20
The difference of the matrices is
-1   -1   -1
-1   -5    1
1     1    -3


14. Experiment no. 14: Program To implement abstract keyword

1. AIM:
            Write a java program to implement the abstract keyword and method overriding.

2. Algorithm:
1.     Start the program.
2.     Create a classes and variables with data types.
3.     Assigns the values of rectangle and triangle.
4.     Create a object of relevant class, to call the procedure.
5.     Given a formula of area.
6.     Print the concatenation of string.
7.     Stop the program.

3. PROGRAM:
                                               
abstract class Figure
{
   double dim1,dim2;
   Figure(double a,double b)
   {
        dim1=a;
        dim2=b;
    }
    abstract double  area();
}
class Rectangle  extends Figure
{
     Rectangle(double a,double b)
      {
           super(a,b);
      }
    double area()
    {
       return(dim1*dim2);
     }
}
class Triangle  extends Figure
{
     Triangle(double a,double b)
      {
           super(a,b);
      }
    double area()
    {
       return((dim1*dim2)/2);
     }
}
class  Abstract
{
public static void main(String args[])
{
     Rectangle r=new  Rectangle(5,5);
      double ar=r.area();
      Triangle t=new  Triangle(5,5);
      double at=r.area();
System.out.println("area of rectangle " + ar);
System.out.println("area of triangle "  + at );
}
}

4. OUTPUT:

area of rectangle 25
area of triangle  12.5*/






















15. Experiment no. 15: Program to implement constructor OverLoading

1. AIM:
            Write a java program to implement the constructor and overloading method program.

2. Algorithm:
1.     Start the program.
2.     Create a class and variables with data types.
3.     Declare the methods in same name with different parameters.
4.     Create a object to call the procedure.
5.     Print the concatenation of values.
6.     Stop the program.

3. PROGRAM:
  class A
{
     int a=2,b=3,c=4;
     A(int c1)
     {
          a=c1;
     }
     A(int c1,int c2)
     {
          a=c1;
          b=c2;
     }
     A(int c1,int c2,int c3)
     {
          a=c1;
          b=c2;
           c=c3;;
     }
     void show()
     {
          System.out.println("The value of a is " + a);
          System.out.println("The value of b is " + b);
          System.out.println("The value of c is " + c);       
    } 
}
class Consover
{
     public static void main(String args[])
     {
          A a=new A(6);
          A a2=new A(7,8);
          A a1=new A(9,10,11);
a.show();
a1.show();
a2.show();

       }
}


4. OUTPUT:

The value of a is 6
The value of a is 7
The value of  b is 8
The value of a is 9
The value of b is 10
The value of c is  11





















16. Experiment no. 16: Program To Implement Dynamic(super class) method dispatch
1. AIM:

Write a java program to implement the super class and sub class method.

2. Algorithm:
1.     Start the program.
2.     Create a classes and variables with data types.
3.     Create a methods void show ().
4.     Create an object of relevant class, to call the procedure.
5.     Print the concatenation of string.
6.     Stop the program.

3. PROGRAM:
import java.io.*;
import java.lang.*;
class A
{
   void show()
   {
        System.out.println("You are in superclass");
   }
}
class B extends A
{
   void show()
   {
        System.out.println("You are in subclass");
   }
}
class Dmd               
{
     public static void main(String args[])   
     {
       A a;           //Creating reference variable
       B b=new B();       //creating object for class B 
        b.show();     
        a=b;                     //Assigning object b to a;
        a.show();
     }
}
4. OUTPUT:
You are in superclass
You are in subclass

17. Experiment no. 17: Declaring and implementing an interface

1. AIM:


            Write a java program to implement the Interface command on multiple class.

2. Algorithm:
1.     Start the program.
2.     Create a classes and variables with data types.
3.     Create a command of interface( ).
4.     Create an object of relevant class, to call the procedure.
5.     Calculate the formula in a program.
6.     Print the concatenation of string.
7.     Stop the program.





3. Program:
interface Pet
{
void speak();
void legs(int x);
String move(String how);
double weight(double x);
}
public class interfaceDemo implements Pet{
public void speak(){
System.out.println("Dog braks");
}
public void legs(int x){
System.out.println("Dog has got" +x+"legs");
}
public String move(String how_to_ move){
System.out.println("Dog moves on"+how_to_move+"on land);
return"";
}
public double weight(double x){
return x;
}
public static void main(string args[]){
double wt;
InterfaceDemo id= new InterfaceDemo();
id.speak();
id.legs(4);
id.move("four legs");
wt=id.weight(24.58);
System.out.println("Dog weights" +wt + "kgs");
}
}

4. Output:
Dog barks
Dog has got 4 legs
dog moves on four legs on land
Dog weights 24.58 kgs
































18. Experiment no. 18: Program To Implement "Inheritance"

1. AIM:
            Write a java program to implement the Inheritance program.

2. Algorithm:
1.     Start the program, import the packages.
2.     Create a class and variables with data types.
3.     Declare the methods in different names in Arithmetic operations.
4.     Arguments give a throws IOExceptions.
5.     Read a string with inputstreamReader(System.in).
6.     convert the string into Integer.parseInt(stdin.readLine());
7.     Create a object to call the procedure.
8.     Print the concatenation of string.
9.     Stop the program.

3. PROGRAM:                                                                           

import java.io.*;
import java.lang.*;
class Add
{
   int c;
   void add(int a,int b)
   {
        c=a+b;
        System.out.println("Result of adding is "+ c);
   }
}
class Sub  extends Add
{
   void sub(int a,int b)
   {
        c=a-b;
        System.out.println("Result of subtracting is "+ c);
   }
}
class Mul  extends Sub
{
   void mul(int a,int b)
   {
        c=a*b;
        System.out.println("Result of multiplying is "+ c);
   }
}
class Inherit               
{
     public static void main(String args[])    throws IOException
     {
          BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
          System.out.println("Enter 2 numbers to perform add,sub and mul");
          int i=Integer.parseInt(stdin.readLine());
          int j=Integer.parseInt(stdin.readLine());
          Mul m=new Mul();
          m.mul(i,j);
          m.add(i,j);
          m.sub(i,j);
     }
}



4. Output
Enter 2 numbers to perform add,sub and mul
10  20
Result of multiplying is 300
Result of adding is 30
Result of subtracting is -10
Enter 2 numbers to perform add,sub and mul
5  10
Result of multiplying is 50
Result of adding is 15
Result of subtracting is -5
Enter 2 numbers to perform add,sub and mul
2  1
Result of multiplying is 2
Result of adding is 3
Result of subtracting is 1  















19. Experiment no. 19: Program to show the Use of This Keyword and Constructor Overloading

1. AIM:

            Write a java program to show the use of This Keyword And Constructor Overloading.

2. Algorithm:
1.     Start the program, import the packages.
2.     Create a class and variables with data types.
3.     Declare the methods in same name with different parameters.
4.     Arguments give a throws IoExceptions.
5.     Read a string with inputstreamReader(System.in).
6.     convert the string into Integer.parseInt(stdin.readLine());
7.     Create a object to call the procedure.
8.     Print the concatenation of string.
9.     Stop the program.

3. PROGRAM:
                                         
import java.io.*;                                //importing  io package 
import java.lang.*;                      //importing Lang package
class A
{
    int Square( int x )
    {
       
int s=x*x;
            return(s);
    }                                 //End of constructor Square with int as return type
    float Square( float x )
    {
        float s=x*x;
        return(s);
    }                                 //End of constructor Square with float as return type
     double Square( double x )
    {
         double s=x*x;
        return(s);
    }                                 //End of constructor Square with double as return type
}                                     //End of  class A

class  Methover
{
       public static void main(String args[])       throws IOException
       {

          System.out.println("Enter the integer number you want to calculate square");
          BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
          int n=Integer.parseInt(stdin.readLine());
          System.out.println("Enter  the floating point number you want to calculate square");
          float m=Float.parseFloat(stdin.readLine());
          System.out.println("Enter  the double data type number uou want to calculate square");
          double p=Double.parseDouble(stdin.readLine());
          A a=new A();
          int sq1=a.Square(n);
          System.out.println("The square of integer number is " + sq1);
          A a1=new A();
          float sq2=a1.Square(m);           
          System.out.println("The square of float number is " + sq2);
          A a2=new A();
          double sq3=a2.Square(p);
          System.out.println("The square of double number is " + sq2);
        }
}


4. OUTPUT:

Enter the integer number you want to calculate square 5 
Enter  the floating point number you want to calculate square 2.5
Enter  the double data type number uou want to calculate square  3.44
The square of integer number is 25
The square of float number is 6.25
The square of double number is 11.56











20. Experiment no. 20: Program to invoke constructors using "Super" Keyword

1. AIM:
Write a java program to  Invoke constructors using "Super" Keyword.

2. Algorithm:

1.     Start the program.
2.     Create a class and variables with data types.
3.     Declare the method of void show ();
4.     Print the concatenation of values.
5.     Stop the program.

3. PROGRAM:
class A
{
     int a,b,c;
     A(int d1,int d2)              //Constructor of class A
     {
          a=d1;
          b=d2;
     }
     void show()
     {
          System.out.println("The value of a is " + a);
          System.out.println("The value of b is " + b);
     }
}            //End of class A
class B extends A
{
     B(int d1,int d2,int d3)     //Constructor of class B
     {
          super(d1,d2);          //super calls constructor A
          c=d3;
     }
     void show()
     {
          System.out.println("The value of a is " + a);
          System.out.println("The value of b is " + b);
          System.out.println("The value of c is " + c);
     }        
}                                //End of class B
class Super1
{
     public static void main(String args[])
     {
          A a=new A(10,20);      //Creating object for class A
          a.show();
          B b=new B(10,20,30);   //Creating object for class B
          b.show();
     }          //End of main
}              //End of class super1

4. OUTPUT:
The value of a is 10
The value of b is 20
The value of a is 10
The value of b is 20
The value of c is 30*/



























21. Experiment no. 21: Program to show The Separate Window multiplication

1. AIM:

            Write a java program to show the separate window of multiplication of three numbers.

2. Algorithm:

1.     Start the program, import the packages.
2.     Create a class and variables with data types.
3.     Declare the string() values.
4.     JOptionPane.showInputDialog command is a window.
5.     JOptionPane.showMessageDialog  dialog box.
6.     Print the windows of out put.
7.     Stop the program.

3. PROGRAM:
import java.io.*;
import javax.swing.JOptionPane;
public class Product
 {
   public static void main( String args[] )
   {
      int x;       // first number
      int y;       // second number
      int z;       // third number
      int result;  // product of numbers
      String xVal;  // first string input by user
      String yVal;  // second string input by user
      String zVal;  // third string input by user
      xVal = JOptionPane.showInputDialog( "Enter first integer:" );
      yVal = JOptionPane.showInputDialog( "Enter second integer:" );
      zVal = JOptionPane.showInputDialog( "Enter third integer:" );
      x = Integer.parseInt( xVal );
      y = Integer.parseInt( yVal );
      z = Integer.parseInt( zVal );
      result = x * y * z;
      JOptionPane.showMessageDialog( null, "The product is " + result );
      System.exit( 0 );
   } // end method main
} // end class Product


4. OUTPUT:

















22. Experiment no. 22: Program To show The MULTITHREADING

1. AIM:
Write a java program to show the multithreading using try and catch() methods.

2. Algorithm:

1.     Start the program.
2.     Create a class and variables User Thread ().
3.     Using try catch () methods.
4.     For loop executions.
5.     Print the try statements and catch () statements.
6.     Print the concatenation of string.
7.     Stop the program.

3. PROGRAM:
class UserThread extends Thread
{
UserThread()
{
super("UserThread");
System.out.println("it is a UserThread");
start();
}
public void main()
{
try
{
for(int i=10;i>0;i--)
{
System.out.println("Uservalue"+i);
Thread.sleep(500);
}
}
catch(InterruptedException ie)
{
System.out.println("User thread Exception");
}
System.out.println("User thread completed");
}
}
 public class ThreadDemo
{
public static void main(String args[])
{
int i;
UserThread ut=new UserThread();
try
{
for(i=1;i==2;i++)
{
System.out.println("main Thread value is" +i);
Thread.sleep(100);
}
}
catch(InterruptedException ie)
{
System.out.println("main thread interreupted");
}
System.out.println("main thread completed");
}
 }
4. OUTPUT:
It is UserThread
Main thread completed






















23. Experiment no. 23: Program to implement Packages

1. AIM:
            Write a java program to implement Arithmetic operations the packages.

2. Algorithm:

1.     Start the program.
2.     Import the Mathc packages.
3.     Create a classes and variables with data types.
4.     Create an object of relevant class, to call the procedure.
5.     Create an object to call the packages.
6.     Return arithmetic operations.
7.     Print the concatenation of string.
8.     Stop the program.

3. PROGRAM:
package Mathc;
public class Arithmetic
{
public int a,b;
public Arithmetic(int p,int q)
{
a=p;
b=q;
}
public int add()
{
return(a+b);
}
public int sub()
{
return(a-b);
}
public int mul()
{
return(a*b);
}
public int div()
{
return(a/b);
}
}



import java.io.*;
import Mypack.*;
class Usepack               
{
     public static void main(String args[])    throws IOException
     {
         BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
          System.out.println("Enter 2 numbers to perform add,sub and mul");
          int i=Integer.parseInt(stdin.readLine());
          int j=Integer.parseInt(stdin.readLine());
          Mul m=new Mul();
            Addsub a=new Addsub();
            a.add(i,j);
          m.mul(i,j);
          m.add(i,j);
          m.sub(i,j);
     }
}

4. Output:

Enter 2 numbers to perform add,sub and mul
10  20
Result of multiplying is 300
Result of adding is 30
Result of subtracting is -10
Enter 2 numbers to perform add,sub and mul
5  10
Result of multiplying is 50
Result of adding is 15
Result of subtracting is -5
Enter 2 numbers to perform add,sub and mul
2  1
Result of multiplying is 2
Result of adding is 3
Result of subtracting is 1  









24. Experiment no. 24 : Program which imports the package Mathc

1. AIM:
            Write a java program to implement Arithmetic operations the packages.

2. Algorithm:
1.     Start the program.
2.     Import the Mathc packages.
3.     Create a classes and variables with data types.
4.     Create an object of relevant class, to call the procedure.
5.     Create an object to call the packages.
6.     Return arithmetic operations.
7.     Print the concatenation of string.
8.     Stop the program.

3. PROGRAM:
import Mathc.*;
public class PackDemo
{
public static void main(String args[])
{
Arithmetic a=new Arithmetic(10,30);
System.out.println(a.add());
System.out.println(a.sub());
System.out.println(a.mul());
System.out.println(a.div());
}
}

4. OUTPUT:
40
-20
300
0







25. Experiment no. 25 : Program to print JAVA IS SIMPLE in different styles and fonts

1. AIM:
            Write a java program to implement the APPLET PACKAGES programs.

2.     Algorithm:

1.     Start the program.
2.     Import the packages of applet,awt.
3.     Create a classes public void paint(Graphics g).
4.     Assume the values of string, color and font.
5.     g.drawString() application of GUI.
6.     Printing in the separated Applet viewer window.
7.     Stop the program.

3.     PROGRAM:
                                               
import java.awt.*;
import java.applet.*;
import javax.swing.*;
/*
*/
public class JavaDemo extends JApplet
{
public void paint(Graphics g)
{
Font f1=new Font("TimesNewRoman",Font.BOLD|Font.ITALIC,20);
g.setFont(f1);
String str="JAVA IS SIMPLE";
g.drawString(str,10,20);
Font f2=new Font("Helvetica",Font.BOLD,30);
g.setFont(f2);
g.setColor(Color.RED);
g.drawString(str,10,60);
Font f3=new Font("TimesNewRoman",Font.PLAIN,40);
g.setFont(f3);
g.setColor(Color.GREEN);
g.drawString(str,10,100);
}
}



4. OUTPUT

*/





















26. Experiment no. 26: Program to draw Lines, Rectangles, Rounded Rectangles, filled Polygons and Ovals

1. AIM:
            Write a java program to implement the APPLET PACKAGES, draw Lines, Rectangles, Rounded Rectangles, filled Polygons programs.

2. Algorithm:
1.     Start the program.
2.     Import the packages of applet,awt,awt.event.
3.     Create a classes: public void paint(Graphics g).
4.     Assume the values of string, color and font.
5.     g.drawString() application of Graphical User Interface.
6.     Printing in the separated Applet viewer window.
7.     Stop the program.

3. PROGRAM:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/*
public class ShapeDemo extends Applet
{
public void paint(Graphics g)
{
setLayout(new FlowLayout());
g.drawLine(10,20,50,20);
g.drawRect(20,30,30,30);
g.setColor(Color.RED);
g.fillRect(20,30,30,30);
g.drawRoundRect(20,70,50,70,15,15);
g.setColor(Color.GREEN);
g.fillRoundRect(20,70,50,70,15,15);
g.drawOval(20,150,50,50);
g.setColor(Color.BLUE);
g.fillOval(20,150,50,50);
}
}




4. OUTPUT:



























27. Experiment no. 27: Program to implement Action Event that performs Arithmetic Operations

1. AIM:

            Write a java program to implement the APPLET PACKAGES, draw event handlers programs.
2. Algorithm:
1.     Start the program.
2.     Import the packages of applet, awt, awt.event.
3.     Create a classes, methods.
4.     Assume the values of string Integer.parseInt.
5.     while using if loops rotating values.
6.     The event arguments execution.
7.     Printing in the separated Applet viewer window.
8.     Stop the program.

3. PROGRAM:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class EventDemo extends JFrame implements ActionListener
{
 public JLabel l1,l2,l3;
 public JTextField t1,t2;
 public JButton b1,b2,b3;
EventDemo( )
 {
   Container c =  getContentPane( );

 c. setLayout(new FlowLayout());
   l1=new JLabel("NUM 1");
   l2 = new JLabel("NUM 2");
   l3 = new JLabel( );

   t1 = new JTextField(6);
   t2 = new JTextField(6);

   b1 = new JButton("ADD");
   b2 = new JButton("SUB");
   b3 = new JButton("MUL");
 
 c.add(l1);
   c.add(t1);
   c.add(l2);
   c.add(t2);
   c.add(b1);
   c.add(b2);
   c.add(b3);
   c.add(l3);

   b1.addActionListener(this);
   b2.addActionListener(this);
   b3.addActionListener(this);
   setSize(200,200);
setVisible(true);
 }
public void actionPerformed(ActionEvent ae)
{
  int n1,n2,n3=0;
  n1 = Integer.parseInt(t1.getText( ).trim());
  n2 =  Integer.parseInt(t2.getText( ).trim());
  String str = "The result is ";
  if(ae.getSource( ) == b1)
     n3=n1 + n2;
    if(ae.getSource( ) == b2)
     n3= n1-n2;
    if(ae.getSource( ) == b3)
      n3=n1*n2;
  l3.setText(str+"   " +n3);
 }
  public static void main(String args[ ] )
    {
         EventDemo e = new EventDemo( );
}
}  

4. OUTPUT

               

28. Experiment no. 28: Program to implement Mouse Listener
(Mouse Events)

1. AIM:
Write a java program to implement the APPLET PACKAGES, draw Mouse event handler programs.

2. Algorithm:
1.     Start the program.
2.     Import the packages of applet, awt, awt.event.
3.      Create a classes, methods.
4.      Mouse moments, mouse Clicked, mouse Pressed, mouse Released, mouse Entered, mouse Exited, mouse Dragged events args.
5.     g.drawString() application of Graphical User Interface.
6.     While rotating mouse event args.
7.     The mouse event arguments execution.
8.     Printing in the separated Applet viewer window.
9.     Stop the program.

3. PROGRAM:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/*
*/
public class MouseDemo extends Applet implements MouseListener,MouseMotionListener
{
int mx=0;
int my=0;
String msg="";
public void init()
{
addMouseListener(this);
addMouseMotionListener(this);
}
public void mouseClicked(MouseEvent me)
{
mx=20;
my=40;
msg="Mouse Clicked";
repaint();
}
public void mousePressed(MouseEvent me)
{
mx=30;
my=60;
msg="Mouse Pressed";
repaint();
}
public void mouseReleased(MouseEvent me)
{
mx=30;
my=60;
msg="Mouse Released";
repaint();
}
public void mouseEntered(MouseEvent me)
{
mx=40;
my=80;
msg="Mouse Entered";
repaint();
}
public void mouseExited(MouseEvent me)
{
mx=40;
my=80;
msg="Mouse Exited";
repaint();
}
public void mouseDragged(MouseEvent me)
{
mx=me.getX();
my=me.getY();
showStatus("Currently mouse dragged"+mx+" "+my);
repaint();
}
public void mouseMoved(MouseEvent me)
{
mx=me.getX();
my=me.getY();
showStatus("Currently mouse is at"+mx+" "+my);
repaint();
}
public void paint(Graphics g)
{
g.drawString("Handling Mouse Events",30,20);
g.drawString(msg,60,40);
}
}

4. OUTPUT


















29. Experiment no. 29: Program to implement KeyListener (Key Events)

1. AIM:

            Write a java program to implement the APPLET PACKAGES, draw Key event Listeners programs.

2. Algorithm:
1.     Start the program.
2.     Import the packages of applet, awt, awt.event.
3.      Create a classes, methods.
4.      Key moments, key Clicked, key Pressed, key Released, key Entered, key Exited, key Dragged events args.
5.     g.drawString() application of Graphical User Interface.
6.     while rotating key event args.
7.     Printing in the separated Applet viewer window.
8.     Stop the program.

3. PROGRAM:

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/*
*/

public class KeyDemo extends Applet implements KeyListener
{
String msg="";
int x=10;
int y=20;
public void init()
{
addKeyListener(this);
requestFocus();
}
public void keyPressed(KeyEvent ke)
{
showStatus("key pressed");
int key=ke.getKeyCode();
switch(key)
{
case KeyEvent.VK_UP:msg=msg+"";
                    break;
case KeyEvent.VK_SHIFT:msg=msg+"";
                       break;
case KeyEvent.VK_F1:msg+="";
                    break;
}
repaint();
}
public void keyReleased(KeyEvent ke)
{
showStatus("Key Released");
}
public void keyTyped(KeyEvent ke)
{
char ch=ke.getKeyChar();
msg=msg+ch;
showStatus("Key Typed");
}
public void paint(Graphics g)
{
g.drawString(msg,40,40);
}


4.  OUTPUT

     







30. Experiment no. 30: Program to implement BorderLayout

1. AIM:
            Write a java program to implement the APPLET PACKAGES, Create Border layout of window.

2. Algorithm:
1.     Start the program.
2.     Import the packages of applet, awt, awt.event.
3.      Create classes and methods.
4.     Declare the Border Layout directions.
5.     Application of Graphical User Interface.
6.     Printing in the separated Applet viewer window.
7.     Stop the program.

3. PROGRAM:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
*/
public class BorderDemo extends Applet
{
  public void init( )
  {
   setLayout(new BorderLayout( ));

   add(new Button("CSE students"),BorderLayout.NORTH);
   add(new Label("studying well"),BorderLayout.SOUTH);
   add(new Button("right"),BorderLayout.EAST);
   add(new Button("left"),BorderLayout.WEST);

   String msg = "This is the Demo for BorderLayout"+"done by Cse-1 students";
   add(new TextArea(msg),BorderLayout.CENTER);
  }
}







4. OUTPUT



























31. Experiment no. 31: Program to implement Radio Listener

1. AIM:
            Write a java program to implement the APPLET PACKAGES, Create Radio buttons in a window.
2. Algorithm:
1.     Start the program.
2.     Import the packages of applet, awt, awt.event.
3.      Create classes, methods.
4.     Declare the Radio buttons and give values.
5.     Application of Graphical User Interface.
6.     Printing in the separated Applet viewer window.
7.     Stop the program.

3. PROGRAM:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class RadDemo extends JFrame implements ActionListener
{
  public JLabel l1;
  public JRadioButton r1,r2,r3;
  RadDemo()
  {
    Container c = getContentPane( );
    c.setLayout(new FlowLayout( ));
    l1=new JLabel();
    r1 = new JRadioButton("CSE");
    r2 = new JRadioButton("ECE");
    r3 = new JRadioButton("EEE");
    ButtonGroup bg = new ButtonGroup( );
    bg.add(r1);
    bg.add(r2);
    bg.add(r3);
    c.add(r1);
    c.add(r2);
    c.add(r3);
    c.add(l1);
    r1.addActionListener(this);
    r2.addActionListener(this);
    r3.addActionListener(this);
    setSize(200,200);
    setVisible(true);
   }
  public void actionPerformed(ActionEvent ae)
  {
    if(ae.getActionCommand()=="CSE")
    {
     l1.setText("You selected CSE");
    }                   
    if(ae.getActionCommand()=="ECE")
    {
     l1.setText("You selected ECE");
    }
    if(ae.getActionCommand()=="EEE")
    {
     l1.setText("You selected EEE");
    }
  }
  public static void main(String args[])
  {
    RadDemo rd=new RadDemo();
  }
}

4. OUTPUT









References:
  1. The complete reference Java, J2SE 5th edition, Herbert Schild, TMH publishing Company limited, New Delhi.
  2. Big Java 2nd edition, Cay Horstmann, John Wiley and Sons.
  3. Java how to programme, sixth edition, H.M.Dietel and P.J. Deitel.
  4. Core java 2,Vol2, Advanced Fundamentals Cay, S. Horshmann and Gary Comell Seventh edition Person edition.

No comments:

Post a Comment