COMP 401 REPRESENTATION Instructor Prasun Dewan MORE ON OBJECTS Test first approach Stubs Two way dependencies Representations with errors Structure vs atomic types Physical vs logical structure Graphics types 2 TEST FIRST APPROACH Write the interface Create a class with stubs for each interface method and constructor If method is procedure method does nothing If method is function it returns 0 or null value No variables need be declared as this point Write a tester for it Write rewrite in one or more stub methods Use tester If tester results not correct go back to 4 Steps may be combined for simple classes 3 MATHEMATICAL POINT X R Y q 4 JAVA COORDINATE SYSTEM 3 2 0 0 X pixels X and Y coordinates must be int values Y Radius and Angle can be double Angle is a decimal value between 0 and 2 5 POINT INTERFACE public interface Point public int getX public int getY public double getAngle public double getRadius Read only properties defining immutable object 6 STUBS FOR ACARTESIANPOINT public 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 7 ACARTESIANPOINT TESTER public 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 angle test 0 10 10 0 Math PI 2 90 degree angle 8 POINT REPRESENTATIONS X Y Cartesian Representation Radius Angle Polar Representation X Radius Does not completely specify X Y Radius Angle the point 9 ALGORITHMS Cartesian Representation R sqrt X2 Y2 X arctan Y X R Y Polar Representation q X R cos Y R sin 10 CLASS ACARTESIANPOINT public 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 11 CLASS APOLARPOINT public 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 12 USING THE INTERFACE AND ITS IMPLEMENTATIONS Point point1 new ACartesianPoint 50 50 Point point2 new APolarPoint 70 5 Math pi 4 point1 point2 Constructor chooses implementation Constructor cannot be in interface 13 REPRESENTING GEOMETRIC OBJECTS Geometric example to show multiple useful implementations of an interface Most geometric objects have multiple representations 14 USING OBJECTEDITOR ObjectEditor edit new ACartesianPoint 25 50 15 JAVA GRAPHICS Y X 16 ADDING TREE VIEW 17 TREE VIEW 18 REMOVING GRAPHICS VIEW 19 TREE ONLY VIEW 20 OBJECTEDITOR GRAPHICS Can automatically display objects representing points rectangles ovals and lines as corresponding graphics Java provides libraries to manually display graphics Has rules for recognizing these objects Rules based on Java graphics standards Inverted coordinate system Cartesian coordinates for points Bounding rectangles for lines rectangles ovals Plus naming conventions 21 OBJECTEDITOR POINT RULES An object is recognized as a point representation if Its interface or class has the string Point in its name It has readable int properties x and y representing Cartesian coordinates Can have additional properties public interface Point public int getX public int getY public double getAngle public double getRadius 22 EQUIVALENT REPRESENTATIONS ObjectEditor edit new APolarPoint 195 05 0 87 ObjectEditor edit new ACartesianPoint 125 149 Stored Computed Computed Stored Cartesian coordinates the same but not the polar coordinates 23 REASON FOR ERROR public int getX return int radius Math cos angle Calculated X and Y values are truncated when converted to int 24 WHAT IS IN A REPRESENTATION Representation determines Space efficiency Time efficiency Precision of various properties 25 TYPING POINT OBJECTS ACartesianPoint APolarPoint Point 26 LINE 27 DESCRIBING LINES Point Bounding Rectangle h2 w2 x2 y2 h2 w2 28 LINE INTERFACE public interface Line public int getX public void setX int newX public int getY public void setY int newY public int getWidth public void setWidth int newVal public int getHeight public void setHeight int newHeight 29 LINE IMPLEMENTATION public class ALine implements Line int x y width height public ALine int initX int initY int initWidth int initHeight x initX y initY width initWidth height initHeight public int getX return x public void setX int newX x newX public int getY return y public void setY int newY y newY public int getWidth return width public void setWidth int newVal width newVal public int getHeight return height public void setHeight int newHeight height newHeight 30 ANOTHERLINE INTERFACE public interface AnotherLine public Point getLocation public void setLocation Point newLocation public int getWidth public void setWidth int newVal public int getHeight public void setHeight int newHeight 31 ALTERNATIVE IMPLEMENTATION WITH TWO DIFFERENT KINDS OF PROPERTIES public class AnAnotherLine implements AnotherLine int width height Point location public AnAnotherLine Point initLocation int initWidth int initHeight location initLocation width initWidth height initHeight public Point getLocation return location public void setLocation Point newVal location newVal public int getWidth return width public void setWidth int newVal width newVal public int getHeight return height public void setHeight int newHeight height newHeight 32 PREDEFINED PRIMITIVE VS PROGRAMMERDEFINED OBJECT Point Programmer defined Object int Predefined Primitive 33 KINDS OF TYPES types Object types Primitive types Classes ABMICalculator double int ABMISpreadsheet AnotherBMISpreadsheet Interfaces BMISpreadsheet type set of operations Point 34 PROGRAMMER DEFINED VS
View Full Document
Unlocking...