import java.util.Arrays; import java.util.Scanner; /** * Calculates the median of a numbered set of real values */ public class Solution { /** * Gets data from standard input */ private Scanner input; /** * Start the execution of the solution * @param args Command line arguments */ public static void main(String args[]) { Solution solution = new Solution(); solution.run(); } /** * Run the solution. This method is called from main() */ public void run() { // Create object to read data from standard input this.input = new Scanner(System.in); // Create an array to store all the values DoubleArray values = new DoubleArray(); // Read all values while ( this.input.hasNextDouble() ) { values.append( input.nextDouble() ); } // Sort all the values values.bubbleSort(); // The median is the value at the center if ( values.getCount() % 2 == 1 ) { // The array has an odd number of elements, we have a value at the center System.out.printf("%.2f\n", values.getAt( values.getCount() / 2 ) ); } else { // We do not have a value at the center, so we calculate the average (mean) // of the two values around the center of the array double sum = values.getAt(values.getCount() / 2 - 1) + values.getAt(values.getCount() / 2); double mean = sum / 2.0; System.out.printf("%.2f\n", mean); } // Close the standard input input.close(); } }