JAVA: Find Nth Smallest Number
Tarih: 9 Ocak 2014 Yorum: 0

(Find Nth Smallest Number) Write a program that finds nth smallest element among 15 integer values created randomly from the range 1-100.  The value n will be given by the user (1 through 15). Searching the array must be performed by a method with the following header:

public static int findNthSmallestNumber(int n, int[ ] numbers)

In your program, firstly, random numbers must be created and stored in an array. Secondly, the user is asked to enter an integer value.  Thirdly, the method is invoked with appropriate parameters.  Lastly, the result returning from the method is displayed on the screen.

Example:
The user enters 5 and the program generates the following numbers.  15,18,5,7,45,78,54,91,63,24,18,77,61,83,65,
The program output should be 18. (5th smallest element).
Note that you are not allowed to use any sort method from the Java library.

 

FindSmallestNumber.java

/**
 * Find Nth Smallest Number. 
 */
 import java.util.Scanner; 
public class FindSmallestNumber {	
    /** Main method */
	public static void main(String[] args) {
		int input;
       	int[] arraylist	= new int[15]; // Create array object.

		random(arraylist); // Create random numbers.
		arraySort(arraylist); // Sort random numbers.

	   	do{
			System.out.print("Please enter a number between 1 and 15: ");
	       	Scanner sc = new Scanner(System.in);
	       	input	= sc.nextInt();   				
	 	}while(input < 1 || input > 15);

		// random numbers to string.
		String result ="";
			for (int j = 0; j < arraylist.length; j++) {
				result += " " + arraylist[j];
			}

       	// Prinf all random number.
       	System.out.println("The Numbers are" + result);

       	// Print user input nth number.
       	if(input == 1){
       		System.out.println("The " + input + "st number is " + findNthSmallestNumber(input,arraylist) + ".");
       	}else if(input == 2){
       		System.out.println("The " + input + "nd number is " + findNthSmallestNumber(input,arraylist) + ".");
       	}else if(input == 3){
       		System.out.println("The " + input + "rd number is " + findNthSmallestNumber(input,arraylist) + ".");
       	}else{
       		System.out.println("The " + input + "th number is " + findNthSmallestNumber(input,arraylist) + ".");
       	} 	
    } // End of main method.

    /** random number find method */
	public static void random(int[] arraylist){
		for(int i=0; i < arraylist.length; i++){
       		arraylist[i]	= (int) (Math.random() * 100);	
       	}
	} // End of random method.

    /** Array sort method */
	public static void arraySort(int [] arraylist){
	    int temp;   
	    for (int i=1; i<arraylist.length; i++){
	        for(int j=0; j < arraylist.length-i; j++){
	            if (arraylist[j] > arraylist [j+1]){
	                temp = arraylist [j];
	                arraylist [j] = arraylist [j+1];
	                arraylist [j+1] = temp;
	            }
	        }
	    }
	} // End of arraySort method.

	/** find nth smallest method. */
	public static int findNthSmallestNumber(int n, int[] numbers){
		return numbers[n-1];
	} // End of findNthSmallesNumber method.

} // End of class.





tema yapımcısı wordpress alexa bilgileri Webmaster Creative Commons v3 ile Lisanslanmıştır!


Akif ARSLAN © 2012 - 2024
Sitede bulunan istediğiniz cümleyi veya içeriği, istediğiniz gibi, istediğiniz yerde, istediğiniz zaman ve istediğiniz kişilerle paylaşabilirsiniz.