Hacks

Name: Bailey Say

interface Turtle {
    
}

// create ArrayLists that satisfy the following

// a) that stores Boolean values
ArrayList<Boolean> booleans = new ArrayList<Boolean>();

// b) that stores Turtle Objects
ArrayList<Turtle> turtles = new ArrayList<Turtle>();

// c) that initializes with 10 Strings
ArrayList<String> strings = new ArrayList<String>(10);
import java.util.ArrayList;

public class Hack2 {
    public static void main(Integer[] args) {
        ArrayList<Integer> randomNumbers = new ArrayList<Integer>();
        randomNumbers.add(1);
        randomNumbers.add(4);
        randomNumbers.add(7);
        randomNumbers.add(12);
        randomNumbers.add(23);
        System.out.println("ArrayList: " + randomNumbers);

        randomNumbers.remove(3);
        randomNumbers.sort(Comparator.naturalOrder());
        randomNumbers.add(14);

        System.out.println(randomNumbers);
    }
}
Hack2.main(null);
public class Hack3 {
    public static void main(String[] args) {
        ArrayList<Integer> values = new ArrayList<Integer>();
        values.add(1);
        values.add(4);
        values.add(7);
        values.add(12);
        values.add(23);
        System.out.println("ArrayList: " + values);
        
        int total = 0;

            for (int i=0; i < values.size(); i++) {
                total += values.get(i);
            }

            System.out.println(total);
    }
}
Hack3.main(null);
int[] arr = {1, 3, 5, 23, 5, 12, 5, 7, 12, 4};

for (int i = 0; i < arr.length; i++) {
 
    // nested loop 1 index ahead
    for (int j = i + 1; j < arr.length; j++) {

        // comparing elements
        int temp = 0;
        if (arr[j] < arr[i]) {

            // write the swap code!
            temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
    }

    // Printing sorted array 
    System.out.print(arr[i] + " ");
}
// code here

int[] arr = {12, 4, 12, 3, 45, 5, 2, 4};

// Even
for (int i = 0; i < arr.length; i += 2) {
    System.out.println(arr[i]);
}

// Odd
for (int i = 1; i < arr.length; i += 2) {
    System.out.println(arr[i]);
}
// code here
int[][] arr2 = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

for (int[] row : arr2) {
    for (int num : row) {
        System.out.print(num + " ");
    }
    System.out.println();
}
public class Test1
{

    public static void main(String[] args)
    {
        int[][] array = { {1,2,3},{-1,-2,-3},{4,5,6} };
        int total = 0;

        //ADD CODE HERE
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                if (i == j) {
                    total += array[i][j];
                }
            }
        }

        System.out.println(total);
    }
}

Test1.main(null);
// code here
public static boolean double23(int[] nums) {

    int twoCount = 0;
    int threeCount = 0;

    for (int num : nums) {
        if (num == 2) {
            twoCount++;
        } else if (num == 3) {
            threeCount++;
        }
    }
    if (twoCount == 2 || threeCount == 2) {
        return true;
    }
    return false;
}

System.out.println(double23(new int[]{2, 2, 3}));
// code here

public class findNumber {

    public static int find(int[][] arr, int n) {

        int count = 0;
        for (int[] row : arr) {
            for (int num : row) {
                if (num == n) {
                    count++;
                }
            }
        }
        return count;
    }

}

findNumber.find(new int[][]{{1, 2, 3}}, 2);

Homework

public class SeatingChart {
    
    private ArrayList<String> chart;

    SeatingChart(ArrayList<String> chart) {
        this.chart = chart;
    }

    public void alphabetize() {
        String temp1 = "";
        String temp2 = "";
        for (int i = 0; i < chart.size() - 1; i++) {
            for (int j = i + 1; j < chart.size(); j++) {
                if (chart.get(i).compareTo(chart.get(j)) > 0) {
                    temp2 = chart.remove(j);
                    chart.add(i, temp2);
                    temp1 = chart.remove(i + 1);
                    chart.add(j, temp1);
                    }
                }
            }
        }

    public void add(String person) {
        chart.add(person);
    }

    public void delete(int index) {
        chart.remove(index);
    }

    public String toString() {
        String response = "";
        for (String person : chart) {
            response += person + " ";
        }
        return response;
    }
}

class Tester {
    public static void main(String[] args) {
        SeatingChart chart = new SeatingChart(new ArrayList<String>(List.of("Don", "Bailey", "Evan")));
        System.out.println(chart);

        chart.alphabetize();
        System.out.println(chart);

        chart.add("Hetvi");
        chart.add("Iris");
        chart.add("Ellen");
        chart.add("Nod");

        chart.alphabetize();
        System.out.println(chart);

        chart.delete(6);
        System.out.println(chart);
    }
}

Tester.main(null);
Don Bailey Evan 
Bailey Don Evan 
Bailey Don Ellen Evan Hetvi Iris Nod 
Bailey Don Ellen Evan Hetvi Iris 

Extra Array/ArrayList FRQ

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