You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
2.2 KiB

6 years ago
  1. // Test driver for the SortingAlgs class
  2. // Do not make any changes to this file!
  3. // Xiwei Wang
  4. import java.io.*;
  5. import java.util.Arrays;
  6. public class TestCardSorting
  7. {
  8. public static void main(String[] args)
  9. {
  10. SortingAlgs mySort = new SortingAlgs();
  11. try
  12. {
  13. // read the decks from file
  14. ObjectInputStream in = new ObjectInputStream(new FileInputStream("Decks.dat"));
  15. Deck[] myDeck = new Deck[5];
  16. myDeck = (Deck[])in.readObject();
  17. for (int i = 0; i < 5; i++)
  18. {
  19. Card[] myCards = myDeck[i].getPartialDeck(myDeck[i].getnumCards());
  20. String before = Arrays.toString(myCards);
  21. System.out.println("Test " + (i + 1) + ":");
  22. System.out.println("The cards before sorting are\n" + before);
  23. System.out.println("------------------------------------");
  24. Card[] testCards = myCards.clone();
  25. mySort.insertionSort(testCards);
  26. String after = Arrays.toString(testCards);
  27. System.out.println("The cards after Insertion Sort are\n" + after);
  28. if (after.equals(myDeck[i].getSortedDeck()))
  29. System.out.println("Your Insertion Sort works correctly.\n");
  30. else
  31. System.out.println("Something is wrong with your Insertion Sort.\n");
  32. testCards = myCards.clone();
  33. mySort.mergeSort(testCards);
  34. after = Arrays.toString(testCards);
  35. System.out.println("The cards after Merge Sort are\n" + after);
  36. if (after.equals(myDeck[i].getSortedDeck()))
  37. System.out.println("Your Merge Sort works correctly.");
  38. else
  39. System.out.println("Something is wrong with your Merge Sort.");
  40. System.out.println("====================================\n");
  41. }
  42. }
  43. catch (Exception e)
  44. {
  45. System.out.println("Error occurred: " + e.getMessage());
  46. }
  47. }
  48. }