WAP to Testing the Math class methods....

public class MainClass
{
   public static void main( String args[] )
   {
      System.out.printf( "Math.abs( 23.7 ) = %f\n", Math.abs( 23.7 ) );
      System.out.printf( "Math.abs( 0.0 ) = %f\n", Math.abs( 0.0 ) );
      System.out.printf( "Math.abs( -23.7 ) = %f\n", Math.abs( -23.7 ) );
      System.out.printf( "Math.ceil( 9.2 ) = %f\n", Math.ceil( 9.2 ) );
      System.out.printf( "Math.ceil( -9.8 ) = %f\n", Math.ceil( -9.8 ) );
      System.out.printf( "Math.cos( 0.0 ) = %f\n", Math.cos( 0.0 ) );
      System.out.printf( "Math.exp( 1.0 ) = %f\n", Math.exp( 1.0 ) );
      System.out.printf( "Math.exp( 2.0 ) = %f\n", Math.exp( 2.0 ) );
      System.out.printf( "Math.floor( 9.2 ) = %f\n", Math.floor( 9.2 ) );
      System.out.printf( "Math.floor( -9.8 ) = %f\n", Math.floor( -9.8 ) );
      System.out.printf( "Math.log( Math.E ) = %f\n", Math.log( Math.E ) );
      System.out.printf( "Math.log( Math.E * Math.E ) = %f\n", Math.log( Math.E * Math.E ) );
      System.out.printf( "Math.max( 2.3, 12.7 ) = %f\n",Math.max( 2.3, 12.7 ) );
      System.out.printf( "Math.max( -2.3, -12.7 ) = %f\n",Math.max( -2.3, -12.7 ) );
      System.out.printf( "Math.min( 2.3, 12.7 ) = %f\n",Math.min( 2.3, 12.7 ) );
      System.out.printf( "Math.min( -2.3, -12.7 ) = %f\n",Math.min( -2.3, -12.7 ) );
      System.out.printf( "Math.pow( 2.0, 7.0 ) = %f\n",Math.pow( 2.0, 7.0 ) );
      System.out.printf( "Math.pow( 9.0, 0.5 ) = %f\n", Math.pow( 9.0, 0.5 ) );
      System.out.printf( "Math.sin( 0.0 ) = %f\n", Math.sin( 0.0 ) );
      System.out.printf( "Math.sqrt( 900.0 ) = %f\n", Math.sqrt( 900.0 ) );
      System.out.printf( "Math.sqrt( 9.0 ) = %f\n", Math.sqrt( 9.0 ) );
      System.out.printf( "Math.tan( 0.0 ) = %f\n", Math.tan( 0.0 ) );
   }
}
 
 
OUTPUT:-
 
Math.abs( 23.7 ) = 23.700000
Math.abs( 0.0 ) = 0.000000
Math.abs( -23.7 ) = 23.700000
Math.ceil( 9.2 ) = 10.000000
Math.ceil( -9.8 ) = -9.000000
Math.cos( 0.0 ) = 1.000000
Math.exp( 1.0 ) = 2.718282
Math.exp( 2.0 ) = 7.389056
Math.floor( 9.2 ) = 9.000000
Math.floor( -9.8 ) = -10.000000
Math.log( Math.E ) = 1.000000
Math.log( Math.E * Math.E ) = 2.000000
Math.max( 2.3, 12.7 ) = 12.700000
Math.max( -2.3, -12.7 ) = -2.300000
Math.min( 2.3, 12.7 ) = 2.300000
Math.min( -2.3, -12.7 ) = -12.700000
Math.pow( 2.0, 7.0 ) = 128.000000
Math.pow( 9.0, 0.5 ) = 3.000000
Math.sin( 0.0 ) = 0.000000
Math.sqrt( 900.0 ) = 30.000000
Math.sqrt( 9.0 ) = 3.000000
Math.tan( 0.0 ) = 0.000000
 

WAP to Getting FileStore information.

import java.nio.file.FileStore;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;

public class Test
 {
  static final long kiloByte = 1024;

  public static void main(String[] args) throws Exception
  {
    FileSystem fileSystem = FileSystems.getDefault();

    for (FileStore fileStore : fileSystem.getFileStores())
     {
      long totalSpace = fileStore.getTotalSpace() / kiloByte;
      long usedSpace = (fileStore.getTotalSpace() - fileStore
          .getUnallocatedSpace()) / kiloByte;
      long usableSpace = fileStore.getUsableSpace() / kiloByte;
      String name = fileStore.name();
      String type = fileStore.type();
      boolean readOnly = fileStore.isReadOnly();
    }
  }
}

Write a program to HTTP Grab with FileChannel.

import java.io.FileOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.SocketChannel;

public class MainClass {

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

    URL u = new URL("http://www.java2s.com");
    String host = u.getHost();
    int port = 80;
    String file = "/";

    SocketAddress remote = new InetSocketAddress(host, port);
    SocketChannel channel = SocketChannel.open(remote);
    FileOutputStream out = new FileOutputStream("yourfile.htm");
    FileChannel localFile = out.getChannel();

    String request = "GET " + file + " HTTP/1.1\r\n" + "User-Agent: HTTPGrab\r\n"
        + "Accept: text/*\r\n" + "Connection: close\r\n" + "Host: " + host + "\r\n" + "\r\n";

    ByteBuffer header = ByteBuffer.wrap(request.getBytes("US-ASCII"));
    channel.write(header);

    ByteBuffer buffer = ByteBuffer.allocate(8192);
    while (channel.read(buffer) != -1) {
      buffer.flip();
      localFile.write(buffer);
      buffer.clear();
    }

    localFile.close();
    channel.close();
  }
}

Write a program to Save Web page to a file.

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;

import javax.swing.text.html.parser.DTD;

public class MainClass
    {
     public static void main(String[] args)
     { 
     try
      {
      URL u = new URL("http://www.java2s.com");
      OutputStream out = new FileOutputStream("test.htm");
      InputStream in = u.openStream();
      DTD html = DTD.getDTD("html");
      System.out.println(html.getName());
      in.close();
      out.flush();
      out.close();
      } 
      catch (Exception e) {
      System.err.println("Usage: java PageSaver url local_file");
    }

  }

}

Write a program to check your own ip address...

import java.net.*;

class CheckIp
{
       public static void main(String args[])
      {
       try
       {
         InetAddress address=InetAddress.getLocalHost();
         System.out.println(address);
        }
     catch(UnknownHostException e)
   {
  System.out.println("could not find yahoo .com");
}
}
}

Write a program to comarision of two string.

class stringmethod
  {
    public static void main(String[] args)
      {
        String string1 = "Hi";
        String string2 = new String("Hello");
        if (string1 == string2)
           {
            System.out.println("The strings are equal.");
            }
        else
               {
                System.out.println("The strings are unequal.");
                }
        }
}               


Output of the program:-


The strings are unequal

Write a program to print a Stact Trace Message.

public class PrintStack
{
           public static void main (String args[])
              {
               String str = "Exception" ;
               int len=0;
           try
              {
             StringBuffer sbuf = new StringBuffer(str);
            len = str.length() ;
           for(int ct=len;ct>=0;ct--)
            {
          System.out.print(sbuf.charAt(ct));
           }
       }
       catch(Exception e)
      {
      e.printStackTrace();
       }
     }
   }



OUTPUT OF THE PROGRAM:-


java.lang.StringIndexOutOfBoundsException: String index out of range: 9
at java.lang.StringBuffer.charAt(Unknown Source)
at PrintStack.main(PrintStack.java:13)

Write a program to take input from user and give the output.

/* Reading from the console. */
import java.util.Scanner;

import static java.lang.System.out;

public class ConsoleInput {

  public static void main(String[] args) {

    // Create a Scanner which is chained to System.in, i.e. to the console.
    Scanner lexer = new Scanner(System.in);

    // Read a list of integers.
    int[] intArray = new int[3];
    out.println("Input a list of integers (max. " + intArray.length + "):");
    for (int i = 0; i < intArray.length;i++)
      intArray[i] = lexer.nextInt();
    for (int i : intArray)
       out.println(i);


    // Read names
    String firstName;
    String lastName;
    String name;
    String repeat;
    do {
      lexer.nextLine(); // Empty any input still in the current line
      System.out.print("Enter first name: ");
      firstName = lexer.next();
      lexer.nextLine();
      System.out.print("Enter last name: ");
      lastName = lexer.next();
      lexer.nextLine();
      name = lastName + " " + firstName;
      System.out.println("The name is " + name);
      System.out.print("Continue? (y/n): ");
      repeat = lexer.next();
    } while (repeat.equals("y"));
    lexer.close();
  }
}

Write a program to calculate the date and the date formet and time formate.

/*
   Formatted Time and Date
*/
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;

import static java.lang.System.out;
import static java.util.Calendar.*;
import static java.util.Locale.*;

class FormattedTimeDate {

  public static void main(String[] args) {

Calendar myCalender = Calendar.getInstance();

{
// Formatting the hour
out.printf("Hour(00-24):%tH\n", myCalender);
out.printf("Hour(01-12):%tI\n", myCalender);
out.printf("Hour(0-23):%tk\n", myCalender);
out.printf("Hour(1-12):%tl\n", myCalender);
}

{
// Formatting the minutes
out.printf("Minutes(00-59):%tM\n", myCalender);
}

{
// Formatting the seconds
out.printf("Seconds(00-60):%tS\n", myCalender);
// out.printf("Millieseconds(000-999):%tL\n", myCalender);
out.printf("Nanoseconds(000000000-999999999):%tN\n", myCalender);
}

{
// PM/pm and AM/am
out.printf("%tp\n", myCalender);
out.printf("%Tp\n", myCalender);
}

{
// Formatting the year
out.printf("%tC%n", myCalender);
out.printf("%tY%n", myCalender);
out.printf("%ty%n", myCalender);
}

{
// Formatting the month
out.printf("%tB%n", myCalender);
out.printf("%tb%n", myCalender);
out.printf("%th%n", myCalender);
out.printf("%tm%n", myCalender);
out.printf(US,"%tB%n", myCalender);

}

{
// Formatting the day
out.printf("%tA%n", myCalender);
out.printf("%ta%n", myCalender);
out.printf("%tj%n", myCalender);
out.printf("%td%n", myCalender);
out.printf("%te%n", myCalender);
out.printf(US,"%tA%n", myCalender);
}

{
// Composite Date/Time Format Conversions
out.printf("%tR%n", myCalender);
out.printf("%tT%n", myCalender);
out.printf("%tr%n", myCalender);
out.printf("%tD%n", myCalender);
out.printf("%tF%n", myCalender);
out.printf("%tc%n", myCalender);
}

{
// Misc. usage
// Note that the specifiers refer to the same argument in the format string.
out.printf("%1$tm %1$te %1$tY%n", myCalender);
out.printf(US, "The world will end on %1$tA, %1$te. %1$tB %1$tY" +
               " at %1$tH:%1$tM:%1$tS.%n", myCalender);
out.printf("The world will end on %1$tA, %1$te. %1$tB %1$tY at %1$tH:%1$tM:%1$tS.%n",
           myCalender);
//out.printf("%1$^-15tA, %1$6te. %1$^10tB %1$tY, %1$tH:%1$tM:%1$tS.%n", <==== !!!!!!!!!
//           myCalender);

Calendar birthdate = new GregorianCalendar(1949, MARCH, 1);
out.printf("Author's Birthday: %1$tD%n", birthdate);
}

  }
}

Write a program of logic gates.

import logicgate.And;
import logicgate.Or;
import logicgate.Not;
import java.util.Scanner;

class Gate
{
public static void main(String[] args)
{
  int a,b;
System.out.println("Enter Values");
 Scanner sc=new Scanner(System.in);
  And and=new And();
  Or or=new Or();
  Not not=new Not();

a=sc.nextInt();
b=sc.nextInt();

and.doOperation(a,b);
or.doOperation(a,b);
not.doOperation(a);
}
}

Write a program to perform 3X3 matrix addition.

class MatrixAdd
{
 public static void main(String arg[])
 {
  int a[][]= new int[3][3];
  int b[][] = new int[3][3];
  int c[][]= new int[3][3];
  int i,j,k=0;
  for(i=0;i<3;i++)
  {
   for(j=0;j<3;j++)
   {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
    a[i][j]=k;
     k++;
    b[i][j]=k;
   }
  }
  System.out.println("1st mat. is");
  for(i=0;i<3;i++)
  {
   for(j=0;j<3;j++)
    {
     System.out.print("  " +a[i][j]);
    }
     System.out.println("\n");
   }
   System.out.println("2nd mat. is");
  for(i=0;i<3;i++)
  {
   for(j=0;j<3;j++)
    {
     System.out.print("  " +b[i][j]);
    }
     System.out.println("\n");
   }
  System.out.println("addtn is");
 for(i=0;i<3;i++)
  {
   for(j=0;j<3;j++)
   {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
    c[i][j]=a[i][j]+b[i][j];
   }
  }
   for(i=0;i<3;i++)
  {
   for(j=0;j<3;j++)
    {
     System.out.print("  " +c[i][j]);
    }
     System.out.println("\n");
   }
 }
}

Write a program to print the diagonal elements of 3X3 matrix .

class Dmatrix
{
 public static void main(String arg[])
 {
  int a[][]= new int[3][3];
  int i,j,k=0;
  for(i=0;i<3;i++)
  {
   for(j=0;j<3;j++)
   {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
    a[i][j]=k;
     k++;
   }
  }
  for(i=0;i<3;i++)
  {
   for(j=0;j<3;j++)
    {
     System.out.print("  " +a[i][j]);
         
     }
     System.out.println("\n");
    }
System.out.println("digo. elment are ");
  for(i=0;i<3;i++)
  {
   for(j=0;j<3;j++)
    {
     if(i!=j)
     System.out.print(" ");
     else
     System.out.println(" "+a[i][j]);
     }
    }
 }
}
 

Write a program to take input from user in a integer and print a table of that value , like Enter Value : 3 3 X 1 = 3 3 X 2 = 6 . 3 X 10 = 30

class Table
{
public static void main(String args[])
{
int a=Integer.parseInt(System.console().readLine("enter the value of a"));
int i,count;
for(i=1;i<=10;i++)
{
count=a*i;
System.out.println(" "+count);
}
}
}

Write a program that ask user to enter values in two integer a and b, now write a code to swap the values of a and b without taking any temporary variable.

import java.util.Scanner;
public class Swap
{
public static void main(String[]hjk)
{
int a,b;
Scanner s1=new Scanner(System.in);
System.out.println("enter first num as a");
a=s1.nextInt();
Scanner s2=new Scanner(System.in);
System.out.println("enter second num as b");
b=s2.nextInt();
a=a+b;
b=a-b;
a=a-b;
System.out.println("the swapped num are");
System.out.println("a="+a);
System.out.println("b="+b);

}
}

WAP to print the pelendrome 5 digits.

class Palendrome
  {
    public static void main(String args[])
      {
        int n=Integer.parseInt(System.console().readLine("enter the 5 no. digitno. n"));
        int a=n;
        int b=a%10;
        b=b+10000;
        a=a/10;

        int c=a%10;
        c=c+1000;
        a=a/10;

        int d=a%10;
        d=d+100;
        a=a/10;

        int e=a%10;
        e=e+10;
        a=a/10;

        int sum=a+b+c+d+e;
        if(sum==n)
        {
          System.out.println("Entered no. is pelendrome");
        }
        else
        {
          System.out.println("Entered no. is pelendrome");
        }
     }
  }

W.A.P. to convert Celsius to Fahrenheit

class ConvertCtoF
  {
     public static void main(String[] args)
       {
         int c;
        
         System.out.println("Fahrenheit is:->");
         for(c=0;c<=100;c++)
           {
             float f=c*(9/5f)+32;
          
             System.out.println("Celsius="+c+"Fahrenheit="+f);
           }
        }
   }
            

Write a program to display grade message according to the marks obtained by the student

import java.util.*;
class StudentGrade
  {
   public static void main(String a[])
     {
       int studentmarks;
       Scanner in=new Scanner(System.in);
       System.out.println("enter the marks of student");
       studentmarks=in.nextInt();
       if((studentmarks>=300)&&(studentmarks<=500))
         {
           System.out.println("Grade A");
         }
       if((studentmarks>=225)&&(studentmarks<=299))
         {
           System.out.println("Grade B");
         }
       if((studentmarks>=165)&&(studentmarks<=224))
         {
           System.out.println("Grade C");
         }
       if(studentmarks<165)
         {
           System.out.println("Fail");
         }
     }
   }

Write a program to check that the entered no is prime or not.

import java.util.*;
class PrimeNo
 {
  public static void main(String[] args)
  {
   int num;
   int i;
   Scanner in=new Scanner(System.in);
   System.out.println("enter the value of num");
   num=in.nextInt();
   for (i=2; i < num ;i++ )
    {
     int n = num%i;
     if (n==0)
       {
         System.out.println("not Prime!");
         break;
       }
    }
     if(i == num)
      {
       System.out.println("Prime number!");
      }
  }
}

Write a program to print the Fibonacci series.

class Fib {
    public static void main(String args[ ])
         {
        int n=Integer.parseInt(System.console().readLine("Enter the value of n="));
        int i,a=0,b=1,c=0;
        System.out.println("fibnoci series is:");
       
        System.out.print(a);
        System.out.print(" "+b);
        for(i=0;i<n-2;i++)
                {
            c=a+b;
            a=b;
            b=c;
            System.out.print(" "+c);
        }
        System.out.println();
        System.out.print("th value of the series is: "+c);
    }
}

Write a program to find out the factorial of the given no.

class Factorial
 {
   public static void main(String args[])
     {
       int i,num;
       num=Integer.parseInt(System.console().readLine("Enter the number="));
       int fact=1;
       for(i=1;i<=num;i++)
         {
           fact=fact*i;
          
         }
      
       System.out.println("factorial of Number="+fact);
     }
 }