Advertisement
Guest User

Untitled

a guest
May 27th, 2015
372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.39 KB | None | 0 0
  1. import tester.*;
  2.  
  3. // Assignment 3
  4. // Pedbereznak, Tom
  5. // TomPed
  6. // Roth, Brian
  7. // rothb
  8.  
  9. interface ILoString {
  10. ILoString sort();
  11. ILoString insert(String s);
  12. boolean isSorted();
  13. boolean isSortedHelper(String s);
  14. ILoString interleave(ILoString that);
  15. ILoString reverse();
  16. ILoString reverseHelp(ILoString acc);
  17. boolean isDoubledList();
  18. boolean isDoubledListHelp(String s);
  19. boolean isPalindromeList();
  20. }
  21.  
  22. class MtLoString implements ILoString {
  23. MtLoString() {
  24. // the constructor is empty because MtLoStrings has no fields
  25. }
  26.  
  27. public ILoString sort() {
  28. return this;
  29. }
  30.  
  31. public ILoString insert(String s) {
  32. return new ConsLoString(s, this);
  33. }
  34.  
  35. public boolean isSorted() {
  36. return true;
  37. }
  38.  
  39. public boolean isSortedHelper(String s) {
  40. return true;
  41. }
  42.  
  43. public ILoString interleave(ILoString that) {
  44. return that;
  45. }
  46.  
  47. public ILoString reverse() {
  48. return new MtLoString();
  49. }
  50.  
  51. public ILoString reverseHelp(ILoString acc) {
  52. return acc;
  53. }
  54.  
  55. public boolean isDoubledList() {
  56. return true;
  57. }
  58.  
  59. public boolean isDoubledListHelp(String s) {
  60. return false;
  61. }
  62.  
  63. public boolean isPalindromeList() {
  64. return true;
  65. }
  66. }
  67.  
  68. class ConsLoString implements ILoString {
  69. String first;
  70. ILoString rest;
  71.  
  72. // the constructor
  73. ConsLoString(String first, ILoString rest) {
  74. this.first = first;
  75. this.rest = rest;
  76. }
  77.  
  78. public ILoString sort() {
  79. return this.rest.sort().insert(this.first);
  80. }
  81.  
  82. public ILoString insert(String s) {
  83. if (this.first.toLowerCase().compareTo(s.toLowerCase()) <= 0) {
  84. return new ConsLoString(this.first, this.rest.insert(s));
  85. }
  86. else {
  87. return new ConsLoString(s, this);
  88. }
  89. }
  90.  
  91. public boolean isSorted() {
  92. return this.rest.isSortedHelper(this.first);
  93. }
  94.  
  95. public boolean isSortedHelper(String s) {
  96. if (this.first.toLowerCase().compareTo(s.toLowerCase()) >= 0) {
  97. return this.rest.isSorted();
  98. }
  99. else {
  100. return false;
  101. }
  102. }
  103.  
  104. public ILoString interleave(ILoString that) {
  105. return new ConsLoString(this.first, that.interleave(this.rest));
  106. }
  107.  
  108. public ILoString reverse() {
  109. return this.rest.reverseHelp(new ConsLoString(this.first, new MtLoString()));
  110. }
  111.  
  112. public ILoString reverseHelp(ILoString acc) {
  113. return this.rest.reverseHelp(new ConsLoString(this.first, acc));
  114. }
  115.  
  116. public boolean isDoubledList() {
  117. return this.rest.isDoubledListHelp(this.first);
  118. }
  119.  
  120. public boolean isDoubledListHelp(String s) {
  121. if (this.first.toLowerCase().equals(s.toLowerCase())) {
  122. return this.rest.isDoubledList();
  123. }
  124. else {
  125. return false;
  126. }
  127. }
  128.  
  129. public boolean isPalindromeList() {
  130. return this.interleave(this.reverse()).isDoubledList();
  131. }
  132. }
  133.  
  134. class ExamplesStrings {
  135. ILoString listOfString1 = new ConsLoString("zzz",
  136. new ConsLoString("aaa",
  137. new ConsLoString("ccc",
  138. new ConsLoString("bbb",
  139. new MtLoString()))));
  140. ILoString listOfString2 = new ConsLoString("ZZZ",
  141. new ConsLoString("aaa",
  142. new ConsLoString("CCC",
  143. new ConsLoString("bBb",
  144. new MtLoString()))));
  145. ILoString listOfString3 = new ConsLoString("a",
  146. new ConsLoString("b",
  147. new ConsLoString("c",
  148. new ConsLoString("d",
  149. new MtLoString()))));
  150. ILoString listOfString4 = new ConsLoString("a",
  151. new ConsLoString("a",
  152. new ConsLoString("c",
  153. new ConsLoString("d",
  154. new MtLoString()))));
  155. ILoString listOfString5 = new ConsLoString("a",
  156. new ConsLoString("b",
  157. new MtLoString()));
  158. ILoString listOfString6 = new ConsLoString("zzz",
  159. new ConsLoString("aaa",
  160. new ConsLoString("aaa",
  161. new ConsLoString("zzz",
  162. new MtLoString()))));
  163. ILoString listOfString7 = new ConsLoString("zzz",
  164. new ConsLoString("aaa",
  165. new ConsLoString("zzz",
  166. new MtLoString())));
  167. ILoString listOfString8 = new ConsLoString("zzz",
  168. new ConsLoString("zzz",
  169. new ConsLoString("aaa",
  170. new ConsLoString("aaa",
  171. new MtLoString()))));
  172. ILoString listOfString9 = new ConsLoString("aaa",
  173. new ConsLoString("zzz",
  174. new ConsLoString("zzz",
  175. new MtLoString())));
  176.  
  177.  
  178. boolean testSort(Tester t) {
  179. return t.checkExpect(this.listOfString1.sort(), new ConsLoString("aaa",
  180. new ConsLoString("bbb",
  181. new ConsLoString("ccc",
  182. new ConsLoString("zzz",
  183. new MtLoString()))))) &&
  184. t.checkExpect(this.listOfString2.sort(), new ConsLoString("aaa",
  185. new ConsLoString("bBb",
  186. new ConsLoString("CCC",
  187. new ConsLoString("ZZZ",
  188. new MtLoString())))));
  189. }
  190.  
  191. boolean testIsSorted(Tester t) {
  192. return t.checkExpect(this.listOfString1.isSorted(), false) &&
  193. t.checkExpect(this.listOfString3.isSorted(), true) &&
  194. t.checkExpect(this.listOfString4.isSorted(), true);
  195. }
  196.  
  197. boolean testInterleave(Tester t) {
  198. return t.checkExpect(this.listOfString1.interleave(this.listOfString2),
  199. new ConsLoString("zzz",
  200. new ConsLoString("ZZZ",
  201. new ConsLoString("aaa",
  202. new ConsLoString("aaa",
  203. new ConsLoString("ccc",
  204. new ConsLoString("CCC",
  205. new ConsLoString("bbb",
  206. new ConsLoString("bBb",
  207. new MtLoString()))))))))) &&
  208. t.checkExpect(this.listOfString5.interleave(this.listOfString1),
  209. new ConsLoString("a",
  210. new ConsLoString("zzz",
  211. new ConsLoString("b",
  212. new ConsLoString("aaa",
  213. new ConsLoString("ccc",
  214. new ConsLoString("bbb",
  215. new MtLoString()))))))) &&
  216. t.checkExpect(this.listOfString1.interleave(this.listOfString5),
  217. new ConsLoString("zzz",
  218. new ConsLoString("a",
  219. new ConsLoString("aaa",
  220. new ConsLoString("b",
  221. new ConsLoString("ccc",
  222. new ConsLoString("bbb",
  223. new MtLoString())))))));
  224. }
  225.  
  226. boolean testReverse(Tester t) {
  227. return t.checkExpect(this.listOfString1.reverse(),
  228. new ConsLoString("bbb",
  229. new ConsLoString("ccc",
  230. new ConsLoString("aaa",
  231. new ConsLoString("zzz",
  232. new MtLoString())))));
  233. }
  234.  
  235. boolean testIsDouble(Tester t) {
  236. return t.checkExpect(this.listOfString1.isDoubledList(), false) &&
  237. t.checkExpect(this.listOfString8.isDoubledList(), true) &&
  238. t.checkExpect(new ConsLoString("a", new ConsLoString("a", new ConsLoString("a", new MtLoString()))).isDoubledList(), false);
  239. }
  240.  
  241. boolean testIsPalindromeList(Tester t) {
  242. return t.checkExpect(this.listOfString1.isPalindromeList(), false) &&
  243. t.checkExpect(this.listOfString6.isPalindromeList(), true) &&
  244. t.checkExpect(this.listOfString9.isPalindromeList(), false) &&
  245. t.checkExpect(this.listOfString7.isPalindromeList(), true);
  246. }
  247.  
  248. /*boolean testMerge(Tester t) {
  249. return t.checkExpect(this.listOfString5.merge(this.listOfString9),
  250. new ConsLoString("a",
  251. new ConsLoString("aaa",
  252. new ConsLoString("b",
  253. new ConsLoString("zzz",
  254. new ConsLoString("zzz",
  255. new MtLoString()))))));
  256. }*/
  257. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement