• Write a Jupyter notebook code example on the following primitive types with a code example (4 to 5 lines), preference would be using array and methods like substring and random as applicable: int, double, boolean, char.
  • Now convert each of the examples to corresponding Wrapper classes, using arrays.
  • Expression of these in Class or PBL forms is an option. But the review must be easy for me to see work.
class Main {
    public static void main(String[] args) {
        int integer = 5;
        boolean boo = true;
        double fl = 3.5;

        Integer num = Integer.valueOf(integer);
        Double db = Double.valueOf(fl);
        Boolean bn = Boolean.valueOf(boo);

        System.out.println(num);
        System.out.println(bn);
        System.out.println(db);
    }
}

Main.main(null);
5
true
3.5

Method and Control Structure Notes

2018 FRQ

public boolean simulate()
{
  int frogPosition = 0;
  int hopsRemaining = maxHops;

  while(frogPosition < goalDistance &&
      frogPosition >= 0 &&
      hopsRemaining > 0)
  {
    frogPosition += hopDistance();
    hopsRemaining--;
  }

  return frogPosition >= goalDistance;
ublic double runSimulations(int num)
{
  int successfulRuns = 0;

  for(int run = 1; run <= num; run++)
    if(simulate())
      successfulRuns++;

  return successfulRuns / (double) num;
}

DiverseArray

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

        // enhanced for loop as values are needed, not index
        for (int num : arr) {
            sum += num;
            System.out.print(num + "\t");  // debug
        }

        return sum;
    }

    public static int[] rowSums(int[][] arr2D) {
        int rows = arr2D.length;        // remember arrays have length
        int[] sumList = new int[rows];  // size of sumList is based on rows

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

        return sumList;
    }

    public static boolean isDiverse(int[][] arr2D) {
        int [] sums = rowSums(arr2D);
        int sumsLength = sums.length;

        // ij loop, two indexes needed in evaluation, similar to bubble sort iteration
        for(int i = 0; i < sumsLength - 1; i++) {
            for (int j = i + 1; j < sumsLength; j++) {
                if (sums[i] == sums[j]) {
                    return false;    // leave as soon as you find duplicate
                }
            }
        }
        return true; // all diverse checks have been made
    }

    public static void main(String[] args) {
        int[][] mat1 = {
                { 1, 3, 2, 7, 3 },                       // row 1
                { 10, 10, 4, 6, 2 },                     // row 2
                { 5, 3, 5, 9, 6 },                       // row 3
                { 7, 6, 4, 2, 1 }                        // row 4
        };
        int[][] mat2 = {
                { 1, 1, 5, 3, 4 },                       // row 1
                { 12, 7, 6, 1, 9 },                      // row 2
                { 8, 11, 10, 2, 5 },                     // row 3
                { 3, 2, 3, 0, 6 }                        // row 4
        };

        System.out.println("Mat1 Diverse: " + isDiverse(mat1));
        System.out.println();
        System.out.println("Mat2 Diverse: " + isDiverse(mat2));
    }

}

DiverseArray.main(null);
1	3	2	7	3	= 	16
10	10	4	6	2	= 	32
5	3	5	9	6	= 	28
7	6	4	2	1	= 	20
Mat1 Diverse: true

1	1	5	3	4	= 	14
12	7	6	1	9	= 	35
8	11	10	2	5	= 	36
3	2	3	0	6	= 	14
Mat2 Diverse: false

DiverseArray Explanation

  1. We are looping through the each 2D array, however we only loop through the rows.
  2. We get the length of the row.
  3. Then, using a for loop, loop through the columns, and add all the values in each column.
  4. After doing so, move on to the next row.

IntByReference

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

  • The code swaps two numbers to make sure that they are in the correct order.
    • swapToLowHighOrder is the method that does the actual swap
  • swapper first gets the two numbers, prints them in the old order
  • It then calls the swapToLowHighOrder method.
  • Main has several test cases.

Math.Random

  • Now to get random integer numbers from a given fixed range, we take a min and max variable to define the range for our random numbers, both min and max are inclusive in the range.
  • Since Math.random() obviously returns double, we can cast it into an int
  • If you multiply Math.random by number N, then you will get a random number from 0 until N
  • If you add a number to the Math.random, then that will be the minimum starting number
  • Review AP exam questions with this.
class random {
 
    // driver code
    public static void main(String args[])
    {
        // define the range
        int max = 10;
        int min = 1;
        int range = max - min + 1;
 
        // generate random numbers within 1 to 10
        for (int i = 0; i < 10; i++) {
            int rand = (int)(Math.random() * range) + min;
 
            // Output is different everytime this code is executed
            System.out.println(rand);
        }
    }
}