2021

Questions

Q1 WordMatch.java

public class WordMatch {
    // instance variable
    private String secret;

    /**
     * Constructor: initializes the secret word
     * @param word the secret word
     */
    public WordMatch(String word) {
        secret = word;
    }

    /**
     * Part (a): Returns a score based on how many times the guess appears in the secret word
     * and how long the guess is.
     * 
     * @param guess the guess word
     * @return the score
     */
    public int scoreGuess(String guess) {
        // TODO: Count number of times guess appears in secret
        // Multiply count by length of guess
        return 0; // placeholder
    }

    /**
     * Part (b): Returns the guess with the highest score
     * If two guesses have the same score, returns the one that comes first alphabetically.
     * 
     * @param options an array of guess words
     * @return the best guess
     */
    public String findBetterGuess(String guess1, String guess2) {
        // TODO: Use scoreGuess to compare the two guesses
        return ""; // placeholder
    }

    // You may include a main method to test your class
    public static void main(String[] args) {
        WordMatch game = new WordMatch("mississippi");
        
        // Example tests (students should add more)
        System.out.println(game.scoreGuess("is")); // expected: 8
        System.out.println(game.scoreGuess("sip")); // expected: 6
        System.out.println(game.findBetterGuess("is", "sip")); // expected: "is"
    }
}

Q2 CombinedTable.java

public class CombinedTable {
    private SingleTable table1;
    private SingleTable table2;

    public CombinedTable(SingleTable t1, SingleTable t2) {
        table1 = t1;
        table2 = t2;
    }

    public boolean canSeat(int num) {
        // TODO: Return true if both tables can seat the given number of guests together
        return false; // Placeholder
    }

    public double getDesirability() {
        // TODO: Compute and return the desirability based on the formula
        return 0.0; // Placeholder
    }
}

Q2 SingleTable.java

public class SingleTable {
    private int seats;
    private double viewQuality;
    private boolean cloth;

    public SingleTable(int s, double vq, boolean c) {
        seats = s;
        viewQuality = vq;
        cloth = c;
    }

    public int getNumSeats() {
        return seats;
    }

    public double getViewQuality() {
        return viewQuality;
    }

    public boolean hasTablecloth() {
        return cloth;
    }
}

Q2 CombinedTableTester.java

public class CombinedTableTester {
    public static void main(String[] args) {
        // Create two SingleTable objects
        SingleTable table1 = new SingleTable(4, 7.5, true);
        SingleTable table2 = new SingleTable(6, 6.0, false);

        // Create a CombinedTable with table1 and table2
        CombinedTable combined = new CombinedTable(table1, table2);

        // Test canSeat
        System.out.println("Can seat 9 guests: " + combined.canSeat(9)); // Expected: true
        System.out.println("Can seat 11 guests: " + combined.canSeat(11)); // Expected: false

        // Test getDesirability
        System.out.println("Desirability: " + combined.getDesirability());
        // Expected: average of 7.5 and 6.0 = 6.75 (no tablecloth on both, so not multiplied)
    }
}

Q3 MemberInfo.java

import java.util.ArrayList;

public class MemberInfo {
    private String name;
    private int gradYear;
    private boolean hasGoodStanding;

    /** Constructs a MemberInfo object for the club member with name, gradYear, and hasGoodStanding */
    public MemberInfo(String name, int gradYear, boolean hasGoodStanding) {
        this.name = name;
        this.gradYear = gradYear;
        this.hasGoodStanding = hasGoodStanding;
    }

    /** Returns the graduation year of the club member. */
    public int getGradYear() {
        return gradYear;
    }

    /** Returns true if the member is in good standing and false otherwise. */
    public boolean inGoodStanding() {
        return hasGoodStanding;
    }

    /** Optional: Returns the name of the member (useful for debugging or display) */
    public String toString() {
        return name + " (" + gradYear + ") - Good Standing: " + hasGoodStanding;
    }
}

Q3 ClubMembers.java

public class ClubMembers {
    private ArrayList<MemberInfo> memberList;

    /** Constructs an empty list of members */
    public ClubMembers() {
        memberList = new ArrayList<MemberInfo>();
    }

    /**
     * Adds new club members to memberList, as described in part (a).
     * Precondition: names is a non-empty array.
     */
    public void addMembers(String[] names, int gradYear) {
        // To be implemented
    }

    /**
     * Removes members who have graduated and returns a list of members who have
     * graduated and are in good standing, as described in part (b).
     */
    public ArrayList<MemberInfo> removeMembers(int year) {
        // To be implemented
        return null;
    }

    /** Optional: View members for testing */
    public void printMembers() {
        for (MemberInfo m : memberList) {
            System.out.println(m);
        }
    }
}

Q3 ClubTester.java

public class ClubTester {
    public static void main(String[] args) {
        ClubMembers club = new ClubMembers();

        // Example addMembers call (to test part a when implemented)
        String[] newNames = {"Alice Brown", "Carlos Diaz"};
        club.addMembers(newNames, 2024);

        // Add more members manually
        club.addMembers(new String[]{"Emily Stone"}, 2022);

        // Test print
        System.out.println("Before removal:");
        club.printMembers();

        // Test removeMembers call (to test part b when implemented)
        ArrayList<MemberInfo> graduated = club.removeMembers(2023);

        System.out.println("\nAfter removal:");
        club.printMembers();

        System.out.println("\nGraduated and in good standing:");
        for (MemberInfo m : graduated) {
            System.out.println(m);
        }
    }
}

Q4 ArrayResizer.java

public class ArrayResizer {

    /** Returns true if and only if every value in row r of array2D is non-zero.
     *  Precondition: r is a valid row index in array2D.
     *  Postcondition: array2D is unchanged.
     */
    public static boolean isNonZeroRow(int[][] array2D, int r) {
        // To be implemented in part (a)
        return false;
    }

    /** Returns the number of rows in array2D that contain all non-zero values.
     *  Postcondition: array2D is unchanged.
     */
    public static int numNonZeroRows(int[][] array2D) {
        int count = 0;
        for (int r = 0; r < array2D.length; r++) {
            if (isNonZeroRow(array2D, r)) {
                count++;
            }
        }
        return count;
    }

    /** Returns a new, possibly smaller, two-dimensional array that contains only rows
     *  from array2D with no zeros, as described in part (b).
     *  Precondition: array2D contains at least one column and at least one row with no zeros.
     *  Postcondition: array2D is unchanged.
     */
    public static int[][] resize(int[][] array2D) {
        // To be implemented in part (b)
        return null;
    }
}

ArrayResizerTester.java

public class ArrayResizerTester {
    public static void main(String[] args) {
        int[][] arr = {
            {2, 1, 0},
            {1, 3, 2},
            {0, 0, 0},
            {4, 5, 6}
        };

        System.out.println("isNonZeroRow(arr, 0): " + ArrayResizer.isNonZeroRow(arr, 0)); // false
        System.out.println("isNonZeroRow(arr, 1): " + ArrayResizer.isNonZeroRow(arr, 1)); // true
        System.out.println("isNonZeroRow(arr, 2): " + ArrayResizer.isNonZeroRow(arr, 2)); // false
        System.out.println("isNonZeroRow(arr, 3): " + ArrayResizer.isNonZeroRow(arr, 3)); // true

        int[][] smaller = ArrayResizer.resize(arr);
        System.out.println("Resized array:");
        if (smaller != null) {
            for (int[] row : smaller) {
                for (int val : row) {
                    System.out.print(val + " ");
                }
                System.out.println();
            }
        } else {
            System.out.println("Returned null.");
        }
    }
}

Scoring Guidelines

Chief Reader Report

Scoring Statistics

Scoring Distribution

Samples and Commentary

Sample Responses Q1

Sample Responses Q2

Sample Responses Q3

Sample Responses Q4

Last updated

Was this helpful?