Advertisement
ydornberg

assn1 main

Oct 10th, 2015
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.40 KB | None | 0 0
  1. //package assign1;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.util.Scanner;
  6.  
  7. public class Main {
  8.  
  9.     public static void main(String[] args) {
  10.         System.out.println("Reading in from txt file.");
  11.         //-------------------------------------------input from file *****
  12.         MyList charLinkedList = new MyList(null);
  13.         try {
  14.             String filename = "list.txt";
  15.            
  16.             File file = new File(filename);
  17.             Scanner in = new Scanner(file);
  18.            
  19.             while(in.hasNextLine()){
  20.                 String line = in.nextLine();
  21.                 char a = line.charAt(0);
  22.                 charLinkedList.insertAtPos(0, a);
  23.             }
  24.             in.close();
  25.         } catch (FileNotFoundException e) {
  26.             e.printStackTrace();
  27.             System.out.println("File not found!");
  28.         }
  29.         System.out.println("Testing list generation and print function:");
  30.         charLinkedList.print();
  31.         System.out.println("\nTesting remove by index: 5 (f), and 0.");
  32.         charLinkedList.remove(0); //-----------------------------------------------------remove head node
  33.         charLinkedList.remove(5);//----------------------------------------------remove node from center of list
  34.         charLinkedList.print();
  35.         System.out.println("\nTesting remove by char: 'a'.");
  36.         charLinkedList.remove('a');//-------------------------------------------------------test remove(char)
  37.         charLinkedList.print();
  38.         System.out.println("\nTesting removeAll char, a.");
  39.         charLinkedList.insertAtPos(3, 'a');
  40.         charLinkedList.insertAtPos(5, 'a');
  41.         charLinkedList.insertAtPos(8, 'a');
  42.         charLinkedList.insertAtPos(10, 'a');
  43.         charLinkedList.print();
  44.         charLinkedList.removeAll('a');//----------------------------------------------------test removeAll
  45.         System.out.println("Position of 'a' = "+charLinkedList.find('a'));
  46.         charLinkedList.print();
  47.         System.out.println("\nTesting contains: search for 'a' then 'l'.");
  48.         System.out.println(charLinkedList.contains('a'));//----------------------------------test contains fail
  49.         System.out.println(charLinkedList.contains('l'));//----------------------------------test cointains success
  50.         System.out.println("\nTesting set: indexes 0-2 to 'z', plus index 20 to 'z'.");
  51.         charLinkedList.set(0,'z');//------------------------------------------------------test set success
  52.         charLinkedList.set(1,'z');
  53.         charLinkedList.set(2,'z');
  54.         charLinkedList.print();
  55.         charLinkedList.set(20,'z');//------------------------------------------------------test set fail
  56.         System.out.println("\nTesting equals: list to nonequal list, then list to equal list.");
  57.         System.out.println(charLinkedList.equals(charLinkedList.sublist(4, 8)));//--------------------test equals fail
  58.         System.out.println(charLinkedList.equals(charLinkedList.sublist(0, charLinkedList.size())));//test equals success
  59.         System.out.println("\nTesting pushFront and pushBack (utilize insertAfter and insertBefore methods).");
  60.         System.out.println("Size before ="+charLinkedList.size());
  61.         charLinkedList.pushFront('y');//---------------------------------------------------test pushfront
  62.         charLinkedList.pushBack('y');//-----------------------------------------------------test pushback
  63.         System.out.println("Size after ="+charLinkedList.size());
  64.         charLinkedList.print();
  65.         System.out.println("\nTesting popFront and popBack.");
  66.         System.out.println("Size before ="+charLinkedList.size());
  67.         charLinkedList.popFront();//----------------------------------------------------------test popfront
  68.         charLinkedList.popBack();//-----------------------------------------------------------test pupback
  69.         System.out.println("Size after ="+charLinkedList.size());
  70.         charLinkedList.print();
  71.         System.out.println("\nTesting swap: first and last value, then fail.");
  72.         charLinkedList.swap(0, 11);//---------------------------------------------------------test swap success
  73.         charLinkedList.print();
  74.         charLinkedList.swap(0, 12);//-----------------------------------------------------------test swap fail
  75.         System.out.println("\nTesting insertAtPos: front, rear, and out of index.");
  76.         charLinkedList.insertAtPos(0, 'a'); //-----------------------------------------------test insertAtPos front
  77.         charLinkedList.insertAtPos(charLinkedList.size()-1, 'b');//--------------------------test insertAtPos back
  78.         charLinkedList.print();
  79.         charLinkedList.insertAtPos(charLinkedList.size(), 'c'); //---------------------------test insertAtPos fail
  80.         System.out.println("\nTesting sublist: sublist(3,6), and out of index.");
  81.         MyList sublist = charLinkedList.sublist(3, 6);//----------------------------------------test sublist success
  82.         sublist.print();
  83.         MyList sublistf = charLinkedList.sublist(3, charLinkedList.size());//--------------------test sublist fails
  84.         sublistf.print();
  85.         System.out.println("\nTesting find: 'f' (4), and q (fail).");
  86.         System.out.println("Position of 'f' = "+charLinkedList.find('f'));//------------------test find success
  87.         System.out.println("Position of 'q' = "+charLinkedList.find('q'));//-------------------test find fail
  88.         System.out.println("\nTesting toArray and MyList(char[],charArray)");
  89.         MyList arraytolist = new MyList(charLinkedList.toArray(),charLinkedList.size());//---test arraytolist
  90.         arraytolist.print();
  91.         System.out.println("\nTesting reverse.");
  92.         charLinkedList.reverse();//---------------------------------------------------------------test reverse
  93.         charLinkedList.print();
  94.         System.out.println("\nTesting toString.");
  95.         System.out.println(charLinkedList.toString());//------------------------------------------test toString
  96.     }
  97.  
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement