Question

In: Computer Science

The program should be able to do the following: In Java accepts one command line parameter....

The program should be able to do the following: In Java

  1. accepts one command line parameter. The parameter specifies the path to a text file containing the integers to be sorted. The structure of the file is as follows:

    1. There will be multiple lines in the file (number of lines unknown).

    2. Each line will contain multiple integers, separated by a single whitespace.

  2. reads the integers from the text file in part a into an array of integers.

  3. sort the integers in ascending order, and then prints out a sorted version of these integers, one per line

  4. Implements the selection sort algorithm.

Solutions

Expert Solution

Code


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;


public class Main {
public static void main(String[] args)
{
int []arr=new int[1000];
int count=0;
String line;
BufferedReader reader;
try
{
reader = new BufferedReader(new FileReader(args[0]));
line = reader.readLine();
while (line != null)
{   
// read next line
String []value=line.split(" ");
for(int i=0;i<value.length;i++)
{
arr[count]=Integer.parseInt(value[i]);
count++;
}
       line = reader.readLine();
}
reader.close();
   }
catch (IOException e)
{
System.out.println(args[1]+" file not found.");
   }
System.out.println("Original Array is: ");
printArray(arr,count);
selectionSort(arr, count);
System.out.println("\n\nAfter sorting array is: ");
printArray(arr,count);
}
public static void selectionSort(int[] arr,int size)
{
for (int i = 0; i < size - 1; i++)
{
int index = i;
for (int j = i + 1; j < size; j++){
if (arr[j] < arr[index]){
index = j;//searching for lowest index
}
}
int smallerNumber = arr[index];   
arr[index] = arr[i];
arr[i] = smallerNumber;
}
}

private static void printArray(int[] arr, int count)
{
for(int i=0;i<count;i++)
System.out.println(arr[i]);
}
}
Numbers.txt file

output

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.


Related Solutions

Write a Java application that accepts a bar code as a command line parameter and prints...
Write a Java application that accepts a bar code as a command line parameter and prints out the ZIP code. Assume that the bar code uses the symbols "|" and ":" for the long and short bars, respectively. Provide warnings on errors in the bar code specifying what exactly is wrong. The bar code input should be in the format specified in Problem 1, including the pair of the full bars at the beginning and at the end. Important: The...
A C program that accepts a single command line argument and converts it in to binary...
A C program that accepts a single command line argument and converts it in to binary with array length of 16 bits. The array should contain the binary of the int argument. the program should also convert negative numbers. Side note the command line arg is a valid signed int.
Write a C program that accepts a port number as a command line argument, and starts...
Write a C program that accepts a port number as a command line argument, and starts an HTTP server. This server should constantly accept() connections, read requests of the form: GET /path HTTP/1.1\r\n\r\n read the file indicated by /path, and send it over the "connect" file descriptor returned by the call to accept().
program c Write a program called filesearch that accepts two command-line arguments: A string A filename...
program c Write a program called filesearch that accepts two command-line arguments: A string A filename If the user did not supply both arguments, the program should display an error message and exit. The program opens the given filename. Each line that contains the given string is displayed. Use the strstr function to search each line for the string. You may assume no line is longer than 255 characters. The matching lines are displayed to standard output (normally the screen).
**Need to use awk command in putty (should be a ONE LINE COMMAND) Write the command...
**Need to use awk command in putty (should be a ONE LINE COMMAND) Write the command that would find all lines that have an email address and place a label email = before the line in the file longfile output will multiple lines similar to this one : using a good awk command the output would be something like this email = From: "Linder, Jann/WDC" <[email protected]> email = To: Mr Arlington Hewes <[email protected]> email = > From: Mr Arlington Hewes...
Create your own function in Java that accepts one input parameter and returns a float number....
Create your own function in Java that accepts one input parameter and returns a float number. You decide the theme.
#Write a function called "load_file" that accepts one #parameter: a filename. The function should open the...
#Write a function called "load_file" that accepts one #parameter: a filename. The function should open the #file and return the contents.# # # - If the contents of the file can be interpreted as # an integer, return the contents as an integer. # - Otherwise, if the contents of the file can be # interpreted as a float, return the contents as a # float. # - Otherwise, return the contents of the file as a # string. #...
Write a Java program which reads a positive integer from the command line, then displays the...
Write a Java program which reads a positive integer from the command line, then displays the sum of all even values up to and including the value provided, followed by the sum of all odd values up to and including the value provided. validate that the command line argument is an integer greater than 0 to validate the type, you can use Integer.parseInt() with a try/catch for NumberFormatException use one or more for loops to perform the even and odd...
Write one Java program and satisfy the following requirements: Write a method called cube that accepts...
Write one Java program and satisfy the following requirements: Write a method called cube that accepts one integer parameter and returns that value raised to the third power. Write a method called randomNumber that returns a random floating-point number in the range of [-20.0, 50.0). (hints: use Random class in the method) Write a method called findMax that accepts three floating-point number as parameters and returns the largest one.(hints: use conditional statement in the method) Overload findMax that accepts one...
A java program with classes and methods to manage employment. Program should be able to store/add...
A java program with classes and methods to manage employment. Program should be able to store/add employees, calculate payroll, display or schedule shifts. All classes should have at least a null constructor and copy constructor. Other constructors are up to your discretion. Include all necessary accessors and modifiers. To test the classes, create a container class with simply a main() method. The container class will have attributes that are class objects of each of the classes you create. The main()...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT