SHARE
TWEET

Untitled

a guest Oct 19th, 2017 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import tester.Tester;
  2.  
  3. //Assignment 6
  4. //Seo Minho
  5. //ashera2
  6. //Mayeux Jacob
  7. //jakemayeux
  8.  
  9. // a generic list
  10. interface IList<T> {
  11.  
  12.   //determines whether a list is sorted
  13.   boolean isSorted(IComparator<T> comparator);
  14.  
  15.   // compares two items of a list
  16.   boolean isSortedHelp(T given, IComparator<T> comparator);
  17.  
  18.   //produce a new list containing all elements from both lists
  19.   IList<T> merge(IList<T> alist, IComparator<T> comparator);
  20.  
  21.   // return -1 if the first element comes first, 0 if the same
  22.   // and 1 if the second element should come first
  23.   int comesFirst(T given, IComparator<T> comparator);
  24.  
  25.   //produce a new list in alphabetical order
  26.   IList<T> sort(IComparator<T> comparator);
  27.  
  28.   // rearrange two items in alphabetical order
  29.   IList<T> sortHelp(T first, IComparator<T> comparator);
  30.  
  31.   // returns true if the two lists are the same
  32.   boolean sameList(IList<T> alist, IComparator<T> comparator);
  33.  
  34.   // checks if the two non-empty lists have the same data
  35.   boolean sameData(ConsList<T> alist, IComparator<T> comparator);
  36.  
  37. }
  38.  
  39. // generic empty list
  40. class MtList<T> implements IList<T> {
  41.   MtList() {}
  42.  
  43.   @Override
  44.   public boolean isSorted(IComparator<T> comparator) {
  45.     return true;
  46.   }
  47.  
  48.   @Override
  49.   public boolean isSortedHelp(T given, IComparator<T> comparator) {
  50.     return true;
  51.   }
  52.  
  53.   @Override
  54.   public IList<T> merge(IList<T> alist, IComparator<T> comparator) {
  55.     return alist;
  56.   }
  57.  
  58.   @Override
  59.   public int comesFirst(T given, IComparator<T> comparator) {
  60.     return 1;
  61.   }
  62.  
  63.   @Override
  64.   public IList<T> sort(IComparator<T> comparator) {
  65.     return this;
  66.   }
  67.  
  68.   @Override
  69.   public IList<T> sortHelp(T first, IComparator<T> comparator) {
  70.     return new ConsList<T>(first, this);
  71.   }
  72.  
  73.   @Override
  74.   public boolean sameList(IList<T> alist, IComparator<T> comparator) {
  75.     return false;
  76.   }
  77.  
  78.   // compare two empty lists
  79.   boolean sameList(MtList<T> alist, IComparator<T> comparator) {
  80.     return true;
  81.   }
  82.  
  83.   @Override
  84.   public boolean sameData(ConsList<T> alist, IComparator<T> comparator) {
  85.     return false;
  86.   }
  87.  
  88. }
  89.  
  90. // generic cons list
  91. class ConsList<T> implements IList<T> {
  92.   T first;
  93.   IList<T> rest;
  94.  
  95.   ConsList(T first, IList<T> rest) {
  96.     this.first = first;
  97.     this.rest = rest;
  98.   }
  99.  
  100.   @Override
  101.   public boolean isSorted(IComparator<T> comparator) {
  102.     return this.rest.isSortedHelp(this.first, comparator);
  103.   }
  104.  
  105.   @Override
  106.   public boolean isSortedHelp(T given, IComparator<T> comparator) {
  107.     if (comparator.compare(given, this.first) <= 0) {
  108.       return this.isSorted(comparator);
  109.     }
  110.     else {
  111.       return false;
  112.     }
  113.   }
  114.  
  115.   @Override
  116.   public IList<T> merge(IList<T> alist, IComparator<T> comparator) {
  117.     if (alist.comesFirst(this.first, comparator) <= 0) {
  118.       return alist.merge(this, comparator);
  119.     }
  120.     else {
  121.       return new ConsList<T>(this.first, alist.merge(this.rest, comparator));      
  122.     }
  123.   }
  124.  
  125.   @Override
  126.   public int comesFirst(T given, IComparator<T> comparator) {
  127.     return comparator.compare(this.first, given);
  128.   }
  129.  
  130.   @Override
  131.   public IList<T> sort(IComparator<T> comparator) {
  132.     return this.rest.sort(comparator).sortHelp(this.first, comparator);
  133.   }
  134.  
  135.   @Override
  136.   public IList<T> sortHelp(T given, IComparator<T> comparator) {
  137.     if(comparator.compare(this.first, given) <= 0) {
  138.       return new ConsList<T>(this.first, this.rest.sortHelp(given, comparator));
  139.     }
  140.     else {
  141.       return new ConsList<T>(given, this);
  142.     }
  143.   }
  144.  
  145.   @Override
  146.   public boolean sameList(IList<T> alist, IComparator<T> comparator) {
  147.     return alist.sameData(this, comparator);
  148.   }
  149.  
  150.   @Override
  151.   public boolean sameData(ConsList<T> alist, IComparator<T> comparator) {
  152.     return comparator.compare(this.first, alist.first) == 0
  153.         && this.rest.sameList(alist.rest, comparator);
  154.   }
  155.  
  156. }
  157.  
  158. // gerneric comparator
  159. interface IComparator<T> {
  160.   // Returns a negative number if t1 comes before t2 in this ordering
  161.   // Returns zero              if t1 is the same as t2 in this ordering
  162.   // Returns a positive number if t1 comes after t2 in this ordering
  163.   int compare(T t1, T t2);
  164. }
  165.  
  166. // compare the length of strings
  167. class StringLengthCompGen implements IComparator<String> {
  168.  
  169.   @Override
  170.   public int compare(String s1, String s2) {
  171.     if (s1.length() < s2.length()) {
  172.       return -1;
  173.     }
  174.     else if (s1.length() == s2.length()) {
  175.       return 0;
  176.     }
  177.     else return 1;
  178.   }
  179. }
  180.  
  181. // compare lexicography of strings
  182. class StringLexCompGen implements IComparator<String> {
  183.  
  184.   @Override
  185.   public int compare(String s1, String s2) {
  186.     if (s1.compareTo(s2) < 0) {
  187.       return -1;
  188.     }
  189.     else if (s1.compareTo(s2) == 0) {
  190.       return 0;
  191.     }
  192.     else {
  193.       return 1;
  194.     }
  195.   }
  196. }
  197.  
  198. //examples class of strings
  199. class ExamplesStringsGen {
  200.  
  201. IList<String> empty = new MtList<String>();
  202. IList<Integer> emptyInt = new MtList<Integer>();
  203. IList<Boolean> emptyBoo = new MtList<Boolean>();
  204.  
  205. IList<String> easy = new ConsList<String>("bb",
  206.    new ConsList<String>("ccc", empty));
  207.  
  208. IList<Integer> easy2 = new ConsList<Integer>(1,
  209.    new ConsList<Integer>(4,
  210.        new ConsList<Integer>(3,
  211.            new ConsList<Integer>(2, emptyInt))));
  212.  
  213. IList<Boolean> easy3 = new ConsList<Boolean>(true,
  214.    new ConsList<Boolean>(true,
  215.        new ConsList<Boolean>(false, emptyBoo)));
  216.  
  217. IList<String> easy4 = new ConsList<String>("c", empty);
  218.  
  219. IList<String> easy5 = new ConsList<String>("a",
  220.    new ConsList<String>("b",
  221.        new ConsList<String>("a", empty)));
  222.  
  223. IList<String> easy6 = new ConsList<String>("a",
  224.     new ConsList<String>("aba",
  225.         new ConsList<String>("ab", empty)));
  226.  
  227. IComparator<String> lex = new StringLexCompGen();
  228.  
  229. IComparator<String> length = new StringLengthCompGen();
  230.  
  231.   // to test compare method
  232.   boolean testCompare(Tester t){
  233.     return
  234.         t.checkExpect(lex.compare("a", "b"), -1)
  235.         && t.checkExpect(lex.compare("b", "a"), 1)
  236.         && t.checkExpect(lex.compare("a", "a"), 0)
  237.         && t.checkExpect(length.compare("aa", "b"), 1)
  238.         && t.checkExpect(length.compare("a", "bb"), -1);
  239.   }
  240.  
  241.   //to test is sorted method
  242.   boolean testIsSorted(Tester t){
  243.     return
  244.         t.checkExpect(easy.isSorted(lex), true)
  245.         && t.checkExpect(easy.isSorted(length), true)
  246.         && t.checkExpect(easy5.isSorted(lex), false);
  247.   }
  248.  
  249.   //to test is sorted help method
  250.   boolean testIsSortedHelp(Tester t){
  251.     return
  252.         t.checkExpect(easy.isSortedHelp("b", lex), true)
  253.         && t.checkExpect(easy.isSortedHelp("a", lex), true)
  254.         && t.checkExpect(easy.isSortedHelp("a", length), true)
  255.         && t.checkExpect(easy.isSortedHelp("aaa", length), false);
  256.   }
  257.  
  258.   //to test merge method
  259.   boolean testMerge(Tester t){
  260.     return
  261.         t.checkExpect(easy.merge(easy4, lex), new ConsList<String> ("bb",
  262.             new ConsList<String>("c",
  263.                 new ConsList<String>("ccc", empty))))
  264.         && t.checkExpect(easy.merge(easy4, length),  new ConsList<String> ("c",
  265.             new ConsList<String>("bb",
  266.                 new ConsList<String>("ccc", empty))));
  267.   }
  268.  
  269.   // to test comes first method
  270.   boolean testComesFirst(Tester t) {
  271.     return t.checkExpect(easy.comesFirst("a", lex), 1)
  272.         && t.checkExpect(easy.comesFirst("dddd", length), -1)
  273.         && t.checkExpect(easy.comesFirst("bb", lex), 0);
  274.   }
  275.  
  276.  
  277.   //to test sort method
  278.   boolean testSort(Tester t){
  279.     return
  280.         t.checkExpect(easy.sort(lex), easy)
  281.         && t.checkExpect(easy.sort(length), easy)
  282.         && t.checkExpect(easy6.sort(length), new ConsList<String> ("a",
  283.             new ConsList<String> ("ab",
  284.                 new ConsList<String> ("aba", empty))));
  285.   }
  286.  
  287.   //to test sort help method
  288.   boolean testSortHelp(Tester t){
  289.     return
  290.         t.checkExpect(easy.sortHelp("a", lex), new ConsList<String> ("a",
  291.             new ConsList<String>("bb",
  292.                 new ConsList<String>("ccc", empty))))
  293.         && t.checkExpect(easy.sortHelp("aaa", length), new ConsList<String> ("bb",
  294.             new ConsList<String> ("ccc",
  295.                 new ConsList<String> ("aaa", empty))));
  296.   }
  297.  
  298. //  //to test same list method
  299. //  boolean testSameList(Tester t){
  300. //    return
  301. //        t.checkExpect(easy.sameList(easy, lex), true)
  302. //        && t.checkExpect(easy.sameList(empty, length), false)
  303. //        && t.checkExpect(empty.sameList(easy4, lex), false)
  304. //        && t.checkExpect(easy.sameList(easy4, length), false)
  305. //        && t.checkExpect(easy5.sameList(easy5, lex), true);
  306. //  }
  307. //
  308. //  //to test same data method
  309. //  boolean testSameData(Tester t){
  310. //    return
  311. //        t.checkExpect(easy.sameData(new ConsList<String>("ccc",
  312. //            new ConsList<String> ("bb", empty)), lex), false)
  313. //        && t.checkExpect(easy.sameData(new ConsList<String>("bb",
  314. //            new ConsList<String> ("ccc", empty)), lex), true)
  315. //        && t.checkExpect(easy4.sameData(new ConsList<String> ("a", empty), length), false);
  316. //  }
  317. }
RAW Paste Data
Top