I have a program that allows a user to input an integer value into an applet (the number entered determines the size of a list). From the value entered the program determines how many comparisons are required to sort the list using the size inputted. The applet is working but the program is just printing out a sorted list instead of the comparison required. I need help finishing up my code. Please and thank you.
import java.util.*; //supplies random class
import java.awt.*; //supplies user interface classes
import java.awt.event.*; //supplies event cleasses
import java.applet.Applet; //supplies applet class
public class TestingUse extends Applet implements ActionListener
{
public void actionPerformed(ActionEvent event) //event handler method
{
size = Short.parseShort(inputField.getText()); //gets integer input from user
values = new int[size]; //generate the size
initValues(size); //fill the array
selectionSort(); //sort the array
inputField.setText(""); //resets input field
printValues();
outLabel.setText("The comparison is: " + what????);
}
void initValues(int size) //gets number of values and randomly generates values
{
Random rand = new Random();
for (int index = 0; index < size; index++)
values[index] = Math.abs(rand.nextInt()) % size;
}
int minIndex(int startIndex, int endIndex) //finds the smallest value in the array and swaps it with the first value
{ //in the array position
int indexOfMin = startIndex;
for (int index = startIndex + 1; index <=endIndex; index++)
if (values[index] < values[indexOfMin])
indexOfMin = index;
return indexOfMin;
}
public void swap(int index1, int index2) //swaps the integers at location index1 and index2 of array values
{
int temp = values[index1];
values[index1] = values[index2];
values[index2] = temp;
}
void selectionSort() //the elements in the array values are sorted
{
int endIndex = size - 1;
for (int current = 0; current < endIndex; current++)
swap(current, minIndex(current, endIndex));
}
void printValues()
{
int value;
for (int index = 0; index < size; index++)
{
value = values[index];
if(((index -1)*index)/2 ==0) //this formula is not doing what I want
System.out.println(value);
else
System.out.println(value + " ");
}
System.out.println();
}
//declare fields for applet viewer
private TextField inputField;
private int[] values;
private int size;
private Label outLabel;
private Button button;
//init method - to set up fields and buttons on applet
public void init()
{
Label label;
label = new Label("Enter # for comparison ");
button = new Button("enter");
button.addActionListener(this);
inputField = new TextField("Value");
outLabel = new Label("The number of comparisons is: ");
add(label);
add(inputField);
add(button);
add(outLabel);
setLayout(new GridLayout(5,0));
}
}