hey, this isnt as much for other people to use, as its so easy, but this is a java applet i just finished for school. seems like a really simple concept but it was actually extremely hard for me to do. i had some problems with the gui and stuff, and im still a noob at java. plz tell me what you think. heres the code:
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SortArray extends JApplet implements ActionListener {
JButton setButton, checkButton, resetButton;
JTextField setField;
String arrayLengthinput = "";
int arrayLengthInput = 0;
String Input = "";
int small = 1000000000;
int large = -99999999;
String array2Sorted = "";
int R = 0;
int array2[];
int array[];
int y = 0;
public void init()
{
Container container = getContentPane();
container.setLayout( new FlowLayout() );
setField = new JTextField( 15 );
setField.setEditable( false );
container.add( setField );
setButton = new JButton( "Set Values" );
setButton.addActionListener( this );
container.add( setButton );
checkButton = new JButton( "Check" );
checkButton.addActionListener( this );
container.add( checkButton );
resetButton = new JButton( "Reset" );
resetButton.addActionListener( this );
container.add( resetButton );
setField.setText( "Values Not Set" );
}
public void actionPerformed( ActionEvent event )
{
if( event.getSource() == setButton )
Mod1();
if( event.getSource() == resetButton )
Clear();
if( event.getSource() == checkButton ){
*if( arrayLengthInput == 0 ){
*setField.setText( "Please Set Values First" );
*}else{
*Sort();
*Show();
}
}
}
public void Mod1()
{
large = -99999999;
arrayLengthinput = JOptionPane.showInputDialog( "How many numbers will you be entering?" );
arrayLengthInput = Integer.parseInt( arrayLengthinput );
array = new int[arrayLengthInput];
array2 = new int[array.length];
for( int r = 0; r < array.length; r++ ) {
R = r + 1;
if( r == 0 && R != array.length )
Input = JOptionPane.showInputDialog( "Please enter the first number. *This will be your first number");
if( r == 0 && R == array.length )
Input = JOptionPane.showInputDialog( "You have chosen to enter only one number. *Please enter your first and last number.");
if( R == array.length && r != 0 )
Input = JOptionPane.showInputDialog( "Please enter the last number. *This will be your last number" );
if( r != 0 && R != array.length )
Input = JOptionPane.showInputDialog( "Please enter the next number. *This will be number " + R + " of the " + array.length + " numbers you decided to input" );
setField.setText( "Values Set" );
array[r] = Integer.parseInt( Input );
}
array2Sorted = "";
}
public void Clear()
{
large = -99999999;
arrayLengthinput = "";
arrayLengthInput = 0;
for( int x = 0; x < array.length; x++ )
array[x] = 0;
for( int X = 0; X < array2.length; X++ )
array2[X] = 0;
small = 1000000000;
array2Sorted = "";
setField.setText( "Values Not Set" );
}
public void Sort()
{
for( int Q = 0; Q < array.length; Q++ ){
for( int q = 0; q < array.length; q++ ){
if( array[q] < small ){
small = array[q];
}
}
for( int e = 0; e < array.length; e++ ){
if( array[e] == small )
array[e] = 1000000000;
}
if( small < 1000000000 )
array2[Q] = small;
small = 1000000000;
}
}
public void Show()
{
for( int T = 0; T < array2.length; T++ ){
*if( array2[T] > large )
*large = array2[T];
}
for( int E = 0; E < array2.length; E++ ){
*if( array2[E] != 0 ){
*array2Sorted += array2[E];
*if( array2[E] != large )
*array2Sorted += ", ";
}
}
JOptionPane.showMessageDialog( null, "Your numbers, in order from least to greatest:\n\n" + array2Sorted );
}
}
and you can see the applet in action Here