CSC152 2004F, Class 7: Pause to Discuss Code Admin: * Locked out of my office today. Sorry if you came looking for me. * Email me your assignments each day. Typical subject: "Homework for DATE: YOURNAME" * Missing: O'Neil (just late) and Johns * Question: Can we do this work at home on our Microsoft (R) Windows (R) machines? * Yes. Go to http://java.sun.com/j2se/1.4.2/download.html * Alternatively. Get 'ssh' and 'sftp' working on your Microsoft (R) Windows (R) machine (ask Help Desk) ssh bessell.cs.grinnell.edu Compile on bessell (or the computer of your choice) Edit on your local windows machine and sftp back and forth * Detailed instructions: (1) Create the file on your local machine. (2) In DOS (agh!) type "sftp lie.cs.grinnell.edu" (3) Enter your password when requested. (4) In FTP, Type "cd CS152/whatever" (e.g., "cd CS152/madlibs") (5) In FTP, Type "put FILENAME.java" (6) In FTP, Type "quit" (Finishes FTP) (7) In DOS, ype "ssh lie.cs.grinnell.edu" (8) Enter your password when requested. (9) In SSH (a lot like the terminal window), type "cd CS152/whatever" (10) In SSH, type "jc FILENAME.java" (11) If everything works fine, you're done. (12) If everything doesn't work fine, edit the file and go back to step 1. * Note for people who don't care about the security of their password: You can telnet instead of ssh and ftp instead of sftp. Overview: * Eschewing mutators * Investigation of your assignments. * An alternative design of TwoDimVectors. * Reconciling the implementations through interfaces. * Testing. * Homework! Eschewing Mutators: * Problem: The stretch method returns a new vector; we use mutators to set the X and Y coordinates of that vector. * Observation: There are two ways to create new vectors new TwoDimVector(); // Creates the "origin vector" new TwoDimVector(xvalue,value); * Both constructors have the same name. * But they have different parameters, so Java can tell which one you mean. * Solution to problem: Use the two-parameter constructor. * Side note: If we have no mutators, we also don't need the zero-parameter constructor. * Side effect: You can't call such a constructor. * Note: If you don't declare a constructor, you get a "free" zero-parameter constructor that does "the obvious thing". If you do declare a constructor, you no longer get that free constructor. * Sam's recommendation: Always declare your own constructors. Detour: Start your papers in advance so that Sam doesn't criticize you for reading in class. /Your Classes/ * What other problems did you encounter? * unitVector and rotateVector gave me "something with letters and numbers all shoved together" * Why? The default way to print an object is to print the class of the object and its address in memory. (Doesn't reveal much.) * The solution: Java looks for a toString method in each class and uses that when it has to print the object. * Observation: You can concatenate strings with +. * Observation: To convert a double to a string, use Double.toString(double) * Detour: The Java compiler is "smart". If you use another class and that class hasn't been recompiled, it will recompile that class. * How do you return a boolean value? * return true; * return false; * return SOME_BOOLEAN_EXPRESSION * For our particular isLessThan, we might use return this.distanceFromOrigin() < other.distanceFromOrigin(); * Which is better for computing the angle? A: return Math.asin(this.ycoord/this.distanceFromOrigin()); S: return Math.atan(this.ycoord/this.xcoord); * Don't need to call another method. (Faster.) * If the vector is "vertical", you'll be dividing by zero. * If the vector is in quadrant II or III, it will seem to be in IV or I. * Solution: Use the less efficient one or add a comparison. /Homework: Write as much of a *new* TwoDimVector class as you can, assuming that your fields are radius and theta. * Challenge problem: Include setX and setY.