July 27, 2012

Prime Factor in java


Calculate Prime Factor in java

The program demonstrate how  to find Least common mutiple and Highest common factor in java.

public class

PrimeFactor{



    /**
     * raviatgmail.com
     *
     * 
     */
    private Scanner scanner_ins = null;                                 // To scan user inputed value
   
    private int totalNumber = 0;                                         //     totalNumber by user
    private int minimumValue = 0;                                       //     Minimum value inside the user inputed array
    private int lcm = 1;
    private int hcf = 1;
    private int [] value_array = null;                                  // User entered value
    private int [] denominators = {1,9,8,7,6,5,4,3,2};
   
   
   
    /**
     * Constructor
     */
    PrimeFactor(){ 
        scanner_ins = new Scanner(System.in);
    }
   
   
    /**
     * getUserInputs()
     *
     * To Get input from user
     */
    private boolean getUserInputs() {
        boolean isZero = false;
       
        System.out.println(" Enter the total number of elements (Say n) : ");
        totalNumber = scanner_ins.nextInt();                                   //scan the user inputs into array
        value_array = new int[totalNumber];                                  // initialized the size of array
       
        for(int i =0 ;i < totalNumber; i++)                                   //This for loop used to get the elements from user
        {
            System.out.print(" Enter the "+(i+1)+"th elements : ");
            value_array[i]=scanner_ins.nextInt();
           
            if(value_array[i] == 0)
                isZero = true;
        }
        return isZero;
    }
   
   
    /**
     * findMinimumValue()
     *
     * To find the minimum value from user inputs
     */
    private void findMinimumValue() {

        int temp =0;                                                           // A temporary variable
        temp = value_array[0];                                              // assingning first value of array to temp
       
        for(int i=1; i value_array[i]){
                temp = value_array[i];                                       // changing the temp value to minimum
            }
           
        }
        minimumValue = temp;
    }
   
   
   

    /**
     * findCommonMultiples()
     *
     * To find the H C F
     */
    private void findCommomMultiples() {
        denominators[0] = minimumValue;
        int reminder =0;   // reminder
        boolean isCommonMultiper = false;  // is a common multipler
        boolean isComputed = false;
       
        for(int denominator : denominators)
        {
                if(isComputed == false){
                   
                    for(int value : value_array)
                    {
                        reminder = value % denominator;
                        if(reminder == 0)
                        {
                            isCommonMultiper = true;
                            continue;
                        }
                        else
                        {
                            isCommonMultiper = false;
                            break;
                        }
                    }
                   
                    if(isCommonMultiper == true)
                    {
                        lcm = lcm * denominator;
                       
                        for(int i = 0; i < totalNumber ; i++)
                        {
                            if(value_array[i] !=0){
                                value_array[i] = value_array[i] / denominator;
                           
                                if(value_array[i] == 1)
                                    isComputed = true;
                            }
           
                        }
                    }

                }  //end of if(is computed)
        }
        hcf = lcm;
    }
   
   

    /**
     * findLCM()
     *
     * To calculate LCM from HCF
     */
    private void findLCM() {

       
        for(int i=0; i < value_array.length; i++)
        {
            lcm = lcm * value_array[i];
        }
       
    }
   
   
   
   
    public static void main(String[] args) {
        // main class where program starts
       
       
        PrimeFactor primeFactor = new PrimeFactor();                  //instance (ins) of Lcm class
        boolean isZeroExist = primeFactor.getUserInputs();          //for getting user input (total elements, elements into array)
       
        if(isZeroExist == false){
            long startTime = System.currentTimeMillis();
            primeFactor.findMinimumValue();                          //To find minimum value user inputed
            primeFactor.findCommomMultiples();                         //To calculated H C F
            primeFactor.findLCM();
            primeFactor.showDetails((System.currentTimeMillis() - startTime));
        }
        else{
            System.out.println("\n\t\t PRIME FACTORS \n ");
           
            System.out.println("\t  H C F is Zero");
            System.out.println("\t  L C M is Zero");
            }
       
    }


    /**
     * showDetails()
     *
     * To Display the output to user
     */

    private void showDetails(long timeTaken) {
       
        System.out.println("\n\t\t PRIME FACTORS \n ");

       
        System.out.print("\n  User inputs   [ ");
        for(int i = 0 ; i < totalNumber ; i++)   //This for loop used to user inputed values
        {
            System.out.print(" " + (value_array[i] * hcf));
        }
        System.out.println(" ]");
       
       
        System.out.print("\n  After dividing by common multiples   [ ");
        for(int i = 0 ; i < totalNumber ; i++)   //This for loop used to view value array after iteration
        {
            System.out.print(" " + value_array[i]);
        }
        System.out.println(" ]");
       
       
        System.out.println("  H C F is "+hcf);
        System.out.println("  L C M is "+lcm);
       
        System.out.println("  Computation Time : "+timeTaken+" ms");
       
    }
}


prime factor

Description : computer science practical question, java interview aptitude questions. The program will find least common multiple and highest common factor.



How to upload image file in java web application [java server page and servlets]

How to upload a Image file from jsp page to servlet 

This is a small example of java web application


 <form action="UserPicsServlet" enctype="multipart/form-data" method="post">
<table>
       <tbody>
<tr>
       <td><input name="filePath" type="file" /></td>
      
        </tr>
<tr></tr>
<tr>
        <td><input name="btnSubmit" type="submit" value="SAVE" /></td>
       <td><input name="btnSubmit" type="reset" value="Cancel" /></td>
       </tr>
</tbody></table>
</form>
Thanking you....

July 20, 2012

Java Ternary Example

Java Ternary Example - An alternate solution for if- else in java.


/**
 * @author ComputerScience
 *
 */
public class TerenaryExample {
    public static void main(String[] args) {
        int i = 0;
        int j = 1;
        String k = i < j ? "i less than j" : "j less than i";
        System.out.println(k);
    }
}


Thanking you....

July 18, 2012

Events

Java events at India


 Java One at India


Great Indian Developer Submit link

Java conference in India link

All java events in this year (calendar) link

link2

Thanking you....

How many Memory does java allocates for boolean datatype?

How many Memory does java allocates for boolean datatype?


Thanking you....

July 16, 2012

How to get the last modified image file in a folder?

This is an example program to get last modified image file from your computer


Description : The Program checks all image file in the folder specified by the user and get the properties of the image.

package imageIO;
import java.io.File;

public class Newest {

   /**
     * return the last modified file with the extension jpeg from the dirPath
     *
     * @param dirPath
     *            : directory for searching last modified jpeg file
     * @return fileName : last modified file with extension jpeg
     */
    public String lastModifiedFile(String dirPath) {
        File dir = new File(dirPath);
        File[] files = dir.listFiles();
        String fileName = files[0].getAbsolutePath();
        long fileValue = files[0].lastModified();
       
        for (int i = 0; i < files.length; i++) {
            String ext = files[i].getName().substring(
                    files[i].getName().lastIndexOf(".") + 1,
                    files[i].getName().length());
           
            if (((ext.equalsIgnoreCase("jpg") || (ext.equalsIgnoreCase("jpeg")))) & (files[i].lastModified() > fileValue)) {
               
                fileValue = files[i].lastModified();
                fileName = files[i].getAbsolutePath();
            }
        }
        return fileName;
    }

    public static void main(String[] crimea) {

        Newest newst = new Newest();
        System.out.println(newst.lastModifiedFile("H:\\GASPIPE\\MOTOX"));

    }
}


The project search for the last modified file in the folder MOTOX. The current program ( java source code) search for JPG and JPEG file. If you need to get  image files having other extensions , you need to add that condition too in the below code.

if (((ext.equalsIgnoreCase("jpg") || (ext.equalsIgnoreCase("jpeg")))) & (files[i].lastModified() > fileValue));


Copy and paste and run it...

Try the code and provide us a feedback.....

July 09, 2012

Project Management Softwares

Listing some open source web based project management software for tracking their projects

  Go to blog

Top project management softwares link1

How to configure Bsnl internet in iball modem/router

read
Thanking you....

Facebook comments