Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package listDriver;
- import list.*;
- import java.util.Random;
- /**
- * Lab 2
- * Test methods added to the List interface
- *
- * @author (sdb)
- * @version (Sep 2017)
- */
- public class DriverLab2
- {
- /**
- * This main method tests the List classes
- * for lab 2, Data Structures and Algorithms
- */
- public static void main (String[] args )
- { List<String> friends = new ArrayList<String> ();
- System.out.println ("Testing ArrayList");
- test (friends);
- List <String> enemies = new LinkedList<String> ();
- System.out.println ("\nTesting LinkedList");
- test (enemies);
- ////// Uncomment the following when ready for problem 2
- // problem2(enemies);
- ////// Uncomment the following when ready for problem 3
- // problem3(friends, enemies);
- }
- private static void problem2(List<String> enemies)
- { List<String> strings = new LinkedList();
- Random rand = new Random(0);
- final int MAX=1000000;
- int sum=0,randInt,ndx;
- System.out.println ("\nTesting problem 2");
- for (int i=0; i<MAX; i++)
- strings.add("str" + i);
- for (int i=0; i<10000; i++)
- { ndx = strings.size() - rand.nextInt(10) - 1;
- sum = sum + strings.get(ndx).length();
- ndx = rand.nextInt(10);
- sum = sum + strings.get(ndx).length();
- }
- System.out.println ("sum is " + sum);
- }
- private static void test (List<String> friends)
- {
- friends.add ("joe");
- friends.add ("mary");
- friends.add ("jim");
- friends.add ("joe"); // Lists may contain duplicate elements
- friends.add (2, "sally"); // Insert at position 2
- friends.remove (0); // Remove joe at position 0
- if (friends.size() != 4)
- System.err.println ("Not correct, possible error in size");
- String s1 = "sal";
- String s2 = "ly"; // s1 + s2 is "sally"
- //if (friends.indexOf (s1+s2) != 1)
- //System.err.println ("Not correct, possible error in indexOf");
- //if (friends.contains ("Jim"))
- //System.err.println ("Not correct, possible error in contains");
- //if (!friends.contains ("jim"))
- //System.err.println ("Not correct, possible error in contains 2");
- friends.add ("mary");
- //if (friends.indexOf(new String("mary")) != 0)
- //System.err.println ("Not correct");
- // if (!friends.remove (new String("jim"))) // remove first occurrence of "jim"
- // System.err.println ("Not correct, remove(Object) returning wrong value");
- friends.remove(2);
- //if (friends.contains ("jim"))
- //System.err.println ("Not correct, possible error in remove(Object)");
- System.out.println ("The list is: " + friends);
- }
- private static void problem3 (List <String> friends, List <String> enemies)
- {
- if (friends.equals ("friends"))
- System.err.println ("Not correct");
- if (friends.equals (new ArrayList <String> ()))
- System.err.println ("Not correct");
- if (enemies.equals (new ArrayList <String> ()))
- System.err.println ("Not correct");
- if (! friends.equals (enemies))
- System.err.println ("Not correct");
- if (! enemies.equals (friends))
- System.err.println ("Not correct");
- friends.add (1, new String ("jimmy"));
- if (enemies.equals (friends))
- System.err.println ("Not correct");
- if (friends.equals (enemies))
- System.err.println ("Not correct");
- enemies.add (1, "Jimmy");
- if (friends.equals (enemies))
- System.err.println ("Not correct");
- if (enemies.equals (friends))
- System.err.println ("Not correct");
- enemies.remove (1);
- enemies.add (1, new String ("jimmy"));
- if (! enemies.equals (friends))
- System.err.println ("Not correct");
- if (! friends.equals (enemies))
- System.err.println ("Not correct");
- System.out.println ("\nProblem 3 complete");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement