Date: Thu, 30 Jan 2003 12:50 PM From: Mark Jacobson To: 810-061-02@uni.edu Subject: Solution to group exercise, LEFT, RIGHT... INDENTATION improved... Very important correction!!!!!!!! When I copied and pasted the Java code, it got ugly formatted in the pine email program, and I was in a hurry to reformat and INDENT it properly again, and needed to vacate the classroom for the next instructor and class. Here is the correctly formatted code. The indentation was WRONG ------------------- on only ONE statement, but it was HORRIBLY wrong, for sure! // See page 35 of the textbook for these concepts... import objectdraw.*; import java.awt.*; public class Dragging extends WindowController { private Location previousPosition; private Text direction; public void begin() { direction = new Text("Direction of dragging", 50, 50, canvas); } public void onMousePress(Location pt) { previousPosition = pt; } public void onMouseDrag(Location pt) { if (previousPosition.getX() < pt.getX()) direction.setText("RIGHT DRAGGING"); else direction.setText("Leftward HO draggings"); previousPosition = pt; // <--- Much better now!!!! } } *******************Further discussion of this************************** public void onMouseDrag(Location pt) { if (previousPosition.getX() < pt.getX()) direction.setText("RIGHT DRAGGING"); else direction.setText("Leftward HO draggings"); previousPosition = pt; <<<---- BAD - NEED TO outdent this } because it is indented too much! This is NOT correct indenting, because previousPosition = pt; is a statement at the same level as the IF statement. It is NOT part of or subordinate to the ELSE clause.... It would be even more horrible if it were not for the fact that at least there was VWS (Vertical White Space). public void onMouseDrag(Location pt) { if (previousPosition.getX() < pt.getX()) direction.setText("RIGHT DRAGGING"); else direction.setText("Leftward HO draggings"); previousPosition = pt; <<<<--- HORRIBLE!!!! } The p in previousPosition needs to be in the same column as the i in the if and the e in the else. ------------------------------------------------------------------------ Note: Using {} for both the if and the else clauses would make it look like this, and this would be great way to do it anyway, even though the {} are NOT required for packaging one statement. public void onMouseDrag(Location pt) { if ( previousPosition.getX() < pt.getX() ) { direction.setText("RIGHT DRAGGING"); } else if ( previousPosition.getX() > pt.getX() ) { direction.setText("Leftward HO draggings"); } previousPosition = pt; // <--- Much better now!!!! } Note the else clause is now an else if ( ) clause. ---- ------- Note also the extra two pairs of braces added.... ------------------------- Have a great weekend. Please read the 2nd chapter of the Copy Central manuscript, as we will cover more of that next week. Mark