-->
Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Tuesday, December 15, 2015

Exception Handling, I/O, Applets



1.       Write a Java Program which takes two file names as command line arguments. The program should copy the contents of first file into the second file. The program should handle FileNotFoundException and ArrayIndexOutofBoundsException. Also provide a generic catch statement which can handle all the types of exceptions. Provide a finally block too.

import java.io.*;
public class Main {
   public static void main(String[] args)
   throws Exception {
      BufferedWriter out1 = new BufferedWriter
      (new FileWriter("srcfile"));
      out1.write("CODEED BY CLINTON \n");
      out1.close();
      InputStream in = new FileInputStream
      (new File("srcfile"));
      OutputStream out = new FileOutputStream
      (new File("destnfile"));
      byte[] buf = new byte[1024];
      int len;
      while ((len = in.read(buf)) > 0) {
         out.write(buf, 0, len);
      }
      in.close();
      out.close();
      BufferedReader in1 = new BufferedReader
      (new FileReader("destnfile"));
      String str;
      while ((str = in1.readLine()) != null) {
         System.out.println(str);
      }
      in.close();
   }
}




2.       Create an applet for the following cases and run it using a Appletviewer and Browser.
a.       Simple applet to display some string.
import java.awt.*;
import java.applet.*;
public class Appl extends Applet {
public void init()
{
}
public void paint(Graphics g) {
g.drawString("A Simple Applet", 30, 30);
}
}
Html display code
<applet code="Appl" width=300 height=70>
</applet>
Output

b.      An applet to draw some shapes.
import java.applet.*;
import java.awt.*;
public class  Rectangle extends Applet{
   public void paint(Graphics g){
      g.drawRect(150,50,200,150);
      g.setColor(Color.BLUE);  
      g.fillRect( 200,100, 300, 150 );
      g.setColor(Color.RED);
      g.drawString("Rectangle",400,200);
   }
}
HTML Code
<applet code="Rect" width=200 height=60>
</applet>
Output

c.       An Applet the display some AWT controls.
Code
import java.applet.Applet;
import java.awt.Button;
public class AWT extends Applet
{
            public void init()
            {
              Button button1 = new Button();
              button1.setLabel("Button A");
              Button button2 = new Button("Button B");
              add(button1);
              add(button2);
        }
}
// <applet code="AWT" width=200 height=60>
</applet>//
Output



Monday, September 21, 2015

java program for Eigenvalues

import Jama.Matrix;
import Jama.EigenvalueDecomposition;

public class Eigenvalues {
   public static void main(String[] args) {
      int N = 5;

      // create a symmetric positive definite matrix
      Matrix A = Matrix.random(N, N);
      A = A.transpose().times(A);

      // compute the spectral decomposition
      EigenvalueDecomposition e = A.eig();
      Matrix V = e.getV();
      Matrix D = e.getD();

      System.out.print("A =");
      A.print(9, 6);
      System.out.print("D =");
      D.print(9, 6);
      System.out.print("V =");
      V.print(9, 6);

      // check that V is orthogonal
      System.out.print("||V * V^T - I|| = ");
      System.out.println(V.times(V.transpose()).minus(Matrix.identity(N, N)).normInf());

      // check that A V = D V
      System.out.print("||AV - DV|| = ");
      System.out.println(A.times(V).minus(V.times(D)).normInf());
      System.out.print("Matrix is randomly choosen");
      
   }

}

Thursday, September 17, 2015

java programs for finding area ,volume,circumfrence and surface area



Write a java  program to find the sum of the series : 1+ x+ x2+x3+….
import java.util.Scanner;
class SumSeries
{
public static void main(String arg[])
{
                Scanner in = new Scanner(System.in);  
                int n;
                System.out.println("enter the nTH element ");
                n = in.nextInt();
                int i=0;
                int sum=0;
                int x;
                System.out.println("enter the power of the series");
                x = in.nextInt();
                while(i<n)
                {
                sum+=Math.pow(x,i);
                i++;
                }
                System.out.println("x="+x+"Sum of the seriees 1+x+x^2.s..="+sum);
}
}
          -------------------------------------------------------------------------------------------------------------------------------
     Write a java program to define a class called Cone and find its area and volume. The area and volume of the cone are evaluated using the formula:
Slant height = sqrt( r 2+h2) , volume = (1/ 3) .pi r 2 h, area = pi. r .sqrt( r 2+h2) .
-----------------------------------------------------------------------------------------------------------------
import java.util.Scanner;
public class Cone
{
            public static void main(String[] args)    
        {  
        double pi = 3.145;
        Scanner in = new Scanner(System.in);
        System.out.println("Enter the radius: ");
        double radius = in.nextDouble();
        System.out.print("Enter the height: ");
        double height = in.nextDouble();
        double slheight;
        double volume;
        double area;
        slheight = Math.sqrt(radius*radius + (height*height));
        volume = (pi*radius*radius*height)/3;
        area = (pi*radius*Math.sqrt(radius*radius+height*height));
        System.out.println("slantheight =is" + slheight);
        System.out.println("volume is = " +volume);
        System.out.println("Area is =" + area);
            }
}
------------------------------------------------------------------------------------------------------------------
   Write a java program to read 2 sides (float values) and included angle value (theta: radian of float type) of a triangle. Find the area of the triangle.
 -------------------------------------------------------------------------------------------------------------
//Thu 17 Sep 2015 16:01:11 IST coded by clinton
import java.util.Scanner;
class AreaTriangle
{
   public static void main(String args[])
   {
                Scanner in = new Scanner(System.in);
              System.out.println("Enter the value of 1st side: ");
              float a = in.nextFloat();
                System.out.println("Enter the value of 2ed side");
                float b = in.nextFloat();
                System.out.println("Enter the value of angle");
                float teta= in.nextFloat();            
                double area = ( 0.5*(a*b*(Math.sin(teta))));
                System.out.println("The Area of a Triangle is: "+area);
   }
}

Write a java program to find the volume and surface area of a cylinder by accepting its radius and height.
------------------------------------------------------------------------------------------------------------
//Thu 17 Sep 2015 15:31:55 IST  coded by clinton.m.u
import java.util.Scanner;
class VolCyli
{
   public static void main(String args[])
   {
                Scanner in = new Scanner(System.in);
              System.out.println("Enter the radius: ");
              double radius = in.nextDouble();
                System.out.println("Enter the hight");
                double hight = in.nextDouble();
                double surface_area = (2*(Math.PI *radius*hight))+(2*(Math.PI*radius*radius));
                System.out.println("The Surface area of a clylinder is: " + surface_area);
                double volume = (Math.PI*radius*radius*hight);            
              System.out.println( "The voulme of the clylinder is:"+volume) ;
   }
}
 ------------------------------------------------------------------------------------------------------------
   Write a program to find the area and volume of the sphere by accepting the radius.
---------------------------------------------------------------------------------------------------
//Thu 17 Sep 2015 15:49:35 IST  coded by clinton
import java.util.Scanner;
class VolSphere
{
   public static void main(String args[])
   {
            Scanner in = new Scanner(System.in);
             System.out.println("Enter the radius: ");
            double radius = in.nextDouble();
            double area = (4*(Math.PI*radius*radius));
            System.out.println("The  area of a sphere is: " + area);
            double volume = ((4/3)*Math.PI*radius*radius*radius);     
             System.out.println( "The voulme of the sphere is:"+volume) ;
   }
}


Write a java program to find the area and circumference of the circle by accepting the values for radius of the circle.

//Thu 17 Sep 2015 11:36:01 IST coded by clinton.m.u
import java.util.Scanner;
class CircleCircum
{
   public static void main(String args[])
   {
                Scanner in = new Scanner(System.in);
              System.out.print("Enter the radius: ");
              double radius = in.nextDouble();
                double area = Math.PI * (radius * radius);
                System.out.println("The area of circle is: " + area);
              double circumference= Math.PI * 2*radius;
              System.out.println( "The circumference of the circle is:"+circumference) ;
   }
}
 Write a java program to accept more than one string and arrange them in alphabetical order.
         –----------------------------------------------------------------------------------------------------------
         import java.util.Scanner;
public class Alphabetical_Order
{
            public static void main(String[] args)
        {
            int n;
            String temp;
            Scanner s = new Scanner(System.in);
            System.out.print("Enter number of words you want to enter:");
            n = s.nextInt();
            String words[] = new String[n];
            Scanner s1 = new Scanner(System.in);
            System.out.println("please Enter all the words:");
            for(int i = 0; i < n; i++)
            {
                words[i] = s1.nextLine();
            }
            for (int i = 0; i < n; i++)
            {
                for (int j = i + 1; j < n; j++)
                {
                    if (words[i].compareTo(words[j])>0)
                    {
                        temp = words[i];
                        words[i] = words[j];
                        words[j] = temp;
                    }
                }
            }
            System.out.println("Words in Sorted Order:");
            for (int i = 0; i < n - 1; i++)
            {
                System.out.print0(words[i] + ",");
            }
            System.out.println(words[n - 1]);
                System.out.println();
        }
}

JavaScript Free Code