About this application:
This application implements Straight Selection Sort algorithm
If there are N numbers
Note: This is SWT application
Source Code:
package selection
import java
import org
import org
import org
import org
import org
import org
import org
import org
import org
import org
import org
import org
import org
import org
import org
import org
/**
* This application implements Straight Selection Sort algorithm
* get the minimum number from the numbers
* number
* this
* please e
*
* @author vivien Data:
*/
public class StraightSelectionSort {
/** The string containing the number wait for sorted */
public String numString = new String();
public Text numText;
public Text resText;
public Button btSort;
public Label errorLabel;
/** The flag to indicate if there is any error for inputed numbers */
public boolean hasError = false;
/** The arrayList containing the double numbers wait for sorted */
public ArrayList<Double> numList = new ArrayList<Double>();
public static void main(String[] args) {
StraightSelectionSort selectionSort = new StraightSelectionSort();
selectionSort
}
/**
* Create the control for the interface
*/
public void createControl() {
Display display = new Display();
Shell shell = new Shell(display);
shell
// Set Title
shell
FormLayout layout = new FormLayout();
shell
FormData fd = new FormData();
// The Start Sort button
btSort = new Button(shell
btSort
fd = new FormData();
fd
fd
fd
btSort
// The Input numbers group
Group numGroup = new Group(shell
numGroup
numGroup
fd = new FormData();
fd
fd
fd
fd
numGroup
// Label for input numbers
Label numLabel = new Label(numGroup
numLabel
fd = new FormData();
fd
fd
fd
numLabel
// Text for input numbers
numText = new Text(numGroup
| SWT
numText
fd = new FormData();
fd
fd
fd
fd
numText
// The results group
Group resGroup = new Group(shell
resGroup
resGroup
fd = new FormData();
fd
fd
fd
fd
resGroup
// Label for results
Label resLabel = new Label(resGroup
resLabel
fd = new FormData();
fd
fd
fd
resLabel
// Text for results
resText = new Text(resGroup
| SWT
resText
resText
fd = new FormData();
fd
fd
fd
fd
resText
// Label for showing error message
errorLabel = new Label(shell
fd = new FormData();
fd
fd
fd
fd
errorLabel
errorLabel
// Listen to the numText change
numText
@Override
public void modifyText(ModifyEvent e) {
numString = numText
hasError = false;
}
});
// If press Return
numText
@Override
public void keyPressed(KeyEvent e) {
if (e
e
btSort
startSort();
}
}
});
// Listen to the button selection
btSort
public void widgetSelected(SelectionEvent e) {
startSort();
}
});
shell
while (!shell
if (!display
display
}
display
}
/**
* Get double values from string
*/
public void getDoubleFromString() {
int index =
// Split string using space
String[] splitedNumbers = numString
if (numList
// Clear the arrayList for last used
numList
for (int i =
if (splitedNumbers[i]
try {
numList
} catch (NumberFormatException e) {
setErrorMessage(
hasError = true;
break;
}
}
}
}
/**
* Start sort the string containing numbers waited for sort
*/
public void startSort() {
if (numString != null)
if (numString
getDoubleFromString();
startStraightSelectionSort();
setResults();
} else {
setErrorMessage(
hasError = true;
}
}
/**
* Set the results to the results group
*/
public void setResults() {
if (!hasError) {
String resString = new String();
for (int i =
if (i != numList
resString = resString + numList
else
// If be the last string
resString = resString + numList
resText
// Clear errorLabel
errorLabel
}
}
/**
* Sort the numbers using Straight selection Sort algorithm
*/
public void startStraightSelectionSort() {
int minPosition =
for (int j =
minPosition = j;
for (int i = j +
if (numList
minPosition = i;
}
}
if (minPosition != j) {
// Exchange the minimum with the first number of the numbers
// waited for sort
double temp = numList
numList
numList
}
}
}
/**
* Set the error message on the error Label
*
* @param errorString
* The string used for set on the errorLabel
*/
public void setErrorMessage(String errorString) {
errorLabel
// Clear the text of results
resText
hasError = true;
}
}
Black
Java核心技術免費提供,內容來源於互聯網,本文歸原作者所有。