import java.lang.*;

public class BinaryAddition {

    public int binaryAdd(int a, int b) {
        String aBin = Integer.toBinaryString(a);
        String bBin = Integer.toBinaryString(b);

        for (int i = 0; i < )
    }
}


BinaryAddition a = new BinaryAddition();
a.binaryAdd(1, 0);
a.binaryAdd(1, 1);
a.binaryAdd(0, 0);
1
10
0
0

Notes

  • Reference: Pointers to memory locations (addresses of primitive data)
  • Auto boxing: Wraps the integer in a box, and the box is a reference to the integer

Hacks

  • Start with some small code excercises
    • 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.
public class PrimToWrap {
    public static void main(String[] args) {
        // create primtowrap for two different arrays.
        int[] a = new int[] {1, 2, 3, 4, 5};
        new PrimToWrap(a);
        int[] b = new int[] {6, 7, 8, 9, 10};
        new PrimToWrap(b);
    }

    PrimToWrap(int[] array) {
        System.out.println("Primitive Array");
        printArray(array);
        printArray(reverseArray(array));

        System.out.println("Wrapper Class Array");
        Integer[] b = convertToWrap(array);
        printArray(b);
        printArray(reverseArray(b));
        System.out.println(" ");
    }

    public int[] reverseArray(int[] a) {
        // New array b
        int[] b = new int[a.length];
        for (int i = 0; i < a.length; i++) {
            // reverse array values
            b[i] = a[a.length - i - 1];
        }
        return b;
    }

    // for wrapper class method
    public Integer[] reverseArray(Integer[] a) {
        // New array b
        Integer[] b = new Integer[a.length];
        for (int i = 0; i < a.length; i++) {
            // reverse array values
            b[i] = a[a.length - i - 1];
        }
        return b;
    }

    public void printArray(int[] a) {
        // Print array
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + " ");
        }
        System.out.println(" ");
    }

    // for wrapper class integer
    public void printArray(Integer[] a) {
        // Print array
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + " ");
        }
        System.out.println(" ");
    }

    public Integer[] convertToWrap(int[] a) {
        // Convert to wrapper class
        Integer[] b = new Integer[a.length];
        for (int i = 0; i < a.length; i++) {
            b[i] = Integer.valueOf(a[i]);
        }
        return b;
    }
}

PrimToWrap.main(null);
Primitive Array
1 2 3 4 5  
5 4 3 2 1  
Wrapper Class Array
1 2 3 4 5  
5 4 3 2 1  
 
Primitive Array
6 7 8 9 10  
10 9 8 7 6  
Wrapper Class Array
6 7 8 9 10  
10 9 8 7 6  
 

Exploring Teacher Code: methodDataTypes. I would like a Jupyter Notebook that goes over key concepts in Teacher code and uses background from ChatGPT and from AP Classroom.

  • Methods are what are called by the user or by a class to do something, which must be defined, be called (with/without parameters), and return something or perform an action. Control structures are the actions performed inside methods that do all of the logic, which includes conditionals, iteration, selection, and recursion.
  • DiverseArray does feature methods and control structures. There are several methods that take in parameters, perform different logical selection and then return outputs or modify values. Matrix also has these methods.
  • DiverseArray and Matrix both use only primitives and no wrapper classes. However, they also have 2D arrays for these primitives and a String[]. The String[] is a reference type, but the primitives are not. The 2D arrays are also reference types, but the primitives are not. The primitives are stored in the 2D arrays, but the 2D arrays are stored in the reference type of the String[].
  • DoNothingByValue is a method that takes in a primitive and does nothing to it. It is passed by value, so it copies the primitive and the original is unchanged.
  • IntByReference - Key knowledge here is understanding that it takes the integer by reference, and then can modify the actual value as it is referenced. This is key to understanding how value vs. reference works.
  • Exploring Menu
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * Menu: custom implementation
 * @author     John Mortensen
 *
 * Uses String to contain Title for an Option
 * Uses Runnable to store Class-Method to be run when Title is selected
 */

// The Menu Class has a HashMap of Menu Rows
public class Menu {
    // Format
    // Key {0, 1, 2, ...} created based on order of input menu
    // Value {MenuRow0, MenuRow1, MenuRow2,...} each corresponds to key
    // MenuRow  {<Exit,Noop>, Option1, Option2, ...}
    Map<Integer, MenuRow> menu = new HashMap<>();

    /**
     *  Constructor for Menu,
     *
     * @param  rows,  is the row data for menu.
     */
    public Menu(MenuRow[] rows) {
        int i = 0;
        for (MenuRow row : rows) {
            // Build HashMap for lookup convenience
            menu.put(i++, new MenuRow(row.getTitle(), row.getAction()));
        }
    }

    /**
     *  Get Row from Menu,
     *
     * @param  i,  HashMap key (k)
     *
     * @return  MenuRow, the selected menu
     */
    public MenuRow get(int i) {
        return menu.get(i);
    }

    /**
     *  Iterate through and print rows in HashMap
     */
    public void print() {
        for (Map.Entry<Integer, MenuRow> pair : menu.entrySet()) {
            System.out.println(pair.getKey() + " ==> " + pair.getValue().getTitle());
        }
    }

    /**
     *  To test run Driver
     */
    public static void main(String[] args) {
        Driver.main(args);
    }

}

// The MenuRow Class has title and action for individual line item in menu
public class MenuRow {
    String title;       // menu item title
    Runnable action;    // menu item action, using Runnable

    /**
     *  Constructor for MenuRow,
     *
     * @param  title,  is the description of the menu item
     * @param  action, is the run-able action for the menu item
     */
    public MenuRow(String title, Runnable action) {
        this.title = title;
        this.action = action;
    }

    /**
     *  Getters
     */
    public String getTitle() {
        return this.title;
    }
    public Runnable getAction() {
        return this.action;
    }

    /**
     *  Runs the action using Runnable (.run)
     */
    public void run() {
        action.run();
    }
}

// The Main Class illustrates initializing and using Menu with Runnable action
public class Driver {
    /**
     *  Menu Control Example
     */
    public static void main(String[] args) {
        // Row initialize
        MenuRow[] rows = new MenuRow[]{
            // lambda style, () -> to point to Class.Method
            new MenuRow("Exit", () -> main(null)),
            new MenuRow("Do Nothing", () -> DoNothingByValue.main(null)),
            new MenuRow("Swap if Hi-Low", () -> IntByReference.main(null)),
            new MenuRow("Matrix Reverse", () -> Matrix.main(null)),
            new MenuRow("Diverse Array", () -> Matrix.main(null)),
            new MenuRow("Random Squirrels", () -> Number.main(null))
        };

        // Menu construction
        Menu menu = new Menu(rows);

        // Run menu forever, exit condition contained in loop
        while (true) {
            System.out.println("Hacks Menu:");
            // print rows
            menu.print();

            // Scan for input
            try {
                Scanner scan = new Scanner(System.in);
                int selection = scan.nextInt();

                // menu action
                try {
                    MenuRow row = menu.get(selection);
                    // stop menu
                    if (row.getTitle().equals("Exit")) {
                        if (scan != null) 
                            scan.close();  // scanner resource requires release
                        return;
                    }
                    // run option
                    row.run();
                } catch (Exception e) {
                    System.out.printf("Invalid selection %d\n", selection);
                }

            } catch (Exception e) {
                System.out.println("Not a number");
            }
        }
    }
}
|   
|   
|   /**
|    * Menu: custom implementation
|    * @author     John Mortensen
|    *
|    * Uses String to contain Title for an Option
|    * Uses Runnable to store Class-Method to be run when Title is selected
|    */
|   
|   // The Menu Class has a HashMap of Menu Rows
|   public class Menu {
|       // Format
|       // Key {0, 1, 2, ...} created based on order of input menu
|       // Value {MenuRow0, MenuRow1, MenuRow2,...} each corresponds to key
|       // MenuRow  {<Exit,Noop>, Option1, Option2, ...}
|       Map<Integer, MenuRow> menu = new HashMap<>();
|   
|       /**
|        *  Constructor for Menu,
|        *
|        * @param  rows,  is the row data for menu.
|        */
|       public Menu(MenuRow[] rows) {
|           int i = 0;
|           for (MenuRow row : rows) {
|               // Build HashMap for lookup convenience
|               menu.put(i++, new MenuRow(row.getTitle(), row.getAction()));
|           }
|       }
|   
|       /**
|        *  Get Row from Menu,
|        *
|        * @param  i,  HashMap key (k)
|        *
|        * @return  MenuRow, the selected menu
|        */
|       public MenuRow get(int i) {
|           return menu.get(i);
|       }
|   
|       /**
|        *  Iterate through and print rows in HashMap
|        */
|       public void print() {
|           for (Map.Entry<Integer, MenuRow> pair : menu.entrySet()) {
|               System.out.println(pair.getKey() + " ==> " + pair.getValue().getTitle());
|           }
|       }
|   
|       /**
|        *  To test run Driver
|        */
|       public static void main(String[] args) {
|           Driver.main(args);
|       }
|   
|   }
Unresolved dependencies:
   - class MenuRow
   - variable Driver
  • Instances of MenuRow and Runnable data types are control structures because they control the flow of execution in a program, as they have conditional statements, loops, branching statements, and/or exception handling.
  • Driver has control structures, the two main ones being the while loop and the try/catch exception handling

Exploring AP FRQs - FRQ 2020 #2

FRQ SUMMARY

  • This FRQ essentially asks to separate a number like "1592" between the last digit "2" and the rest "159". Then you are supposed to check how many numbers of digits there are using getCheck() (counting at zero), so 159 would return 2. Then you test if the getCheck value matches the last digit "2".
  • Something important to note about this FRQ is that it requires the use of attributes, as well as primitive data types.
  • This FRQ may require use of control structures such as if statements, additionally if you were to go beyond the AP requirements and implement error checking (incorrect number input such as being too large or negative) then you would likely have to have try/catch implementation.
// a.) Implement isValid based on requirements

public class CheckDigit {
    private static int incorrect;

    public static int getCheck(int num) {
        /* implementation not shown */
    }

    public static boolean isValid(int numWithCheckDigit) {
        /* to be implemented in part (a) */
        // get check digit and separate from number
        int num = numWithCheckDigit / 10;
        int check = numWithCheckDigit % 10;
        // test if check digit is correct
        if (check == getCheck(num)) {
            return true;
        } else {
            return false;
        }
    }
    

    
}

b.) I would change the CheckDigit class to have an attribute defined as private static int incorrect, then inside of the isValid method, I would change it so that if the getCheck() method returns false, incorrect is incremented by one.