/*********************************************** * CS2510 Spring 2011 * Lecture #3 * Data and Methods (functions) using FunJava ***********************************************/ // Allows us to use the Tester (checkExpect) in our program import tester.*; /* Class Diagram: Locations +----------+ | Loc | +----------+ | int x | | int y | +----------+ */ // Represents a 2D Location in the X/Y Plane class Loc{ int x; int y; Loc(int x, int y){ this.x = x; this.y = y; } /* Loc template... the laundry list ** Fields of "this" class ... this.x ... -- int ... this.y ... -- int ** We also add methods that we implement (and test) ... this.distTo0() ... -- double ... this.moveLeft() ... -- Loc ... this.move(int, int) -- Loc ** If we have methods that we can call on fields of "this" then they go HERE ... */ /* We still do purpose statements, but the contract and * header are now part of the (Java-like) language */ // Calculate the distance to (0,0) from "this" Loc double distTo0(){ /* Note the infix math, extra parentheses are * also no longer significant (unlike in Racket) */ return Math.sqrt((this.x * this.x) + (this.y * this.y)); } // Move this Loc to the Left 5 units Loc moveLeft(){ /* Make/return a new location, 5 units to * the left (negative X) */ return new Loc((this.x - 5), this.y); } // Move this Loc right/down by DX/DY Loc move(int dx, int dy){ return new Loc(this.x + dx, this.y + dy); } // Calculate the distance between this and that location double distTo(Loc that){ return (that.move(-this.x, -this.y)).distTo0(); } } // Examples and Tests for our Loc class and methods class LocExamples{ LocExamples(){} Loc here = new Loc(4, 3); Loc there = new Loc(4, 4); /* Typical anatomy of a test method * * boolean testFred(Tester t){ * return (t.checkexpect(Result, Expected) && * ... * t.checkexpect(Result, Expected)); * } * * The tests are connected with "&&", Java's boolean * "and" operator ("or" is pronounced "||" in Java). */ // Test our Loc.distTo0(...) Method boolean testDistTo0(Tester t){ return t.checkExpect(this.here.distTo0(), 5.0); } // Test our Loc.moveLeft(...) Method boolean testMoveLeft(Tester t){ return t.checkExpect(this.here.moveLeft(), new Loc(-1, 3)); } // Test our Loc.move(...) Method boolean testMove(Tester tee){ return tee.checkExpect(this.here.move(1, 3), new Loc(5, 6)); } // Test our Loc.distTo(...) Method boolean testDistTo(Tester t){ return t.checkExpect(this.there.distTo(this.here), 1.0); } } /* Class Diagram (again): IShapes +---------------+ | IShape | +-------+-------+ / \ +-+-+ | +-------+------+ | | +------+---+ +----+---+ +--------+ | Circle | | Rect | +->| Loc | +----------+ +--------+ | +--------+ | Loc c +--+ | Loc tl +--+ | int x | | int r | | | int w | | | int y | +----------+ | | int h | | +--------+ | +--------+ | | | +--------------+ */ // Represents a Shape interface IShape{ // Compute distance to (0,0) for this IShape double distTo0(); } // Represents a Circle Shape class Circle implements IShape{ Loc c; int r; Circle(Loc c, int r){ this.c = c; this.r = r; } /* For now, don't worry about the * * "Cannot reduce the visibility..." * * Errors... once we graduate to full Java we'll learn * how to do away with these annoying issues. */ // Distance to (0,0) for this Circle double distTo0(){ return this.c.distTo0() - this.r; } } // Represents a Rectangle Shape class Rect implements IShape{ Loc tl; int w; int h; Rect(Loc tl, int w, int h){ this.tl = tl; this.w = w; this.h = h; } // Distance to (0,0) for this Rect double distTo0(){ return this.tl.distTo0(); } } // Examples and Tests in the next Lecture...