Logout

November 2016 Train Routes

TrainCompany.java
1    package november2016_train_routes; 
2     
3    public class TrainCompany 
4    { 
5        private String companyName; 
6        private String companyCode; 
7        private int numberOfJourneys; 
8        private Journey[] journeyHistory = new Journey[100000]; 
9     
10       public TrainCompany(String x, String y) 
11       { 
12           this.companyName = x; 
13           this.companyCode = y; 
14           this.numberOfJourneys = 0; 
15       } 
16       public TrainCompany(){ 
17    
18       } 
19    
20       public String getCompanyName() { 
21           return companyName; 
22       } 
23    
24       public int getNumberOfJourneys() { 
25           return numberOfJourneys; 
26       } 
27    
28       public String getCompanyCode() { 
29           return companyCode; 
30       } 
31    
32       // accessor and mutator methods 
33    
34       public Journey getJourney(int x) 
35       { 
36           return journeyHistory[x]; 
37       } 
38    
39       public void addJourney(Journey j) 
40       { 
41           journeyHistory[numberOfJourneys] = j; 
42           numberOfJourneys++; 
43       } 
44    
45       public double averageDelay() 
46       {   //code missing 
47           return -999.9;//just to get rid of the return red squigglies 
48       } 
49    
50       // returns the average delay for all of a company’s journeys 
51       public String longestDelay(Codes[] c) 
52       {   //code missing 
53           return "";//just to get rid of the return red squigglies 
54       } 
55       // returns the route name for the journey with the longest delay 
56       public String toString(Codes[] c) 
57       {   //code missing 
58           return ""; 
59       } 
60   } 
61   
Journey.java
1    package november2016_train_routes; 
2     
3    public class Journey 
4    { 
5        private String routeCode; 
6        // A unique identifier for a particular route 
7        private double delay; 
8        // Minutes late in arriving at the destination 
9        private boolean weatherRelated; 
10       // Equals true if the journey is affected by the weather, 
11       // otherwise false. 
12       // Constructor which initializes all 3 of the above data items 
13       // accessor and mutator methods. 
14    
15       public Journey(String routeCode, double delay, boolean weatherRelated){ 
16           this.routeCode = routeCode; 
17           this.delay = delay; 
18           this.weatherRelated = weatherRelated; 
19       } 
20    
21       public double getDelay() { 
22           return delay; 
23       } 
24    
25       public String getRouteCode() { 
26           return routeCode; 
27       } 
28    
29       public boolean getWeatherRelated(){ 
30           return weatherRelated; 
31       } 
32   } 
33   
Codes.java
1    package november2016_train_routes; 
2     
3    public class Codes 
4    { 
5        private String routeName; // e.g. New Amsterdam – Diamond City 
6        private String routeCode; 
7     
8        public Codes(String a, String b) 
9        { 
10           this.routeName = a; 
11           this.routeCode = b; 
12       } 
13    
14       public String getRouteCode() 
15       { 
16           return routeCode; 
17       } 
18    
19       public String getRouteName() 
20       { 
21           return routeName; 
22       } 
23   } 
24   
MainForTrainRoutes.java
1    package november2016_train_routes; 
2     
3    public class MainForTrainRoutes { 
4        public static void main(String[] args) { 
5            TrainCompany[] allCompanies = new TrainCompany[3]; 
6            allCompanies[0] = new TrainCompany("Southern","T290"); 
7            allCompanies[1] = new TrainCompany("Northern","T400"); 
8            allCompanies[2] = new TrainCompany("Eastern","T155"); 
9            Journey s = new Journey("J100",3, false); 
10           Journey t = new Journey("J103",8, true); 
11           Journey u = new Journey("J104",10, true); 
12           allCompanies[0].addJourney(s); 
13           allCompanies[1].addJourney(t); 
14           allCompanies[0].addJourney(u); 
15           allCompanies[0].addJourney(new Journey("J101",6, false)); 
16           System.out.println(allCompanies[0].getCompanyCode()); 
17           System.out.println(allCompanies[0].getJourney(1).getDelay()); 
18           System.out.println(allCompanies[1].getNumberOfJourneys()); 
19       } 
20   } 
21