//**************************************************************************** //SortAndPrint.java file for Spring 2006, CS1501 Project 1: Anagrams //This class has a method that sorts an array of strings, a method that // prints the array of strings to a file, and a method that prints the // array to the standard output. // (This code was adapted from a Spring 2006 student project submission.) //**************************************************************************** import java.io.*; public class SortAndPrint { private String[] found; //String array to be sorted/printed private int foundSize; //Size of the array //******************************************************************** // Name: SortAndPrint(Constructor) // Description: Takes in the array to be sorted/printed //******************************************************************** public SortAndPrint(String[] anagrams) { foundSize = anagrams.length; found = anagrams; }//End Constructor //******************************************************************** // Name: printFound // Description: Prints the found words to the screen. //******************************************************************** public void printFound() { System.out.println("PRINTING!"); for(int a = 0; a < foundSize; a++) System.out.println(a + ": " +found[a]); } //******************************************************************** // Name: sort // Description: Sorts the found array alphabetically. //******************************************************************** public void sort() { System.out.println("SORTING!"); int newCount = 0; for (int i = 0; i < foundSize; i++) { int min = i; int j; //Selection sort //(Feel free to change this to a more efficient sorting algorithm!) for (j = i + 1; j < foundSize; j++) { if (found[j].compareTo(found[min]) < 0) min = j; }//end for String temp = found[min]; found[min] = found[i]; found[i] = temp; }//end for }//end method sort //******************************************************************** // Name: toFile // Description: Outputs the found words to a passed file name. //******************************************************************** public void toFile(String fileOut) { try { PrintWriter outFile = new PrintWriter(new BufferedWriter(new FileWriter(fileOut))); for(int a=0; a