IB pastPaper 16N P2

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);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值