Java!

Tech topics & all that sweet sweet jazz

Moderator: nobile

User avatar
nobile
Kakaist
Kakaist
Posts: 522
Joined: Wed Dec 12, 2001 00:32
MySpace: www.myspace.com/nobile01
Location: Honduras
Contact:

Java!

Postby nobile » Sun Mar 19, 2006 06:36

Ok, so here are a bazillion versions of the same program, for some reason we decided to do at least three different ways of the same thing, the program is supposed to show the arrays and order them.
Sorry for the great mess of the code, if you guys want to comment on it, please I'd be thankful for it ^_^
[syntax="java"]
import javax.swing.*;
class Burbujas
{
public void asc(int y[])
{
int k=y.length-1,tmp,numB=0;
while(numB<k)
{
int a=k;
int b=numB;
numB=y.length;
for(int j=b;j<a;j++)
{
if(y[j]>y[j+1])
{
tmp=y[j+1];
y[j+1]=y[j];
y[j]=tmp;
if (j<numB)
{
numB=j-1;
if(numB<0)
{
numB=0;
}
}
else if(j>k)
{
k=j+1;
}
}
}
}
}
public void des(int x[])
{
int k=x.length-1,tmp,numB=0;
while(numB<k)
{
int a=k;
int b=numB;
numB=x.length;
for(int j=b;j<a;j++)
{
if(x[j]<x[j+1])
{
tmp=x[j+1];
x[j+1]=x[j];
x[j]=tmp;
if (j<numB)
{
numB=j-1;
if(numB<0)
{
numB=0;
}
}
else if(j>k)
{
k=j+1;
}
}
}
}
}
}
class BurbujaMejorada
{
public static void main(String arg[])
{
String sn[]=new String[10],salida=" ",sop;
int in[]=new int[10],iop;
Burbujas ad=new Burbujas();
for(int i=0;i<in.length;i++)
{
sn[i]=JOptionPane.showInputDialog("number "+(i+1)+": ");
in[i]=Integer.parseInt(sn[i]);
}
do
{
sop=JOptionPane.showInputDialog("Menu\n"
+"1 Ascending\n"
+"2 Descending\n"
+"3 Exit\n"
+"Choice: ");
iop=Integer.parseInt(sop);
switch(iop)
{
case 1:a.asc(in);
for(int i=0;i<in.length;i++)
{
salida+=in[i]+"\n";
}
JOptionPane.showMessageDialog(null,salida);
salida=" ";
break;
case 2:a.des(in);
for(int i=0;i<in.length;i++)
{
salida+=in[i]+"\n";
}
JOptionPane.showMessageDialog(null,salida);
salida=" ";
break;
case 3:break;
default:JOptionPane.showMessageDialog(null,"Give another number","Error",JOptionPane.ERROR_MESSAGE);
}
}while(iop!=3);
System.exit(0);
}
}
[/syntax]
Regular one...


[syntax="java"]
import javax.swing.JOptionPane;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class Burbuja_Mejorada_I extends JFrame
{
public Burbuja_Mejorada_I()
{
super("R E S U L T S");
setSize(500,300);
setVisible(true);
}

public void ordenamBurbuja( int arreglo2[])
{
int nBajo = 0;
int nAlto = arreglo2.length -1;

while(nBajo< nAlto)
{
int alto = nAlto;
int bajo = nBajo;

nBajo = arreglo2.length;

for( int elemento = bajo; elemento < alto; elemento++)
{

if(arreglo2[ elemento ] > arreglo2[elemento + 1])
{
intercambiar(arreglo2, elemento, elemento + 1);
if (elemento < nBajo)
{
nBajo = elemento - 1;
if (nBajo < 0)
{
nBajo = 0;
}
}
else if (elemento > nAlto)
{
nAlto = elemento + 1;
}
}
}
}
}

public void intercambiar(int arreglo3[], int primero, int segundo)
{
int almacen;

almacen = arreglo3[primero];
arreglo3[primero] = arreglo3[segundo];
arreglo3[segundo] = almacen;
}

public void paint (Graphics g)
{
super.paint(g);

int arreglo[] = {2,4,8,1,3,5,7,6,9,10};

String salida1 = "Original array: ";
String salida1_1 = " ";

for ( int contador = 0; contador < arreglo.length; contador++)
salida1_1 += " " + arreglo[ contador ];

ordenamBurbuja(arreglo);

String salida2 = "Ascending order: ";
String salida2_1 = " ";

for ( int contador =0; contador < arreglo.length; contador++)
salida2_1 += " " + arreglo[contador];

g.setColor(Color.WHITE);
g.setFont(new Font("Monospaced", Font.BOLD,30));
g.fillRect(3,3,500,300 );

g.setColor(Color.BLUE);
g.setFont(new Font("Monospaced", Font.BOLD,30));
g.drawRect(5,31,488,263 );

g.setColor(Color.BLUE);
g.drawRect(50,40,400,50 );//Title's rectangle
g.drawRect(60,49,380,32 );//Title's rectangle

g.setColor(Color.BLACK);
g.setFont(new Font("Monospaced",Font.BOLD + Font.ITALIC,18));
g.drawString("ASCENDING ORDER ", 122, 63);
g.drawString("WITH IMPROVED BUBBLE METHOD", 85, 78);

g.setFont(new Font("Monospaced", Font.ITALIC + Font.BOLD,14));
g.setColor(Color.BLUE);
g.drawString(salida1, 50, 105);

g.setFont(new Font("Monospaced", Font.ITALIC + Font.BOLD,14));
g.setColor(Color.RED);
g.drawString(salida1_1, 60, 125);

g.setColor(Color.BLACK);
g.setFont(new Font("Monospaced", Font.ITALIC,30));
g.drawString("______________________", 50, 130);

g.setFont(new Font("Monospaced", Font.ITALIC + Font.BOLD,14));
g.setColor(Color.BLUE);
g.drawString(salida2, 50, 160);

g.setFont(new Font("Monospaced", Font.ITALIC + Font.BOLD,14));
g.setColor(Color.RED);
g.drawString(salida2_1, 60, 180);
}

public static void main(String arg[])
{
Burbuja_Mejorada_I aplicacion = new Burbuja_Mejorada_I();
aplicacion.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
[/syntax]
The sort of prettier version...


[syntax="java"]
import java.awt.*;
import javax.swing.*;

public class AppletBurbuja extends JApplet
{
int n[] = {5,8,23,6,8,2,345,0,23,9};
int k=n.length-1,tmp,numB=0,x[] = new int[10],y[] = new int[10];
String salida;
JTextArea areaSalida=new JTextArea();
public void init()
{
for(int i=0;i<10;i++)
{
x[i]=n[i];
y[i]=n[i];
}
while(numB<k)
{
int a=k;
int b=numB;
numB=y.length;
for(int j=b;j<a;j++)
{
if(y[j]>y[j+1])
{
tmp=y[j+1];
y[j+1]=y[j];
y[j]=tmp;
if (j<numB)
{
numB=j-1;
if(numB<0)
{
numB=0;
}
}
else if(j>k)
{
k=j+1;
}
}
}
}//ascending

numB=0;
k=n.length-1;
while(numB<k)
{
int a=k;
int b=numB;
numB=x.length;
for(int j=b;j<a;j++)
{
if(x[j]<x[j+1])
{
tmp=x[j+1];
x[j+1]=x[j];
x[j]=tmp;
if (j<numB)
{
numB=j-1;
if(numB<0)
{
numB=0;
}
}
else if(j>k)
{
k=j+1;
}
}
}
}//descending
salida="normal ascending descending\n";
for(int i=0;i<10;i++)
{
salida+="\n"+n[i]+" "+y[i]+" "+x[i];
}
areaSalida.setText(salida);
Container contenedor = getContentPane();
contenedor.add(areaSalida);
}
}
[/syntax]
Headache applet version =X
Beauty is in the eyes of the beholder <3

vrap
Kakaist
Kakaist
Posts: 89
Joined: Sun Nov 11, 2001 15:52
Location: Manipal, India
Contact:

Postby vrap » Sun Mar 19, 2006 07:38

I have just one comment : use better identifiers! The middle program is fine, but you should use better variable names than n, b, k etc. It's not so big a deal most of the time, but someone should be able to understand a bit of code within a minute or so of looking at it, and single letter names don't help that one bit, right?
Image
i'm having the most perfect hallucination

User avatar
nobile
Kakaist
Kakaist
Posts: 522
Joined: Wed Dec 12, 2001 00:32
MySpace: www.myspace.com/nobile01
Location: Honduras
Contact:

Postby nobile » Sun Mar 19, 2006 07:44

=P
=D yea you're right, the x, y and k vars are completely lost, but the others are kind of what they stand for, I mean, a = ascending, d = descending, n = number, etc...
I had some troubles with the applet program, we wanted to make it not using the JTextArea thing, but it never worked, the last thing we had before giving up was somethign like this:
[syntax="java"]
import java.awt.*;
import javax.swing.JApplet;
import java.awt.event.*;

public class APPLET_BURBUJA extends JApplet
{
int n[] = {5,8,23,6,8,2,345,0,23,9};
String sx[]=new String[10],sy[]=new String[10],sn[]=new String[10];
int k=n.length-1,tmp,numB=0;
int x[] = new int[10];
int y[] = new int[10];

public void init()
{
for(int i=0;i<10;i++)
{
x[i]=n[i];
y[i]=n[i];
}
while(numB<k)
{
int a=k;
int b=numB;
numB=y.length;
for(int j=b;j<a;j++)
{
if(y[j]>y[j+1])
{
tmp=y[j+1];
y[j+1]=y[j];
y[j]=tmp;
if (j<numB)
{
numB=j-1;
if(numB<0)
{
numB=0;
}
}
else if(j>k)
{
k=j+1;
}
}
}
}//ascending

numB=0;
k=n.length-1;
while(numB<k)
{
int a=k;
int b=numB;
numB=x.length;
for(int j=b;j<a;j++)
{
if(x[j]<x[j+1])
{
tmp=x[j+1];
x[j+1]=x[j];
x[j]=tmp;
if (j<numB)
{
numB=j-1;
if(numB<0)
{
numB=0;
}
}
else if(j>k)
{
k=j+1;
}
}
}
}//descending
for(int i=0;i<10;i++)
{
sx[i]=""+x[i]+" ";
sy[i]=""+y[i]+" ";
sn[i]=""+n[i]+" ";
}
}

public void paint(Graphics g)
{
super.paint(g);

g.setColor(Color.BLUE);
g.drawRect(135,1,225,38 );//Title's rectangle
g.drawRect(145,10,205,20 );//Title's rectangle

g.setColor(Color.BLACK);
g.drawString("*** ARRAYS ***", 160, 25 );

g.setColor(Color.RED);//normal
g.drawString(sn[0],165,120);
g.drawString(sn[1],165,140);
g.drawString(sn[2],165,160);
g.drawString(sn[3],165,180);
g.drawString(sn[4],165,200);
g.drawString(sn[5],165,220);
g.drawString(sn[6],165,240);
g.drawString(sn[7],165,260);
g.drawString(sn[8],165,280);
g.drawString(sn[9],165,300);
g.drawString(sn[10],165,320);

g.setColor(Color.WHITE);//descending
g.drawString(sx[0],190,120);
g.drawString(sx[1],190,140);
g.drawString(sx[2],190,160);
g.drawString(sx[3],190,180);
g.drawString(sx[4],190,200);
g.drawString(sx[5],190,220);
g.drawString(sx[6],190,240);
g.drawString(sx[7],190,260);
g.drawString(sx[8],190,280);
g.drawString(sx[9],190,300);
g.drawString(sx[10],190,320);

g.setColor(Color.BLUE);//ascending
g.drawString(sy[0],210,120);
g.drawString(sy[1],210,140);
g.drawString(sy[2],210,160);
g.drawString(sy[3],210,180);
g.drawString(sy[4],210,200);
g.drawString(sy[5],210,220);
g.drawString(sy[6],210,240);
g.drawString(sy[7],210,260);
g.drawString(sy[8],210,280);
g.drawString(sy[9],210,300);
g.drawString(sy[10],210,320);
}
}
[/syntax]
It only showed the first group of things, and when we commented that one, it showed the second, and if we commented those two, it would show the third, it never showed the three of them at the same time O_o

I've had a very hard time understanding the applets logic, haven't found them interesting at all, or even fun to make, and now I feel like I can't program anymore x_x
Beauty is in the eyes of the beholder <3

vrap
Kakaist
Kakaist
Posts: 89
Joined: Sun Nov 11, 2001 15:52
Location: Manipal, India
Contact:

Postby vrap » Sun Mar 19, 2006 15:27

I really wish I could test out the code for you. I'm having serious problems with my comp, something to do with being unable to open large archives so I can't install the JDK :/
Image
i'm having the most perfect hallucination

User avatar
nobile
Kakaist
Kakaist
Posts: 522
Joined: Wed Dec 12, 2001 00:32
MySpace: www.myspace.com/nobile01
Location: Honduras
Contact:

Re: Java!

Postby nobile » Mon Jul 30, 2007 06:23

*notices the syntax thing doesn't work anymore*

Any plans in re-enabling it? =P
Beauty is in the eyes of the beholder <3

User avatar
Smeagol
Kakaist
Kakaist
Posts: 1422
Joined: Wed Nov 07, 2001 14:32
Location: Vänersborg, Sweden
Contact:

Re: Java!

Postby Smeagol » Mon Jul 30, 2007 08:13

yea of course, got to look into it, the ordinary [code] doesn't work as it should so i reckon we gotta get that plugin back :)
He's not heavy, he's my brother.


Return to “Geek Juice”

Who is online

Users browsing this forum: No registered users and 7 guests