Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tester.*;
- import javalib.colors.*;
- import javalib.worldimages.*;
- // Assignment 3
- // Pedbereznak, Tom
- // TomPed
- // Roth, Brian
- // rothb
- // to represent the types of a Mobile
- interface IMobile {
- // to compute the total weight of this Mobile
- int totalWeight();
- // to compute the total height of this Mobile
- int totalHeight();
- // to determine if this Mobile is balanced, meaning that there is no net torque
- // (weight of node * length of strut)
- boolean isBalanced();
- // to combine this balanced Mobile with a given balanced Mobile
- IMobile buildMobile(int length, int strut, IMobile that);
- // to compute the current width of this Mobile
- int curWidth();
- // to produce an image of this Mobile if it were hanging from a nail at a given Posn
- WorldImage drawMobile(Posn p);
- }
- // to represent a simple Mobile
- class Simple implements IMobile {
- int length;
- int weight;
- IColor color;
- // the constructor
- Simple(int length, int weight, IColor color) {
- this.length = length;
- this.weight = weight;
- this.color = color;
- }
- /* TEMPLATE:
- * Fields:
- * ... this.length ... -- int
- * ... this.weight ... -- int
- * ... this.color ... -- IColor
- *
- * Methods:
- * ... this.totalWeight() ... -- int
- * ... this.totalHeight() ... -- int
- * ... this.isBalanced() ... -- boolean
- * ... this.buildMobile(int, int, IMobile) ... -- IMobile
- * ... this.curWidth() ... -- int
- * ... this.drawMobile(Posn) ... -- WorldImage
- */
- // computes the total weight of this simple Mobile
- public int totalWeight() {
- return this.weight;
- }
- // computes the total height of this simple Mobile
- public int totalHeight() {
- return this.length + (this.weight / 10);
- }
- // determines if this simple Mobile is balanced, meaning that there is no net torque
- // (weight of node * length of strut) (always true)
- public boolean isBalanced() {
- return true;
- }
- // combines this balanced simple Mobile with the given balanced simple Mobile
- public IMobile buildMobile(int length, int strut, IMobile that) {
- int len = (that.totalWeight() * strut) / (this.totalWeight() + that.totalWeight());
- return new Complex(length, len, (strut - len), this, this);
- }
- // computes the current width of this simple Mobile
- public int curWidth() {
- if ((this.weight % 10) != 0) {
- return (1 + (this.weight / 10));
- }
- else {
- return (this.weight / 10);
- }
- }
- // produces an image of this simple Mobile if it were hanging from a nail at the given Posn
- public WorldImage drawMobile(Posn p) {
- return new LineImage(p, new Posn(p.x, p.y + this.length), new Black()).overlayImages(
- new RectangleImage(new Posn(p.x - (this.weight / 2), p.y + this.length),
- this.weight, this.weight, this.color));
- }
- }
- // to represent a complex Mobile
- class Complex implements IMobile {
- int length;
- int leftside;
- int rightside;
- IMobile left;
- IMobile right;
- // the constructor
- Complex(int length, int leftside, int rightside, IMobile left, IMobile right) {
- this.length = length;
- this.leftside = leftside;
- this.rightside = rightside;
- this.left = left;
- this.right = right;
- }
- /* TEMPLATE:
- * Fields:
- * ... this.length ... -- int
- * ... this.leftside ... -- int
- * ... this.rightside ... -- int
- * ... this.left ... -- IMobile
- * ... this.right ... -- IMobile
- *
- * Methods:
- * ... this.totalWeight() ... -- int
- * ... this.totalHeight() ... -- int
- * ... this.isBalanced() ... -- boolean
- * ... this.buildMobile(int, int, IMobile) ... -- IMobile
- * ... this.curWidth() ... -- int
- * ... this.drawMobile(Posn) ... -- WorldImage
- *
- * Methods of fields:
- * ... this.left.totalWeight() ... -- int
- * ... this.right.totalWeight() ... -- int
- * ... this.left.totalHeight() ... -- int
- * ... this.right.totalHeight() ... -- int
- * ... this.left.isBalanced() ... -- boolean
- * ... this.right.isBalanced() ... -- boolean
- * ... this.left.buildMobile(int, int, IMobile) ... -- IMobile
- * ... this.right.buildMobile(int, int, IMobile) ... -- IMobile
- * ... this.left.curWidth() ... -- int
- * ... this.right.curWidth() ... -- int
- * ... this.left.drawMobile(Posn) ... -- WorldImage
- * ... this.right.drawMobile(Posn) ... -- WorldImage
- */
- // computes the total weight of this complex Mobile
- public int totalWeight() {
- return this.left.totalWeight() + this.right.totalWeight();
- }
- // computes the total height of this complex Mobile
- public int totalHeight() {
- return this.length + Math.max(this.left.totalHeight(), this.right.totalHeight());
- }
- // determines if this complex Mobile is balanced, meaning that there is no net torque
- // (weight of node * length of strut)
- public boolean isBalanced() {
- return (this.left.totalWeight() * this.leftside) ==
- (this.right.totalWeight() * this.rightside) &&
- left.isBalanced() && right.isBalanced();
- }
- // combines this balanced complex Mobile with the given balanced complex Mobile
- public IMobile buildMobile(int length, int strut, IMobile that) {
- int len = (that.totalWeight() * strut) / (this.totalWeight() + that.totalWeight());
- return new Complex(length, len, (strut - len), this, that);
- }
- // computes the current width of this complex Mobile
- public int curWidth() {
- return 0;
- }
- // produces an image of this complex Mobile if it were hanging from a nail at the given Posn
- public WorldImage drawMobile(Posn p) {
- return new LineImage(p, new Posn(p.x, p.y + this.length), new Black()).overlayImages(
- new LineImage(new Posn(p.x - this.leftside, p.y + this.length),
- new Posn(p.x + this.rightside, p.y + this.length), new Black()),
- this.left.drawMobile(new Posn(p.x - this.leftside,
- p.y + this.length)),
- this.right.drawMobile(new Posn(p.x + this.rightside,
- p.y + this.length)));
- }
- }
- // examples
- class ExamplesMobiles {
- IMobile exampleSimple = new Simple(2, 20, new Blue());
- static IMobile exampleComplex =
- new Complex(1, 9, 3, new Simple(1, 36, new Blue()),
- new Complex(2, 8, 1, new Simple(1, 12, new Red()),
- new Complex(2, 5, 3, new Simple(2, 36, new Red()),
- new Simple(1, 60, new Green()))));
- IMobile example3 = new Complex(2, 2, 2, this.exampleComplex, this.exampleComplex);
- // to test the totalWeight() method in the classes that represent Mobiles
- boolean testTotalWeight(Tester t) {
- return t.checkExpect(this.exampleSimple.totalWeight(), 20) &&
- t.checkExpect(this.exampleComplex.totalWeight(), 144) &&
- t.checkExpect(this.example3.totalWeight(), 288);
- }
- // to test the totalHeight() method in the classes that represent Mobiles
- boolean testTotalHeight(Tester t) {
- return t.checkExpect(this.exampleSimple.totalHeight(), 4) &&
- t.checkExpect(this.exampleComplex.totalHeight(), 12) &&
- t.checkExpect(this.example3.totalHeight(), 14);
- }
- // to test the isBalanced() method in the classes that represent Mobiles
- boolean testIsBalanced(Tester t) {
- return t.checkExpect(this.exampleSimple.isBalanced(), true) &&
- t.checkExpect(this.exampleComplex.isBalanced(), true) &&
- t.checkExpect(this.exampleSimple.buildMobile(10, 10,
- this.exampleSimple).isBalanced(), true) &&
- t.checkExpect(this.example3.isBalanced(), true) &&
- t.checkExpect(new Complex(4, 17, 33, this.exampleComplex,
- this.example3).isBalanced(), false) &&
- t.checkExpect(new Complex(4, 100, 50, this.exampleComplex,
- this.example3).isBalanced(), true);
- }
- // to test the buildMobile(int, int, IMobile) in the classes
- // that represent Mobiles
- boolean testBuildMobile(Tester t) {
- return t.checkExpect(this.exampleComplex.buildMobile(4, 150, this.example3),
- new Complex(4, 100, 50, this.exampleComplex, this.example3)) &&
- t.checkExpect(this.exampleSimple.buildMobile(10, 10, this.exampleSimple),
- new Complex(10, 5, 5, this.exampleSimple, this.exampleSimple));
- }
- /* // to test the curWidth() method in the classes that represent Mobiles
- boolean testCurWidth(Tester t) {
- return t.checkExpect(this.exampleSimple.curWidth(), 2) &&
- t.checkExpect(this.exampleComplex.curWidth(), 26) &&
- t.checkExpect(new Complex(10, 5, 5, this.exampleSimple,
- this.exampleSimple).curWidth(), 14);
- } */
- // to test the drawMobile(Posn) method in the classes that represent Mobiles
- /*boolean testDrawMobile(Tester t) {
- return t.checkExpect(this.exampleSimple.drawMobile(new Posn(0, 0)),
- (new LineImage(new Posn(0, 0), new Posn(0, 2),
- new Black())).overlayImages(new RectangleImage(
- new Posn(-10, 2),
- 20, 20, new Blue()))) &&
- t.checkExpect(this.exampleComplex.drawMobile(new Posn(0,0)),
- new LineImage(0, new Posn(0, 1), new Black()).overlayImages(
- new LineImage(new Posn(-9, 1), new Posn(3, 1), new Black()),
- this.left.drawMobile(new Posn(p.x - this.leftside,
- p.y + this.length)),
- new LineImage(0, new Posn(0, 1), new Black()).overlayImages(
- new LineImage(new Posn(-9, 1), new Posn(3, 1), new Black()),
- this.right.drawMobile(new Posn(p.x + this.rightside,
- p.y + this.length)));
- } */
- public static void main(String args[]) {
- System.out.println(exampleComplex.drawMobile(new Posn(0,0)));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement