2D Array

public class Item {
    private String name;
    private int value;

    public Item(String itemName, int itemValue) {
        name = itemName;
        value = itemValue;
    }

    public String getName() {
        return name;
    }

    public int getValue() {
        return value;   
    }
}
public class ItemGrid {
    private Item[][] grid;

    public ItemGrid() {
        grid = new Item[3][4];

        grid[0][0] = new Item("acorn", 7);
        grid[0][1] = new Item("book", 10);
        grid[0][2] = new Item("carrot", 8);
        grid[0][3] = new Item("desk", 9);
        grid[1][0] = new Item("egg", 5);
        grid[1][1] = new Item("flag", 8);
        grid[1][2] = new Item("globe", 8);
        grid[1][3] = new Item("harp", 9);
        grid[2][0] = new Item("island", 7);
        grid[2][1] = new Item("jacket", 19);
        grid[2][2] = new Item("kale", 8);
        grid[2][3] = new Item("lunch", 16);
    }

    public boolean isValid(int xPos, int yPos) {
        try {
            Item item = grid[xPos][yPos];
            return true;
        } catch(ArrayIndexOutOfBoundsException e) {
            return false;
        }
    }

    public String mostValuableNeighbor(int r, int c) {
    
        int middle = grid[r][c].getValue(); 

        if (isValid(r, c - 1) && isValid(r, c + 1)) {
            int left = grid[r][c - 1].getValue();
            int right = grid[r][c + 1].getValue();
            if (middle >= left && middle >= right) {
                return grid[r][c].getName();
            }
            if (left >= middle && left >= right) {
                return grid[r][c - 1].getName();
            }
            if (right >= middle && right >= left) {
                return grid[r][c + 1].getName();
            }
        }
        if (!isValid(r, c - 1)) {
            int right = grid[r][c+1].getValue();
            if (middle >= right) {
                return grid[r][c].getName();
            } else {
                return grid[r][c + 1].getName();
            }
        }
        if (!isValid(r, c+1)) {
            int left = grid[r][c - 1].getValue();
            if (middle >= left) {
                return grid[r][c].getName();
            } else {
                return grid[r][c - 1].getName();
            }
        }
        return null;
    }


    public double findAverage() {
        int sum = 0;
        int count = 0;
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                sum += grid[i][j].getValue();
                count++;
            }
        }
        //System.out.println((double)sum/(double)count);
        return (double)sum/(double)count;
    }

    public static void main(String[] args) {
        ItemGrid ig = new ItemGrid();
        System.out.println(ig.mostValuableNeighbor(0, 2));
        System.out.println(ig.mostValuableNeighbor(1, 1));
        System.out.println(ig.mostValuableNeighbor(2, 0));
        System.out.println(ig.mostValuableNeighbor(2, 3));

        System.out.println("Average: " + ig.findAverage());
    }
}    
ItemGrid.main(null)
book
flag
jacket
lunch
Average: 9.5

Arrays, ArrayLists

public class CarRepair {
    private int mechanicNum;
    private int bayNum;

    public CarRepair(int m, int b) {
        mechanicNum = m;
        bayNum = b;
    }

    public int getMechanicNum() {
         return mechanicNum; 
    }

    public int getBayNum() { 
        return bayNum; 
    }

// There may be other instance variables, constructors, and methods not shown.

}
public class RepairSchedule {
    /** Each element represents a repair by an individual mechanic in a bay. */
    private ArrayList<CarRepair> schedule;

    /** Number of mechanics available in this schedule. */
    private int numberOfMechanics;

    /** Constructs a RepairSchedule object.
    * Precondition: n >= 0
    */

    public RepairSchedule(int n) {
        schedule = new ArrayList<CarRepair>();
        numberOfMechanics = n;
    }
    /** Attempts to schedule a repair by a given mechanic in a given bay as described in part (a).
    * Precondition: 0 <= m < numberOfMechanics and b >= 0
    */

    public boolean addRepair(int m, int b) {
    /* to be implemented in part (a) */
        boolean available = true;
        for (CarRepair car : schedule) {
            if (m == car.getMechanicNum()) {
                available = false;
            }
            if (b == car.getBayNum()) {
                available = false;
            }
        }

        if (available) {
            schedule.add(new CarRepair(m, b));
        }
        return available;
    }
    /** Returns an ArrayList containing the mechanic identifiers of all available mechanics,

    * as described in part (b).

    */

    public ArrayList<Integer> availableMechanics() {
        ArrayList<Integer> mechanic = new ArrayList<Integer>();
        for (int i = 0; i <= numberOfMechanics; i++) {
            Integer num = new Integer(i);
            mechanic.add(num);
        }

        for (CarRepair car : schedule) {
            //Integer removeNum = new Integer(car.getMechanicNum());
            int removeNum = car.getMechanicNum();
            for (int i = mechanic.size() - 1; i >= 0; i--) {
                if (mechanic.get(i).intValue() == removeNum) {
                    mechanic.remove(i);
                }
            }
        }

        return mechanic;
    }

    /** Removes an element from schedule when a repair is complete. */

    /* 
    public void carOut(int b) {

    }
    */

    public static void main(String[] args) {
        RepairSchedule schedule1 = new RepairSchedule(5);
        System.out.println(schedule1.addRepair(0, 0));
        System.out.println(schedule1.addRepair(0, 3));
        System.out.println(schedule1.addRepair(1, 3));
        System.out.println(schedule1.availableMechanics());
    }

}
RepairSchedule.main(null);
true
false
true
[2, 3, 4, 5]

Class

public class AdditionPattern {
    private int current;
    private int add;

    public AdditionPattern(int current, int add) {
        this.current = current;
        this.add = add;
    }

    public int currentNumber() {
        return current;
    }

    public void next() {
        current += add;
    }

    public void prev() {
        if (current - add >= 0) {
            current -= add;
        }
    }

    public static void main(String[] args) {
        AdditionPattern plus3 = new AdditionPattern(2, 3);
        System.out.println(plus3.currentNumber());
        plus3.next();
        System.out.println(plus3.currentNumber());
        plus3.next();
        plus3.next();
        System.out.println(plus3.currentNumber());
        plus3.prev();
        plus3.prev();
        plus3.prev();
        System.out.println(plus3.currentNumber());
        plus3.prev();
        System.out.println(plus3.currentNumber());
    }
}
AdditionPattern.main(null)
2
5
11
2
2

Methods and Control Structure

public class CheckDigit {
    /** Returns the check digit for num, as described in part (a).
    * Precondition: The number of digits in num is between one and six, inclusive.
    * num >= 0
    */

    public static int getCheck(int num) {
        int digitNum = getNumberOfDigits(num);
        int result = 0;
        for (int i = 1; i <= digitNum; i++) {
            result += (getDigit(num, i) * (8 - i));
        }
        return result % 10;
    }


    /** Returns true if numWithCheckDigit is valid, or false otherwise, as described in part (b).
     * Precondition: The number of digits in numWithCheckDigit is between two and seven, inclusive.
    * numWithCheckDigit >= 0
    */

    public static boolean isValid(int numWithCheckDigit) {
        int num = numWithCheckDigit/10;

        if (getCheck(num) == (numWithCheckDigit % 10)) {
            return true;
        }
        return false;

    }


    /** Returns the number of digits in num. */

    public static int getNumberOfDigits(int num) {
        /* implementation not shown */
        int count = 0;
        if (num == 0) {
            return 1;
        }
        while (num != 0) {
            num /= 10; 
            count++;
        }

        return count;

    }

    /** Returns the nth digit of num.
    * Precondition: n >= 1 and n <= the number of digits in num
    */

    public static int getDigit(int num, int n) {
        int numDigit = getNumberOfDigits(num) - n;
        for (int i = 0; i < numDigit; i++) {
            num /= 10; 
        }
        if (getNumberOfDigits(num) == 1) {
            return num;
        } else {
            return num % 10;
        }
    }

    // There may be instance variables, constructors, and methods not shown.

    public static void main(String[] args) {
        System.out.println(CheckDigit.getDigit(6123555, 7));

        System.out.println(CheckDigit.getCheck(54));
        System.out.println(CheckDigit.isValid(549));

        // System.out.println(CheckDigit.getNumberOfDigits(5));
    }

}
CheckDigit.main(null)
5
9
true