If you'd like, you can follow along with the lessons at this link.

Clone this repo (git clone https://github.com/Rebecca-123/rmr-tri3) and work on the hacks below. You can transfer these hacks to your personal fastpages once you're finished.

ArrayList Hacks

Hack 1

public class Turtle {
    private String name;
    
    public Turtle(String name) {
        this.name = name;
        
    }
    
    public static void main(String[] args) {
        ArrayList<Boolean> boolArr = new ArrayList<Boolean>();

        ArrayList<Turtle> turtles = new ArrayList<Turtle>();

        ArrayList<String> strings = new ArrayList<String>();

        for (int i = 0; i<10; i++) {
            strings.add("xdd");
        }

        System.out.println(strings.toString());
    }
}
Turtle.main(null);
[xdd, xdd, xdd, xdd, xdd, xdd, xdd, xdd, xdd, xdd]

Hack 2

Choose 3 different methods from above to change around this sample ArrayList:

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(0);
        randomNumbers.add(10);
        randomNumbers.set(0, 7);

        System.out.println("ArrayList: " + randomNumbers);
    }
}
Hack2.main(null);
ArrayList: [1, 4, 7, 12, 23]
ArrayList: [7, 7, 12, 23, 10]

Hack 3

Here is some sample code for the total sum of integers. Finish the loop!

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: " + total);
    }
}
Hack3.main(null);
ArrayList: [1, 4, 7, 12, 23]
total: 47

Hack 4

Complete the Selection sort sample code by writing code for a swap of elements.

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

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]) {
            temp = arr[j];
            arr[j] = arr[i];
            arr[i] = temp;
        }
    }

    // Printing sorted array 
    System.out.print(arr[i] + " ");
}
1 2 3 4 5 

Array and 2D Array Hacks

Hack 5

Make an array and iterate through it to only print the elements with even indexes (this includes 0). Then iterate through it again to only print the odd indexes.

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

for (int i = 0; i < arr.length; i++) {
    if (i % 2 == 0) {
        System.out.print(arr[i] + " ");
    }
}
System.out.println();
for (int i = 0; i < arr.length; i++) {
    if (i % 2 != 0) {
        System.out.print(arr[i] + " ");
    }
}
1 3 5 
2 4 

Hack 6

Create a 2D array and iterate through it to print all of its elements.

int[][] arr = {{1, 2, 3}, {4, 5, 6}};

for (int row = 0; row < arr.length; row++) {
    for (int col = 0; col < arr[0].length; col++) {
        System.out.print(arr[row][col] + " ");
    }
    System.out.println();
}
1 2 3 
4 5 6 

Homework!

Write a seating chart program for your group. Meet the following requirements:

  • A 2D array, array, or ArrayList
  • A minimum of 3 methods to manipulate the seating chart (ex. alphabetize, shuffle seats, add/replace/delete people)
  • Print results

You will be graded on:

  • Completion of at least 5 of the above hacks (you can complete these during the in-class activity)
  • Completion of the homework
  • Extra credit if you complete a College Board FRQ involving arrays/ArrayLists

Complete the hacks/homework on your personal fastpages.

import java.util.ArrayList;

public class Seating {
    public ArrayList<String> chart;

    public Seating() {
        chart  = new ArrayList<String>();
    }

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

    public void replace (int index, String newName) {
        chart.set(index, newName);
    }

    public void alphabetize() {
        int length = chart.size();
        for (int i = 0; i<length - 1; i++) {
            for (int j = i + 1; j < length; j++) {
                if (chart.get(i).compareTo(chart.get(j)) > 0) {
                    String temp = chart.get(j);
                    chart.set(j, chart.get(i));
                    chart.set(i, temp);
                }
            }
        }
    }

    public void print() {
        int length = chart.size();
        for (int i = 0; i < length; i++) {
            System.out.println(chart.get(i) + " ");
        }
    }

    public static void main (String[] args) {
        Seating seating = new Seating();
        seating.add("Sahil");
        seating.add("Samuel");
        seating.add("Everitt");
        seating.print();
        seating.replace(0, "Nathan");
        System.out.println();
        seating.print();
        seating.alphabetize();
        System.out.println();
        seating.print();
    }
}
Seating.main(null);
Sahil 
Samuel 
Everitt 

Nathan 
Samuel 
Everitt 

Everitt 
Nathan 
Samuel