Advertisement
Guest User

Untitled

a guest
May 28th, 2015
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.36 KB | None | 0 0
  1. import tester.*;
  2. import javalib.colors.*;
  3. import javalib.worldimages.*;
  4.  
  5. // Assignment 3
  6. // Pedbereznak, Tom
  7. // TomPed
  8. // Roth, Brian
  9. // rothb
  10.  
  11. // to represent the types of a Mobile
  12. interface IMobile {
  13. // to compute the total weight of this Mobile
  14. int totalWeight();
  15.  
  16. // to compute the total height of this Mobile
  17. int totalHeight();
  18.  
  19. // to determine if this Mobile is balanced, meaning that there is no net torque
  20. // (weight of node * length of strut)
  21. boolean isBalanced();
  22.  
  23. // to combine this balanced Mobile with a given balanced Mobile
  24. IMobile buildMobile(int length, int strut, IMobile that);
  25.  
  26. // to compute the current width of this Mobile
  27. int curWidth();
  28.  
  29. // to produce an image of this Mobile if it were hanging from a nail at a given Posn
  30. WorldImage drawMobile(Posn p);
  31. }
  32.  
  33. // to represent a simple Mobile
  34. class Simple implements IMobile {
  35. int length;
  36. int weight;
  37. IColor color;
  38.  
  39. // the constructor
  40. Simple(int length, int weight, IColor color) {
  41. this.length = length;
  42. this.weight = weight;
  43. this.color = color;
  44. }
  45.  
  46. /* TEMPLATE:
  47. * Fields:
  48. * ... this.length ... -- int
  49. * ... this.weight ... -- int
  50. * ... this.color ... -- IColor
  51. *
  52. * Methods:
  53. * ... this.totalWeight() ... -- int
  54. * ... this.totalHeight() ... -- int
  55. * ... this.isBalanced() ... -- boolean
  56. * ... this.buildMobile(int, int, IMobile) ... -- IMobile
  57. * ... this.curWidth() ... -- int
  58. * ... this.drawMobile(Posn) ... -- WorldImage
  59. */
  60.  
  61. // computes the total weight of this simple Mobile
  62. public int totalWeight() {
  63. return this.weight;
  64. }
  65.  
  66. // computes the total height of this simple Mobile
  67. public int totalHeight() {
  68. return this.length + (this.weight / 10);
  69. }
  70.  
  71. // determines if this simple Mobile is balanced, meaning that there is no net torque
  72. // (weight of node * length of strut) (always true)
  73. public boolean isBalanced() {
  74. return true;
  75. }
  76.  
  77. // combines this balanced simple Mobile with the given balanced simple Mobile
  78. public IMobile buildMobile(int length, int strut, IMobile that) {
  79. int len = (that.totalWeight() * strut) / (this.totalWeight() + that.totalWeight());
  80. return new Complex(length, len, (strut - len), this, this);
  81. }
  82.  
  83. // computes the current width of this simple Mobile
  84. public int curWidth() {
  85. if ((this.weight % 10) != 0) {
  86. return (1 + (this.weight / 10));
  87. }
  88. else {
  89. return (this.weight / 10);
  90. }
  91. }
  92.  
  93. // produces an image of this simple Mobile if it were hanging from a nail at the given Posn
  94. public WorldImage drawMobile(Posn p) {
  95. return new LineImage(p, new Posn(p.x, p.y + this.length), new Black()).overlayImages(
  96. new RectangleImage(new Posn(p.x - (this.weight / 2), p.y + this.length),
  97. this.weight, this.weight, this.color));
  98. }
  99. }
  100.  
  101. // to represent a complex Mobile
  102. class Complex implements IMobile {
  103. int length;
  104. int leftside;
  105. int rightside;
  106. IMobile left;
  107. IMobile right;
  108.  
  109. // the constructor
  110. Complex(int length, int leftside, int rightside, IMobile left, IMobile right) {
  111. this.length = length;
  112. this.leftside = leftside;
  113. this.rightside = rightside;
  114. this.left = left;
  115. this.right = right;
  116. }
  117.  
  118. /* TEMPLATE:
  119. * Fields:
  120. * ... this.length ... -- int
  121. * ... this.leftside ... -- int
  122. * ... this.rightside ... -- int
  123. * ... this.left ... -- IMobile
  124. * ... this.right ... -- IMobile
  125. *
  126. * Methods:
  127. * ... this.totalWeight() ... -- int
  128. * ... this.totalHeight() ... -- int
  129. * ... this.isBalanced() ... -- boolean
  130. * ... this.buildMobile(int, int, IMobile) ... -- IMobile
  131. * ... this.curWidth() ... -- int
  132. * ... this.drawMobile(Posn) ... -- WorldImage
  133. *
  134. * Methods of fields:
  135. * ... this.left.totalWeight() ... -- int
  136. * ... this.right.totalWeight() ... -- int
  137. * ... this.left.totalHeight() ... -- int
  138. * ... this.right.totalHeight() ... -- int
  139. * ... this.left.isBalanced() ... -- boolean
  140. * ... this.right.isBalanced() ... -- boolean
  141. * ... this.left.buildMobile(int, int, IMobile) ... -- IMobile
  142. * ... this.right.buildMobile(int, int, IMobile) ... -- IMobile
  143. * ... this.left.curWidth() ... -- int
  144. * ... this.right.curWidth() ... -- int
  145. * ... this.left.drawMobile(Posn) ... -- WorldImage
  146. * ... this.right.drawMobile(Posn) ... -- WorldImage
  147. */
  148.  
  149. // computes the total weight of this complex Mobile
  150. public int totalWeight() {
  151. return this.left.totalWeight() + this.right.totalWeight();
  152. }
  153.  
  154. // computes the total height of this complex Mobile
  155. public int totalHeight() {
  156. return this.length + Math.max(this.left.totalHeight(), this.right.totalHeight());
  157. }
  158.  
  159. // determines if this complex Mobile is balanced, meaning that there is no net torque
  160. // (weight of node * length of strut)
  161. public boolean isBalanced() {
  162. return (this.left.totalWeight() * this.leftside) ==
  163. (this.right.totalWeight() * this.rightside) &&
  164. left.isBalanced() && right.isBalanced();
  165. }
  166.  
  167. // combines this balanced complex Mobile with the given balanced complex Mobile
  168. public IMobile buildMobile(int length, int strut, IMobile that) {
  169. int len = (that.totalWeight() * strut) / (this.totalWeight() + that.totalWeight());
  170. return new Complex(length, len, (strut - len), this, that);
  171. }
  172.  
  173. // computes the current width of this complex Mobile
  174. public int curWidth() {
  175. return 0;
  176. }
  177.  
  178. // produces an image of this complex Mobile if it were hanging from a nail at the given Posn
  179. public WorldImage drawMobile(Posn p) {
  180. return new LineImage(p, new Posn(p.x, p.y + this.length), new Black()).overlayImages(
  181. new LineImage(new Posn(p.x - this.leftside, p.y + this.length),
  182. new Posn(p.x + this.rightside, p.y + this.length), new Black()),
  183. this.left.drawMobile(new Posn(p.x - this.leftside,
  184. p.y + this.length)),
  185. this.right.drawMobile(new Posn(p.x + this.rightside,
  186. p.y + this.length)));
  187. }
  188. }
  189.  
  190. // examples
  191. class ExamplesMobiles {
  192. IMobile exampleSimple = new Simple(2, 20, new Blue());
  193. static IMobile exampleComplex =
  194. new Complex(1, 9, 3, new Simple(1, 36, new Blue()),
  195. new Complex(2, 8, 1, new Simple(1, 12, new Red()),
  196. new Complex(2, 5, 3, new Simple(2, 36, new Red()),
  197. new Simple(1, 60, new Green()))));
  198. IMobile example3 = new Complex(2, 2, 2, this.exampleComplex, this.exampleComplex);
  199.  
  200. // to test the totalWeight() method in the classes that represent Mobiles
  201. boolean testTotalWeight(Tester t) {
  202. return t.checkExpect(this.exampleSimple.totalWeight(), 20) &&
  203. t.checkExpect(this.exampleComplex.totalWeight(), 144) &&
  204. t.checkExpect(this.example3.totalWeight(), 288);
  205. }
  206.  
  207. // to test the totalHeight() method in the classes that represent Mobiles
  208. boolean testTotalHeight(Tester t) {
  209. return t.checkExpect(this.exampleSimple.totalHeight(), 4) &&
  210. t.checkExpect(this.exampleComplex.totalHeight(), 12) &&
  211. t.checkExpect(this.example3.totalHeight(), 14);
  212. }
  213.  
  214. // to test the isBalanced() method in the classes that represent Mobiles
  215. boolean testIsBalanced(Tester t) {
  216. return t.checkExpect(this.exampleSimple.isBalanced(), true) &&
  217. t.checkExpect(this.exampleComplex.isBalanced(), true) &&
  218. t.checkExpect(this.exampleSimple.buildMobile(10, 10,
  219. this.exampleSimple).isBalanced(), true) &&
  220. t.checkExpect(this.example3.isBalanced(), true) &&
  221. t.checkExpect(new Complex(4, 17, 33, this.exampleComplex,
  222. this.example3).isBalanced(), false) &&
  223. t.checkExpect(new Complex(4, 100, 50, this.exampleComplex,
  224. this.example3).isBalanced(), true);
  225. }
  226.  
  227. // to test the buildMobile(int, int, IMobile) in the classes
  228. // that represent Mobiles
  229. boolean testBuildMobile(Tester t) {
  230. return t.checkExpect(this.exampleComplex.buildMobile(4, 150, this.example3),
  231. new Complex(4, 100, 50, this.exampleComplex, this.example3)) &&
  232. t.checkExpect(this.exampleSimple.buildMobile(10, 10, this.exampleSimple),
  233. new Complex(10, 5, 5, this.exampleSimple, this.exampleSimple));
  234. }
  235.  
  236. /* // to test the curWidth() method in the classes that represent Mobiles
  237. boolean testCurWidth(Tester t) {
  238. return t.checkExpect(this.exampleSimple.curWidth(), 2) &&
  239. t.checkExpect(this.exampleComplex.curWidth(), 26) &&
  240. t.checkExpect(new Complex(10, 5, 5, this.exampleSimple,
  241. this.exampleSimple).curWidth(), 14);
  242. } */
  243.  
  244. // to test the drawMobile(Posn) method in the classes that represent Mobiles
  245. /*boolean testDrawMobile(Tester t) {
  246. return t.checkExpect(this.exampleSimple.drawMobile(new Posn(0, 0)),
  247. (new LineImage(new Posn(0, 0), new Posn(0, 2),
  248. new Black())).overlayImages(new RectangleImage(
  249. new Posn(-10, 2),
  250. 20, 20, new Blue()))) &&
  251. t.checkExpect(this.exampleComplex.drawMobile(new Posn(0,0)),
  252. new LineImage(0, new Posn(0, 1), new Black()).overlayImages(
  253. new LineImage(new Posn(-9, 1), new Posn(3, 1), new Black()),
  254.  
  255.  
  256. this.left.drawMobile(new Posn(p.x - this.leftside,
  257. p.y + this.length)),
  258.  
  259. new LineImage(0, new Posn(0, 1), new Black()).overlayImages(
  260. new LineImage(new Posn(-9, 1), new Posn(3, 1), new Black()),
  261.  
  262.  
  263.  
  264.  
  265.  
  266. this.right.drawMobile(new Posn(p.x + this.rightside,
  267. p.y + this.length)));
  268.  
  269.  
  270. } */
  271. public static void main(String args[]) {
  272. System.out.println(exampleComplex.drawMobile(new Posn(0,0)));
  273. }
  274. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement