// Name: // Lab Section: // Primary TA: // Email: // Date: // CS101 Lab 7 // RoomSet.java /* * Instructions: * Fill in the RoomSet(Room) constructor * Pick a color at random (currently, color 0,0,0 is picked * read the Color API (clickable from the lab web page description) * to see how to use new Color(int,int,int) * Implement union * Try the selfTest() * Implement addRoom * Test some more */ import java.awt.Color; import cs101.terminal.*; import cs101.canvas.*; public class RoomSet { private static int SetNumber=0; private int num; private RoomList setMembers; private Color color; public RoomSet() { num = ++SetNumber; setMembers = null; color = new Color( 0, 0, 0 ); // fix this to pick a random color } public RoomSet(Room singleton) { this(); // call our default constructor /* Below, you should add the singleton room to the set */ } public RoomSet union(RoomSet other) { // fill this in to create a new set that is the union of // this set and the other set // set the color of the new set as described in the lab handout return new RoomSet(); } public void addRoom(Room r) { // fill in to add room r to this set. You can assume that // room r is not previously in this set. // once that is done, we inform r that it is in this set: r.nowMemberOf(this); } public String toString() { return "Set "; // add more stuff to make this meaningful } Color getColor() { return color; } public static void selfTest() { Terminal.println("\nBegin self test of RoomSet (see canvas)"); CS101Canvas cv = new CS101Canvas(); Room r11 = new Room(1,1,50, cv); Room r12 = new Room(1,2,50, cv); Room r13 = new Room(1,3,50, cv); Room r14 = new Room(1,4,50, cv); Room r15 = new Room(1,5,50, cv); Room r21 = new Room(2,1,50, cv); Room r22 = new Room(2,2,50, cv); Room r23 = new Room(2,3,50, cv); Room r24 = new Room(2,4,50, cv); Room r25 = new Room(2,5,50, cv); Terminal.println("Rooms are now placed, each in its own set."); Terminal.println("We now union rooms in the top row"); cv.sleep(500); Terminal.println("Rooms 2 and 3"); r12.getSet().union(r13.getSet()); cv.sleep(1500); Terminal.println("Rooms 4 and 5"); r14.getSet().union(r15.getSet()); cv.sleep(1500); Terminal.println("Rooms 2 and 5 (and therefore, Rooms 3 and 4 also)"); r12.getSet().union(r15.getSet()); cv.sleep(1500); Terminal.println("Rooms 1 and 5 (and therefore, all rooms"); r11.getSet().union(r15.getSet()); cv.sleep(1500); cv.sleep(1000); Terminal.println("We now union rooms in the bottom row, right to left"); r24.getSet().union(r25.getSet()); cv.sleep(500); r23.getSet().union(r24.getSet()); cv.sleep(500); r22.getSet().union(r23.getSet()); cv.sleep(500); r21.getSet().union(r22.getSet()); cv.sleep(500); cv.sleep(1000); Terminal.println("We now union the leftmost rooms in each row"); r11.getSet().union(r21.getSet()); cv.sleep(1000); Terminal.println("End self test of RoomSet (see canvas)\n"); } }