Thursday, December 23, 2010

Sir Assignment -1

INDEX

Sr.
DEFINITION
1
Write a program to find AVERAGE of 3 byte type variable.

2
Write a program to find sum of 5 short type variables.
3
Write a program to perform different bit wise operations.
4
Write a java program that will compute the amount of interest that is earned on an investment over a period of 5 years.  The value of the investment at the end of each year is output.
5
Write a program to convert Fahrenheit to Celsius. The formula for the conversion is as follows:  Celsius =( 5/9 ) * ( Fahrenheit – 32 )
6
Print out the alphabet and ascii value on one line of output.
e.g. A 65 B 66 C 67….Z 90
7
Write a program that displays the divisors of number, and then it prints the result.
8
Write a program to find primes nos. from 2 to 100.
9
Write program in java to generate following output. Use Command line argument for this. (Hint : Accept N number from user on command line)
*
*      *
*      *      *
*     *      *      *
*      *      *
*     *
*
     *
        *    *
        *    *    *
        *    *    *     *
        *    *    *
        *    *
        *
1 2 3 4 5 1 2 3 4 5
1 2 3 4       2 3 4 5
1 2 3             3 4 5
1 2                   4 5
1                         5
1 2                   4 5
1 2 3             3 4 5
1 2 3 4       2 3 4 5
1 2 3 4 5 1 2 3 4 5



*
*   *   *
*

(Input : 3)
10
Write a simple java application to print a pyramid. The first line has one character, 2nd line has two characters and so on. The character to be used in the pyramid is taken as a command line argument.
A) Input    :    HEMAL
    Output  : H
                   H       E
                   H       E       M
                   H       E       M       A
                   H        E       M       A       L

B) Input :  HEMAL
         
                         H
                   E          E  
               M                   M
            A                          A
        L                                 L
           A                           A
               M                   M        
                   E          E   
                         H



Program: - 1

/*write a program to find AVERAGE of 3 byte type variable .*/
import java.io.*;

class test
{
          byte a,b,c,A,Ave;
                            
          public void line(int k)
          {
                   for (int i =0; i<k;i++)
                   {
                             System.out.print("-");
                   }
          }

          test()
          {
                   a=0;b=0;c=0;
          }
         
          public void getdata()
          {       
                            
                   try
                   {
                            
                             DataInputStream ds=new DataInputStream(System.in);
                             line(60);
                             System.out.println();
         
                             for (int i=0;i<3;i++)
                             {
                                      System.out.println();
                                      System.out.print(" ==> Enter any number : ");
                                      String ab=ds.readLine();
                                      System.out.println();
                                      int ij=Integer.parseInt(ab);
                                      A+=ij;
                             }
         
         
                             line(60);
                             System.out.println();
                             System.out.println(" \n ==> The Sum is       : " +A);
                             Ave = ((byte)(A/3));
                             System.out.println(" \n ==> Average of 3 byte type variable (" + A +"/3) : " +Ave);
                             System.out.println();
                             line(60);
                   }
                   catch(Exception e)
                   {
                   }
          }
}

class Aver
{
          public static void main (String args[])
          {
                   test t = new test();
                  
                   try
                   {
                             t.getdata();
                   }
                   catch(Exception e)
                   {
                             System.out.println(" \n ==> Exception Occure :" +e);
                   }
          }
}
Output:-

F:\>javac Aver.java

Note: Aver.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.

F:\>java Aver
------------------------------------------------------------

 ==> Enter any number : 1


 ==> Enter any number : 2


 ==> Enter any number : 3

------------------------------------------------------------

 ==> The Sum is       : 6

 ==> Average of 3 byte type variable (6/3) : 2

------------------------------------------------------------
Program: - 2

/*Write a program to find sum of 5 short type variable .*/
class a
{
          short  s[] = {10,4,2,6,3};
          int sum = 0;
          int n;
          float f = 0.0f;
         
          public void putdata()
          {
                   for (int i =0;i<5;i++)
                             System.out.println (" ==> The Short Variables are : " +s[i]);
                             sum +=s[i]; // Automatic Casting.
                   }
                   line1(60);
                   System.out.println(" ==> The sum of this short array is : "+sum);
                   f=sum/5;
                   line1(60);
                   System.out.println(" ==> The Average is the : "+f);
          }

          public void line1(int n)
          {
                   for (int i =0;i<n;i++)
                   {
                             System.out.print("-");
                   }
                   System.out.println();
          }
         
}

class sht
{
          public static void main(String args[])
          {
                   a a1 = new a();
                  
                   a1.line1(60);
                   a1.putdata();
                   a1.line1(60);
          }
}

Output:-

F:\>javac sht.java

F:\>java sht
------------------------------------------------------------
 ==> The Short Variables are :  10
 ==> The Short Variables are :  4
 ==> The Short Variables are :  2
 ==> The Short Variables are :  6
 ==> The Short Variables are :  3
------------------------------------------------------------
 ==> The sum of this short array is : 25
------------------------------------------------------------
 ==> The Average is the : 5.0
------------------------------------------------------------


Program: - 3

/*Write a program to perform different bit wise operations.*/
public class Main
{
          public static void main(String args[])
          {
                   String binary[] = {   "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111","1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
                  
                   int i,j,k,l,m,n;

                   int a = 3; // 0 + 2 + 1 or 0011 in binary
                   int b = 6; // 4 + 2 + 0 or 0110 in binary
                   int c = a | b;
                   int d = a & b;
                   int e = a ^ b;
                   int f = (~a & b) | (a & ~b);
                   int g = ~a & 0x0f;
                  
                   i = b << 2;
                   j = b >> 2;
                   k = a|= 4;
                   l = b >>= 1;
                   m = c <<= 1;
                   n = a ^= c;
         
                   System.out.println();              

                   System.out.println("        a = " +a);
                   System.out.println("        b = " +b);
                    System.out.println("      a|b = " +c);
                   System.out.println("      a&b = " +d);
                    System.out.println("      a^b = " +e);
                   System.out.println("~a&b|a&~b = " +f);
                   System.out.println("       ~a = " +g);

                   System.out.println();
  
                    System.out.println("        a = " + binary[a]);
                   System.out.println("        b = " + binary[b]);
                    System.out.println("      a|b = " + binary[c]);
                   System.out.println("      a&b = " + binary[d]);
                    System.out.println("      a^b = " + binary[e]);
                   System.out.println("~a&b|a&~b = " + binary[f]);
                   System.out.println("       ~a = " + binary[g]);
                            
                   System.out.println();
  
                   System.out.println("   b << 2 = " + i);
                   System.out.println("   b >> 2 = " + j);
                   System.out.println("    a|= 4 = " +k);
                   System.out.println("  b >>= 1 = " +l);
                   System.out.println("  c <<= 1 = " +m);
                   System.out.println("  a  ^= c = " +n);
          }
}
Output:-

F:\>javac Main.java
F:\>java Main

        a = 9
        b = 3
      a|b = 14
      a&b = 2
      a^b = 5
~a&b|a&~b = 5
       ~a = 12

        a = 1001
        b = 0011
      a|b = 1110
      a&b = 0010
      a^b = 0101
~a&b|a&~b = 0101
       ~a = 1100

   b << 2 = 24
   b >> 2 = 1
    a|= 4 = 7
  b >>= 1 = 3
  c <<= 1 = 14
  a  ^= c = 9

Program: - 4

/* Write a java program that will compute the amount of interest that is earned on an investment over a period of 5 years.  The value of the investment at the end of each year is output. */

class cmInt 
{                
          public static void main (String args[])
          {
                   a a1 = new a();

                   a1.line(60);
                   a1.getdata();
                   a1.line(60);
                   a1.sInt();
                   a1.cInt();
                   a1.line(60);
          }
}

class a
{
          int p,n;
          private float r ;
          double I,b,c,d,e;


          a()
          {
                   r = 2.0f;
                   p = 2000;
                   n = 5;
                  
                   I = 0.0;
                   b = 0.0;
                   c = 0.0;
                   d = 0.0;
                   e = 0.0;
          }
          public void line(int n)
          {
                   for (int i = 0;i<n;i++)
                   {
                             System.out.print("-");
                   }
                   System.out.println();
          }
         
          public void cInt()
          {
                   b = r/100;
                   c = 1+b;
                   d = c*c;
                   e = p*d;
                   System.out.println(" ==> The Compound Interest is : " +e);
          }

          public void sInt()
          {
                   I = p*r*n/100;
                  
                   System.out.println(" ==> The Simple Interest is   : " +I);
          }
                  
          public void getdata()
          {
                   System.out.println(" ==> The Principal Amount is  : " +p);
                   System.out.println (" ==> The Rate is              : " +r);
                   System.out.println(" ==> The number of years is   : " +n);
          }
}       
Output:-
------------------------------------------------------------
 ==> The Principal Amount is  : 2000
 ==> The Rate is              : 2.0
 ==> The number of years is   : 5
------------------------------------------------------------
 ==> The Simple Interest is   : 200.0
 ==> The Compound Interest is : 2080.799998176098
------------------------------------------------------------




Program: - 5

/* Write a program to convert Fahrenheit to Celsius.The formula for the conversion is as follows:Celsius =( 5/9 )*(Fahrenheit – 32 ) */

class cnv
{
          public static void main (String args[])
          {
                   a a1 = new a();
                  
                   System.out.println();
                   a1.line(60);
                   a1.c1();
                   a1.line(60);
          }
}

class a
{
          float f ;
          float d;

          a()
          {
                   f=40.0f;
          }

          public void line(int n)
          {
                   for (int i=0;i<n;i++)
                   {
                             System.out.print("-");
                   }
                   System.out.println();
          }

          public void c1()
          {
                   d=( 5/9 );
                   float e =(f - 32 );
                   float g = d-e;
                   System.out.println(" ==> The Fehrenheit to celsius is : "+g);
          }       
         
}

Output:-

F:\>javac cnv.java

F:\>java cnv

------------------------------------------------------------
 ==> The Fehrenheit to celsius is : -8.0
------------------------------------------------------------

Program: - 6

/* Print out the alphabet and ascii value on one line of output.
          e.g. A 65 B 66 C 67….Z 90 */
class bcd
{
         

          public void ch()
          {
                   String k = new String("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
                   System.out.println();
                   for(int i=0;i<26;i++)
                   {
                             char p = (k.charAt(i));
                             System.out.print(p+" ");
                             System.out.print((int)p+" ");
                   }
          }
}
class as
{
          public static void main (String args[])
          {
                   bcd b = new bcd();
                   b.ch();
          }
}

Output:-

F:\>javac as.java
F:\>java as

A 65 B 66 C 67 D 68 E 69 F 70 G 71 H 72 I 73 J 74 K 75 L 76 M 77 N 78 O 79 P 80 Q 81 R 82 S 83 T 84 U 85 V 86 W 87 X 88 Y 89 Z 90

Program: - 7

/* Write a program that displays the divisors of number, and then it prints the result.*/
import java.io.*;

class abc
{
          public static void main(String args[])
          {
                   int n1,n2;
                   float res;
                   DataInputStream ds = new DataInputStream(System.in);
         
                   try
                   {
                             System.out.print("\n ==> Enter val1 : ");
                             n1 = Integer.parseInt(ds.readLine());
         
                             System.out.print("\n ==> Enter val2 : ");
                             n2 = Integer.parseInt(ds.readLine());
                  
                             System.out.println("\n ==> So that Devisor is : "+n2);

                             res= (float)n1/n2;
                             System.out.println("\n ==> Result is : " +res);
                   }
                   catch(ArithmeticException a)
                   {
                             System.out.println(" Exception is : "+a);
                   }
                   catch(Exception e)
                   {       
                             System.out.println(" Exception is : "+e);
                   }
          }
}

Output:-

F:\>javac abc.java
Note: abc.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.

F:\>java abc

 ==> Enter val1 : 24

 ==> Enter val2 : 2

 ==> So that Devisor is : 2

 ==> Result is : 12.0

Program: - 8

/* Write a program to find primes nos. from 2 to 100.*/
class p
{
          public void p1()
          {
                   for (int i=2;i<=200;i++)
                   {
                             int k = 0;

                             for (int j=2;j<=i;j++)
                             {                
                                      if (i%j ==0)
                                      {
                                                k=1;
                                      }
                                      break;
                             }
                             if (k==0)
                             {
                                      System.out.print(" "+i);
                             }
                   }

          }
}
class prime
{
          public static void main (String args[])
          {
                   p op = new p();
                   op.p1();
          }
}





Output:-

F:\>javac prime.java

F:\>java prime

 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99 101 103 105 107 109 111 113 115 117 119 121 123 125 127 129 131 133 135 137 139 141 143 145 147 149 151 153 155 157 159 161 163 165 167 169 171 173 175 177 179 181 183 185 187 189 191 193 195 197 199

Program: - 9

/* Write program in java to generate following output. Use Command line argument for this. (Hint: Accept N number from user on command line)

                 *
              *      *
          *      *      *
       *     *      *      *
          *      *      *
              *     *
                 *
        *
        *    *
        *    *    *
        *    *    *     *
        *    *    *
        *    *
        *
 1 2 3 4 5 1 2 3 4 5
 1 2 3 4       2 3 4 5
 1 2 3             3 4 5
 1 2                   4 5
 1                         5
 1 2                   4 5
 1 2 3             3 4 5
 1 2 3 4       2 3 4 5
 1 2 3 4 5 1 2 3 4 5



               *
           *   *   *
               *

(Input : 3)

*/
a) class aa
{
          int i,j,s;

          public void t()
          {                
                   System.out.println();
                   for (i = 5;i>=1;i--)
                   {
                             for (s=1;s<=i;s++)
                             {
                                      System.out.print(" ");
                             }
                   for(j=i;j<=5;j++)
                   {
                             System.out.print("* ");
                   }
                   System.out.println();
                   }
          }

          public void t1()
          {                
                   for (i = 4;i>=1;i--)
                   {
                             for (s=i;s<=4;s++)
                             {
                                      System.out.print(" ");
                             }
                   for(j=1;j<=i;j++)
                   {
                             System.out.print(" *");
                   }
                   System.out.println();
                   }
          }       
}

class tr1
{
          public static void main(String args[])
          {
                   aa a = new aa();
                   a.t();
                   a.t1();
          }
}
Output:-

     *
    * *
   * * *
  * * * *
 * * * * *
  * * * *
   * * *
    * *
     *


b)
public class one
{ 
    public static void main (String[] args) 
    { 
         String starsingle = "*";     
         final int MAX_ROWS = 4; 
         
         for (int row = MAX_ROWS; row >= 0; row--) 
         {   
           for (int starA = 2; starA >= row; starA--)   
           { 
                   System.out.print (" *"); 
           } 
           System.out.println(); 
         } 
          
        for (int row = MAX_ROWS; row >= 0; row--) 
        { 
            for (int starB = 1; starB <= row; starB++) 
            { 
                      System.out.print (" *"); 
            } 
            System.out.println(); 
         }  
          
   }
}

Output:-

F:\>javac one.java

F:\>java one


 *
 * *
 * * *
 * * * *
 * * *
 * *
 *
Program: - 10

/* Write a simple java application to print a pyramid. The first line has one character, 2nd line has two characters and so on. The character to be used in the pyramid is taken as a command line argument.
A) Input    :  HEMAL
    Output: H
                H       E
                H       E       M
                H       E       M       A
                H       E       M       A       L */

import java.io.*;


class pyra
{
          public static void main (String args[])
          {
                   p1 p = new p1();
                   p.line(60);
                   System.out.println(" ==> Just Testing. ");
                   p.line(60);
                   p.tri();
                   p.line(60);
          }
}

class p1
{
          //String s[];
          DataInputStream ds = new DataInputStream(System.in);

          public void line(int n)
          {
                   for (int i=0;i<n;i++)
                   {
                             System.out.print("-");
                   }
                   System.out.println();
          }

          public void tri()
          {
                   try
                   {
                             System.out.print(" ==> Enter your Name : ");
                             String s = ds.readLine();
                             line(60);


                             System.out.println();
                             for (int i=0;i<s.length();i++)
                             {       
                                      System.out.print(" ==>");
                                      for (int j=0;j<=i;j++)
                                      {
                                                System.out.print(" "+ s.charAt(j));
                                      }
                                      System.out.println("\n");
                             }
                   }
                   catch(Exception e)
                   {
                             System.out.println(" Exception is : " +e);
                   }
          }
                  
}

Output:-

F:\>javac pyra.java
Note: pyra.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.

F:\>java pyra
------------------------------------------------------------
 ==> Just Testing.
------------------------------------------------------------
 ==> Enter your Name : HEMAL
------------------------------------------------------------

 ==> H

 ==> H E

 ==> H E M

 ==> H E M A

 ==> H E M A L

------------------------------------------------------------

No comments:

Post a Comment