Methods and Control Structures

public class Digits {
/** The list of digits from the number used to construct this object.
* The digits appear in the list in the same order in which they appear in the original number.
*/ 
    private ArrayList<Integer> digitList;
    /** Constructs a Digits object that represents num.
    * Precondition: num >= 0
    */

    public Digits (int num) {
        /* to be implemented in part (a) */
    /** Returns true if the digits in this Digits object are in strictly increasing order;
     * false otherwise. */
        this.digitList = new ArrayList<Integer>();
        while (num > 0) {
            digitList.add(0, num % 10);
            num /= 10;
        }
    }
    public boolean isStrictlyIncreasing () {
        /* to be implemented in part (b) */
        for (int i = 0; i < digitList.size() - 1; i++) {
            if (digitList.get(i) > digitList.get(i + 1)) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        Digits d1 = new Digits(123456789);
        System.out.println(d1.isStrictlyIncreasing());
    }
}

Digits.main(null);
true

Classes

public interface StudyPractice {
/** Returns the current practice problem. */
    String getProblem();
/** Changes to the next practice problem. */
    void nextProblem();
}

public class MultPractice implements StudyPractice {

    private int firstInt;
    private int secondInt;
    private String problem;

    MultPractice(int firstInt, int secondInt) {
        this.firstInt = firstInt;
        this.secondInt = secondInt;
        this.problem = firstInt + " TIMES " + secondInt;
    }

    @Override
    public String getProblem() {
        return this.problem;
    }

    @Override
    public void nextProblem() {
        secondInt++;
        this.problem = firstInt + " TIMES " + secondInt;
    }

    public static void main(String[] args) {
        MultPractice m1 = new MultPractice(4, 5);
        for (int i = 0; i < 10; i++) {
            System.out.println(m1.getProblem());
            m1.nextProblem();
        }
    }
}

MultPractice.main(null);
4 TIMES 5
4 TIMES 6
4 TIMES 7
4 TIMES 8
4 TIMES 9
4 TIMES 10
4 TIMES 11
4 TIMES 12
4 TIMES 13
4 TIMES 14

Arrays and Arraylists

public class Phrase {
    private String currentPhrase;

    /** Constructs a new Phrase object. */
    public Phrase (String p) {
        currentPhrase = p;
    }

    /** Returns the index of the nth occurrence of str in the current phrase;
    * returns -1 if the nth occurrence does not exist.
    * Precondition: str.length () > 0 and n > 0
    * Postcondition: the current phrase is not modified.
    */
    public int findNthOccurrence(String str, int n) {
        int counter = 0;
        for (int i = 0; i < currentPhrase.length() - str.length() + 1; i++) {
            if (currentPhrase.substring(i, i + str.length()).equals(str)) {
                counter++;
                if (counter == n) {
                    return i;
                }
            }
        }
        return -1;
    }

    /* implementation not shown */
    /** Modifies the current phrase by replacing the nth occurrence of str with repl.
    * If the nth occurrence does not exist, the current phrase is unchanged.
    * Precondition: str.length () > 0 and n > 0
    */
    public void replaceNthOccurrence(String str, int n, String repl) {
        /* to be implemented in part (a) */ 
        int index = findNthOccurrence(str, n);
        if (index != -1) {
            currentPhrase = currentPhrase.substring(0, index) + repl + currentPhrase.substring(index + str.length(), currentPhrase.length());
        }
    }


    /** Returns the index of the last occurrence of str in the current phrase;
    * returns -1 if str is not found.
    * Precondition: str.length () > 0
    * Postcondition: the current phrase is not modified.
    */
    public int findLastOccurrence(String str) {
    /* to be implemented in part (b) */ 
        int index = -1;
        for (int i = 0; i < currentPhrase.length() - str.length(); i++) {
            if (currentPhrase.substring(i, i + str.length()).equals(str)) {
                index = i;
            }
        }
        return index;

    }
    /** Returns a string containing the current phrase. */ 
    public String toString() {
        return currentPhrase;
    }

}

class Tester {

    public static void main(String[] args) {
        Phrase p1 = new Phrase("I love to code code code");
        System.out.println(p1.findNthOccurrence("code", 1));

        p1.replaceNthOccurrence("code", 3, "in CSA!");
        System.out.println(p1);

        System.out.println(p1.findLastOccurrence("code"));
    }
}

Tester.main(null);
10
I love to code code in CSA!
15

2D Arrays

public class Position {

    private int r;
    private int c;

    /** Constructs a Position obiect with row r and column c. */ 
    public Position (int r, int c) {
        /* implementation not shown */
        this.r = r;
        this.c = c;
    }

    public static Position findPosition(int num, int[][] arr) {
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                if (arr[i][j] == num) {
                    return new Position(i, j);
                }
            }
        }
        return null;
    }

    public static Position[][] getSuccessorArray(int[][] arr) {
        Position[][] successorArray = new Position[arr.length][arr[0].length];
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                successorArray[i][j] = findPosition(arr[i][j] + 1, arr);
            }
        }
        return successorArray;
    }

    public String toString() {
        return "(" + this.r + ", " + this.c + ")";
    }

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

    public static void main(String[] args) {
        int[][] arr = new int[][]{{1, 7, 4}, {2, 8, 9}, {3, 5, 6}};
        Position[][] successor = getSuccessorArray(arr);
        for (Position[] row : successor) {
            for (Position pos : row) {
                System.out.print(pos + " ");
            }
            System.out.println();
        }
    }
}

Position.main(null);
(1, 0) (1, 1) (2, 1) 
(2, 0) (1, 2) null 
(0, 2) (2, 2) (0, 1)