Hack 1

public void drawLine(int n) {
    for (int i = 1; i <= n; i++) {
        System.out.print("*");
    }
    if (n > 0){
        System.out.println();
        drawLine(n - 1);
    }
}
drawLine(10);
**********
*********
********
*******
******
*****
****
***
**
*

Hack 2

public class Country {
    private String name;
    private int size;

    public Country(String name, int size){
        this.name = name;
        this.size = size;
    }

    public String getName(){
        return name;
    }

    public int getSize(){
        return size;
    }

    public String toString(){
        return name + " " + size;
    }
}
public class Sort {
    public static ArrayList<Country> countryList = new ArrayList<Country>();

    public Sort(ArrayList<Country> countryList) {
        this.countryList = countryList;
    
    }

    public static ArrayList<Country> SortCountries() {
        int min_idx = 0;
        for(int i = 0; i < countryList.size(); i++){
            min_idx = i;
            for (int j = i+1; j < countryList.size(); j++) {
                if (countryList.get(j).getSize() < countryList.get(min_idx).getSize())
                    min_idx = j;
            }
            Country temp = countryList.get(min_idx);
            countryList.set(min_idx, countryList.get(i));
            countryList.set(i, temp);
        }

        return countryList;
    }

    public static void main(String[] args){
        countryList.add(new Country("India", 100));
        countryList.add(new Country("China", 200));
        countryList.add(new Country("America", 50));
        System.out.println(SortCountries());
    }
}

Sort.main(null);
[America 50, India 100, China 200]

Hack 3

  • Test if two arraylists contain the same elements in reverse order
public class Compare {
    public boolean compareArrayLists(ArrayList<Integer> list1, ArrayList<Integer> list2){
        if (list1.size() != list2.size()) {
            return false;
        }
        for (int i = 0; i < list1.size(); i++) {
            if (list1.get(i) != list2.get(list2.size()- i - 1)){
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args){
        Compare compare = new Compare();

        ArrayList<Integer> list1 = new ArrayList<Integer>();
        list1.add(3);
        list1.add(2);
        list1.add(5);

        ArrayList<Integer> list2 = new ArrayList<Integer>();
        list2.add(5);
        list2.add(2);
        list2.add(3);

        System.out.println(list1);
        System.out.println(list2);

        System.out.println("Reversed? " + compare.compareArrayLists(list1, list2));
    }
}
Compare.main(null);
[3, 2, 5]
[5, 2, 3]
Reversed? true
  • Overwrite all the elements in an arraylist with the alphabet
public class Change {
    public void removeHalf (ArrayList<Integer> list) {
        // ArrayList<Integer> result = new ArrayList<Integer>();
        // int size = list.size();
        for (int i = 1; i < list.size(); i++){
            list.remove(i);
        }
    }

    public static void main(String[] args){
        Change change = new Change();

        ArrayList<Integer> list = new ArrayList<Integer>();
        list.add(3);
        list.add(2);
        list.add(5);
        list.add(7);
        list.add(9);
        list.add(1);

        System.out.println(list);
        change.removeHalf(list);
        System.out.println(list);
    }
}
Change.main(null);
[3, 2, 5, 7, 9, 1]
[3, 5, 9]