Friday, May 10, 2013

Java Math Function Example



Hello Friends,

Hope you will like this article.

In this article we will be familiar with maths functions of java. Here example is given and by running this example code to your pc, you will easily get and be familiar with all these functions.


package mathsfunctions;

public class MathsFunctions {

    public static void main(String args[]){

    int i = 2;
    int j = -4;
    double x = 19.8;
    double y = 0.38;

    System.out.println("i is " + i);
    System.out.println("j is " + j);
    System.out.println("x is " + x);
    System.out.println("y is " + y);


    /*
     Math.round(variable) Function
     -----------------------------
      It gives rounding value of variable, depending upon value of variable is
      less than or greater than 0.50

     */

    System.out.println("Value of Math.round(x) is : "+Math.round(x));
    System.out.println("Value of Math.round(y) is : "+Math.round(y));
    //  ----------------------------------------------------------------------------

    /*
     Math.abs(variable) Function
     ---------------------------
     It gives integer value of variable, if it is negatie than it converts
     to positive number
     */

    System.out.println("Value of Math.abs(i) is : " + Math.abs(i));
    System.out.println("Value of Math.abs(j) is : " + Math.abs(j));
    System.out.println("Value of Math.abs(x) is : " + Math.abs(x));
    System.out.println("Value of Math.abs(y) is : " + Math.abs(y));

    //  ---------------------------------------------------------------------------

    /*
     Math.min(var1,var2) Function
     ----------------------------
     It gives minimum number value between the two variables
     */

     System.out.println("Minimum between " +i+" and j " +j+ " is : "  + Math.min(i,j));
     System.out.println("Minimum between " +x+" and j " +y+ " is : "  + Math.min(x,y));
     System.out.println("Minimum between " +i+" and j " +x+ " is : "  + Math.min(i,x));
     System.out.println("Minimum between " +y+" and j " +j+ " is : "  + Math.min(y,j));

     //  ---------------------------------------------------------------------------

     /*
     Math.max(var1,var2) Function
     ----------------------------
     It gives maximum number value between the two variables
     */

     System.out.println("Maximum between " +i+" and j " +j+ " is : "  + Math.max(i,j));
     System.out.println("Maximum between " +x+" and j " +y+ " is : "  + Math.max(x,y));
     System.out.println("Maximum between " +i+" and j " +x+ " is : "  + Math.max(i,x));
     System.out.println("Maximum between " +y+" and j " +j+ " is : "  + Math.max(y,j));

     //  ---------------------------------------------------------------------------

     /*
      Math.ceil(variable)
      -------------------
      It gives integer value greater than or equal to the number
      */

     System.out.println("Value of Math.ceil(i) is : " + Math.ceil(i));
     System.out.println("Value of Math.ceil(j) is : " + Math.ceil(j));
     System.out.println("Value of Math.ceil(x) is : " + Math.ceil(x));
     System.out.println("Value of Math.ceil(y) is : " + Math.ceil(y));

     //  ---------------------------------------------------------------------------

     /*
      Math.floor(variable)
      -------------------
      It gives integer value less than or equal to the number
      */

     System.out.println("Value of Math.floor(i) is : " + Math.floor(i));
     System.out.println("Value of Math.floor(j) is : " + Math.floor(j));
     System.out.println("Value of Math.floor(x) is : " + Math.floor(x));
     System.out.println("Value of Math.floor(y) is : " + Math.floor(y));

     //  ---------------------------------------------------------------------------

     /*
      Math.pow(var1,var2)
      -------------------
      It gives var1 raised to var2 value
      */

    System.out.println("Value of pow(3.0, 2.0) is : "  + Math.pow(3.0,2.0));
    System.out.println("Value of pow(5.0, 1.5) is : "  + Math.pow(5.0,1.5));
    System.out.println("Value of pow(3, -1) is : "     + Math.pow(3,-1));

    //  ---------------------------------------------------------------------------

     /*
      Math.sqrt(variable)
      -------------------
      It gives square value root of variable
      */

    for (i=0; i < 10; i++) {
      System.out.println(
       "The square root of " + i + " is " + Math.sqrt(i));
    }

    //  ---------------------------------------------------------------------------

     /*
      Math.random(variable)
      -------------------
      It gives random number between o to 9
      */

    System.out.println("Example of Random Number : " + Math.random());
    System.out.println("Example of another Random Number : " + Math.random());

    }
}



Thursday, April 18, 2013

What will happen of your Google Account when you Die ?



What will happened of your Gmail Account when you die ?

Have you think like this before, I don't think so. But as Gmail.com is very popular website for sending email. Google has think that if people die then what to do with his/her data ? Answer came into the mind that Can we give access to another person from user's relative or friends whom he/she trust.

Google has started their search engine before 10 years and become popular. After launching successful search engine google has started a number of products and counting...  Do you aware that how many products are launched by the google ? We will give you brief summary of the same in the forthcoming article.
 
If you have google account and you are using more than 10 Applications from google.com then you have to think that what will happen if you die and your account is locked then you may loose some important conversations or documents as you are using number of google products.

So Finally Google came up with the "Google Inactive" Option. Here you need to setup the person details to whom you would like to give access if you die or you are enable to access your google account for few months or more.

To setup Google Inactive Click here 




Wednesday, April 17, 2013

WhatsApp Messaging : The Most Popular Communication Medium



WhatsApp Messaging : The Most Popular Communication Medium

You all are aware with the Number of messengers available in the market. You may be using Yahoo Chat before a 3-5 Years, Now Its time to Change the Messaging Service. Yahoo chat service is very much popular in the world but people are changing day by day. 

Now a days people has stated new messenger service for computer users  Like Google Hang Out and for smartphone users WhatsApp is very much popular one. I can be sure that most of the Smartphone users are using WhatsApp because of their rich set of functionality and attractive look.

Now Its your time to answer What You Think ?

- WhatsApp 
- Other  

Do you like WhatsApp or Other Application for Chat, Write your answer in comments... 

Tuesday, April 16, 2013

User Defined Exception in Java with Example



User defined Exception in Java


Hello Friends,


 

                        In this article we will see User Defined Exception. User Defined Exception means exception class created by User. Built-in exceptions provided by the Exception and Error classes may not always be enough to trap errors occurring in the program. There may be case where there is a need to create user-defined exceptions. User-defined class should be subclass of the Exception class.

The advantage of sub-classing the exception the Exception class is that the new exception type can be caught separately from other subclasses of Throwable.

Example of User-defined Exception is following,


class UseDefinedException extends Exception{
                String msg = "";
                int marks;
                public UseDefinedException(){

                }
                public UseDefinedException(String str){
                                super(str);
                }
                public String toString(){
                                if(marks <= 50){
                                                msg = "You have failed";
                                }

                                if (marks > 50){
                                                msg = "You have Passed";
                                }
                                return msg;
                }
}

public class Test{
                public static void main(String args[]){
                                Test t = new Test();
                                t.method();
                }
                public void method(){
                                try{
                                                int i=0;

                                                if( i < 50)
                                                                throw new UseDefinedException();

                                }
                                catch(UseDefinedException ude){
                                                System.out.println("User Defined Exception :: "+ude);
                                }
                }
}

It's Output will be,
 



Monday, April 15, 2013

Java Exception | Exceptions Handling Examples | Try Catch Finally Block



Hi Guys,

I am back with the Latest Topics. What is Exception in Java. Handling Exceptions with Examples.



             In this article, we will see Exception. Exception is very important part of any programming language. Basically, an exception is an abnormal behavior existing during a normal execution of a program. For example: When writing to a file if there does not exist required file then an appropriate exception will be thrown by java code.

Exception

          When an error occurred during the execution of a program, an exception occurs. In other words, exception is a run time error. This may happen because of the occurrence of certain events that arise at the time of execution.

            When an exception occurs, the program terminates unexpectedly and control returns to the operating system(OS). For language that do not support exception handling, programmers have to take care of errors manually. It is important to ensure that the program does not terminate unexpectedly due to the occurrence of an exception.

Handling Exception

            Exception Handling is performed to identify errors and prevent program to stop execution due to errors. Whenever an exception occurs in a program where proper exception handling mechanism is not provided, the program aborts programs stops it's execution.

            Error handling in Java is performed with the help of exception-handling model. According to this model, when an error occurs, an exception is thrown. This exception is caught in block of code. Exception have to be caught, otherwise the program is terminated.

            Exception Handling in Java is managed through five keywords: try, catch, throw, throws and finally.  Program Statements that have to be monitored for exceptions, are contained within try block. By catch keyword, the programmer can catch the exception and handle it. To manually throw an exception, the keyword throw is used. The throws clause is used in a method to specify that this method will throw out an exception. The code in finally block gets executed no matter whether an exception is generated or not.


 'try' and 'catch' Block

Whenever there is a possibility of an exception being generated in a program, it is better to handle it explicitly. This can be done using the try and catch. The advantage of try and catch keywords are that, they fix the error and prevent the program from terminating abruptly.

It is possible to have multiple catch statements for one try block. The execution of a program can continue once the exception has been handled.

For Ex.

try{

            int num = calculate(9,0);

            System.out.println(num);

}catch(Exception ex){

            ex.printStackTrace();

}



int calculate(int a, int b){

            int num = a/b;

            return num;

}

In above example, exception will generate because, number cannot be divided by zero.
Note: The catch block never executed if no error occurs.


The 'finally' block

            'finally' block is placed after catch block, and it is compulsory executes whether catch block execute or not. The code inside finally block will compulsory execute.

For Example, sometimes we don't close connection with database after query get executed. So, in this case, generally we write close connection code in finally. So, possibilities of error generation due to so many connection instance to database, will be reduced.
            'finally' block will only not execute, if programmer calls System.exit(0); in try block.

For Example,

try{

            Connection conn;

            conn = DBConnection();

}catch(Exception ex){

            ex.printStackTrace();

}finally{

            closeConnection();

}


Multiple catch blocks

Sometimes a single piece of code may generate more than one type of error. In such cases this errors should be separated by multiple catch blocks. Each catch statement is seen in order of appearance and the first one that matches the exception is executed. After one of the catch statements is executed, the rest of the catch statements are ignored.

Following example will illustrate, multiple catch() blocks are used to handle different exceptions.

For Example,

class ExceptionCode{

           

            public void calculate(){

                        try{

                                    int num=0;

                                    int num1 = 42/0;

                        }catch(ArithmaticException ae){

                                    ae.printStackTrace();

                        }catch(Exception ex){

                                    ex.printStackTrace();

                        }

            }



            public static void main(String args[]){

                        ExceptionCode obj = new ExceptionCode();

                        obj.calculate();

            }

}