Implementing ArrayList Algorithms FRQ Practice

Responses receive AI feedback against a rubric after submission. Scores are practice estimates.

Question 1 of 5

Crate is a student-defined class with getter getWeight. Write lightest so it returns the smallest weight in crates. Assume crates is not empty. Use ArrayList methods size and get only.

public class Crate {
    private int weight;
    private String label;
    public Crate(int weight, String label) {
        this.weight = weight;
        this.label = label;
    }
    public int getWeight() {
        return weight;
    }
    public String getLabel() {
        return label;
    }
}

public int lightest(ArrayList<Crate> crates) {
    /* write this method */
}

Question 2 of 5

Write insertSorted so value is placed into nums, which is already in increasing order, and the list stays sorted. Use add(index, obj) once. Do not use extra lists.

public void insertSorted(ArrayList<Integer> nums, int value) {
    /* write this method */
}

Question 3 of 5

Two parallel lists store rider names and lap points. Write pointsFor so it returns the sum of points for every entry whose name equals target. Assume names and points have the same size. Traverse them together with one index.

public int pointsFor(ArrayList<String> names, ArrayList<Integer> points, String target) {
    /* write this method */
}

Question 4 of 5

Complete the missing rotate-right results. Then write the missing loop that uses set to shift values right and place last at index 0.

ArrayList<String> bay = new ArrayList<String>();
bay.add("inlet");
bay.add("cove");
bay.add("spit");
String last = bay.get(bay.size() - 1);
/* missing shift loop */
bay.set(0, last);
Step bay
before rotate [inlet, cove, spit]
after shift, before set(0, last)
after set(0, last)

Question 5 of 5

Write dropLight so every Crate whose getWeight() is less than 10 is removed from crates. Adjust the loop index after each remove so neighboring light crates are not skipped. Use remove(index).

public class Crate {
    private int weight;
    public Crate(int weight) {
        this.weight = weight;
    }
    public int getWeight() {
        return weight;
    }
}

public void dropLight(ArrayList<Crate> crates) {
    /* write this method */
}