/******************************************************************** In this lab we will design two different ways to represent a transit system. We will then design methods for the system, such as finding all stations on a given route, or finding a route from one station to another. The goals are: experience how the way the data is represented affects the design of methods that process the data understand the distinction between information and its representation as classes of data recall and practice writing methods using the accumulator style - and understand the need for accumulators *******************************************************************/ /******************************************************************* Problem statement: The information about the city transit system allows the passengers to find out about the routes and stations and schdules. We are asked to design this computerized system. For each station we need to record the name, and its location. Each of the train routes has a name and a color. For each route segment between two stations, we need to know the origin and destination, the route that connects them, and the time it takes to travel from one station to another. The transit system record all this information. We impose two restrictions on the system as follows: 1. There are at most two routes that lead from any given station to another station. 2. It is impossible to travel through the same station twice in one trip. Here is a sample transit system: Sample big transit: A / \ / \ / \ / \ B C / \ / \ / \ / \ F G S E \ / \ /| / | / \ / | / | H K | N | / \ | / \ \ / \|/ \ \ / P T Z Blue: A -5- B -3- F -8- Z Red: A -3- C -4- S -5- K -3- Z Green: B -2- G -5- K -3- P Yellow: C -3- E -4- N -5- P White: G -4- H Black: N -3- T S -6- P You may want to draw a color picture, to better understand the available information. Here is another, simpler system we will work with at the beginning: Sample simple transit: B / \ / \ F G \ / \ | / \ | H K | / \ \ / \ \ / P Z Blue: B -3- F -8- Z Green: B -2- G -5- K -3- P Red: K -3- Z White: G -4- H ********************************************************************/ /******************************************************************* Here is one data definition for this system: ANextStop is one of - empty - OneStop OneStop is a structure Route route int minutes Station station Station consists of String name Posn loc ANextStop left ANextStop right Route is a structure String name Color color Transit is a list of Station-s We give you the class definitions, mainly because the implementation of the 'same' method needed for comparisons and test cases is tedious. However, you need to complete the design of these classes by making examples of data that represent the simple transit system. *******************************************************************/ /* ; ; ; ; ; ;;;; ; ; ; ; ; ; ; ; ; ; ;;; ; ;; ;; ;;; ; ; ;; ; ; ;; ;; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;;;; ; ; ; ;;;;;; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;;; ;;;;; ; ; ; ;;;; ; ; ; */ interface ISame{ // is this object the same as the given one boolean same(Object obj); } /* ; ; ; ; ;;;;; ; ; ; ; ; ; ; ; ; ; ;;; ; ; ;;;; ;;; ; ; ; ; ; ; ; ; ; ; ; ;;;; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;;;;;; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;; ; ; ; ; ; ;;; ;; ; ;; ;;;; ; ; ; */ /* +-------------+ | Route | +-------------+ | String name | | Color color | +-------------+ */ // to represent a route in a transit system class Route implements ISame{ String name; Color color; Route(String name, Color color) { this.name = name; this.color = color; } /******* add your methods below *****************************/ boolean same(Object obj){ return this.name.equals(((Route)obj).name) && this.color.equals(((Route)obj).color); } /* // ** DRAFT TEMPLATE ** Edit as needed. ??? mmm() { ... this.name ... ... this.color ... } */ } /* +-----------+ | ANextStop | +-----------+ +-----------+ / \ --- | -------------------- | | +--------+ +-----------------+ | MTStop | | OneStop | +--------+ +-----------------+ +--------+ | Route route | | int minutes | | Station station | +-----------------+ */ /* ; ; ; ; ; ; ; ;;;; ; ; ;; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;;; ; ; ;;;; ; ;;;; ;;; ; ;; ; ; ; ; ; ; ; ; ; ; ; ;; ; ; ; ;; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;;;;;;; ; ; ; ;;;;;; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;; ; ; ; ; ; ; ; ; ; ;; ; ; ; ; ; ; ;;;; ; ; ;; ;;; ;; ;;; ; ;; ; ; ; ; ; ; */ // to represent the route to the next stop abstract class ANextStop implements ISame{ /******* add your methods below *****************************/ // is this ANextStop the same as the given object boolean same(Object obj){ return this.sameStop((ANextStop)obj); } // is this ANextStop the same as the given ANextStop abstract boolean sameStop(ANextStop astop); // is this ANextStop the same as the given MTStop boolean sameMTStop(MTStop mts){ return false; } // is this ANextStop the same as the given OneStop boolean sameOneStop(OneStop onest){ return false; } /* // ** DRAFT TEMPLATE ** Edit as needed. // purpose statement abstract ??? mmm(); */ } /* ; ; ; ; ; ; ;;;;;;; ;;;; ; ;; ;; ; ; ; ; ;; ;; ; ; ; ; ; ; ; ; ; ; ;;;; ;;; ; ;; ; ; ; ; ; ; ;; ; ; ; ;; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;; ; ; ; ; ; ;;; ;; ;;; ; ;; ; ; ; ; ; ; */ // to represent no further stop class MTStop extends ANextStop { MTStop() { } /******* add your methods below *****************************/ boolean sameStop(ANextStop astop){ return astop.sameMTStop(this); } boolean sameMTStop(MTStop mtst){ return true; } /* // ** DRAFT TEMPLATE ** Edit as needed. ??? mmm() { } */ } /* ; ; ; ; ;;;; ;;;; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;; ;;; ; ;;;; ;;; ; ;; ; ; ; ;; ; ; ; ;; ; ; ; ;; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;;;;;; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;; ; ; ;;;; ; ; ;;;; ;;; ;; ;;; ; ;; ; ; ; ; ; ; */ // to represent next stop in a transit system class OneStop extends ANextStop { Route route; int minutes; Station station; OneStop(Route route, int minutes, Station station) { this.route = route; this.minutes = minutes; this.station = station; } /******* add your methods below *****************************/ boolean sameStop(ANextStop onest){ return onest.sameOneStop(this); } boolean sameOneStop(OneStop onest){ return this.route.same(onest.route) && this.minutes == onest.minutes && this.station.same(onest.station); } /* // ** DRAFT TEMPLATE ** Edit as needed. ??? mmm() { ... this.route ... ... this.minutes ... ... this.station ... } */ } /* ; ; ; ; ;;;; ; ; ; ; ; ; ; ; ; ; ;;;; ;;; ;;;; ; ;;; ; ;; ; ;; ; ; ; ; ; ; ; ;; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;;;; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;;; ;; ;;;;; ;; ; ;;; ; ; ; ; ; */ /* +-----------------+ | Station | +-----------------+ | String name | | Posn loc | | ANextStop left | | ANextStop right | +-----------------+ */ // to represent a station in a transit system class Station implements ISame{ String name; Posn loc; ANextStop left; ANextStop right; Station(String name, Posn loc, ANextStop left, ANextStop right) { this.name = name; this.loc = loc; this.left = left; this.right = right; } /******* add your methods below *****************************/ boolean same(Object obj){ return this.name.equals(((Station)obj).name) && this.loc.x == ((Station)obj).loc.x && this.loc.y == ((Station)obj).loc.y && this.left.same(((Station)obj).left) && this.right.same(((Station)obj).right); } /* // ** DRAFT TEMPLATE ** Edit as needed. ??? mmm() { ... this.name ... ... this.loc ... ... this.left ... ... this.right ... } */ } class Examples{ Examples() {} } /******************************************************************* Run the program with your examples. We are now ready to design several methods for this transit system. Design the needed methods that will count how many stations can we reach from a this station. Design the needed methods that will determine whether we can reach a given station from this station. Finally, design the methods that will produce a list of stations we need to go through to travel from this station to the given station. Include the origin and destination in the list. Produce an empty list of stations, if there is no way of getting there from here. You may copy the code for list of Objects from one of the lectures to save the time on designing the equality test methods. This problem has a slight complication. As we travel along the possible routeings, we need to remember where we came from, so that when we reach the desired station, we can produce the list of all stations on the route. This is accumulated knowledge that is difficult to reproduce, and is best passed on to the next step. So, your method may include an additional argument - the list of stations on the path so far. Of course, we start the search with an empty list. *******************************************************************/