Program not executing correctly
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! ...
Code :
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++;
}
}
}
Re: Program not executing correctly
Re: Program not executing correctly
Re: Program not executing correctly
Re: Program not executing correctly
Im in need of help ... not criticism ... and its asking a different question thankyou
Re: Program not executing correctly
Quote:
Originally Posted by
daemonlies
Im in need of help ... not criticism ... and its asking a different question thankyou
Its not criticism, it is help...I do recommend reading the last link I provided in post #2, as it might help you understand the point of linking to crossposts.
Re: Program not executing correctly
I understand your post on the 'cross posting' point ... and if i could i would have done it correctly in the first place ... and only posted once ... as for the stackoverflow, i have aready been in talks with the members from stack about my coding as they see that as a beginners level and should not have been posted on their site.