import draw.*; import geometry.*; /* --- CSU213 Fall 2006 Lab Notes --------- Copyright 2006 Viera K. Proulx Lab 5: All people are equal, but some are more equal than others... */ /* Goal: Learn how to determine the equality of two self-referential data */ /* +------------------------+ | LoStars |<------------------+ +------------------------+ | +------------------------+ | | LoStars move() | | +------------------------+ | | | / \ | --- | | | ------------------------------- | | | | +------------------------+ +------------------------+ | | MTLoStars | | ConsLoStars | | +------------------------+ +------------------------+ | +------------------------+ +-| Star first | | | LoStars move() | | | LoStars rest |-+ | +------------------------+ | +------------------------+ | | | | LoStars move() | | | | +------------------------+ | | | +--+ | | v +------------------------+ +-------+ | Star | | Posn | +------------------------+ +-------+ +-| CartPt loc | | int x | | | int lifespan | | int y | | +------------------------+ +-------+ | | Star move() | +-------+ | +------------------------+ | | / \ | --- v | +------------------------------------------------------------------+ | CartPt | +------------------------------------------------------------------+ +------------------------------------------------------------------+ | CartPt translate(int dx, int dy) | +------------------------------------------------------------------+ */ //------------------------------------------------------------------------- //------------------------------------------------------------------------- // to represent a cartesian point class CartPt extends Posn { CartPt(int x, int y) { super(x, y); } // produce this point translated by the given dx and dy CartPt translate(int dx, int dy){ return new CartPt(this.x + dx, this.y + dy); } } //------------------------------------------------------------------------- //------------------------------------------------------------------------- // to represent a shooting star class Star { CartPt loc; int lifespan; Star(CartPt loc, int lifespan) { this.loc = loc; this.lifespan = lifespan; } // move this star horizontally dx pixels and vertically dy pixels Star move(int dx, int dy){ return new Star(this.loc.translate(dx, dy), this.lifespan - 1); } } //------------------------------------------------------------------------- //------------------------------------------------------------------------- // to represent a list of Stars abstract class ALoStars { // produce a list of all stars in this list moved by dx and dy abstract ALoStars move(int dx, int dy); } //------------------------------------------------------------------------- //------------------------------------------------------------------------- // to represent an empty llist of Stars class MTLoStars extends ALoStars { MTLoStars() {} // produce a list of all stars in this list moved by dx and dy ALoStars move(int dx, int dy){ return this; } } //------------------------------------------------------------------------- //------------------------------------------------------------------------- // to represent a non-empty list of Starss class ConsLoStars extends ALoStars { Star first; ALoStars rest; ConsLoStars(Star first, ALoStars rest) { this.first = first; this.rest = rest; } // produce a list of all stars in this list moved by dx and dy ALoStars move(int dx, int dy){ return new ConsLoStars(this.first.move(dx, dy), this.rest.move(dx, dy)); } } class Examples { Examples(){} // Sample data Star s1 = new Star(new CartPt(20, 40), 10); Star s2 = new Star(new CartPt(30, 40), 5); Star s3 = new Star(new CartPt(10, 30), 8); Star s4 = new Star(new CartPt(10, 50), 10); ALoStars mtstars = new MTLoStars(); ALoStars list1 = new ConsLoStars(s1, mtstars); ALoStars list2 = new ConsLoStars(s2, list1); ALoStars list3 = new ConsLoStars(s3, list2); ALoStars list4 = new ConsLoStars(s4, list3); // dealing with the empty list boolean testEmptyLists = true; // dealing with a list with one item boolean testConsLists = true; // dealing with a list with more than one item boolean testMultiLists = true; // test the method move boolean testMove(){ return true; } boolean testAll(){ return this.testMove(); } }