I'm having issues with executing a program that I have written. The program simply gets a user to input values whereby trying to guess a random number. I get to finding the median and the middle number but the program won't calculate them both. I need to use a insertion sort to be able to execute the median, but for some reason the program wont even do middle and median.
Could someone have a look at my code and see what I'm doing wrong please! ...
public void findMiddleNumber() { middleNumber = test[( guesses / 2 )]; calculateMedian(); } public void calculateMedian() { System.out.println( "Before:" ); System.out.println( sort ); // print unsorted array insertionSort(); System.out.println( "After:" ); System.out.println( sort ); // if (( size % 2 ) == 0 ) // even median = ( sort[ size / 2 ] + sort[ size / 2 - 1 ]) / 2.0; else median = sort[ size / 2 ]; findGuessedNumbers(); } public void insertionSort() { sort = new int[ size ]; for ( int i = 0; i < size; i++) sort[ i ] = inputGuess; sort(); } public void sort() { int insert; for( int next = 1; next < sort.length; next++) { insert = sort[ next ]; int moveItem = next; while( moveItem > 0 && sort[ moveItem -1 ] > insert ) { sort[ moveItem ] = sort[ moveItem -1 ]; moveItem--; } sort[ moveItem ] = insert; } } public void findGuessedNumbers() { finalOutput(); } public void finalOutput() { JOptionPane.showMessageDialog( null,"Congratulations, your guess " + "is correct." + "\n\n1. Middle number from all guessed " + "numbers by the user is: " + middleNumber + "\n\r2. Median " + " value of all guessed numbers by the user is: " + median + "\n\r3. A position (array index) of correctly " + "guessed number in sorted array is: " + positionIndex, "Final Output", JOptionPane.PLAIN_MESSAGE ); } public static void main( String [] args ) { GuessGame application = new GuessGame(); application.setDefaultCloseOperation( EXIT_ON_CLOSE ); } class GuessHandler implements ActionListener { @Override public void actionPerformed ( ActionEvent event ) { inputGuess = Integer.parseInt( guessInput.getText() ); test[guesses] = inputGuess; processGuessedNumber( inputGuess ); guesses++; } } }