// Lecture 8 // CS U213 Fall 2008 // mobile-methods.java /* Sample mobiles: --------------- Simple mobile | | 10 blue Complex mobile | | | ------------+----- | | ------+------ | | | | 10 | 40 red 10 green blue */ import draw.*; import geometry.*; import colors.*; /* +---------+ | IMobile |<---------------+ +---------+ | +---------+ | | | / \ | --- | | | --------------------- | | | | +--------------+ +---------------+ | | Simple | | Complex | | +--------------+ +---------------+ | | int length | | int length | | | int weight | | int leftside | | | IColor color | | int rightside | | +--------------+ | IMobile left |----+ | IMobile right |----+ +---------------+ */ // to represent a mobile interface IMobile { // compute the total weight of this mobile int totalWeight(); // compute the total length of this mobile //int totalLength(); // is this mobile is balanced? //boolean isBalanced(); } // to represent an item hanging at the end of a mobile class Simple implements IMobile { int length; int weight; IColor color; 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.totalLength() ... -- int ... this.isBalanced() ... -- boolean */ // compute the total weight of this simple mobile int totalWeight(){ return this.weight; } } // to represent a part of support structure for a mobile class Complex implements IMobile { int length; int leftSide; int rightSide; IMobile left; IMobile right; 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.totalLength() ... -- int ... this.isBalanced) ... -- boolean METHODS FOR FIELDS: ... this.left.totalWeight() ... -- int ... this.left.totalLength() ... -- int ... this.left.isBalanced) ... -- boolean ... this.right.totalWeight() ... -- int ... this.right.totalLength() ... -- int ... this.right.isBalanced() ... -- boolean */ // compute the total weight of this complex mobile int totalWeight(){ return this.left.totalWeight() + this.right.totalWeight(); } } /* Sample mobiles: --------------- simple1: | | 10 blue complex2 (with complex1 on the left and simple2 on the right) complex1 (with simple3 on the left, simple1 on the right) | | | ------------+------ | | ------+------ | | | | 10 | 40 red 10 green blue */ class Examples { Examples(){} IMobile simple1 = new Simple(20, 10, new Blue()); IMobile simple2 = new Simple(30, 40, new Green()); IMobile simple3 = new Simple(10, 10, new Red()); IMobile complex1 = new Complex(10, 5, 5, this.simple3, this.simple1); IMobile complex2 = new Complex(30, 10, 5, this.complex1, this.simple2); // test the method totalWeight for the IMobile class hierarchy boolean testTotalWeight = (check this.simple1.totalWeight() expect 10) && (check this.complex1.totalWeight() expect 20) && (check this.complex2.totalWeight() expect 60); }