Slide 1More on ObjectsTest-First ApproachMathematical PointJava Coordinate SystemPoint InterfaceStubs for ACartesianPointACartesianPoint TesterPoint RepresentationsAlgorithmsClass: ACartesianPointClass: APolarPointUsing the Interface and Its ImplementationsRepresenting Geometric ObjectsUsing ObjectEditorJava GraphicsAdding Tree ViewTree ViewRemoving Graphics ViewTree-only ViewObjectEditor GraphicsObjectEditor Point RulesEquivalent RepresentationsReason for ErrorWhat is in a Representation?Typing Point ObjectsLine?Describing LinesLine InterfaceLine ImplementationAnotherLine InterfaceSlide 32Predefined, Primitive vs. Programmer-Defined, ObjectKinds of TypesProgrammer-defined vs. Predefined TypesStructured vs. Atomic PropertiesStructure vs. Atomic TypesDecomposing AnAnotherLine InstancePhysical StructureLogical StructureLogical Structure Shown by ObjectEditorPhysical Structure Shown by DebuggerTwo Ways to Decompose ObjectsDrawing the Physical Structure of a Value of Some TypeDrawing the Logical Structure of a Value of Some TypeInstance-Specific StructureInstance-Specific StructureAtomic vs. Structure TypesGraphics Types Sees So FarObjectEditor Shape RulesXY-based ObjectEditor Shape RulesPoint-based ObjectEditor Shape RulesObjectEditor Bounding Box RulesObjectEditor Shape RulesObjectEditor TextBox RulesObjectEditor Label RulesLabel ImplementationUsing ALabelCreating Other Geometric ObjectsACartesianPlaneACartesianPlaneACartesianPlane PropertiesACartesianPlane PropertiesACartesianPlane StubsACartesianPlane ImplementationACartesianPlane ImplementationACartesianPlane ImplementationRefactoring Label CodeRefactoring Label CodeACartesianPlaneShuttle LocationShuttle LocationShuttle LocationShuttleLocation PropertiesAShuttleLocation ImplementationAShuttleLocation ImplementationAlternate Views of Logical StructureLines of CompositionEXTRA SLIDESAScalableNesterCOMP 401REPRESENTATIONInstructor: Prasun Dewan2MORE ON OBJECTSTest-first approachStubsTwo-way dependenciesRepresentations with errorsStructure vs. atomic typesPhysical vs. logical structureGraphics types3TEST-FIRST APPROACHWrite the interfaceCreate a class with stubs for each interface method and constructorIf method is procedure method does nothingIf method is function, it returns 0 or null valueNo variables need be declared as this point!Write a tester for itWrite/rewrite in one or more stub methodsUse testerIf tester results not correct, go back to 4Steps may be combined for simple classes!4MATHEMATICAL POINTX.Y Rq.5JAVA COORDINATE SYSTEMYX(0,0)(3,2)pixelsX and Y coordinates must be int valuesRadius and Angle can be doubleAngle is a decimal value between 0 and 26POINT INTERFACEpublic interface Point {public int getX(); public int getY(); public double getAngle(); public double getRadius(); }Read-only properties defining immutable object!7STUBS FOR ACARTESIANPOINTpublic class ACartesianPoint implements Point {public ACartesianPoint(int theX, int theY) { }public int getX() {return 0;}public int getY() {return 0;} public double getAngle() {return 0;}public double getRadius() {return 0;}}8ACARTESIANPOINT TESTERpublic class ACartesianPointTester {public void test (int theX, int theY, double theCorrectRadius, double theCorrectAngle) {Point point= new ACartesianPoint (theX, theY);double computedRadius = point.getRadius();double computedAngle = point.getAngle();System.out.println("------------");System.out.println(“X:" + theX);System.out.println(“Y:" + theY);System.out.println("Expected Radius:" + theCorrectRadius);System.out.println("Computed Radius:" + computedRadius);System.out.println(“Radius Error:" + (theCorrectRadius - computedRadius));System.out.println("Expected Angle:" + theCorrectAngle);System.out.println("Computed Angle:" + computedAngle);System.out.println(“Angle Error:" + (theCorrectAngle - computedAngle));System.out.println("------------");}public void test () {test (10, 0, 10.0, 0); // 0 degree angletest (0, 10, 10.0, Math.PI / 2); // 90 degree angle}}9POINT REPRESENTATIONSX, Y (Cartesian Representation)Radius, Angle (Polar Representation)X, RadiusX, Y, Radius, Angle…Does not completely specify the point10ALGORITHMSX.Y Rq.Cartesian RepresentationR = sqrt (X2 * Y2) = arctan (Y/X)Polar RepresentationX = R*cos()Y = R*sin()11CLASS: ACARTESIANPOINTpublic class ACartesianPoint implements Point {int x, y;public ACartesianPoint(int theX, int theY) {x = theX;y = theY;}public int getX() {return x;}public int getY() {return y;} public double getAngle() {return Math.atan2(y, x);}public double getRadius() {return Math.sqrt(x*x + y*y);}}12CLASS: APOLARPOINTpublic class APolarPoint implements Point {double radius, angle;public APolarPoint(double theRadius, double theAngle) {radius = theRadius ;angle = theAngle;}public int getX() {return (int) (radius*Math.cos(angle));}public int getY() {return (int) (radius*Math.sin(angle));}public double getAngle() {return angle;} public double getRadius() {return radius;}}13USING THE INTERFACE AND ITS IMPLEMENTATIONSPoint point1 = new ACartesianPoint (50, 50); Point point2 = new APolarPoint (70.5, Math.pi()/4); point1 = point2;Constructor chooses implementationConstructor cannot be in interface14REPRESENTING GEOMETRIC OBJECTSGeometric example to show multiple useful implementations of an interfaceMost geometric objects have multiple representations15USING OBJECTEDITORObjectEditor.edit(new ACartesianPoint (25, 50));16JAVA GRAPHICSYX17ADDING TREE VIEW18TREE VIEW19REMOVING GRAPHICS VIEW20TREE-ONLY VIEW21OBJECTEDITOR GRAPHICSCan automatically display objects representing points, rectangles, ovals, and lines as corresponding graphicsJava provides libraries to manually display graphicsHas rules for recognizing these objectsRules based on Java graphics standardsInverted coordinate systemCartesian coordinates for pointsBounding rectangles for lines, rectangles, ovalsPlus naming conventions22OBJECTEDITOR POINT RULESAn object is recognized as a point representation if:Its interface or class has the string “Point” in its nameIt has readable int properties, x and y, representing Cartesian coordinatesCan have additional propertiespublic interface Point {public int getX(); public int getY(); public double getAngle(); public double getRadius(); }23ObjectEditor.edit(new APolarPoint (195.05, 0.87)); ObjectEditor.edit(new ACartesianPoint (125, 149)); EQUIVALENT
View Full Document