Thursday, 16 October 2014

import java.util.Timer;
import java.util.TimerTask;

class MyTimerTask extends TimerTask {
  public void run() {
    System.out.println("Timer task executed.");
  }
}

class TTest {
  public static void main(String args[]) {
    MyTimerTask myTask = new MyTimerTask();
    Timer myTimer = new Timer();

    myTimer.schedule(myTask, 1000500);

    try {
      Thread.sleep(5000);
    catch (InterruptedException exc) {
    }

    myTimer.cancel();
  }
}

Sunday, 5 October 2014

JAVA Program to Dispay Current Time in AM PM Method

Time Display USING JAVA

A program to show the current time using SimpleDateFormat Method.


//A small java program to print the time on the screen
import java.util.Date;
import java.text.SimpleDateFormat;

public class DateFormat {
   
    public static void main(String[] args) {

            Date date = new Date();
            String strDateFormat = "HH:mm:ss a";
            SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
            System.out.println(sdf.format(date));
    }
    
}

Output:


00:15:13 AM

Saturday, 4 October 2014

Java Code to Find the Perimeter

Perimeter of a Circle Using Java

A Java program to accept the radius of a Circle from user and calculate the Perimeter.


//A small java program to calculate the Perimeter of a circle

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class CirclePerimeter {

        public static void main(String[] args) {
               
                int radius = 0;
                System.out.println("Please enter radius of a circle");
               
                try
                {
                        //get the radius from console
                        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                        radius = Integer.parseInt(br.readLine());
                }
                //if invalid value was entered
                catch(NumberFormatException ne)
                {
                        System.out.println("Invalid radius value" + ne);
                        System.exit(0);
                }
                catch(IOException ioe)
                {
                        System.out.println("IO Error :" + ioe);
                        System.exit(0);
                }
               
                /*
                 * Perimeter of a circle is
                 * 2 * pi * r
                 * where r is a radius of a circle.
                 */
               
                //NOTE : use Math.PI constant to get value of pi
                double perimeter = 2 * Math.PI * radius;
               
                System.out.println("Perimeter of a circle is " + perimeter);
        }
}

Output:


Please enter radius of a circle
12
Perimeter of a circle is 75.39822368615503

Friday, 3 October 2014

Java Applet Security

How Java Applet Deals with Security?


Java keeps a "firewall" between a networked application and the computer.

Java Achieves this protection with the help of a Java Compatible Web Browser by confining a Java Program to the Java Execution Environment and not allowing its access to the others parts of the Computer.

Thursday, 2 October 2014

Plantform Independent Java

How Java Achives Platform Independance.


Why  Java is known for Platform Independance.


Quick Note about the Platform Independance (Portability) Feature of Java Language.


          The most important feature of Java is its portability. A program created in one platform has the ability to run on another.

C/C++

          Eg: When we create a C/C++ program in Windows Operating System, it doesn't have the ability to run in a system which runs on Linux Operating System. 
Platform Dependence of C/C++
The main reason behind this is, C/C++ code takes a few components from its Operating System to run the generated code. So the platform change affects the execution of the program. 

Java

Plantform Independence of Java
Java overcomes the platform independence with the help of Java Virtual Machine (JVM). Java creates a layer between the language and the Operating System. The layer is called Virtual Machine (JVM). There are different Virtual Machines for different Operating Systems as JVM dependent on the Operating System. We need to install the Java Virtual Machine compatible for each Operating System for all devices. Java run on top of this Virtual Machine and make the portability easier. 

        The compilation of Java program makes a class file which contains the bytecode. The bytecode is same for all Operating System and just the JVM deals with it in all Platforms.