Pages

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

Sunday, December 26, 2010

Sort Selection Algorithm Using Java

/**
 * Class provide sorting algorithm based on sort selection
 * 
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @date    : December 27, 2010
 * @version : 1.0
 */

class Sorting{


    /*
     * data array of integer
     */
    private int[] data;

    /*
     * len size of the data
     */
    private int len;

    /*
     * Constructor service
     * @param data array of integer
     */
    public Sorting(int[] data){
        this.data = data;
        this.len = this.data.length;
    }

    /*
     * method to get ascending series of the data
     * @return array in ascending order
     */
    public int[] getAsc(){       
        for(int i=0;i<this.len;i++){
            int minIndex = i;
            int tempValue = this.data[minIndex];
            for(int j=i;j<this.len-1;j++){
                if(this.data[j+1]<this.data[minIndex]){
                    minIndex = j+1;
                }
            }
            this.data[i] = this.data[minIndex];
            this.data[minIndex] = tempValue;
        }
        return this.data;
    }

    /*
     * method to get data in descending order
     * @return array in descending order
     */
    public int[] getDesc(){
        for(int i=0;i<this.len;i++){
            int maxIndex = i;
            int tempValue = this.data[maxIndex];
            for(int j=i;j<this.len-1;j++){
                if(this.data[j+1]>this.data[maxIndex]){
                    maxIndex = j+1;
                }
            }
            this.data[i] = this.data[maxIndex];
            this.data[maxIndex] = tempValue;
        }
        return this.data;
    }

}
       

Selection Algorithm Using Java

/**
 * Class to provide selection algorithm implementation
 * 
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @date    : December 27, 2010
 * @version : 1.0
 */

class SelectionAlgorithm{

    /*
     * data array of integer
     */
    private int[] data;

    /*
     * len size of the data
     */
    private int len;


    /**
     * A constructor service
     * @param data array of integer
     */
    public SelectionAlgorithm(int[] data){
        this.data = data;
        this.len = this.data.length;
    }

    /**
     * method to get an index of min value of the data
     * @return minIndex int index of min value
     */
    public int getMinIndex(){
        int minIndex = 0;
        for(int i=1;i<this.len;i++){
            if(this.data[i]<this.data[minIndex]){
                minIndex = i;
            }
        }
        return minIndex;
    }

    /**
     * method to get an index of max value of the data
     * @return maxIndex int an index of max value
     */
    public int getMaxIndex(){
        int maxIndex = 0;
        for(int i=1;i<this.len;i++){
            if(this.data[i]>this.data[maxIndex]){
                maxIndex = i;
            }
        }
        return maxIndex;
    }

    /**
     * method to get min value
     * @return minValue int min value of the data
     */
    public int getMinValue(){
        int minValue = this.data[0];
        for(int i=1;i<this.len;i++){
            if(this.data[i]<minValue){
                minValue = this.data[i];
            }
        }
        return minValue;
    }

    /**
     * method to get max value
     * @return maxValue int max value of the data
     */
    public int getMaxValue(){
        int maxValue = this.data[0];
        for(int i=1;i<this.len;i++){
            if(this.data[i]>maxValue){
                maxValue = this.data[i];
            }
        }
        return maxValue;
    }

}
          

Friday, December 17, 2010

Java - FolderDir

/**
 *
 * This class is equivalen with DIR command in windows system.
 *
 * @author irfanudin ridho
 * @date december 19, 2010
 * @email irfan.ub@gmail.com
 * @version 1.0
 *
 */


import java.io.File;
import java.util.ArrayList;


class FolderDir{
   
    /*
     * Constructor of FolderDir
     * @param f String folder you search to know
     */
    public FolderDir(String f){
        file = new File(f);
        files = file.listFiles();

    }
   
    /**
     * method to get to know how many files in the directory
     *
     * @return int Count the files
     */
    public int getFilesCount(){
        int fileCount = 0;
        for(int i=0;i<files.length;i++){
            if(files[i].isFile()){
                fileCount = fileCount + 1;
            }
        }
        return fileCount;
    }
   
    /**
     * method to get to know how many folders in the directory
     *
     * @return int Count the folders
     */
    public int getFolderCount(){
        int folderCount = 0 ;
        for(int i=0;i<files.length;i++){
            if(files[i].isDirectory()){
                folderCount = folderCount + 1;
            }
        }
        return folderCount;
    }
   
    /**
     * method to list files as an array
     *
     * @return Array Array of Object for Files
     */
    public Object[] listFiles(){
        ArrayList<String> alFiles = new ArrayList<String>();
        for(int i=0;i<files.length;i++){
            if(files[i].isFile()){
                alFiles.add(files[i].toString());
            }
        }

        return alFiles.toArray();
    }
   
    /**
     * method to list folders as an array
     *
     * @return Array Array of Object for Folders
     */
    public Object[] listFolders(){
        ArrayList<String> alFolders = new ArrayList<String>();
        for(int i=0;i<files.length;i++){
            if(files[i].isDirectory()){
                alFolders.add(files[i].toString());
            }
        }

        return alFolders.toArray();
    }

    private File file;
    private File files[];
}