Problem 1: [20 points total] A: [3 points total - must include purpose statements, constructors] // to represent email routing interface IMail{ } // to represent a new email message class Send implements IMail{ int sender; int address; String text; Send(int sender, int address, String text){ this.address = address; this.text = text; } } // to represent one forwarding step of an email message routing class Fwd implements IMail{ int routing; IMail packet; Fwd(int routing, IMail packet){ this.routing = routing; this.packet = packet; } } B: [2 points total: 1 for Send, 1 for several Fwd-s for the given message, 1 point for the new example] // examples and tests for the mail forwarding problem class Examples{ Examples(){} IMail msg = new Send(1, 5, "hello"); IMail fwd7 = new Fwd(7, this.msg); IMail fwd3 = new Fwd(3, this.fwd7); IMail fwd8 = new Fwd(8, this.fwd3); IMail fwd5 = new Fwd(5, this.fwd8); IMail msgbye = new Send(6, 4, "bye"); IMail fwd7bye = new Fwd(7, this.msgbye); IMail fwd1bye = new Fwd(1, this.fwd7bye); IMail fwd4bye = new Fwd(4, this.fwd1bye); C: [3 points: purpose and header; body; examples/tests] // in the interface IMail: ------------------------- // count how many time this mail has been forwaded int countf(); // in the class Send: ------------------------------ // count how many time this mail has been forwaded int countf(){ return 0; } // in the class Fwd: ------------------------------- // count how many time this mail has been forwaded int countf(){ return 1 + this.packet.countf(); } // in the class Examples: --------------------------- // tests for the method countf boolean testCountf = (check this.msg.countf() expect 0) && (check this.fwd3.countf() expect 2) && (check this.fwd5.countf() expect 4); D: [3 points: purpose/header, body; examples/tests] // in the interface IMail: ------------------------- // produce the text of this mail String msgText(); // in the class Send: ------------------------------ // produce the text of this mail String msgText(){ return this.text; } // in the class Fwd: ------------------------------- // produce the text of this mail String msgText(){ return this.packet.msgText(); } // in the class Examples: --------------------------- // tests for the method msgText boolean testMsgText = (check this.msg.msgText() expect "hello") && (check this.fwd3.msgText() expect "hello") && (check this.fwd5.msgText() expect "hello"); E: [3 points: purpose and header; body; examples/tests] // in the interface IMail: ------------------------- // find the address to which this mail has beend addressed int getAddress(); // in the class Send: ------------------------------ // find the address to which this mail has beend addressed int getAddress(){ return this.address; } // in the class Fwd: ------------------------------- // find the address to which this mail has beend addressed int getAddress(){ return this.packet.getAddress(); } // in the class Examples: --------------------------- // tests for the method getAddress boolean testGetAddress = (check this.msg.getAddress() expect 5) && (check this.fwd3.getAddress() expect 5) && (check this.fwd5.getAddress() expect 5); F: [3 points: purpose and header; body; examples/tests] // in the interface IMail: ------------------------- // has this mail been delivered? boolean delivered(); // in the class Send: ------------------------------ // has this mail been delivered? boolean delivered(){ return false; } // in the class Fwd: ------------------------------- // has this mail been delivered? boolean delivered(){ return this.routing == this.getAddress(); } // in the class Examples: --------------------------- // tests for the method delivered boolean testDelivered = (check this.msg.delivered() expect false) && (check this.fwd3.delivered() expect false) && (check this.fwd5.delivered() expect true); G: [4 points: 1 point for the template for the class Send; 1 point for the fields in the class Fwd --- with their types, 1 point for the methods defined in Fwd - with their types, 1 point for the methods for the field packet in the class Fwd --- with their types] // in the class Send: ------------------------------ /* TEMPLATE: FIELDS: ... this.sender ... -- int ... this.address ... -- int ... this.text ... -- String METHODS: ... this.countf() ... -- int ... this.msgText() ... -- String ... this.getAddress() ... -- int ... this.delivered() ... -- boolean */ // in the class Fwd: ------------------------------- /* TEMPLATE: FIELDS: ... this.routing ... -- int ... this.packet ... -- IMail METHODS: ... this.countf() ... -- int ... this.msgText() ... -- String ... this.getAddress() ... -- int ... this.delivered() ... -- boolean METHODS FOR FIELDS: ... this.packet.countf() ... -- int ... this.packet.msgText() ... -- String ... this.packet.getAddress() ... -- int ... this.packet.delivered() ... -- boolean */ //```````````````````````````````````````````````````````````````` Problem 2: [15 points total] A: [2 points total - 1 point definitions; 1 point examples] // to represent a list of pebbles interface ILoP { } // to represent an empty list of pebbles class MtLoP implements ILoP { MtLoP() {} } // to represent a nonempty list of pebbles class ConsLoP implements ILoP { Pebble first; ILoP rest; ConsLoP(Pebble first, ILoP rest){ this.first = first; this.rest = rest; } } // in the class Examples: ------------------------------------------------- Pebble p5red = new Pebble(5, "red"); Pebble p3red = new Pebble(3, "red"); Pebble p7red = new Pebble(7, "red"); Pebble p8red = new Pebble(8, "red"); Pebble p2blue = new Pebble(2, "blue"); Pebble p6blue = new Pebble(6, "blue"); Pebble p9blue = new Pebble(9, "blue"); Pebble p4blue = new Pebble(4, "blue"); ILoP mt = new MtLoP(); ILoP allred = new ConsLoP(this.p5red, new ConsLoP(this.p3red, new ConsLoP(this.p7red, new ConsLoP(this.p8red, this.mt)))); ILoP mixed = new ConsLoP(this.p5red, new ConsLoP(this.p2blue, new ConsLoP(this.p9blue, new ConsLoP(this.p8red, this.mt)))); ILoP no2blue = new ConsLoP(this.p5red, new ConsLoP(this.p2blue, new ConsLoP(this.p8red, new ConsLoP(this.p9blue, this.mt)))); ILoP redsfirst = new ConsLoP(this.p5red, new ConsLoP(this.p8red, new ConsLoP(this.p2blue, new ConsLoP(this.p9blue, this.mt)))); B: [3 points: purpose and header; body; examples/tests] // in the interface ILoP: ------------------------------------------------- // count the number of pebbles of the given color in this list of pebbles int countColor(String col); // in the class MtLoP: ---------------------------------------------------- // count the number of pebbles of the given color in this list of pebbles int countColor(String col){ return 0; } // in the class ConsLoP: ------------------------------------------------- // count the number of pebbles of the given color in this list of pebbles int countColor(String col){ if (this.first.col.equals(col)){ return 1 + this.rest.countColor(col); } else{ return this.rest.countColor(col); } } // in the class Examples: ------------------------------------------------- // test the method countColor for lists of pebbles boolean testCountColor = (check this.mt.countColor("red") expect 0) && (check this.no2blue.countColor("red") expect 2) && (check this.allred.countColor("red") expect 4); C: [4 points: purpose and header; 2 points body; examples/tests] // in the interface ILoP: ------------------------------------------------- // produce a list of only the red pebbles from this list of pebbles ILoP onlyReds(); // in the class MtLoP: ---------------------------------------------------- // produce a list of only the red pebbles from this list of pebbles ILoP onlyReds(){ return this; } // in the class ConsLoP: ------------------------------------------------- // produce a list of only the red pebbles from this list of pebbles ILoP onlyReds(){ if (this.first.col.equals("red")){ new ConsLoP(this.first, this.rest.onlyReds()); } else{ return this.rest.onlyReds(); } } // in the class Examples: ------------------------------------------------- // test the method onlyReds for lists of pebbles boolean testOnlyReds = (check this.mt.onlyReds() expect this.mt) && (check this.mixed.onlyReds() expect new ConsLoP(this.p5red, new ConsLoP(this.p8red, this.mt))) && (check this.allred.onlyReds() expect this.allred); D: [6 points: purpose and header; body; examples/tests; helper method: purpose and header; body; examples/tests] // in the interface ILoP: ------------------------------------------------- // does this list contain two blue pebbles in a row? boolean twoBlue(); // does this list start with a blue, or contain two blue pebbles in a row? boolean nextBlue(); // in the class MtLoP: ---------------------------------------------------- // does this list contain two blue pebbles in a row? boolean twoBlue(){ return false; } // does this list start with a blue, or contain two blue pebbles in a row? boolean nextBlue(){ return false; } // in the class ConsLoP: ------------------------------------------------- // does this list contain two blue pebbles in a row? boolean twoBlue(){ if (this.first.col.equals("blue")){ return this.rest.nextBlue(); } else{ return this.rest.twoBlue(); } } // does this list start with a blue, or contain two blue pebbles in a row? boolean nextBlue(){ if (this.first.col.equals("blue")){ return true; } else{ return this.rest.twoBlue(); } } // in the class Examples: ------------------------------------------------- // test the method twoBlue for lists of pebbles boolean testTwoBlue = (check this.mt.twoBlue() expect false) && (check this.no2blue.twoBlue() expect false) && (check this.mixed.twoBlue() expect true) && (check this.allred.twoBlue() expect false); // test the method nextBlue for lists of pebbles boolean testNextBlue = (check this.mt.nextBlue() expect false) && (check this.no2blue.nextBlue() expect false) && (check this.mixed.nextBlue() expect true) && (check this.allred.nextBlue() expect false); //```````````````````````````````````````````````````````````````` Problem 3: [22 points total] A: [3 points total - 1 point definitions; 1 point implements for both Flight and ConsLoF, 1 point purpose statements] +-------------+ | Itinerary |<-------+ +-------------+ | / \ | --- | | | ------------------ | | | | | +----------------+ | | | ConsLoF | | | +----------------+ | | +-| Flight first | | | | | Itinerary rest |--+ | | +----------------+ | v +---------------+ +----------+ | Flight | | v +---------------+ | +-----------+ | String origin | | | ClockTime | | String dest | | +-----------+ | ClockTime dep |----+ | int hr | | ClockTime arr |----+ | int min | +---------------+ +-----------+ // to represent the time of departure or arrival on 24 hour clock class ClockTime{ int hr; int min; // to represent a list of flights interface Itinerary { // to represent a one flight itinerary // one leg of multi-leg itinerary, or a direct flight class Flight implements Itinerary{ String origin; String dest; ClockTime dep; ClockTime arr; // to represent an itinerary that consists of several legs of flights class ConsLoF implements Itinerary { Flight first; Itinerary rest; B: [4 points total - 1 point ClockTime examples, 1 point Flight examples, 2 points itineraries examples] // in the class Examples: ------------------------------------------------- ClockTime t9h30m = new ClockTime(9, 30); ClockTime t10h00m = new ClockTime(10, 0); ClockTime t12h00m = new ClockTime(12, 0); ClockTime t13h00m = new ClockTime(13, 0); ClockTime t14h00m = new ClockTime(14, 0); ClockTime t15h00m = new ClockTime(15, 0); ClockTime t16h00m = new ClockTime(16, 0); ClockTime t18h00m = new ClockTime(18, 0); ClockTime t21h00m = new ClockTime(21, 0); ClockTime t20h00m = new ClockTime(20, 0); ClockTime t12h30m = new ClockTime(12, 30); ClockTime t13h30m = new ClockTime(13, 30); ClockTime t14h30m = new ClockTime(14, 30); ClockTime t15h30m = new ClockTime(15, 30); ClockTime t16h30m = new ClockTime(16, 30); ClockTime t23h30m = new ClockTime(23, 30); /* Flights: ORD -> SFO 12:30 - 15:00 ORD -> BOS 13:00 - 15:30 ORD -> DFW 14:00 - 16:30 ORD -> JFK 13:00 - 16:00 ORD -> SFO 12:00 - 14:30 JFK -> DFW 18:00 - 20:00 BOS -> ORD 10:00 - 12:00 DFW -> SFO 21:00 - 23:30 SFO -> DFW 15:00 - 18:00 */ Flight ordSFO = new Flight("ORD", "SFO", this.t12h30m, this.t15h00m); Flight ordBOS = new Flight("ORD", "BOS", this.t13h00m, this.t15h30m); Flight ordDFW = new Flight("ORD", "DFW", this.t14h00m, this.t16h30m); Flight ordJFK = new Flight("ORD", "JFK", this.t13h00m, this.t16h00m); Flight jfkDFW = new Flight("JFK", "DFW", this.t18h00m, this.t20h00m); Flight bosORD = new Flight("BOS", "ORD", this.t10h00m, this.t12h00m); Flight dfwSFO = new Flight("DFW", "SFO", this.t21h00m, this.t23h30m); Flight sfoDFW = new Flight("SFO", "DFW", this.t15h00m, this.t18h00m); /* Itineraries: Boston -> San Francisco: bosORD - ordDFW - dfwSFO JFK -> San Francisco: jfkDFW - dfwSFO Invalid itineraries: bosORD-ordSFO (not enought time between flights) bosORD - dfwSFO (the connecting fight leaves from a different airport) */ Itinerary bosSFOroute = new ConsLoF(this.bosORD, new ConsLoF(this.ordDFW2, dfwSFO)); Itinerary jfkSFOroute = new ConsLoF(this.jfkDFW, dfwSFO); C: [2 points total - 1 point itinerary with not enough time between flights; 1 point itinerary that continues from a different airport] // used for the isValid problem ... // not enough time t change planes Itinerary bosDFW1wrong1 = new ConsLoF(this.bosORD, this.ordDFW1); // deprating from a different airport Itinerary bosSFOwrong2 = new ConsLoF(this.bosORD, this.dfwSFO); D: [16 points total - ] // isValid() method: needs helper that uses the first flight as accumulator // is ValidAcc(Flight firstLeg): needs a helper to determine whether this flight is a connecting flight for the firstLeg: in the class Flight // isConnecting(Flight firstLeg): should have a helper to determine if there is enough time - in the class ClockTime 2 points for identifying all helpers 3 points for the design of isValid 3 points for examples for isValid - true and two false ones 4 points for templates for all classes 4 points for the helper methods: bodies and examples - 2 each ------------------------------------------------------------------------------ // in the class ClockTime: ---------------------------------------------------- // represent this time as minutes since midnight int minutes(){ return this.hr * 60 + this.min; } // is this time at least an hour before the given time? boolean timeEnough(ClockTime that){ return this.minutes() + 60 <= that.minutes(); } // in the class Flight: -------------------------------------------------------- // is this list of flights a valid itinerary? boolean isValid(){ return true; } // is this flight a valid itinerary connecting to the given flight? boolean isValidAfter(Flight inflight){ return inflight.connectingTo(this); } // is the given flight a valid connecting flight to this one? boolean connectingTo(Flight after){ return this.dest.equals(after.origin) && this.arr.timeEnough(after.dep); } //** Note: the methods connectingTo and isValidAfter do the same thing --- //** they have been derived by following the arrows in two different //** contexts // in the class ConsLoF: ------------------------------------------------------ // is this list of flights a valid itinerary? boolean isValid(){ return this.rest.isValidAfter(this.first); } // is this list of flights a valid itinerary connecting to the given flight? boolean isValidAfter(Flight inflight){ return inflight.connectingTo(this.first) && this.rest.isValidAfter(this.first); } // in the class Examples: ------------------------------------------------- // not enough time t change planes Itinerary bosDFW1wrong1 = new ConsLoF(this.bosORD, this.ordSFO); // deprating from a different airport Itinerary bosSFOwrong2 = new ConsLoF(this.bosORD, this.dfwSFO); // test the method minutes in the class ClockTime boolean testMinutes = (check this.t12h00m.minutes() expect 720) && (check this.t13h30m.minutes() expect 810); // test the method timeEnough in the class ClockTime boolean testTimeEnough = (check this.t12h00m.timeEnough(this.t13h30m) expect true) && (check this.t14h30m.timeEnough(t14h00m) expect false) && (check this.t13h30m.timeEnough(t14h30m) expect true) && (check this.t13h30m.timeEnough(t14h00m) expect false); // test the method connectingTo in the class Flight boolean testConnectingTo = (check this.bosORD.connectingTo(this.ordDFW) expect true) && (check this.bosORD.connectingTo(this.ordSFO) expect false) && (check this.bosORD.connectingTo(this.dfwSFO) expect false); // test the method isValidAfter for the list of flights boolean testIsValidAfter = (check this.ordDFW.isValidAfter(this.bosORD) expect true) && (check this.jfkSFOroute.isValidAfter(this.ordJFK) expect true) && (check this.jfkSFOroute.isValidAfter(this.ordDFW) expect false) && (check this.bosSFOroute.isValidAfter(this.ordBOS) expect false); // test the method isValid for the list of flights boolean testIsValid = (check this.ordBOS.isValid() expect true) && (check this.bosSFOroute.isValid() expect true) && (check this.bosDFW1wrong1.isValid() expect false) && (check this.bosSFOwrong2.isValid() expect false);