Search This Blog

Showing posts with label in java. Show all posts
Showing posts with label in java. Show all posts

Friday, 11 April 2014

How to Format and Display Number to Currency in Java


Java  put comma, just like the way write a bigger amount. This type of Locale specific issue becomes really tricky if you are doing this by hard-coding. Suppose you started your business with USD, and then start selling your product on England, if you keep storing price in database in multiple currency you will face hell lot of issue. Instead of that, just keep price in one currency in database, get the latest FX rate and covert price in real time for display. Java API has rich support for formatting currency by java.text.NumberFormat class, and good knowledge of it will save lot of your time, so do invest some time on this class. 

import java.text.NumberFormat;
import java.util.Locale;

/**
 * How to format Number to different currency in Java. Following Java program
 * will show you, how you can display double value in different currency e.g.
 * USD, GBP and JPY. This example show price in multiple currency.
 * 
* @author
 */
public class Test {

    public static void main(String args[]) {
        double price = 100.25;

        showPriceInUSD(price, getExchangeRate("USD"));
        showPriceInGBP(price, getExchangeRate("GBP"));
        showPriceInJPY(price, getExchangeRate("JPY"));

    }

    /**
     * Display price in US Dollar currency
     *
     * @param price
     * @param rate
     */
    public static void showPriceInUSD(double price, double rate) {
        double priceInUSD = price * rate;
        NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(Locale.US);
        System.out.printf("Price in USD : %s %n", currencyFormat.format(priceInUSD));

    }

    /**
     * Display prince in British Pound
     *
     * @param price
     * @param rate
     */
    public static void showPriceInGBP(double price, double rate) {
        double princeInGBP = price * rate;
        NumberFormat GBP = NumberFormat.getCurrencyInstance(Locale.UK);
        System.out.printf("Price in GBP : %s %n", GBP.format(princeInGBP));
    }

    /**
     * Display prince in Japanese Yen
     *
     * @param price
     * @param rate
     */
    public static void showPriceInJPY(double price, double rate) {
        double princeInJPY = price * rate;
        NumberFormat currency = NumberFormat.getCurrencyInstance(Locale.JAPAN);
        System.out.printf("Price in JPY : %s %n", currency.format(princeInJPY));
    }

    /**
     * @return FX exchange rate for USD
     * @param currency
     */
    public static double getExchangeRate(String currency) {
        switch (currency) {
            case "USD":
                return 1;
            case "JPY":
                return 102.53;
            case "GBP":
                return 0.60;
            case "EURO":
                return 0.73;
            default:
                throw new IllegalArgumentException(String.format("No rates available for currency %s %n", currency));
        }
    }

}


Output
Price in USD : $100.25 
Price in GBP : £60.15  
Price in JPY : 10,279


Th NumberFormat class is on java.text package and not on java.util package. I have instance when people searching for this class, thinking that by importing java.util.* they should get NumberFormat class as well.

Wednesday, 9 April 2014

Comparator vs Comparable in Java


Comparator vs Comparable in Java


Comparators and comparable in Java are two interfaces which is used to implement sorting in Java. It’s often required to sort objects stored in any collection classes like ArrayList, HashSet or in Array and that time we need to use either  compare() or  compareTo() method defined in java.util.Comparator and java.lang.Comparable. In this Java tutorial we will see example of  Comparator and Comparable to sort object in Java and discuss some best practices around when to use Comparator interface etc. Any way before moving ahead Let’s see some important differences between Comparable and Comparator in Java.

Comparator vs Comparable

Here are some of the common differences, which is worth remembering to answer this question if asked during a telephonic or face to face interview:

1) Comparator in Java is defined in java.util package while Comparable interface in Java is defined in java.lang package, which very much says that Comparator should be used as an utility to sort objects which Comparable should be provided by default.

2) Comparator interface in Java has method public int compare (Object o1, Object o2) which returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second. While Comparable interface has method public int compareTo(Object o) which returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

3) If you see then logical difference between these two is Comparator in Java compare two objects provided to him, while Comparable interface compares "this" reference with the object specified. Comparable interface.

4) Comparable in Java is used to implement natural ordering of object. In Java API StringDate and wrapper classes implements Comparable interface.Its always good practice to override compareTo() for value objects.

5) If any class implement Comparable interface in Java then collection of that object either List or Array can be sorted automatically by using  Collections.sort() or Arrays.sort() method and object will be sorted based on there natural order defined by CompareTo method.

6)Objects which implement Comparable in Java  can be used as keys in a SortedMap like TreeMap or elements in a SortedSet  for example TreeSet, without specifying any Comparator.

These were combination of some theoretical and practical differences between Comparator and Comparator interface in Java. It does help you to decide when to use Comparator vs Comparable but things will be more clear when we some best practices around using both of these interfaces. Now let’s see an example of Comparator in Java:

 

Example of using Comparator and Comparable in Java

So in Summary if you want to sort objects based on natural order then use Comparable in Java and if you want to sort on some other attribute of object then use Comparator in Java. Now to understand these concepts lets see an example or real life coding:


1) There is class called Person, sort the Person based on person_id, which is primary key in database
2) Sort the Person based on there name.

For a Person class, sorting based on person_id can be treated as natural order sorting and sorting based on name field can be implemented using Comparator interface. To sort based on person_id we need to implement compareTo() method.


public class Person implements Comparable {
    private int person_id;
    private String name;
   
    /**
     * Compare current person with specified person
     * return zero if person_id for both person is same
     * return negative if current person_id is less than specified one
     * return positive if specified person_id is greater than specified one
     */

    @Override 
    public int compareTo(Object o) {

        Person p = (Person) o; 
        return this.person_id - o.person_id ;
    }
    ….
}

Generally you should not use difference of integers to decide output of compareTo method as result of integer subtraction can overflow but if you are sure that both operands are positive then its one of the quickest way to compare two objects. See my post things to remember while overriding compareTo in Java for more tips on compareTo.

And for sorting based on person name we can implement compare(Object o1, Object o2) method of Java Comparator class.

/**
 * Comparator implementation which sorts Person objects on person_id field
 */

public class SortByPerson_ID implements Comparator{

    public int compare(Object o1, Object o2) {

        Person p1 = (Person) o;
        Person p2 = (Person) o; 
        return p1.getPersonId() - p2.getPersonId();
    }
}

Similar guidelines applies while implementing compare() method as well and instead of using subtraction operator, its better to use logical operator to compare whether two integers are equal to, less than or greater than. You can write several types of Java Comparator based upon your need for example  reverseComparator , ANDComparator ORComparator etc which will return negative or positive number based upon logical results. String in Java even provides an special comparator called CASE_INSENSITIVE_ORDER, to perform case insensitive comparison of String objects.



How to Compare String in Java
String is immutable in Java and one of the most used value class. For comparing String in Java we should not be worrying because String implements Comparable interface and provides a lexicographic implementation for CompareTo method which compare two strings based on contents of characters or you can say in lexical order. You just need to call String.compareTo(AnotherString) and Java will determine whether specified String is greater than , equal to or less than current object. See my post 4 example to compare String in Java for alternatives ways of comparing String.


How to Compare Dates in Java
Dates are represented by java.util.Date class in Java and like String,  Date also implements Comparable in Java so they will be automatically sorted based on there natural ordering if they got stored in any sorted collection like TreeSet or TreeMap. If you explicitly wants to compare two dates in Java you can call Date.compareTo(AnotherDate) method in Java and it will tell whether specified date is greater than , equal to or less than current String.

When to use Comparator and Comparable in Java
At last let’s see some best practices and recommendation on when to use Comparator or Comparable in Java:

1) If there is a natural or default way of sorting Object already exist during development of Class than use Comparable. This is intuitive and you given the class name people should be able to guess it correctly like Strings are sorted chronically, Employee can be sorted by there Id etc. On the other hand if an Object can be sorted on multiple ways and client is specifying on which parameter sorting should take place than useComparator interface. for example Employee can again be sorted on name, salary or department and clients needs an API to do that. Comparator implementation can sort out this problem.

2) Some time you write code to sort object of a class for which you are not the original author, or you don't have access to code. In these cases you can not implement Comparable and Comparator is only way to sort those objects.

3) Beware with the fact that How those object will behave if stored in SorteSet or SortedMap like TreeSet and TreeMap. If an object doesn't implement Comparable than while putting them into SortedMap, always provided corresponding Comparator which can provide sorting logic.

4) Order of comparison is very important while implementing Comparable or Comparator interface. for example if you are sorting object based upon name than you can compare first name or last name on any order, so decide it judiciously. I have shared more detailed tips on compareTo on my post how to implement CompareTo in Java.

5) Comparator has a distinct advantage of being self descriptive  for example if you are writing Comparator to compare two Employees based upon there salary than name that comparator as SalaryComparator, on the other hand compareTo()



Find Minimum and Maximum value of Array in Java


Find Minimum and Maximum value of Array in Java


public class FindMinAndMaxValueInArray {
       public static void main(String[] args) {
              int arr[] = { 69, 23, 47, 81, 92, 88, 52, 48, 56, 66, 65, 76, 71, 85,
                           49, 53, 56, 61, 65, 84 };
              findMaxValue(arr);
              findMinValue(arr);
       }

       public static void findMinValue(int[] arr) {
              int min = arr[0];
              for (int i = 0; i < arr.length; i++) {
                     if (min > arr[i]) {
                           min = arr[i];
                     }
              }
              System.out.println("minimum value of the array is:" + min);
       }

       public static void findMaxValue(int[] arr) {
              int max = arr[0];
              for (int i = 0; i < arr.length; i++) {
                     if (max < arr[i]) {
                           max = arr[i];
                     }
              }
              System.out.println("maximum value of array is:" + max);

       }
}
output
maximum value of array is:92
minimum value of the array is:23

Sunday, 6 April 2014

Find Duplicates in an array

Find Duplicates in an array


package com.blogspot.rajvishwakarma15;

public class CheckDuplictesInArray {
       public static void main(String[] args) {
              String[] strArr = new String[] { "a""b""c""s""d""e""a",
                           "b""s" };
              detectDuplicates(strArr);
       }

       public static void detectDuplicates(String[] arr) {

              for (int i = 0; i < arr.length; i++) {
                     for (int j = i + 1; j < arr.length; j++) {
                           if (arr[j] == arr[i]) {
                      System.out.println("Duplicate found! Original was at location "
                               + (i + 1) + " and match is at " + (j + 1));
                                  System.out.println("Duplicated value is:" + arr[j]);
                           }
                     }
              }

       }
}


OUTPUT
Duplicate found! Original was at location 1 and match is at 7 Duplicated value is:a

Duplicate found! Original was at location 2 and match is at 8 Duplicated value is:b

Duplicate found! Original was at location 4 and match is at 9 Duplicated value is:s

Wednesday, 2 April 2014

Write a program to find sum of each digit in the given number using recursion.

Write a program to find sum of each digit in the given number using recursion.


Description:
Below example shows how to find out sum of each digit in the given number using recursion logic. For example, if the number is 259, then the sum should be 2+5+9 = 16.


package com.blogspot.rajvishwakarma18;
public class MyNumberSumRec {
    int sum = 0;
     
    public int getNumberSum(int number){
         
        if(number == 0){
            return sum;
        } else {
            sum += (number%10);
            getNumberSum(number/10);
        }
        return sum;
    }
     
    public static void main(String a[]){
        MyNumberSumRec mns = new MyNumberSumRec();
        System.out.println("Sum is: "+mns.getNumberSum(223));
    }
}

Output:
Sum is: 7