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:
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() );
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 );
}
}
lol thanx, the end result wasnt that great tho but i think its one of the better pieces of coding i've done.
04-11-2005, 05:06 PM
MetalSkin
strange i got an exception about the Array version.... i'm running 1.4.1 JVM as the Web JVM, what version was it written in?
04-11-2005, 05:07 PM
Illegalx17
hmmm. . .idk what you mean about the array version, im using jdk 1.5.0
04-11-2005, 05:13 PM
MetalSkin
ahh, i c. u have used some of the later funtionality. very nice mate... nothing like a good sort.
If you want to go the next step, start reading up on Collections. You coudl change your code to use a collection, add each entry to the collection and then sort the collection. Some collections have self ordering. Only problem with a collection is that you can only add an object, so it would be an Integer not an int that you add.
Otherwise you could make a generic Class call it Value, implement java.lang.Comparable. thus you could test for the types of objects being compared and then compare appropriatly. thus you could mix both int, string, decimal.
04-11-2005, 05:14 PM
Illegalx17
whoa, im not near that far :p im in a java class, and im not that great at java. i dont get what you mean about objects. . .theres such thing as type Integer?
04-11-2005, 08:28 PM
MetalSkin
hehe... okay... reboot!
In all Object based lanugages or OOPS, you have primitives and objects. In jave we have:
boolean - true or false
char - one byte of data, unsigned
byte - one byte of data signed
int - integer, non factional
long - same as integer just goes bigger
float - factional version of int
double - factional version of long
now in these you can access with code just like:
Code:
* *boolean bo = true;
* *char c = 'a';
* *byte b = 0x03;
* *int i = 0;
* *long l = 0l;
* *float f = 0.0f;
* *double d = 0.0d;
Now for each primitive there is a corresponding 'wrapper' class, to allow you to perform operations on them as you would an object. They are all under java.lang.*. For example:
Code:
* *Boolean bo = new Boolean(true);
* *Byte b = new Byte('a');
* *Character c = new Character('a');
* *Integer i = new Integer(0);
* *Long l = new Long(0);
* *Float f = new Float(0.0f);
* *Double d = new Double(0.0);
now as a object, you can use them in collections, you can use the Comparator Intereface, which requires and object and a whole heap of other things. Generaly you would use primiatives, cause you cannot perform math operations on objects (but you could in c++).
So when you create a collection, you can store in it any object.
04-12-2005, 12:10 AM
Illegalx17
ok i kinda get it, we are almost to that chapter in my class, so i will learn all about that soon.
04-12-2005, 03:10 AM
Relentless
Ok, dude I love it. It can help me with my homework :D