int a = 1;
int b = 1;

int sum = a+b;

System.out.println("Decimal Sum: " + sum); // Print Decimal Sum
System.out.println("Binary Sum: " + Integer.toBinaryString(sum)); // Print and Convert to Binary Sum
Decimal Sum: 2
Binary Sum: 10
public class IntByValue {
    
    public static void changeInt(int n) {
        System.out.println("In changeInt method");
        System.out.println("\tBefore n += 10: n = " + n); // prints 5
        n = n += 10;
        System.out.println("\tAfter n += 10: n = " + n); // prints 10
    }

    public static void main(String[] args) {
        int n = 5;
        System.out.println("Main method before changeInt(n): n = " + n); // prints 5
        changeInt(n);
        System.out.println("Main method after changeInt(n): n = " + n); // still prints 5
    }
}
IntByValue.main(null);
Main method before changeInt(n): n = 5
In changeInt method
	Before n += 10: n = 5
	After n += 10: n = 15
Main method after changeInt(n): n = 5
public class IntByReference {
    private int value;

    public IntByReference(Integer value) {
        this.value = value;
    }

    public String toString() {
        return (String.format("%d", this.value));
    }

    public void swapToLowHighOrder(IntByReference i) {
        if (this.value > i.value) {
            int tmp = this.value;
            this.value = i.value;
            i.value = tmp;
        }
    }

    public static void swapper(int n0, int n1) {
        IntByReference a = new IntByReference(n0);
        IntByReference b = new IntByReference(n1);
        System.out.println("Before: " + a + " " + b);
        a.swapToLowHighOrder(b);  // conditionally build swap method to change values of a, b
        System.out.println("After: " + a + " " + b);
        System.out.println();
    }

    public static void main(String[] ags) {
        IntByReference.swapper(21, 16);
        IntByReference.swapper(16, 21);
        IntByReference.swapper(16, -1);
    }

}
IntByReference.main(null);
Before: 21 16
After: 16 21

Before: 16 21
After: 16 21

Before: 16 -1
After: -1 16

Hacks

/// Primitive Data Type "int" within an array
public class Example {
    public static void main(String[] args) {
      int[] numbers = {1, 2, 3, 4, 5}; // define array called numbers that have 5 int values
      int sum = 0;
      for (int i = 0; i < numbers.length; i++) { // iterate over array to sum the int values
        sum += numbers[i];
      }
      System.out.println("Sum of numbers: " + sum); // outputs the sum
    }
  }
Example.main(null);
Sum of numbers: 15
/// Primitive Data Type "double" within an array
public class DoubleArrayExample {
    public static void main(String[] args) {
        // declare an array of doubles
        double[] myArray = { 1.99, 5.5, 20.1, 0.9999 }; // declare an array with 4 values
        // print out the values of the array
        for (int i = 0; i < myArray.length; i++) {
            System.out.println("Element " + i + " = " + myArray[i]); // loop through the array using a for loop and print out each element in the array. Note that we use the double primitive type to declare the array and assign values to it.
        }
    }
}
DoubleArrayExample.main(null);
Element 0 = 1.99
Element 1 = 5.5
Element 2 = 20.1
Element 3 = 0.9999
/// Primitive Data Type "boolean" within an array
public class BooleanArrayExample {
    public static void main(String[] args) {
        // declare an array of booleans
        boolean[] myArray = { true, true, false, false }; // declare array with boolean data types

        // print out the values of the array
        for (int i = 0; i < myArray.length; i++) {
            System.out.println("Element " + i + " = " + myArray[i]);
        }
    }
}
BooleanArrayExample.main(null);
Element 0 = true
Element 1 = true
Element 2 = false
Element 3 = false
/// Primitive Data Type "char" 

import java.util.Random;

public class CharExample {
    public static void main(String[] args) {
        Random rand = new Random(); // create random object to generate random number
        char[] vowels = {'a', 'b', 'c', 'd', 'e'}; // array of vowels
        char randomVowel = vowels[rand.nextInt(vowels.length)]; // next Int method of the rand object generates a random integer that retrieves a random vowel

        System.out.println("The random letter A-E is " + randomVowel);
    }
}
CharExample.main(null);
The random letter A-E is c
import java.util.Arrays;
import java.util.Random;

public class WrapperClassesExample {
    public static void main(String[] args) {
        // int
        Integer num = Integer.valueOf(42);
        Integer[] arr = {Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3), Integer.valueOf(4), Integer.valueOf(5)};
        System.out.println("The number is " + num);
        System.out.println("The third element of the array is " + arr[2]);
        
        // double
        Double d = Double.valueOf(1.99);
        System.out.println("The double " + d);
        
        // boolean
        Boolean b = Boolean.valueOf(true);
        System.out.println("The boolean is " + b);
        
        // char
        Character c = Character.valueOf('z');
        System.out.println("The value of char is " + c);
        
        // Using substring method on String
        String str = "Hello, World!";
        String substr = str.substring(7, 12);
        System.out.println("The substring is " + substr);
        
        // Generating random numbers
        Random rand = new Random();
        Integer randNum = Integer.valueOf(rand.nextInt(1000));
        System.out.println("The random number is " + randNum);
        
        // Sorting an array
        Arrays.sort(arr);
        System.out.println("The sorted array is " + Arrays.toString(arr));
    }
}

WrapperClassesExample.main(null);
The number is 42
The third element of the array is 3
The double 1.99
The boolean is true
The value of char is z
The substring is World
The random number is 263
The sorted array is [1, 2, 3, 4, 5]

Methods And Control Structures

  • What are methods: Methods are code functions when called preform a specific function.
  • What are Control Structures: A block of code that can change the path we take through those instructions, an example of this is a loop.

Mr. M's Code

  • Diverse Array: Has loops and if statements and uses data types since it has int and arrays.
  • Random: Generates type number greater than or equal to 0.0 and less than 1.0.
  • Do Nothing by Value: Doesn't change #s locally and changes variables subvalues.
  • Int by Reference: Value of int changes locally
  • Menu: Use of cat, try, and while

2017 FRQ

Part A

public static Position findPosition(int num, int[][] intArr) {
    
    for (int row=0; row < intArr.length; row++) {   
        for (int col=0; col < intArr[0].length; col++) {  
            if (intArr[row][col] == num) {
                return new Position(row, col);
            }
        }
    }
    return null;
}

Part B

public static Position[][] getSuccessorArray(int[][] intArr) {
    
    Position[][] newArr = new Position[intArr.length][intArr[0].length];

    for (int row=0; row < intArr.length; row++) { // Forward Searching
        for (int col=0; col < intArr[0].length; col++) { 
            newArr[row][col] = findPosition(intArr[row][col]+1, intArr); // // Assign the result of calling the 'findPosition' method to the current element in 'newArr'
        }
    }
    return newArr;
}