0 members and 526 guests
No Members online

» Site Navigation
» Stats
Members: 35,442
Threads: 103,075
Posts: 826,688
Top Poster: cc.RadillacVIII (7,429)
|
-
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
If you want help...
Screw you
If you make sigs...
Screw you
-
Long......*scrolls down page*
Creator of the GFXvoid Header......................................Retired GFXvoid Staff.
Currently: Never Here
-
lol thanx, the end result wasnt that great tho but i think its one of the better pieces of coding i've done.
If you want help...
Screw you
If you make sigs...
Screw you
-
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?
-
hmmm. . .idk what you mean about the array version, im using jdk 1.5.0
If you want help...
Screw you
If you make sigs...
Screw you
-
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.
-
whoa, im not near that far 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?
If you want help...
Screw you
If you make sigs...
Screw you
-
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.
-
ok i kinda get it, we are almost to that chapter in my class, so i will learn all about that soon.
If you want help...
Screw you
If you make sigs...
Screw you
-
Ok, dude I love it. It can help me with my homework
Similar Threads
-
By javagirl in forum Introductions
Replies: 1
Last Post: 03-14-2006, 01:31 PM
-
By Morphius in forum The Void
Replies: 0
Last Post: 02-23-2005, 05:48 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|