16(b)
public class Main {
static class TrainCompany {
private String companyName;
private String companyCode;
private int numberOfJourneys;
private Journey[] journeyHistory = new Journey[100000];
public TrainCompany(String x, String y) {
this.companyName = x;
this.companyCode = y;
this.numberOfJourneys = 0;
}
public String getCompanyName() {
return companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
public String getCompanyCode() {
return companyCode;
}
public void setCompanyCode(String companyCode) {
this.companyCode = companyCode;
}
public int getNumberOfJourneys() {
return numberOfJourneys;
}
public Journey getJourney(int x) {
return journeyHistory[x];
}
public void addJourney(Journey j) {
journeyHistory[numberOfJourneys] = j;
numberOfJourneys++;
}
public double averageDelay(){
double sum = 0;
int count = 0;
for(int i=0; i<numberOfJourneys; i++){
if(journeyHistory[i].weatherRelated == false){
sum = sum+journeyHistory[i].getDelay();
count = count+1;
}
}
if(count == 0) return 0;
return sum/count;
}
}
static class Journey {
private String routeCode;
private int delay;
private boolean weatherRelated;
public Journey(String routeCode, int delay, boolean weatherRelated) {
this.routeCode = routeCode;
this.delay = delay;
this.weatherRelated = weatherRelated;
}
// Accessors
public String getRouteCode() {
return routeCode;
}
public int getDelay() {
return delay;
}
public boolean isWeatherRelated() {
return weatherRelated;
}
// Mutators
public void setRouteCode(String routeCode) {
this.routeCode = routeCode;
}
public void setDelay(int delay) {
this.delay = delay;
}
public void setWeatherRelated(boolean weatherRelated) {
this.weatherRelated = weatherRelated;
}
}
public static void main(String[] args) {
TrainCompany[] allCompanies = new TrainCompany[3];
allCompanies[0] = new TrainCompany("Southern", "T290");
allCompanies[1] = new TrainCompany("Northern", "T400");
allCompanies[2] = new TrainCompany("Eastern", "T155");
// Add journeys to companies
Journey s = new Journey("J100", 3, false);
Journey t = new Journey("J103", 8, true);
Journey u = new Journey("J104", 10, true);
allCompanies[0].addJourney(s);
allCompanies[1].addJourney(t);
allCompanies[0].addJourney(u);
allCompanies[0].addJourney(new Journey("J101", 6, false));
// Calculate and print average delay for a specific company (e.g., Southern)
double averageDelay = allCompanies[0].averageDelay();
System.out.println("Average delay for Southern company (excluding weather-related delays): " + averageDelay);
}
}
17(b)
public class Main {
static class TrainCompany {
private String companyName;
private String companyCode;
private int numberOfJourneys;
private Journey[] journeyHistory = new Journey[100000];
public TrainCompany(String x, String y) {
this.companyName = x;
this.companyCode = y;
this.numberOfJourneys = 0;
}
public String getCompanyName() {
return companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
public String getCompanyCode() {
return companyCode;
}
public void setCompanyCode(String companyCode) {
this.companyCode = companyCode;
}
public int getNumberOfJourneys() {
return numberOfJourneys;
}
public Journey getJourney(int x) {
return journeyHistory[x];
}
public void addJourney(Journey j) {
journeyHistory[numberOfJourneys] = j;
numberOfJourneys++;
}
public double averageDelay(){
double sum = 0;
int count = 0;
for(int i=0; i<numberOfJourneys; i++){
if(journeyHistory[i].weatherRelated == false){
sum = sum+journeyHistory[i].getDelay();
count = count+1;
}
}
if(count == 0) return 0;
return sum/count;
}
public String longestDelay(Codes[] c){
String route = "xxx";
int maxDelay = -1;
String maxCode = "";
for(int i=0; i<numberOfJourneys; i++){
if(journeyHistory[i].weatherRelated == false && journeyHistory[i].getDelay() > maxDelay){
maxDelay = journeyHistory[i].getDelay();
maxCode = journeyHistory[i].getRouteCode();
}
}
for(int i=0; i<c.length; i++){
if(c[i].getRouteCode() == maxCode)
route = c[i].routeName;
}
return route;
}
}
static class Journey {
private String routeCode;
private int delay;
private boolean weatherRelated;
public Journey(String routeCode, int delay, boolean weatherRelated) {
this.routeCode = routeCode;
this.delay = delay;
this.weatherRelated = weatherRelated;
}
// Accessors
public String getRouteCode() {
return routeCode;
}
public int getDelay() {
return delay;
}
public boolean isWeatherRelated() {
return weatherRelated;
}
// Mutators
public void setRouteCode(String routeCode) {
this.routeCode = routeCode;
}
public void setDelay(int delay) {
this.delay = delay;
}
public void setWeatherRelated(boolean weatherRelated) {
this.weatherRelated = weatherRelated;
}
}
public static class Codes {
private String routeName; // e.g. New Amsterdam – Diamond City
private String routeCode;
public Codes(String a, String b) {
this.routeName = a;
this.routeCode = b;
}
public String getRouteCode() {
return routeCode;
}
public String getRouteName() {
return routeName;
}
public void setRouteCode(String s){
routeCode = s;
}
}
public static void main(String[] args) {
TrainCompany[] allCompanies = new TrainCompany[3];
allCompanies[0] = new TrainCompany("Southern", "T290");
allCompanies[1] = new TrainCompany("Northern", "T400");
allCompanies[2] = new TrainCompany("Eastern", "T155");
// Add journeys to companies
Journey s = new Journey("J100", 3, false);
Journey t = new Journey("J103", 8, true);
Journey u = new Journey("J104", 10, false);
Journey v = new Journey("J105", 18, false);
allCompanies[0].addJourney(s);
allCompanies[0].addJourney(t);
allCompanies[0].addJourney(u);
allCompanies[0].addJourney(v);
Codes[] allCodes = new Codes[1000];
for (int i = 0; i < allCodes.length; i++) {
// 这里假设填充的数据是样例数据,实际情况应根据需要自行填充
String routeName = "Route " + i; // 样例路线名称
String routeCode = "J" + i; // 样例路线码
allCodes[i] = new Codes(routeName, routeCode);
}
allCodes[100].setRouteCode("J100");
allCodes[104].setRouteCode("J104");
allCodes[105].setRouteCode("J105");
// Test longestDelay method
String longestDelayRoute = allCompanies[0].longestDelay(allCodes);
System.out.println("Route with the longest delay not caused by weather: " + longestDelayRoute);
}
}