A class called Bubbles in which all the code is placed in
the constructor -- namely getting
the number of scores and then the actual scores.Then creating an object of
another class "sort"
The object is called piggy. I then call 2 methods in that class "sort_data"
and "print".
Both classes "Bubbles" and "sort" exist in the same pacjage.
Both are user written. -- not predefined.
And parameters are being passed (arrays by reference) and an int
value.containing the number of
elements in the array.
package untitled9;
import javax.swing.JOptionPane;
public class Bubbles
{
public Bubbles()
{
int ne = Integer.parseInt(JOptionPane.showInputDialog("Enter number of scores ?"));
int scores[] = new int [ne]; // dynamically declaring an array
for (int a=0; a<ne; a++)
scores[a] = Integer.parseInt(JOptionPane.showInputDialog("Please enter exam score ?"));
sort piggy = new sort ();
piggy.sort_data (scores, ne);
piggy.print (scores,ne); // calling the print method on an object of the sort class
}
public static void main(String[] args)
{
Bubbles bub=new Bubbles();
System.exit(0);
}
}
********************************************************
package untitled9;
import javax.swing.JOptionPane;
public class sort
{
public sort()
{
// no code in the constructor right now
}
public void sort_data (int scores[], int ne)
{
int a,b,c;
for (a=0; a<(ne-1); a++)
{
for (b=a+1; b<ne; b++)
{
if (scores[a] < scores [b])
{
c=scores [a];
scores[a]=scores[b];
scores[b]=c;
}
}
}
}
public void print(int scores[], int ne)
{
String out = "";
for (int a=0; a<ne; a++)
out += scores[a]+"\n";
JOptionPane.showMessageDialog(null,out);
}
}