/* * Lab 3 * * The purpose of today's lab is to design methods for more complex class * hierarchies, including unions and self-referential classes. * * You should you use the design recipe for all methods you add to any class: * 1. Data Analysis and Design * 2. Purpose Statement and Header * 3. Examples * 4. Template * 5. Body * 6. Test * * In some cases you will be given parts of the method, such as the header * or the body. In these cases you should still complete all of the other * steps of the design recipe in the correct order. * * Before continuing, please make sure that the language level is set to * "ProfessorJ - Beginner" */ /**********************************************************************/ /* * Methods in Unions * * In this example you will be designing a union of classes for working * with two different systems of determining distance, Cartesian distance * and Manhattan distance. * * Insert the following classes below: * * - A Point is an abstract class that has an x (int) and y (int) coordinate. * - A CartPoint is a Point with no additional fields. * - A ManhPoint is a Point with no additional fields. * * Notice that the subclasses don't have any fields other than those in * the superclass. The differences will be in the methods. */ /**********************************************************************/ /**********************************************************************/ /* * The distance formulas for Cartesian and Manhattan points are as follows: * * Given a point p, its distance to the point (0,0) is * * Cartesian Distance = sqrt((p.x)^2 + (p.y)^2) * Manhattan Distance = abs(p.x) + abs(p.y) * * In other words, Cartesian distance corresponds to a straight line * between two points and Manhattan distance is the sum of the horizontal * and vertical distances (as if you were traveling along city blocks * in Manhattan). * * Now, add a method named distToZero to both CartPoint and ManhPoint. * This method should return either the Cartesian or Manhattan distance * of the point to (0,0). Follow the design recipe for methods! The methods * are shown below but you need to provide the purpose statements, templates, * and examples. * * In CartPoint: * * double distToZero() { * return Math.sqrt((this.x * this.x) + (this.y * this.y)); * } * * In ManhPoint: * * double distToZero() { * return Math.abs(this.x) + Math.abs(this.y); * } */ /* * Now, let's build on this by adding a closerToZeroThan method to this class * hierarchy. Design and implement an closerToZeroThan method that returns * true if the point is closer to zero than the given number (a double). Use the * design recipe! */ /* * Now let's consider lists of points. We will create a self-referential union * of classes to represent such lists. * * APointList is one of * - MTPointList * - ConsPointList * Point first * APointList rest * * Implement these classes below. */ /**********************************************************************/ /**********************************************************************/ /* * Let's add some methods to the classes representing lists of Points. * * Consider the problem of determining how many of the Points in a list * have a certain property. For example, how many Points in APointList are * closer to zero than a given distance? We can develop a method * numCTZT (a.k.a "closer to zero than") that computes this. * * First, we need to understand the problem by constructing examples. How many * examples will we need? Well, we'd better have at least one example for each * kind of APointList--MTPointList and ConsPointList. Since we're comparing distances * to a given number, we'd better also construct an example that contains points both * less than and greater than the given distance. Go construct some examples. * * * After constructing examples, write down the template: purpose and abstract method * declaration in the abstract class, one clause of the method in each variant of the union. * Don't forget question four in building the template: is there any self-reference in the * class diagram? Make sure to add recursive calls in the template at these points. * * * Next, tackle each clause of the method declaration, based on your examples. Start * with the empty list. * * Finally, we need to consider numCTZT for the ConsPointList class. First try to figure * out what each piece of the template represents. What is this.first? What is the * result of this.rest.numCTZT(...) ? How should you combine these two pieces of information * to get the result? * * In Java, we can make decisions with the "if" statement. It looks like this: * if () * return ; * else * return ; * * * Build an "if" statement that computes the right result for the ConsPointList clause of the * numCTZT method. * * * * * The following code is the numCTZT method for the ConsPointList class: * * int numCTZT(double dist) { * if(this.first.closerToZeroThan(dist)) * return 1 + this.rest.numCTZT(dist); * else * return this.rest.numCTZT(dist); * } * * Lets dissect this code and figure out what each part does. Answer each of * the questions below: * * Q: What does first.closerToZeroThan(dist) return? * A: * * Q: What does rest.numCTZT(dist) return if rest is an MTPointList? * A: * * Q: What does rest.numCTZT(dist) return if rest is a ConsPointList? * A: * */ /* * Try a very similar problem. Add a method numFFZT ("num farther from zero * than") to both the MTPointList and ConsPointList classes. This method * should return the number of points in the list that are farther from zero * than the given distance (a double). */ /* Try one more. Add a method hasCTZT to both the MTPointList and ConsPointList * classes that returns true if the list contains a Point closer to zero than * the given distance. * * If you get stuck, make sure you wrote down examples. Make sure your examples * include the full range that we considered for the numCTZT method. */ /* * A look ahead to next week * * Consider the following questions relating to topics to appear in latter * labs. * * How would you find all of the Points in a PointList that are closer to zero * than a given distance? Can you return another list with all such points? * How would you construct that list? */ /**********************************************************************/ //Class for containing examples: class Examples { Examples() {} // Don't mess with this line! // Examples from above: // boolean t1 = (new GroceryItem("Spaghetti", 64, 3, 2).atkinsDietCheck() == false); /* * Run the following code in your interactions window: * * Examples e = new Examples(); * */ }