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

Create Your Own Programming Language E-Book

Review Of This Product :
  • A 77 pages PDF detailing core concepts and applying them to a custom language
    (included: ePub & Mobi formats).
  • Proposed extensions to the language with solutions at the end of the book.
  • Full source code of three languages in Ruby & Java. Easy to extend and play with.
  • Explaining step by step how to extend the JVM language.
  • Most books on compiler are priced at more than $100 and are long and boring. My system, which contains a book (fun and to the point), exercises & solutions, three languages you can use however you want and a screencast, is only $39.99. And I even give it to you for free if you're not happy.

What they said about this product :

1. “The book I want to read.” — Matz, creator of the Ruby language 

2.  “It's been a lot of fun, and surprisingly little work to get a language that covers 95% of what JavaScript can do.” — Jeremy Ashkenas, created CoffeeScript after reading the book.

3.  “I love this book! It helped me create my own Awesome-to-PHP compiler. It's not a full featured compiler yet but I had a lot of fun coding it and I learned a LOT.” — Julien Desrosiers, created Phlower after reading the book.




Create Java MP3 To Wav Converter



*********************************************************************
COMPLETE SOURCE CODE FOR : MainClass.java
*********************************************************************




*********************************************************************
COMPLETE SOURCE CODE FOR : SwingWorker.java
*********************************************************************

A Java Program To Play A MP3 File Using JLayer

Watch video tutorial below :


*********************************************************************
COMPLETE SOURCE CODE FOR : TestPlay.java
*********************************************************************

import javazoom.jl.player.Player;
import java.io.FileInputStream;

public class TestPlay
{
    public static void main(String[]args)
    {
        try
        {
            FileInputStream fis=new FileInputStream("C:\\Documents and Settings\\UserXP\\Desktop\\01-flo_rida_(feat_david_guetta)-clab_cant_handle_me.mp3");
            Player playMp3=new Player(fis);

            playMp3.play();
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }
}

Open folder or directory in Windows 7 using java

Complete source code below will show you, how to open folder through java in Windows 7.

************************************************************************
COMPLETE SOURCE CODE FOR : OpenFolderInJava.java
Thank you to Ankur Pandya for this source codes.
************************************************************************

/// file OpenFolderInJava.java ////

import java.util.*;
import java.io.*;
import java.awt.Desktop;

class OpenFolderInJava{

public static void main(String[] arg){
String path = "."; // path to the directory to be opened
File file = new File("C:\\");
Desktop desktop = null;
// Before more Desktop API is used, first check
// whether the API is supported by this particular
// virtual machine (VM) on this particular host.
if (Desktop.isDesktopSupported()) {
desktop = Desktop.getDesktop();
}
try {
desktop.open(file);
}
catch (IOException e){
}
}
}

/// end of file ///


************************************************************************
JUST COMPILE AND EXECUTE
************************************************************************

Java Hotel Management Program (Philippine)

*********************************************************************
COMPLETE SOURCE CODE FOR : TTKH.java
BY : Phi
*********************************************************************


/**
* @(#)TTKH.java
*
* TTKH application
*
* @author
* @version 1.00 2010/3/21
*/
import javax.swing.*;
import java.awt.*;

public class TTKH {

public static void main(String[] args) {
try {
// Set System L&F
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
}
catch (UnsupportedLookAndFeelException e) {
// handle exception
}
catch (ClassNotFoundException e) {
// handle exception
}
catch (InstantiationException e) {
// handle exception
}
catch (IllegalAccessException e) {
// handle exception
}


//=----------------------------------------------------

// TODO, add your application code
JFrame frame = new JFrame();


JPanel panel = new JPanel();
JPanel khungchitiet = new JPanel();
//JPanel p1 = new JPanel();

//JPanel p2 = new JPanel();
//p2.setLayout(new FlowLayout());
JPanel khungtimkiem = new JPanel();
GridLayout g2 = new GridLayout(2,2);
khungtimkiem.setLayout(new GridLayout(5,2,10,10));
GridLayout g1 = new GridLayout(1,1);
khungchitiet.setLayout(new GridLayout(7,2,10,10));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// frame.setLocationByPlatform(true);

frame.setSize(680,470);


// Giao dien

JLabel ldemuc = new JLabel("Thong Tin Khach Hang");
ldemuc.setFont(new Font("Tahoma", Font.BOLD, 25));
ldemuc.setLayout(new FlowLayout());
panel.add(ldemuc);



// Khung Chi tiet
JLabel ltenKH = new JLabel("Ten KH");
JTextField ttenKH = new JTextField("Nguyen Van A",1);
JLabel lngaynhan = new JLabel("Ngay Nhan Phong");
JTextField tngaynhan = new JTextField("12/05/2009",1);
JLabel lngaysinh = new JLabel("Ngay Sinh");
JTextField tngaysinh = new JTextField("11/09/1986",1);
JLabel lngaytra = new JLabel("Ngay Tra Phong") ;
JTextField tngaytra = new JTextField("22/05/2009",1);
JLabel lgioitinh = new JLabel("Gioi Tinh");
JTextField tgioitinh = new JTextField("Nam",1);
JLabel lsodt = new JLabel("So Dien Thoai");
JTextField tsodt = new JTextField("0912345677",1);
JLabel lloaiphong = new JLabel("Loai Phong");
JTextField tloaiphong = new JTextField("Vip",1);
JLabel lcmnd = new JLabel("So CMND");
JTextField tcmnd = new JTextField("036365888",1);
JLabel lmaKH = new JLabel("Ma KH");
JTextField tmaKH = new JTextField("001",1);
JLabel lemail = new JLabel("Email");
JTextField temail = new JTextField("nguoidep@gmail.com",1);
JLabel lspace = new JLabel("--------------");
JLabel lspace1 = new JLabel(" ");
JLabel lspace2 = new JLabel("");
JLabel lspace3 = new JLabel("--------------");
JLabel lspace4 = new JLabel(" ");
JLabel lspace5 = new JLabel(" ");
JLabel lchitiet = new JLabel("Chi Tiet Khach Hang") ;
lchitiet.setFont(new Font("Tahoma", Font.BOLD, 15));
JLabel ltimkiem = new JLabel("Tim Kiem Nhanh") ;
ltimkiem.setFont(new Font("Tahoma", Font.BOLD, 15));



khungchitiet.add(lchitiet);
khungchitiet.add(lspace);
khungchitiet.add(lspace1);
khungchitiet.add(lspace2);
//khungchitiet.add(lspace3);
khungchitiet.add(lmaKH);
khungchitiet.add(tmaKH);
khungchitiet.add(lngaynhan);
khungchitiet.add(tngaynhan);
khungchitiet.add(ltenKH);
khungchitiet.add(ttenKH);
khungchitiet.add(lngaytra);
khungchitiet.add(tngaytra);
khungchitiet.add(lgioitinh);
khungchitiet.add(tgioitinh);
khungchitiet.add(lsodt);
khungchitiet.add(tsodt);
khungchitiet.add(lngaysinh);
khungchitiet.add(tngaysinh);
khungchitiet.add(lloaiphong);
khungchitiet.add(tloaiphong);
khungchitiet.add(lcmnd);
khungchitiet.add(tcmnd);
khungchitiet.add(lemail);
khungchitiet.add(temail);
khungchitiet.add(ltimkiem);
khungchitiet.add(lspace3);
khungchitiet.add(lspace4);
khungchitiet.add(lspace5);




// phan tim kiem

JLabel lmaKH2 = new JLabel("Ma KH");
lmaKH2.setForeground(Color.red);
JTextField tmaKH2 = new JTextField("Nhap ma khach hang...");
Choice c1 = new Choice();
c1.addItem("Vip");
c1.addItem("Phong Gia dinh");
c1.addItem("Phong Quat");
c1.addItem("Phong May lanh");
//JLabel lgioitinh2 = new JLabel("Gioi tinh");
//JRadioButton rnam = new JRadioButton("Nam",true);
//JRadioButton rnu = new JRadioButton("Nu",false);
JLabel lngaynhan2 = new JLabel("Ngay Nhan");
lngaynhan2.setForeground(Color.red);
JTextField tngaynhan2 = new JTextField("Nhap ngay nhan dd/mm/yyyy");
JLabel lcmnd2 = new JLabel("So CMND");
JTextField tcmnd2 = new JTextField("Nhap so");
JLabel lngaytra2 = new JLabel("Ngay Tra");
lngaytra2.setForeground(Color.red);
JTextField tngaytra2 = new JTextField("Nhap ngay tra dd/mm/yyyy");
JLabel ltenKH2 = new JLabel("Ten KH");
JTextField ttenKH2 = new JTextField("Nhap ten KH");
JLabel ls1 = new JLabel(" ");
JLabel ls2 = new JLabel(" ");
JLabel ls3 = new JLabel(" ");
JLabel ls4 = new JLabel(" ");
JLabel ls5 = new JLabel(" ");
JLabel ls6 = new JLabel(" ");
JButton b1 = new JButton("Lam Moi");
JButton b2 = new JButton("Tim");
JButton b3 = new JButton("Thoat");
//JLabel ls5 = new JLabel(" ");
JLabel loaiphong = new JLabel("Loai Phong");
khungtimkiem.add(lmaKH2);
khungtimkiem.add(tmaKH2);
khungtimkiem.add(lngaynhan2);
khungtimkiem.add(tngaynhan2);
khungtimkiem.add(ltenKH2);
khungtimkiem.add(ttenKH2);
khungtimkiem.add(lngaytra2);
khungtimkiem.add(tngaytra2);
khungtimkiem.add(lcmnd2);
khungtimkiem.add(tcmnd2);
khungtimkiem.add(ls1);
khungtimkiem.add(ls2);
khungtimkiem.add(loaiphong);
khungtimkiem.add(c1);
khungtimkiem.add(ls3);
khungtimkiem.add(b2);
khungtimkiem.add(ls5);
khungtimkiem.add(ls6);
khungtimkiem.add(b1);
khungtimkiem.add(b3);


frame.setLayout(new BorderLayout());
frame.add(panel, BorderLayout.NORTH);
//frame.add(p1, BorderLayout.LINE_START);
frame.add(khungchitiet, BorderLayout.WEST);
//frame.add(p2, BorderLayout.WEST);
//frame.add(p2, BorderLayout.LINE_END);
frame.add(khungtimkiem, BorderLayout.SOUTH);
frame.setVisible(true);
}
}


*********************************************************************
JUST COMPILE AND EXECUTE IT
*********************************************************************

Java radians to degree


*********************************************************************
COMPLETE SOURCE CODE FOR : RadiansToDegree.java
*********************************************************************


public class RadiansToDegree
{
public static void main(String[]args)
{
//radians value
double radians=1.5707963267948966;

//Get degree value for radians value equal to 1.5707963267948966
double degree=Math.toDegrees(radians);

System.out.println(degree);
}
}


*********************************************************************
JUST COMPILE AND EXECUTE IT
*********************************************************************

Java degree to radians


*********************************************************************
COMPLETE SOURCE CODE FOR : DegreeToRadians.java
*********************************************************************


public class DegreeToRadians
{
public static void main(String[]args)
{
//90 degree
double degree=90;

//Get radians value for 90 degree
double radians=Math.toRadians(degree);

System.out.println(radians);
}
}


*********************************************************************
JUST COMPILE AND EXECUTE IT
*********************************************************************

Free java decompiler

This is one of java decompiler that can be use to get java source file (file with .java as it's file extension) from any java class file (file with .class as it's file extension). Class file is a result after compiling process success without any syntax error.

Click link below to download

1. JD-GUI




2. Cavaj Java Decompiler


3. JCavaj Java Decompiler

Java get binary value from alphabet

Today, i have learned about how to get binary value from alphabet. You can try compile and execute java program below to get all binary value for A-Z and a-z.


*********************************************************************
COMPLETE SOURCE CODE FOR : JavaGetBinaryFromLetter.java
*********************************************************************


public class JavaGetBinaryFromLetter
{
public static void main(String[]args)
{
String a="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

for(int i=0;i<a.length();i++)
{
char b=a.charAt(i);
int temp=(int)b;
String c=Integer.toBinaryString(temp);
System.out.println("BINARY VALUE FOR LETTER "+b+" IS : "+c);
}
}
}


*********************************************************************
JUST COMPILE AND EXECUTE IT
*********************************************************************

Java ternary operator

Today i have learned about ternary operator in java. Before this, when i found it in java code, i will ask myself what it is and what we call it...now i will share with you what i understand about ternary operator.Now, see java code below


************************************************************
COMPLETE SOURCE CODE FOR : JavaTernaryOperator.java
************************************************************


public class JavaTernaryOperator
{
public static void main(String[]args)
{
int a=0;
int b=4;
int c=5;

//THIS IS EXAMPLE OF TERNARY OPERATOR
a=(b>c?b:c);

System.out.println(a);
}
}


************************************************************
JUST COMPILE AND EXECUTE IT
************************************************************

In the java code above, example of ternary operator is a=(b>c?b:c);.So, what it mean ?

This is the answer :
Value a is equal to b if b larger than c or value a is equal to c if b smaller than c.So, java code above has same meaning with java code below.


public class JavaTernaryOperator
{
public static void main(String[]args)
{
int a=0;
int b=4;
int c=5;

if(b>c)
{
a=b;
}
else
{
a=c;
}

System.out.println(a);
}
}


Now, you can choose which one you want to use when write your java program. It seem when we use ternary operator we can make our program more shorter.

Java hexadecimal to binary

Complete source code below is a simple java program that can be use to get binary value from hexadecimal value.

******************************************************************
COMPLETE SOURCE CODE FOR : JavaHexToBin.java
******************************************************************


import java.util.Scanner;

public class JavaHexToBin
{
public static void main(String[]args)
{
String[]hex={"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"};
String[]binary={"0000","0001","0010","0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1101","1110","1111"};

Scanner s=new Scanner(System.in);
System.out.print("Put hex value : ");
String userInput=s.next();
String result="";
for(int i=0;i<userInput.length();i++)
{
char temp=userInput.charAt(i);
String temp2=""+temp+"";
for(int j=0;j<hex.length;j++)
{
if(temp2.equalsIgnoreCase(hex[j]))
{
result=result+binary[j];
}
}
}
System.out.println("IT'S BINARY IS : "+result);
}
}


******************************************************************
JUST COMPILE AND EXECUTE IT
******************************************************************

Java determine pixel was clicked or not

Complete source code below just a simple java program that will determine if you was clicked or not on 8 black dots. If you was clicked , program will inform you that the dot was clicked.I hope you can see all 8 black dots that i mean because it is too small and try click on it to see what happen.

***************************************************************************
COMPLETE SOURCE CODE FOR : JavaDeterminePixelClicked.java
***************************************************************************


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class JavaDeterminePixelClicked extends Canvas implements MouseListener
{
boolean [] con={ false,false,false,false,false,false,false,false };
int[][]coor=new int[8][2];
Robot r;

public JavaDeterminePixelClicked()
{
addMouseListener(this);
try
{
r=new Robot();
}
catch(Exception exception)
{
exception.printStackTrace();
}

JFrame a=new JFrame(">>>>>TRY CLICK AT BLACK DOT<<<<<");
a.getContentPane().add(this,BorderLayout.CENTER);
a.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
a.setSize(500,500);
a.setLocationRelativeTo(null);
a.setVisible(true);
}

public void paint(Graphics g)
{
setBackground( new Color (255,255,255) );
super.paint(g);
g.setColor( new Color (0,0,0) );
g.drawLine(30,30,30,30);
g.drawLine(40,40,40,40);
g.drawLine(50,50,50,50);
g.drawLine(60,60,60,60);
g.drawLine(70,70,70,70);
g.drawLine(80,80,80,80);
g.drawLine(90,90,90,90);
g.drawLine(100,100,100,100);
}

public void mouseClicked(MouseEvent event)
{
Color temp=r.getPixelColor(event.getLocationOnScreen().x,event.getLocationOnScreen().y);
if(temp.getRed()==0&&temp.getGreen()==0&&temp.getBlue()==0)
{
int upperLeftX=getLocationOnScreen().x;
int upperLeftY=getLocationOnScreen().y;
int currentX=event.getLocationOnScreen().x;
int currentY=event.getLocationOnScreen().y;
if(currentX-upperLeftX==30&¤tY-upperLeftY==30)
{
if(con[0]==false)
{
con[0]=true;
}
else
{
JOptionPane.showMessageDialog(null,"This point was clicked");
}
}
if(currentX-upperLeftX==40&¤tY-upperLeftY==40)
{
if(con[1]==false)
{
con[1]=true;
}
else
{
JOptionPane.showMessageDialog(null,"This point was clicked");
}
}
if(currentX-upperLeftX==50&¤tY-upperLeftY==50)
{
if(con[2]==false)
{
con[2]=true;
}
else
{
JOptionPane.showMessageDialog(null,"This point was clicked");
}
}
if(currentX-upperLeftX==60&¤tY-upperLeftY==60)
{
if(con[3]==false)
{
con[3]=true;
}
else
{
JOptionPane.showMessageDialog(null,"This point was clicked");
}
}
if(currentX-upperLeftX==70&¤tY-upperLeftY==70)
{
if(con[4]==false)
{
con[4]=true;
}
else
{
JOptionPane.showMessageDialog(null,"This point was clicked");
}
}
if(currentX-upperLeftX==80&¤tY-upperLeftY==80)
{
if(con[5]==false)
{
con[5]=true;
}
else
{
JOptionPane.showMessageDialog(null,"This point was clicked");
}
}
if(currentX-upperLeftX==90&¤tY-upperLeftY==90)
{
if(con[6]==false)
{
con[6]=true;
}
else
{
JOptionPane.showMessageDialog(null,"This point was clicked");
}
}
if(currentX-upperLeftX==100&¤tY-upperLeftY==100)
{
if(con[7]==false)
{
con[7]=true;
}
else
{
JOptionPane.showMessageDialog(null,"This point was clicked");
}
}
}
}

public void mouseEntered(MouseEvent event)
{
//NOTHING
}

public void mouseExited(MouseEvent event)
{
//NOTHING
}

public void mousePressed(MouseEvent event)
{
//NOTHING
}

public void mouseReleased(MouseEvent event)
{
//NOTHING
}

public static void main(String[]args)
{
JavaDeterminePixelClicked jdpc = new JavaDeterminePixelClicked ();
}
}


***************************************************************************
JUST COMPILE AND EXECUTE IT
***************************************************************************

Java get degree from radian

Complete source code below just a simple java program that we can use to get degree value from radian value.

*****************************************************************
COMPLETE SOURCE CODE FOR : GetDegreeFromRadian.java
*****************************************************************


public class GetDegreeFromRadian
{
public static void main(String[]args)
{
//CHANGE RADIAN VALUE TO WHAT VALUE THAT YOU WANT
double radian=1.00;

//PRINT ANGLE VALUE THAT WE GET FROM RADIAN
System.out.println( "IT'S ANGLE IS : " + radian* (180/Math.PI) );
}
}


*****************************************************************
JUST COMPILE AND EXECUTE IT
*****************************************************************

Java receive user input and sort it in ascending order

Complete source code below is a simple java program that will receive user input and after that sort it in ascending order before display it's result.

*********************************************************************
COMPLETE SOURCE CODE FOR : AscendingString.java
*********************************************************************


import java.util.Scanner;

public class AscendingString
{
public static void main(String[]args)
{
Scanner s=new Scanner(System.in);
String[]a=new String[5];
for(int i=0;i<5;i++)
{
System.out.println(i+1+".Put your String");
String temp=s.nextLine();
a[i]=temp;
}

System.out.println("**********");
System.out.println("RESULT");
System.out.println("**********");

for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
String temp=a[i];
String tempB=a[j];
if(temp.compareTo(tempB)<0)
{
a[i]=tempB;
a[j]=temp;
}
}
}

for(int i=0;i<5;i++)
{
System.out.println(a[i]);
}
}
}


*********************************************************************
JUST COMPILE AND EXECUTE IT
*********************************************************************

Java recursive method

Sometime we need to solve a problem using the same way. For example you need to build a program that will print number from 1 until number that input by a user. First program will print number without using recursive technique and second program we will use recursive technique to print number from 1 until number that input by a user.You should see how a recursive method will do the same thing until problem solve.

FIRST PROGRAM :


import java.util.Scanner;

public class Test
{
public static void main(String[]args)
{
Scanner s=new Scanner(System.in);
System.out.println("Put a number");
int a=s.nextInt();

System.out.println("IT'S RESULT");
System.out.println("***************");

for(int i=1;i<=a;i++)
{
System.out.println(i);
}
}
}


SECOND PROGRAM :
print method in PrintNumber class is example of recursive method.


import java.util.Scanner;

public class Test
{
public static void main(String[]args)
{
Scanner s=new Scanner(System.in);
System.out.println("Put a number");
int myNumber=s.nextInt();
PrintNumber pn=new PrintNumber();
System.out.println("IT'S RESULT");
System.out.println("***************");
pn.print(myNumber);
}
}

class PrintNumber
{
public void print(int a)
{
if(a==1)
{
System.out.println(a);
}
else
{
int temp=a-1;
print(temp);
System.out.println(a);
}
}
}


For example if you put 2 into second program, when it enter print method in PrintNumber class, firstly program will check if the number is 1 or not. If not, the number will be minus with 1 before it send it's result to print method again. In this case it will send 1 to print method. So System.out.println(a) for a=2 will stop for a while until a=1. Now after it send 1 to print method, condition for a==1 will be true. 1 will be print, and System.out.println(a) for a=2 will continue again by print 2.

I hope it will help.

Java count all white space in String

Yesterday, before i go to sleep i tried to answer a simple question from a site. The question is about how to count all white space from String that input by a user. After a few time i failed, i found code below :

*********************************************************
COMPLETE SOURCE CODE FOR : CountAllWhitespaceInString.java
*********************************************************


import java.util.Scanner;

public class CountAllWhitespaceInString
{
public static void main(String[]args)
{
boolean a=true;
Scanner sc=new Scanner(System.in);

while(a)
{
System.out.println("Type EXIT to exit");
String b=sc.nextLine();
int whitespaceNumber=0;

if(!b.equalsIgnoreCase("exit"))
{
try
{
for(int i=0;i<=b.length()-1;i++)
{
char temp=b.charAt(i);
if(temp==' ')
{
whitespaceNumber++;
}
}
System.out.println("Whitespace number is : "+whitespaceNumber);
}
catch(Exception exception)
{
System.out.println("DON'T DO THAT");
}
}
else
{
a=false;
}
}
}
}


*********************************************************
JUST COMPILE AND EXECUTE IT
*********************************************************

Java reverse String and simplest palindrome checker

Today, i have learn something new and it's totally simple. Sometime when don't know about that, we will use our way that take a time to complete a simple task. What i learn today is, how to reverse String. The easiest way to reverse a String for example from "i love you" to "uoy evol i" is using method reverse() in StringBuffer class. It is also very useful especially when you want to create a palindrome checker program. Your program will be the shortest palindrome checker program in the world i think, hehehehe. I have also created a new one before, but it quit long. You can review it by click here. Now we will go to coding part. I will create two simple programs. Firstly to reverse "i love you" to "uoy evol i". Secondly i will create my shortest palindrome checker program that i ever made. Don't waste our time by hear what i said.

*************************************************************
COMPLETE SOURCE CODE FOR : ReverseString.java
*************************************************************


public class ReverseString
{
public static void main(String[]args)
{
String a="i love you";
StringBuffer b=new StringBuffer(a);

System.out.println(b.reverse());
}
}


*************************************************************
LIKE USUAL, JUST COMPILE AND EXECUTE IT
*************************************************************

*************************************************************
COMPLETE SOURCE CODE FOR : MySimplestPalindromeChecker.java
*************************************************************


import javax.swing.JOptionPane;

public class MySimplestPalindromeChecker
{
public static void main(String[]args)
{
String a=JOptionPane.showInputDialog(null,"What word that you want to check ?","Palindrome Checker",JOptionPane.QUESTION_MESSAGE);

if(a!=null)
{
String b=a;
StringBuffer c=new StringBuffer(b);
if(a.equals(c.reverse().toString()))
{
JOptionPane.showMessageDialog(null,"THIS IS A PALINDROME WORD");
}
else
{
JOptionPane.showMessageDialog(null,"THIS IS NOT A PALINDROME WORD");
}
}
}
}


*************************************************************
JUST COMPILE AND EXECUTE IT
*************************************************************

Square root symbol in java

After i spend a day to find how to print square root symbol in java through command prompt, someone tell me from java forum if our command prompt did not have a glyph for the square root character, this job will undone. But when i try use it in JOptionPane's message box it perform well.

In java "\u221A" is refer to square root symbol. It is Unicode.You can try to visit link below for more details :
Click here

If you want to print square root symbol through your command prompt, you can try something like this. I hope your command prompt can support for this thing.


public class Test2
{
public static void main(String[]args)
{
System.out.println("\u221A");
}
}


If you want to put it in JOptionPane's message box (this is perform well in my computer), you can try this :


import javax.swing.JOptionPane;

public class Test2
{
public static void main(String[]args)
{
JOptionPane.showMessageDialog(null,"\u221A");
}
}

Java simple rgb to hex converter

Complete source code below is a small java program that convert rgb value to hex value. Now i will share with you how to get hex value from rgb value. Before i go far, let we see a description about hex value structure.


VALUE THAT YOU GET AFTER DIVIDE OR MULTIPLY

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15
HEX VALUE0123456789ABCDEF

TABLE A

For example :
FFFFFF is a hex value for white and it's rgb value is 255 255 255.First FF is value for red, second FF is value for green and last FF is for blue.So it mean, first 255 in rgb is equal to first FF in hex value.Now i will show you, how to get FF from 255.For this tutorial i show you only for red part. You can try other last two part for green and blue. Now take 255 and devide it with 16. You should get 15.9375. Take value before floating point. In this case we get 15. Look at TABLE A at above. 15 is equal to F. Now you get first F.For second F, take value after floating point and multiply it with 16. In this case you should multiply 0.9375 with 16. It's result is equal to 15 and it also hold F.Now you get hex value for red part.I hope you can understand what i tried to say.

********************************************************************
COMPLETE SOURCE CODE FOR : RGBtoHEX.java
********************************************************************


import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.GridLayout;
import java.awt.Color;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class RGBtoHEX implements ActionListener
{
JTextField r=new JTextField(10);
JTextField g=new JTextField(10);
JTextField b=new JTextField(10);

JLabel rLabel=new JLabel("R",JLabel.CENTER);
JLabel gLabel=new JLabel("G",JLabel.CENTER);
JLabel bLabel=new JLabel("B",JLabel.CENTER);

JPanel panelTextField=new JPanel();
JPanel panelLabel=new JPanel();

JButton button=new JButton("Calculate Now !");

JTextField result=new JTextField(30);

public RGBtoHEX()
{
button.addActionListener(this);

r.setHorizontalAlignment(JTextField.CENTER);
g.setHorizontalAlignment(JTextField.CENTER);
b.setHorizontalAlignment(JTextField.CENTER);

result.setHorizontalAlignment(JTextField.CENTER);
result.setBackground(new Color(255,255,255));
result.setEditable(false);

panelLabel.setLayout(new GridLayout(1,3));
panelLabel.add(rLabel);
panelLabel.add(gLabel);
panelLabel.add(bLabel);

panelTextField.setLayout(new GridLayout(1,3));
panelTextField.add(r);
panelTextField.add(g);
panelTextField.add(b);

JFrame myMainWindow=new JFrame("RGB to HEX");
myMainWindow.getContentPane().setLayout(new GridLayout(4,1));
myMainWindow.getContentPane().add(panelLabel);
myMainWindow.getContentPane().add(panelTextField);
myMainWindow.getContentPane().add(button);
myMainWindow.getContentPane().add(result);

myMainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myMainWindow.setSize(300,130);
myMainWindow.setLocationRelativeTo(null);
myMainWindow.setVisible(true);
}

public void actionPerformed(ActionEvent event)
{
try
{
String hexValue="#";
r.setEditable(false);
g.setEditable(false);
b.setEditable(false);

double red=Double.parseDouble(r.getText());
double green=Double.parseDouble(g.getText());
double blue=Double.parseDouble(b.getText());

for(int i=1;i<=3;i++)
{
double temp=0;

if(i==1)
{
temp=red;
}
else if(i==2)
{
temp=green;
}
else if(i==3)
{
temp=blue;
}

double devideResult=temp/16;

String stringDevideResult=Double.toString(devideResult);

int pointIndexInString=stringDevideResult.indexOf(".");

String firstValue=stringDevideResult.substring(0,pointIndexInString);

double multiplySixteen=(devideResult-(Double.parseDouble(firstValue)))*16;

String stringMultiplySixteen=Double.toString(multiplySixteen);

pointIndexInString=stringMultiplySixteen.indexOf(".");

String secondValue=stringMultiplySixteen.substring(0,pointIndexInString);

if(firstValue.equalsIgnoreCase("10"))
{
firstValue="A";
}
if(firstValue.equalsIgnoreCase("11"))
{
firstValue="B";
}
if(firstValue.equalsIgnoreCase("12"))
{
firstValue="C";
}
if(firstValue.equalsIgnoreCase("13"))
{
firstValue="D";
}
if(firstValue.equalsIgnoreCase("14"))
{
firstValue="E";
}
if(firstValue.equalsIgnoreCase("15"))
{
firstValue="F";
}
if(secondValue.equalsIgnoreCase("10"))
{
secondValue="A";
}
if(secondValue.equalsIgnoreCase("11"))
{
secondValue="B";
}
if(secondValue.equalsIgnoreCase("12"))
{
secondValue="C";
}
if(secondValue.equalsIgnoreCase("13"))
{
secondValue="D";
}
if(secondValue.equalsIgnoreCase("14"))
{
secondValue="E";
}
if(secondValue.equalsIgnoreCase("15"))
{
secondValue="F";
}

hexValue=hexValue+firstValue+secondValue;
}


result.setText(hexValue);
r.setEditable(true);
g.setEditable(true);
b.setEditable(true);
}
catch(Exception exception)
{
JOptionPane.showMessageDialog(null,"Program error and it will terminate","ERROR",JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
}

public static void main(String[]args)
{
RGBtoHEX converter=new RGBtoHEX();
}
}


********************************************************************
JUST COMPILE AND EXECUTE IT
********************************************************************

RELAXING NATURE VIDEO