Advertisement
Guest User

Untitled

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