Methods and Control structures

  • Methods in overall coding are blocks of code that can be called upon and reused when needed within anywhere of the code.
  • When defining methods, one should be aware of the variable type that the method would return, parameters it takes in, of course the code, and then its implementation in calling.

Data Types

  • Data types in Java are different values that could be stored in a variable in Java. As far as it goes, there are primitive data types and non primitive.

Looking at the Diverse Arrays/Matrix

  • 2D Arrays are slightly tricky - the outer int[][] array is considered a non-primitive data type, meaning that values like null can be stored. However, within each array only primitive data types of the respective variable type can be stored.

  • Within Mort's code he defines various methods within the Diverse array class, beginning with the arraySum method. It takes in an array, and finds the sum of the integers in the array.

 public class DiverseArray {
    public static int arraySum(int[] arr) {
        int sum = 0;    

        for (int num : arr) {
            sum += num;
            System.out.print(num + "\t"); 
        }

        return sum;
    }
  • Next, he creates the sum of 2d arrays. Within the method he uses the other method defined previously to return a list of the sums of each row of the 2d array.
public static int[] rowSums(int[][] arr2D) {
    int rows = arr2D.length;        
    int[] sumList = new int[rows];  

    for (int i = 0; i < rows; i++) {
        sumList[i] = arraySum(arr2D[i]);
        System.out.println("= \t" + sumList[i]);  // debug
    }

    return sumList;
}
  • The final method simply checks if the rows are similar to each other

Math.random

  • Math.random generates a random value from 0 to 1. When multiplied by a range it generates a value in that range.
  • In Number.java, it is used like so
    public Number() {
      int SIZE = 36; int MIN = 3; int RANGE = SIZE - MIN + 1;  // constants for initialization
      this.number = (int)(Math.random()*RANGE) + MIN;  // observe RANGE calculation and MIN offset
      this.index = Number.COUNT++;    // observe use of Class variable COUNT and post increment
    }
  • Here a range is set with variables so that it could be easily altered. Also be sure to cast the number as an integer so that a decimal value doesn't occur.

DoNothingByValue.java

  • Overall, the DoNothingByValue demonstrates several methods of passing arguments.
  • The DoNothings method does nothing, it just shows how to pass in arguments

  • Within the changeIt method, functions are done locally to the variables, however in the end, the arr outside the method is not altered, and thus is not changed. ``` public static void changeIt(int [] arr, int val, String word) { arr = new int[5]; val = 0; word = word.substring(0, 5);

    System.out.print("changeIt: "); // added for (int k = 0; k < arr.length; k++) {

      arr[k] = 0;
      System.out.print(arr[k] + " "); // added
    

    } System.out.println(word); // added

}

* The changeIt2 demonstrates the creation of non primitive data type, even though the data type has the same name, they can be referred differently with different alterations to the array while still keeping its original values in a seperate one. If you print 'arr' it will be the original list.

public static void changeIt2(int [] nums, int value, String name) { nums = new int[5]; // new creates new memory address value = 0; // primitives are pass by value name = name.substring(0, 5); // all wrapper classes have automatic "new", same as word = new String(word.substring(0, 5));

// this loop changes nums locally
System.out.print("changeIt2: ");
for (int k = 0; k < nums.length; k++) {
    nums[k] = 0;
    System.out.print(nums[k] + " ");
}
System.out.println(name);

}

* In changeIt3 'arr' is actually modified, and the original is modified. However, since a new string object for word was created, the original 'word' will not be modified because now it is a localized variable to the method.

public static String changeIt3(int [] arr, String word) { word = new String(word.substring(0, 5)); // wrapper class does a "new" on any assignment

System.out.print("changeIt3: ");
for (int k = 0; k < arr.length; k++) {
    arr[k] = 0;                          // int array is initialized to 0's, not needed
    System.out.print(arr[k] + " ");

}
System.out.println(word);

return word;

}

* For changeIt4, first the Triple type should be explained. Simply put, Triple can store three values within the data structure, each can be with a different data type. They are referred by left, middle, and right.
* This simply shows how to modify an object passed by parameter.

public static Triple<int[], Integer, String> changeIt4(Triple<int[], Integer, String> T) { T.setOne(new int[5]); T.setTwo(0); // primitives are pass by value T.setThree(T.getThree().substring(0, 5)); // all wrapper classes have automatic "new", same as word = new String(word.substring(0, 5));

// this loop changes nums locally
System.out.print("changeIt4: ");
for (int i : T.getOne()) {
    System.out.print(i + " ");
}
System.out.println(T.getThree());

return T;

} ```