CSC152 2004F, Class 6: Objects and Classes, Continued Admin: * Today's class rougher than I'd like due to illness. Sorry. * CS Tutoring available 9-10 Sunday-Thursday in 2417 or 2435. * How did the homework for today differ from the in-class exercise? * Goal: More detailed thought * parameters, return values * other methods * You will not turn in today's homework, but we will discuss it. Overview: * Review * Using TwoDimVectors * Other methods for a TwoDimVector class * Constructors * Design question: Should these vectors be mutable? /Review/ * Classes provide templates for objects * Classes have three parts * Fields, which specify the kinds of data each object has (e.g., supplyOfInk for pens, color for pens) * Methods, which specify what the object can do. * Observers, which extract information, but don't change the object (e.g., getX) * Mutators, which change the object (and may also return information) (e.g., squirtAllTheInkInThisPenAt(Person x)) * Constructors, which are used to build new objects * Started to build TwoDimVector, vectors in two space * Fields: xcoord, ycoord (other alternatives, too) * Methods: getX, getY, getDistanceFromOrigin, setX, setY * Note: Within methods, you refer to the fields as this.FIELDNAME E.g., public double getX() { return this.xcoord; } // getX() /Using Your Own Objects/ * Create new ones with CLASS NAME = new CLASS(); * E.g., TwoDimVector vec = new TwoDimVector(); * Call their methods with vec.methodName(); * Question: Where does the code go? * The code for main usually goes in one class/file. * The code for each of your own classes goes in a separate file. * Question: What happens when one of our methods returns an object? * Return an object. (You should have declared that class already.) * Encapsulation suggests that if we create two vectors and change one, we shouldn't have changed the other. /Two Dimensional Vectors, Revisited/ * Stretch them! public RETURNTYPE stretch(double factor) * Return a new vector (the stretched version of the original vector) * Change the old vector, returning nothing * Find the unit vector public TwoDimVector unit() * Find the angle from the origin public double getTheta() * Rotate the vector public TwoDimVector rotate(double angle) * Dot product public double dotProduct(TwoDimVector other) public double dotProduct(TwoDimVector first, TwoDimVector second) * We use the first, since there's an implicit first vector (the one supplying the dot product method). * "Empty" the vector public void toOrigin() * When your procedure returns nothing, use "void" as the return type * Add another vector to this vector public TwoDimVector add(TwoDimVector other) * Flip across the x=y line public TwoDimVector flip() * Compare two vectors to each other by r value public boolean greaterThan(TwoDimVector other) public boolean lessThan(TwoDimVector other) public boolean equalTo(TwoDimVector other) * Change both x and y values public void set(double newx, double newy); * Subtract public TwoDimVector subtract(TwoDimVector other) * Point of exercise: * For each kind of object, there is a wide variety of methods we might choose to include * If pushed far enough, we may start coming up with silly answers * Return types can be troublesome (key design decision: Do we provide methods that change the underlying object or not?) (In particular, should we provide setX, setY, toOrigin, and set?) * There's always an implicit "this vector", so operations that need two arguments take only one parameter (e.g., dot product) /Constructors (or how to build vectors that are non-zero)/ * Constructors are used to build new objects. * Constructors look a lot like methods except * No return type * Must be named the same thing as the class * You call constructors with new CLASSNAME(parameters) /Homework/ * Make your own copy of the TwoDimVector and Main classes. * Remove all the mutators. * Implement and test as much of you can of: unit, comparison operations, rotate, getTheta, flip * Email me any questions you may have