2009年6月8日 星期一

Homework 6-1-2009 Modular Sorting

Write a sort method which takes a double array as parameter
and returns the sorted array. Call this method in a main program.

Hint: The lab is a rewriting of Lab Sorting
to make the sorting procedure a reusable method.



-----.





----
package javaapplication8;

/**
*
* @author KFN
*/
public class Sort
{

/** Creates a new instance of Sort */
public Sort()
{
}

public static void sort( double[] score ) {
int index = 0;
double temp = 0.0;
for( index = score.length-1; index >-1 ; --index)
{
for (int j = 0; j < index; j++)
{
if(score[index] > score[j])
{
temp = score[j];
score[j] = score[index];
score[index] = temp;
}
}
}


}

}

---
package javaapplication8;

/**
*
* @author KFN
*
*/
import java.util.Scanner;
public class ArrayOfScores
{

/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
double[] score = new double[5];
int index;
double max, temp;

System.out.println("Enter 5 Scores");
score[0] = keyboard.nextDouble();
max = score[0];
for(index = 1; index < score.length; ++index)
score[index] = keyboard.nextDouble();

Sort.sort( score );
/*
Sort a = new Sort();
a.sort( score );
*/
System.out.println("The scores are" );
for(index = 0; index< score.length; index++)
System.out.println(score[index]);

沒有留言:

張貼留言