Showing posts with label Java : BlueJ. Show all posts
Showing posts with label Java : BlueJ. Show all posts

Thursday, 3 October 2013

Permutation Iterative : Java : BlueJ

Permutation Iterative : Java : BlueJ

Objective :

The class PermutationIterative computes all the Unique Permutations of a string removing duplicates characters and white spaces. The Algorithm is a simple one ! All the permutations are  printed in lexicographic form.

Here the Original string is validated (duplicate and white spaces are removed) and sorted using bubble sort technique. It finds the next Permutation of a given string by the following set of rules:
  • Start scanning the string from the end such that arr[i] < arr[i+1]. Here it is advisable to start counting from S.length-1.
  • If i occurs to be less than 0, no more permutations are possible.
  • Start scanning from end such that arr[j]>arr[i].
  • Swap the elements arr[i] and arr[j]
  • reverse the elements after arr[i].
  • Now you have the next permutation and run a loop until the function return false.
The program will automatically run !n times and all the inputs will be in lexicographic form. This program is faster and memory efficient and can be easily applied in other languages.

BlueJ Program Screenshot :



Java Program Source Code :

/**
 * The class PermutationIterative computes all the Unique Permutations
 * of a string removing duplicates characters and whitespaces.
 * @AUTHOR SHANTANU KHAN 
 * @DATE  
 */
import java.util.*;
public class PermutationIterative
{
    private char[] S; // INSTANCE VARIABLE
    private int count;
    public PermutationIterative(String Word){ // CONSTRUCTOR
        S=removeInvalid(Word).toCharArray();
        bubbleSort();
    }
    
    private boolean permuteNext()
    {
        int i,j;
        i=S.length-2; // 1. STARTING FROM END FINDING i SUCH THAT S[i]<S[i+1]
        while(i>=0 && S[i]>S[i+1])
            i--;
        if(i<0) // NO MORE PERMUTATIONS POSSIBLE
            return false;
        j=S.length-1; // 2. ITERATING BACK FINDING j S[j]>S[i]
        while((int)S[j]<(int)S[i])
            j--;
        char temp=S[i]; // 3. SWAPPING arr[i] AND arr[j]
        S[i]=S[j];
        S[j]=temp;
        int f=i+1,b=S.length-1; // 4. REVERSING ELEMENTS AFTER i
        while(f<b){
            temp=S[f];
            S[f++]=S[b];
            S[b--]=temp;
        }
        return true;
    }
    
    public void permute(){
        System.out.println(S); // PRINT THE ORIGINAL WORD
        count++; // FOR THE FIRST PERMUTATION
        while(permuteNext()){ // EXECUTION ENDS WITHIN THE permuteNext()
            System.out.println(S);
            count++;
        }
        System.out.println("\nNo. of Permutations : "+count);
    }
    
    private static String removeInvalid(String s){
        String output="";
        for(int i=0;i<s.length();i++){
            if(!Character.isWhitespace(s.charAt(i))&&(output==""||output.indexOf(s.charAt(i))<0))
                output+=s.charAt(i);
        }
        return output;
    }
    
    public void bubbleSort()
    {
        for(int i=S.length-1;i>0;i--){ // NUMBER OF PASSES
            for(int j=0;j<i;j++){        // NUMBER OF COMPARISONS IN EACH PASS
                if(S[j]>S[j+1]){     // IF GREATER, SWAPPING J AND J+1 ELEMENT
                    char temp=S[j];    S[j]=S[j+1];    S[j+1]=temp; // SWAPPING ELEMENTS
                }
            } // END OF COMPARISION (j) LOOP
        } // END OF PASS (i) LOOP
    }
    
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter the Word : ");
        PermutationIterative obj=new PermutationIterative(sc.nextLine());
        obj.permute();

7 Segment Display : Java : BlueJ

Objective :

7 Segment Display with 0-F with Decimal Point
A seven-segment display (SSD), or seven-segment indicator, is a form of electronic display device for displaying decimal numerals that is an alternative to the more complex dot-matrix displays. Seven-segment displays are widely used in digital clocks, electronic meters, and other electronic devices for displaying numerical information. A seven segment display, as its name indicates, is composed of seven elements. Individually on or off, they can be combined to produce simplified representations of the arabic numerals. Often the seven segments are arranged in an oblique (slanted) arrangement, which aids readability. In most applications, the seven segments are of nearly uniform shape and size (usually elongated hexagons, though trapezoids and rectangles can also be used), though in the case of adding machines, the vertical segments are longer and more oddly shaped at the ends in an effort to further enhance readability.

Truth Table Of A Seven Segmented Display 0-9
Each of the numbers 0, 6, 7 and 9 may be represented by two or more different glyphs on seven-segment displays.
The seven segments are arranged as a rectangle of two vertical segments on each side with one horizontal segment on the top, middle, and bottom. Additionally, the seventh segment bisects the rectangle horizontally. There are also fourteen-segment displays and sixteen-segment displays (for full alphanumerics); however, these have mostly been replaced by dot-matrix displays.
The segments of a 7-segment display are referred to by the letters A to G, as shown to the right, where the optional DP decimal point (an "eighth segment") is used for the display of non-integer numbers.

BlueJ Program Screenshot :



Java Program Source Code :

The Source Code Uses Certain Image Files So : For Proper Distribution, The Program is Available for Download in Two Formats :

1. For Windows (Direct Executable .exe) Recommended  [Link] :  http://www.fileden.com/files/2012/1/12/3250168/7SegmentDigitalBCD.zip

2. For Other Platform (Mac, Ubuntu, Linux) [Link] :
http://www.fileden.com/files/2012/1/12/3250168/DigitalBCD.jar

Open URL In Default System Browser : Java : BlueJ

Objective :

Opening an URL from your Java Program in the User-Default browser, with Java SE 6 there is. Unless your platform doesn't support this. Well Most Platform Running Latest Version of Java and With A Default Browser Would Do It Fine !

In Java SE 6 a new class has been added to the java.awt package: Desktop. This class has a public class method isDesktopSupported() which returns true if the class is supported on the current platform, and false otherwise.


If the class is supported, you can retrieve an instance using the class method getDesktop().


You can use the browse() method to display a URI in the default browser. If this browser is not able to handle the specified URI, the application registered for handling the URIs of the specified type is invoked. For example, the URI news:comp.lang.perl.misc might invoke a Usenet client.


However, before you call the browse() method make sure to check if this action is supported on the current platform by calling the isSupported() method with java.awt.Desktop.Action.BROWSE as the value of the first and only parameter.


Note that even though this method returns true, it's still possible that the platform doesn't support the URI - for example if there is no application registered for the given scheme. In this case the browse() method will throw an IOException.

Besides the browse() method there are 5 more methods, providing support for the 4 following Desktop actions:

EDIT : The edit() method launches the associated editor application and opens a file for editing.
MAIL : There are two mail() methods. The first one doesn't take any parameters and launches the mail composing window of the user-default mail client. The second method takes a single parameter: a "mailto" URI. : It also launces the mail composing window of the user-default mail client, but additionally fills in the message fields specified by the "mailto" URI (for example, to, subject, etc.).
OPEN : The open() method launches the associated application to open the file. If the specified file is a directory, the file manager of the current platform is launched to open it.
PRINT : Prints a file with the native desktop printing facility, using the associated application's print command.

BlueJ Program Screenshot :




Java Program Source Code :

/**
 * The Program Runs in Command Prompt Or in IDE using URLs
 * in the form of Parameters. All URLs must be in http:// form.
 * © SHANTANU KHAN 
 * @ shantanukhan1995@gmail.com
 * @website 0code.blogspot.com 
 * Program Type : BlueJ Program - Java
 */
import java.net.URI;
import java.awt.Desktop;
public class OpenURL
{
    public static void main(String[] args)
    {
        if(!java.awt.Desktop.isDesktopSupported())
        {
            System.err.println("Desktop is not supported (fatal)");
            System.exit(1);
        }
        if(args.length==0)
        {
            System.out.println( "Usage: OpenURI [URI [URI ... ]]" );
            System.exit( 0 );
        }
        java.awt.Desktop desktop = java.awt.Desktop.getDesktop();
        if(!desktop.isSupported(java.awt.Desktop.Action.BROWSE))
        {
            System.err.println( "Desktop doesn't support the browse action (fatal)" );
            System.exit( 1 );
        }
        for(String arg:args)
        {
            try{
                java.net.URI uri = new java.net.URI(arg);
                desktop.browse( uri );
            }
            catch(Exception e){
                System.err.println(e.getMessage());
            }
        }
    }