Objects COMP 14 Fall 1998 Prasun Dewan1 9 Programmer Defined Types As we have seen before Java provides us with a variety of types such as int double boolean char and String Compared to other languages this is a rich set of types However these types do not meet all of our needs Point To understand why we need to define our own types consider the problem of following the trajectory of a rocket we have launched Assume the trajectory is confined to a plane whose origin is the launching point Let us say we are interested in statistics about the highest point of the rocket the angle a straight line to it from the origin makes with the horizontal axis and its total horizontal and vertical distance from the origin As you may remember from Math these are referred to as the angle radius R X and Y coordinates respectively of the point Figure 1 The angle and radius of a point are called its Polar coordinates and the X and Y coordinates are called its Cartesian coordinates Each of these pairs of coordinates completely represents the point and it is possible to convert one representation to the other Highest pt X R Y Launching pt Figure 1 Tracking the Highest Point We can imagine two versions of the program one in which the users enter the Cartesian coordinates of the point and another in which they enter the Polar coordinates 1 Copyright Prasun Dewan 1998 1 Objects Algorithm A high level algorithm for this problem would be 1 Get a representation of the point 2 Print out the angle radius X and Y coordinates of the point To do step 1 we must decide how the point is input We could use either the Cartesian representation which consists of its X and Y coordinates or the Polar representation which consists of its radius and angle coordinates Let us for now decide on using the Cartesian representations This means that in step 2 we must be able to convert from Cartesian coordinates to Polar coordinates From Figure 1 we can see that R sqrt X2 Y2 arctan Y X Java Implementation We are now ready to write the Java code for this problem class ARocketTracker public static void main String args System out println Please Enter Cartesian Coordinates of Highest Point double highestX AKeyboard readDouble double highestY AKeyboard readDouble double highestRadius Math sqrt highestX highestX highestY highestY double highestAngle Math sqrt Math atan highestY highestX print highestX highestY highestRadius highestAngle public static void print double x double y double radius double angle System out println Highest Horizontal Distance x System out println Highest Vertical Distance y System out println Highest Total Distance radius System out println Highest Angle angle A Different Representation If we chose Polar coordinates we must know how to convert from them to Cartesian coordinates X radius cos Y radius sin 2 Objects A Polar implementation is public static void main String args System out println Please Enter Polar Coordinates of Highest Point double highestRadius AKeyboard readDouble double highestAngle AKeyboard readDouble double highestX highestRadius Math cos highestAngle double highestY highestRadius Math sin highestAngle print highestX highestY highestRadius highestAngle Problems with using predefined types We did this problem without defining any new type using the existing Java types such as double As a consequence our two solutions have several problems No get function While the code to output the point information is in a separate method the code to input a point is in the main method Ideally we want a function that inputs this data for us However a function can return only one value whereas in both representation we need to return two double values It would be nice if these two values could be combined into one composite value of type Point Passing multiple representation parameters If we had such a type we would need to pass a single point parameter to our print method rather than four different values which is more tedious and error prone since we may accidentally put the parameters in the wrong order Difficult to change representation As we switched from representation to another we had to change every line of the main method except the call to print It would be nice if we could change our choice of the representation without changing any other part of our program Mixing tracking and conversion The tracking program must worry about converting between the two representations of a point It would be nice if this task was the responsibility of the type Point Difficult to reuse Because both tracking and conversion are mixed to together it is difficult to reuse one without the other By creating the type Point we would be able to use the conversion code in other applications such as a drawing application Using the type Point Assume we can define a type Point that has the properties above Our main method could then be public static void main String args Point highest getPoint print highest Instead of declaring multiple double variables for storing the representation of Point this method simply declares a single variable of type Point It calls getPoint to create a new instance of this type based on what the user inputs and then calls print to output it Let us next look at print public static void print Point p System out println Higest Horizontal Distance p getX System out println Highest Vertical Distance p getY System out println Highest Total Distance p getRadius 3 Objects System out println Highest Angle p getAngle This method assumes that the type Point defines methods to get the x y radius and angle coordinates of a point Multiple Type Implementations So far we have not yet picked a representation for Point Let us assume we have available to us two implementations of type Point ACartesianPoint and APolarPoint which use the Cartesian and Polar representations respresentations of the point Based on the format in which the input data is available we decide which implementation to choose This choice is made in getPoint It could pick the Cartesian implementation public static Point getPoint System out println Please Enter Cartesian Coordinates of Highest Point return new ACartesianPoint AKeyboard readDouble AKeyboard readDouble or the Polar implementation public static Point getPoint System out println Please Enter Polar Coordinates of Highest Point return new APolarPoint AKeyboard readDouble AKeyboard readDouble In either case this method returns new instance of type Point In the first case the instance
View Full Document
Unlocking...