Computer Systems and Elements of Programming Java programming: Lecture 6 - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

Computer Systems and Elements of Programming Java programming: Lecture 6

Description:

Edinburgh London 2005 11 20 07 05 2005 11 20 12 05 5 50 ... request is for the coach that leaves Edinburgh (to London) at 7:05 am on 20/11/05. ... – PowerPoint PPT presentation

Number of Views:22
Avg rating:3.0/5.0
Slides: 28
Provided by: dcsB
Category:

less

Transcript and Presenter's Notes

Title: Computer Systems and Elements of Programming Java programming: Lecture 6


1
Computer Systems
and Elements of Programming Java programming
Lecture 6
  • Dr Niki Trigoni
  • Department of Computer Science
  • and Information Systems
  • Birkbeck College, University of London
  • Email niki_at_dcs.bbk.ac.uk
  • Web Page http//www.dcs.bbk.ac.uk/niki

2
Review of lecture 5
  • If an array is defined to store objects of a
    class C, we can store in it objects of C or of
    any class that inherits from C.
  • Java decides dynamically how to implement a
    method invocation on an object. The
    implementation selected depends on the specific
    class that the object is instance of.
  • It is possible to pass user values to a Java
    application through command-line arguments of the
    main method
  • The classes BufferedReader, FileReader and
    FileWriter can be used to read and write files.
  • The class String includes many methods that allow
    us to handle String objects like equals, concat,
    etc.

3
Overview of lecture 6
  • The TravelAgent case study
  • Say that we would like to develop a travel
    agent system that enables customers to book coach
    tickets.
  • Problem description
  • Which classes should we use?
  • Which fields and methods should each class have?
  • What should the main method do to start the
    application?

4
Problem description
  • We are given an input file of scheduled routes,
    which has the format
  • ltfromCitygt lttoCitygt ltdepartYeargt ltdepartMonthgt
    ltdepartDaygt ltdepartHoursgt ltdepartMinutesgt
    ltarrivalYeargt ltarrivalMonthgt ltarrivalDaygt
    ltarrivalHoursgt ltarrivalMinutesgt ltduration pricegt
  • For example
  • Edinburgh London 2005 11 20 07 05 2005 11 20 12
    05 5 50
  • Edinburgh London 2005 11 21 07 05 2005 11 20 12
    05 5 50
  • Edinburgh London 2005 11 22 07 05 2005 11 20 12
    05 5 60
  • Edinburgh London 2005 11 23 07 05 2005 11 20 11
    05 4 70
  • London Oxford 2005 11 25 14 00 2005 11 25 16 00
    2 15
  • London Oxford 2005 11 26 14 00 2005 11 26 17 00
    3 10
  • London Oxford 2005 11 27 14 00 2005 11 27 15 00
    1 20
  • London Oxford 2005 11 28 14 00 2005 11 28 16 00
    2 15
  • Glasgow Birmingham 2005 12 25 12 00 2005 12 25
    16 00 4 40
  • Glasgow Birmingham 2005 12 26 12 00 2005 12 26
    15 00 3 50
  • Comments
  • For example, the first line denotes that there
    is coach from Edinburgh to London, that leaves at
    705 am on 20/11/05 and arrives at 1205 am on
    20/11/05, the duration of the trip is 5 hours,
    and the ticket costs 50 pounds.

5
Problem description
  • We are given a second file of customer requests,
    which has lines of the form
  • specific ltfromCitygt lttoCitygt ltdepartYeargt
    ltdepartMonthgt ltdepartDaygt ltdepartHoursgt
    ltdepartMinutesgt
  • cheapest ltfromCitygt lttoCitygt ltdepartYeargt
    ltdepartMonthgt ltdepartDaygt ltdepartHoursgt
    ltdepartMinutesgt ltarrivalYeargt ltarrivalMonthgt
    ltarrivalDaygt ltarrivalHoursgt ltarrivalMinutesgt
    ltmaxDurationgt
  • shortest ltfromCitygt lttoCitygt ltdepartYeargt
    ltdepartMonthgt ltdepartDaygt ltdepartHoursgt
    ltdepartMinutesgt ltarrivalYeargt ltarrivalMonthgt
    ltarrivalDaygt ltarrivalHoursgt ltarrivalMinutesgt
    ltmaxPricegt
  • For example
  • specific Edinburgh London 2005 11 20 07 05
  • cheapest London Oxford 2005 11 25 13 00 2005 11
    30 13 00 2
  • shortest Glasgow Birmingham 2005 12 25 12 00
    2005 12 31 12 00 60
  • Comments
  • The first request is for the coach that leaves
    Edinburgh (to London) at 705 am on 20/11/05.
  • Request 2 is for the cheapest coach route that
    departs from London at 1300 (on 25/11/05) at the
    earliest, and arrives in Oxford at 1300 (on
    30/11/05) at the latest, and does not take more
    than 2 hours.
  • Request 3 is for the shortest coach route that
    departs from Glasgow at noon (on 25/12/05) at the
    earliest and arrives in Birmingham at noon (on
    31/12/05) and costs less than 60 pounds.

6
Identifying classes and attributes
RouteRequest Route route
SpecificRequest DateTime departureTime
FlexibleRouteRequest DateTime earliestDepartureTi
me DateTime latestArrivalTime
CheapestRouteRequest int maxDuration
ShortestRouteRequest int maxPrice
7
The Route and RouteRequest classes
public class Route private String from
private String to public Route (String
inputFrom, String inputTo) from
new String(inputFrom) to new
String(inputTo) public String getFrom
() return from public
String getTo () return to
public boolean equals (Route otherRoute)
if (from.equals(otherRoute.from)
to.equals(otherRoute.to)) return
true return false public void
print() System.out.print(from"
"to" ")
public class RouteRequest private Route
route public RouteRequest (Route inRoute)
route inRoute
public Route getRoute () return
route public void print()
route.print()
8
The SpecificRouteRequest class
public class DateTime private int year
private int month private int day
private int hours private int minutes
public DateTime (int inYear, int inMonth, int
inDay, int inHours, int inMinutes)
year inYear month inMonth
day inDay hours inHours
minutes inMinutes public boolean
equals (DateTime dt) public
boolean greaterEqual (DateTime dt)
public boolean lessEqual (DateTime dt)
public void print() System.out.print(year
" "month" "day" " hours " " minutes)

public class SpecificRouteRequest extends
RouteRequest private DateTime
departureTime public SpecificRouteRequest
(Route inRoute, DateTime inDepartureTime)
super(inRoute) departureTime
inDepartureTime public DateTime
getDepartureTime () return
departureTime public void
print () System.out.print("Specific
") super.print()
departureTime.print() System.out.println(
)
9
The FlexibleRouteRequest class
public class FlexibleRouteRequest extends
RouteRequest private DateTime
earliestDepartureTime private DateTime
latestArrivalTime public
FlexibleRouteRequest (Route inRoute, DateTime
inEarliestDepTime,
DateTime inLatestArrTime)
super (inRoute)
earliestDepartureTime inEarliestDepTime
latestArrivalTime inLatestArrTime
public DateTime getEarliestDepartureTime ()
return earliestDepartureTime
public DateTime getLatestArrivalTime ()
return latestArrivalTime
public void print ()
super.print() System.out.print(
" ") earliestDepartureTime.print()
System.out.print(" ")
latestArrivalTime.print()
10
The CheapestRouteRequest class
public class CheapestRouteRequest extends
FlexibleRouteRequest private int
maxDuration public CheapestRouteRequest
(Route inRoute, DateTime inEarliestDepTime,
DateTime
inLatestArrTime, int inMaxDuration )
super (inRoute, inEarliestDepTime,
inLatestArrTime) maxDuration
inMaxDuration public int
getMaxDuration () return
maxDuration public void print ()
System.out.print("Cheapest ")
super.print() System.out.println("
"maxDuration)
11
The ShortestRouteRequest class
public class ShortestRouteRequest extends
FlexibleRouteRequest private int maxPrice
public ShortestRouteRequest (Route
inRoute, DateTime inEarliestDepTime,
DateTime inLatestArrTime,
int inMaxPrice ) super (inRoute,
inEarliestDepTime, inLatestArrTime)
maxPrice inMaxPrice public
int getMaxPrice () return maxPrice
public void print ()
System.out.print("Shortest ")
super.print() System.out.println ("
"maxPrice)
12
The RouteSchedule class
RouteSchedule Route route DateTime
departureTime DateTime arrivalTime int
duration int price Route getRoute
() DateTime getDepartureTime () DateTime
getArrivalTime () int getDuration () int getPrice
() void print()
13
The RouteScheduleDirectory class
RouteScheduleDirectory RouteSchedule
routeSchedules int noOfSchedules void add
(RouteSchedule newRouteSchedule) RouteSchedule
getMatchingSchedule (RouteRequest req)
14
The RoutingScheduleDirectory class
public class RoutingScheduleDirectory //
we must add the method getMatchingSchedule
public RouteSchedule getMatchingSchedule
(RouteRequest request)
RouteSchedule resultSchedule null int
i for (i0 iltnoOfRouteSchedules
i) if (request.matchesFirstSche
duleBetterThanSecond (routeSchedulesi,
resultSchedule)) resultSchedule
routeSchedulesi return
resultSchedule // The method
matchesFirstScheduleBetterThanSecond is defined
in RouteRequest // and it is overriden in the
subclasses SpecificRouteRequest,
CheapestRouteRequest // and ShortestRouteRequest
(for details see the code at //
www.dcs.bbk.ac.uk/niki/CSEP.htm under Lecture 6
code)
15
The Booking class
Booking RouteRequest request RouteSchedule
schedule void print ()
16
The BookingDirectory class
BookingDirectory Booking bookings int
noOfBookings void add ( Booking newBooking
) void print ()
17
The TravelAgent class
TravelAgent RouteScheduleDirectory
scheduleDir BookingDirectory bookingDir
void loadScheduledRoutes (String
routeSchedulesFile) void loadRequestsAndMakeBo
okings (String customerRequestsFile) void
printResultingBookings ()
18
The TravelAgent class
public class TravelAgent public void
loadScheduledRoutes (String routeSchedulesFile)
try
System.out.println("SCHEDULES ")
FileReader fileReader new FileReader
(routeSchedulesFile) BufferedReader
reader new BufferedReader (fileReader)
String line reader.readLine()
while (line ! null)
System.out.println (line)
processRouteScheduleLine (line)
line reader.readLine()
reader.close() catch
(Exception e)
System.out.println("Exception " e.toString() )

19
The TravelAgent class
public class TravelAgent public void
processRouteScheduleLine (String line)
String wordsInLine line.split(" ")
Route route new Route (wordsInLine0,
wordsInLine1) DateTime depTime new
DateTime (Integer.parseInt(wordsInLine2), In
teger.parseInt(wordsInLine3), Integer.parseI
nt(wordsInLine4), Integer.parseInt(wordsInLi
ne5), Integer.parseInt(wordsInLine6) )
DateTime arrTime new DateTime
(Integer.parseInt(wordsInLine7), Integer.par
seInt(wordsInLine8), Integer.parseInt(wordsI
nLine9), Integer.parseInt(wordsInLine10),
Integer.parseInt(wordsInLine11) )
int tripDuration Integer.parseInt(wordsInLine1
2) int tripPrice Integer.parseInt(wor
dsInLine13) RouteSchedule
schedule new RouteSchedule (route, depTime,
arrTime,
tripDuration, tripPrice)
scheduleDir.add (schedule)
20
The TravelAgent class
TravelAgent RouteScheduleDirectory
scheduleDir BookingDirectory bookingDir
void loadScheduledRoutes (String
routeSchedulesFile) void loadRequestsAndMakeBo
okings (String customerRequestsFile) void
printResultingBookings ()
21
The TravelAgent class
public class TravelAgent public void
loadRequestsAndMakeBookings (String
customerRequestsFile) try
System.out.println("REQUESTS
") FileReader fileReader new
FileReader (customerRequestsFile)
BufferedReader reader new BufferedReader
(fileReader) String line
reader.readLine() while (line !
null)
System.out.println (line)
processRequestLine (line) line
reader.readLine()
reader.close() catch
(Exception e)
System.out.println("Exception " e.toString() )

22
The TravelAgent class
public class TravelAgent public void
processRequestLine (String line)
String wordsInLine line.split(" ")
RouteRequest request if
(wordsInLine0.equals("specific"))
request new SpecificRouteRequest (
(new Route(wordsInLine1,
wordsInLine2)), (new
DateTime(Integer.parseInt (wordsInLine3),
Integer.parseInt (wordsInLine4),
Integer.parseInt
(wordsInLine5),
Integer.parseInt (wordsInLine6),
Integer.parseInt (wordsInLine7))))
else // continue code in next page

23
The TravelAgent class
public class TravelAgent public void
processRequestLine (String line) //
continuing code from previous page else
if (wordsInLine0.equals("cheapest"))
request new CheapestRouteReques
t ( (new Route(wordsInLine1,
wordsInLine2)), (new
DateTime(Integer.parseInt (wordsInLine3),
Integer.parseInt
(wordsInLine4),
Integer.parseInt (wordsInLine5),
Integer.parseInt (wordsInLine6),
Integer.parseInt
(wordsInLine7))), (new
DateTime(Integer.parseInt (wordsInLine8),
Integer.parseInt
(wordsInLine9),
Integer.parseInt (wordsInLine10),
Integer.parseInt (wordsInLine11),
Integer.parseInt
(wordsInLine12))),
Integer.parseInt (wordsInLine13))
else // continue code in next page
24
The TravelAgent class
public class TravelAgent public void
processRequestLine (String line) //
continuing code from previous page else
if (wordsInLine0.equals("shortest"))
request new ShortestRouteReques
t ( (new Route(wordsInLine1,
wordsInLine2)), (new
DateTime(Integer.parseInt (wordsInLine3),
Integer.parseInt
(wordsInLine4),
Integer.parseInt (wordsInLine5),
Integer.parseInt (wordsInLine6),
Integer.parseInt
(wordsInLine7))), (new
DateTime(Integer.parseInt (wordsInLine8),
Integer.parseInt
(wordsInLine9),
Integer.parseInt (wordsInLine10),
Integer.parseInt (wordsInLine11),
Integer.parseInt
(wordsInLine12))),
Integer.parseInt (wordsInLine13))
else // continue code in next page

25
The TravelAgent class
public class TravelAgent public void
processRequestLine (String line) //
continuing code from previous page else
System.out.println("A request
line must start with either of the words")
System.out.println("specific cheapest
shortest") return
RouteSchedule matchingSchedule
scheduleDir.getMatchingSchedule (request)
if (matchingSchedule ! null)
Booking booking new Booking (request,
matchingSchedule) bookingDir.add
(booking)
26
The TravelAgentManager class
public class TravelAgentManager public
static void main (String argv)
if (argv.length ! 2)
System.out.println("TravelAgentManager takes two
parameters")
System.out.println("1. the name of the input file
with the scheduled routes")
System.out.println("2. the name of the input file
with the customer requests")
return String
inputScheduledRoutesFile argv0
String inputCustomerRequestsFile argv1
TravelAgent agent new TravelAgent()
agent.loadScheduledRoutes
(inputScheduledRoutesFile)
agent.loadRequestsAndMakeBookings
(inputCustomerRequestsFile)
agent.printResultingBookings () return

27
Summary of lecture 6
  • The TravelAgent case study included examples of
  • fields with class types
  • fields with array types
  • if, while and for statements
  • array handling
  • inheritance
  • string handling
  • method overriding and dynamic method lookup
  • The complete code and examples of input files
    can be found in www.dcs.bbk.ac.uk/niki/CSEP.htm
    if you click on Lecture 6 code.
Write a Comment
User Comments (0)
About PowerShow.com