AP Computer Science A – College Board | 美国高中计算机考试 | Java代考

import java.lang.Math;

// q1 a

public int getNextProductWeight() {

    double prob = Math.random();

    if (prob < 0.2) {

        return baseWeight;

    } else if (prob < 0.5) {

        return baseWeight + 1;

    } else {

        return baseWeight + 2;

    }

}

// q1 b

public int packBoxes(int numProducts, int maxBoxWeight) {

    int boxNum = 0;

    int currentWeight = 0;

    for (int i = 1; i <= numProducts; i++) {

        int w = getNextProductWeight();

        currentWeight += w;

        if (currentWeight > maxBoxWeight) {

            boxNum++;

            currentWeight = w;

        }

        if (i == numProducts && currentWeight <= maxBoxWeight) {

            boxNum++;

        }

    }

    return boxNum;

}

// q2

public class EnhancedRelatedWord extends RelatedWord {

    public EnhancedRelatedWord(String bw) {

        super(bw);

    }

    public boolean isRelated(String str) {

        if (super.isRelated(str)) {

            return true;

        } else {

            String bw = super.getBaseWord();

            int idx1 = bw.length();

            int idx2 = str.length();

            if (bw.substring(0,2).equals(str.substring(0,2)) && bw.substring(idx1-2).equals(str.substring(idx2-2))) {

                return true;

            } else {

                return false;

            }

        }

    }

}

// q3 a

public Bracket(String[] competitorNames) {

    competitorList = new ArrayList<Competitor>();

    for (int i = 0; i < competitorNames.length; i++) {

        String name = competitorNames[i];

        Competitor c = new Competitor(name, i+1);

        competitorList.add(c);

    }

}

// q3 b

public ArrayList<Match> buildMatches() {

    ArrayList<Match> matches = new ArrayList<Match>();

    int start, pairNum;

    if (competitorList.size() % 2 == 1) {

        start = 1;

        pairNum = (competitorList.size() – 1) / 2;

    } else {

        start = 0;

        pairNum = competitorList.size() / 2;

    }

    for (int i = start; i < start + pairNum; i++) {

        Competitor c1 = competitorList.get(i);

        Competotor c2 = competitorList.get(competitorList.size() – 1 – i);

        Match m = new Match(c1, c2);

        matches.add(m);

    }

    return matches;

}

// q4 a

public int findSpotInRow(int r) {

    Car[] spots = carsInLot[r];

    for (int col = 0; col < spots.length; col++) {

        if (spots[col] == null) {

            return col;

        }

    }

    return -1;

}

// q4 b

public void parkCars(ArrayList<Car> carList) {

    while (carList.size() > 0) {

        Car car = carList.get(0);

        for(int j = 0; j < carsInLot.size(); j++) {

            int col = findSpotInRow(j);

            if (col > -1) {

                carList.remove(0);

                carsInLot[j][col] = car;

                break;

            }

        }

        if (lotFull()) {

            break;

        }

    }

}