You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1.6 KiB

SortArgs

Instructions

Create a file named SortArgs.java.

Write a function sort that sorts the given array specified in the parameters and prints it to the standard output. The elements should be separated by spaces followed by a a new line character. All the given elements are valid numbers.

Expected Functions

public class SortArgs {
    public static void sort(String[] args) {
        // your code here
    }
}

Usage

Here is a possible ExerciseRunner.java to test your function :

import java.io.*;

public class ExerciseRunner {
    public static void main(String[] args) throws IOException {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        PrintStream printStream = new PrintStream(outputStream);

        var defaultOut = System.out;

        System.setOut(printStream);
        SortArgs.sort(new String[]{"4", "2", "1", "3"});
        System.setOut(defaultOut);

        String output = outputStream.toString();
        System.out.println(output.equals("1 2 3 4\n"));
    }
}

and its output :

$ javac *.java -d build
$ java -cp build ExerciseRunner
true
$

Notions

Command-Line Arguments Conditional statement String Array File