Homework

Write a class called ArrayMethods that contains two methods that utilize/manipulate an array. You may choose 2 of the 4 options below to complete. Extra credit for doing all 4 options. Make sure to show test cases for all of the options you choose.

Options for hacks (Pick two):

  • Swap the first and last element in the array
  • Replace all even elements with 0
public class ArrayMethods {
    private int[] values = {1, 2, 3, 4, 5, 6};

    public void printElements(){
        for(int i = 0; i < values.length; i++){
            System.out.println(values[i]);
        }
    }

    public void swapElements(){
        int firstElement = values[0];

        values[0] = values[values.length-1];
        values[values.length-1] = firstElement;
    }

    public void zeroAll() {
        for(int i = 0; i < values.length; i++){
            values[i] = 0;
        } 
    }

    public static void main(String[] args) {
        System.out.println("First and Last Element Swap: ");
        ArrayMethods swapElements = new ArrayMethods();
        swapElements.swapElements();
        swapElements.printElements();

        System.out.println("Replacing All Elements w/ Zero: ");
        ArrayMethods zeroAll = new ArrayMethods();
        zeroAll.zeroAll();
        zeroAll.printElements();
    }
}

ArrayMethods.main(null);
First and Last Element Swap: 
6
2
3
4
5
1
Replacing All Elements w/ Zero: 
0
0
0
0
0
0