Herzlich Willkommen

Live processing contents

Sunday, October 3, 2010

Basic Java Syntacs


PENJUMLAHAN
import java.util.Scanner;
public class Addition
{    
    public static void main(String args[])
    {
    Scanner input = new Scanner (System.in);
    int number1;
    int number2;
    int sum;

    System.out.print("Enter first integer: ");
    number1 = input.nextInt();

    System.out.print("Enter second integer: ");
    number2 = input.nextInt();

    sum = number1 + number2;
    System.out.printf("sum is %d\n", sum);
    }
}

XOR
public class XORDemo {
    public static void main(String[] args) {
        int numberA = 16;
        int numberB = 32;

        //
        // Operator ^ is used for doing bitwise exclusive OR operation
        //
        int result = numberA ^ numberB;

        System.out.println(numberA + " ^ " + numberB + " = " + result);

        //
        // Print the result in binary format
        //
        System.out.println(Integer.toBinaryString(numberA) +
                " ^ " + Integer.toBinaryString(numberB) +
                " = " + Integer.toBinaryString(result));
    }
}
COMMAND LINE ARGUMENT
public class ArgumentParsingExample
{
    public static void main(String[] args)
    {
        // When creating a Java application we will sometime need to pass a
        // couple parameters when the program is executed. To get the passed
        // parameter from the command line we can read the argument passed to
        // the main method just as the signature above. As you know to make a
        // class executable you need to create a method as follow:
        //
        // public static void main(String[] args) {}
        //
        // As you can see, this method take an array of String as the parameter.
        // You can guess that this array is the parameter that we passed in
        // the command line.
        for (int i = 0; i < args.length; i++)
        {
            System.out.println("Argument " + (i + 1) + " = " + args[i]);
        }
       
        // If you want to check if the number of supplied parameters meet the
        // program requirement you can check the size of the arguments array.
        if (args.length < 3)       
        {
            System.out.println("You must call the program as follow:");
            System.out.println("java ArgumentParsingExample arg1 arg2 arg3");
           
            // Exit from the program with an error status, for instance we
            // return -1 to indicate that this program exit abnormally
            System.exit(-1);
        }
       
        System.out.println("Hello, Welcome to ArgumentParsingExample Program");
    }
}

MilliSecond to Date

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class MillisecondsToDate {
    public static void main(String[] args) {
        //
        // Create a DateFormatter object for displaying date information.
        //
        DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss.SSS");
       
        //
        // Get date and time information in milliseconds
        //
        long now = System.currentTimeMillis();

        //
        // Create a calendar object that will convert the date and time value
        // in milliseconds to date. We use the setTimeInMillis() method of the
        // Calendar object.
        //
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(now);
       
        System.out.println(now + " = " + formatter.format(calendar.getTime()));
    }
}
 

No comments:

Post a Comment