Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.10 KB | None | 0 0
  1. package listDriver;
  2. import list.*;
  3. import java.util.Random;
  4.  
  5. /**
  6. * Lab 2
  7. * Test methods added to the List interface
  8. *
  9. * @author (sdb)
  10. * @version (Sep 2017)
  11. */
  12. public class DriverLab2
  13. {
  14. /**
  15. * This main method tests the List classes
  16. * for lab 2, Data Structures and Algorithms
  17. */
  18. public static void main (String[] args )
  19. { List<String> friends = new ArrayList<String> ();
  20. System.out.println ("Testing ArrayList");
  21. test (friends);
  22.  
  23. List <String> enemies = new LinkedList<String> ();
  24. System.out.println ("\nTesting LinkedList");
  25. test (enemies);
  26.  
  27. ////// Uncomment the following when ready for problem 2
  28. // problem2(enemies);
  29.  
  30. ////// Uncomment the following when ready for problem 3
  31. // problem3(friends, enemies);
  32. }
  33.  
  34.  
  35. private static void problem2(List<String> enemies)
  36. { List<String> strings = new LinkedList();
  37. Random rand = new Random(0);
  38. final int MAX=1000000;
  39. int sum=0,randInt,ndx;
  40. System.out.println ("\nTesting problem 2");
  41. for (int i=0; i<MAX; i++)
  42. strings.add("str" + i);
  43. for (int i=0; i<10000; i++)
  44. { ndx = strings.size() - rand.nextInt(10) - 1;
  45. sum = sum + strings.get(ndx).length();
  46. ndx = rand.nextInt(10);
  47. sum = sum + strings.get(ndx).length();
  48. }
  49. System.out.println ("sum is " + sum);
  50. }
  51.  
  52. private static void test (List<String> friends)
  53. {
  54. friends.add ("joe");
  55. friends.add ("mary");
  56. friends.add ("jim");
  57. friends.add ("joe"); // Lists may contain duplicate elements
  58. friends.add (2, "sally"); // Insert at position 2
  59. friends.remove (0); // Remove joe at position 0
  60. if (friends.size() != 4)
  61. System.err.println ("Not correct, possible error in size");
  62.  
  63.  
  64. String s1 = "sal";
  65. String s2 = "ly"; // s1 + s2 is "sally"
  66. //if (friends.indexOf (s1+s2) != 1)
  67. //System.err.println ("Not correct, possible error in indexOf");
  68. //if (friends.contains ("Jim"))
  69. //System.err.println ("Not correct, possible error in contains");
  70. //if (!friends.contains ("jim"))
  71. //System.err.println ("Not correct, possible error in contains 2");
  72. friends.add ("mary");
  73.  
  74. //if (friends.indexOf(new String("mary")) != 0)
  75. //System.err.println ("Not correct");
  76.  
  77. // if (!friends.remove (new String("jim"))) // remove first occurrence of "jim"
  78. // System.err.println ("Not correct, remove(Object) returning wrong value");
  79. friends.remove(2);
  80. //if (friends.contains ("jim"))
  81. //System.err.println ("Not correct, possible error in remove(Object)");
  82. System.out.println ("The list is: " + friends);
  83.  
  84. }
  85.  
  86. private static void problem3 (List <String> friends, List <String> enemies)
  87. {
  88.  
  89. if (friends.equals ("friends"))
  90. System.err.println ("Not correct");
  91. if (friends.equals (new ArrayList <String> ()))
  92. System.err.println ("Not correct");
  93. if (enemies.equals (new ArrayList <String> ()))
  94. System.err.println ("Not correct");
  95. if (! friends.equals (enemies))
  96. System.err.println ("Not correct");
  97. if (! enemies.equals (friends))
  98. System.err.println ("Not correct");
  99. friends.add (1, new String ("jimmy"));
  100. if (enemies.equals (friends))
  101. System.err.println ("Not correct");
  102. if (friends.equals (enemies))
  103. System.err.println ("Not correct");
  104. enemies.add (1, "Jimmy");
  105. if (friends.equals (enemies))
  106. System.err.println ("Not correct");
  107. if (enemies.equals (friends))
  108. System.err.println ("Not correct");
  109. enemies.remove (1);
  110. enemies.add (1, new String ("jimmy"));
  111. if (! enemies.equals (friends))
  112. System.err.println ("Not correct");
  113. if (! friends.equals (enemies))
  114. System.err.println ("Not correct");
  115. System.out.println ("\nProblem 3 complete");
  116. }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement