College Board MC Quiz

Score

img

Corrections

13)

C is incorrect because {17, 37, 21, 42, 18, 69, 48, 28, 39} would be the result if the code segment in the for loop was changed to numbers[k] = numbers[k] + 3.

A is the correct answer because the values of the loop control variable k starts at 1 and is incremented by 3 as long as k is less than numbers.length. As a result, k will have the values 1, 4, 7 and then when it becomes 10, the loop will end. Only the values in numbers at indexes 1, 4, and 7 will be updated to the value of the sum of the element at the index that is one less than k (the element before the one being updated) and 3. So, numbers[1] will be assigned the value of numbers[0] + 3 or 20, numbers[4] will be assigned the value of numbers[3] + 3 or 45, and numbers[7] will be assigned the value of numbers[6] + 3 or 51. All other values will be unchanged.

18)

E is incorrect because this would be the result if the division used was floating point division instead of integer division and the result was cast to an int, as in (int)(404.0 / 10 * 10 + 1).

D is the correct answer because the first operation that is executed is 404 / 10. Since both 404 and 10 are integers, integer division is used resulting in 40. The value 40 is then multiplied by 10, resulting in 400, and finally 1 is added, meaning 401 is printed.

29)

D is incorrect because the value 1 is only returned when num is a single digit number.

A is the correct answer because each recursive call is made with the value num / 10. The expression num / 10 uses integer division and evaluates to an integer that is num with the right most digit removed. For example, 258 / 10 = 25. Each time the recursive call is made, what is returned is 1 plus the result of the recursive call. For example, what(258) = 1 + what(25) and what(25) = 1 + what(2) and what(2) = 1. Therefore what(258) = 1 + 1 + 1 or 3, which is the number of digits in 258.


Notes

See weekly notes here


Hacks

Early Seed

  • Write a sample binary addition 1 + 1 = 10
  • Have Java Code cell on screen at start of lecture

See code here


Small Code Excercises

  • Write a Jupyter notebook on the primitive data types. Use array and methods like substring and random as applicable: int, double, boolean, char
  • Next, convert each of the examples to corresponding Wrapper classes, using arrays

See code here


Key Concepts

Methods

  • A method is a block of code which only runs when it's called
    • Parameters, or data can be passed into a method
  • Methods are used to perform certain actions. Another word for them is a function
  • See more information on methods here

Control Structures

  • Control structure are blocks of code that can change the path of execution. There are 3 kinds of control structures:
    1. Condiditional Branches - used for choosing between 2+ paths (if/else statements)
    2. Loops - used it iterate through multiple values/objects and repeatedly run specific code blocks (for, while, and do while loops)
    3. Branching Statements - used to break the flow of control in loops (break and continue)
  • See more information on control structures here

Teacher's Code Examples

DiverseArray Example
  • Methods?
    • Yes, DiverseArray does contain methods. Examples of methods are: arraySum(), rowSums(), and isDiverse().
  • Control Structures?
    • Yes, DiverseArray does contain control stuctures. There are examples of for loops and if statements.
  • Fit data types?
    • Yes, DiverseArray has data types of int and boolean. It also has int[] and int[][].
Matrix Example
  • Methods?
    • Yes, Matrix does contain methods. Examples of methods are: toString(), reverse(), keypad(), and numbers().
  • Control Structures?
    • Yes, Matrix does contain control structures. There are examples of for loops.
  • Fit data types?
    • Yes, Matrix has data types of int. There are also int[][] and String.
Number Example
  • Math.random() is a static method that returns a floating-point that is ≥ 0 and < 1.
  • See more information on Math.random() here
DoNothingByValue Example
  • Methods?
    • Yes, DoNothingByValue does contain methods. Examples of methods are: changeIt(), changeIt2(), and changeIt3().
  • Control Structures?
    • Yes, DoNothingByValue does contain control structures. There are examples of for loops.
  • Fit data types?
    • Yes, DoNothingByValue has data types of int. There are also String, ArrayList, and int[].
IntByReference Example
  • Methods?
    • Yes, IntByReference does contain methods. Examples of methods are: toString(), swapToLowHighOrder(), and swapper().
  • Control Structures?
    • Yes, IntByReference does contain control structures. There are examples of if statements.
  • Fit data types?
    • Yes, IntByReference has data types of int. There are also String, ArrayList, and int[].
  • Find a way to make Menu work on a Jupyter Notebook.
  • Are instances of MenuRow and Runnable data types and control structures?
    • MenuRow() represents the constructor for the class MenuRow. In the constructor, all varibles in the class are initalized.
    • Runnable represents an object. In the method getAction(), the return type is Runnable.
  • Does Driver have control structures? If so, list all the control Structures.
    • Yes, Driver has control structures.
      • while loop
      • try/catch statements
      • if statements

College Board FRQ

  • Look at 1 unique FRQ per pair/treo on AP Classroom that goes over Methods and Control Structures
  • Provide teacher a Jupyter Notebook, Video, and/or Code that cover key concepts
    • Make this better than AP Classroom, specifically trying to get these reviews to cover key parts in under Five minutes
    • This option could use your PBL project and concepts if they were tailored to Teaching.
  • Explore Testing requirements in AP Classroom Video
  • Explore AP FRQ that teaches us about Methods and Control Structures FRQ, 18:10
  • Make sure that code runs completely, complete any portion of FRQ that is undone. Complete FRQs in a Jupyter notebook with markdown description and comments is a requirement
  • Integrate Data Types into the discussion as this is import concept when studying early materials
  • Integrate Definition of Methods and Control Structures in comments and definitions.

See FRQ instructions here

See code here


Code

Binary Addition

/*
 * adds two binary numbers
 */
public class BinaryAddition
{
    /*
     * tester method
     */
    public static void main(String[] Args)
    {
        String sum;
        String binary1 = "0110";
        String binary2 = "1011";

        // outputs numbers in binary form
        System.out.printf("number #1 in binary: \t\t%s\n", binary1);
        System.out.printf("number #2 in binary: \t\t%s\n\n", binary2);

        // calls addNums method
        sum = addNums(binary1, binary2);

        // outputs the sum in binary form
        System.out.printf("sum in binary: \t\t\t%s", sum);
    }

    /*
     * takes two binary strings, converts them into ints, and
     * adds the two numbers together
     */
    public static String addNums(String binary1, String binary2)
    {
        // Integer.parseInt() --> String to int
        int num1 = Integer.parseInt(binary1, 2);
        int num2 = Integer.parseInt(binary2, 2);
        int sum = num1 + num2;

        // outputs sum in decimal (number) form
        System.out.printf("number #1 in decimal form: \t%d\n", num1);
        System.out.printf("number #2 in decimal form: \t%d\n", num2);
        System.out.printf("\nsum in decimal form: \t\t%d\n", sum);

        // Integer.toBinaryString(sum); --> add numbers and converts the result to the binary String
        return Integer.toBinaryString(sum);
    }
}
BinaryAddition.main(null);
number #1 in binary: 		0110
number #2 in binary: 		1011

number #1 in decimal form: 	6
number #2 in decimal form: 	11

sum in decimal form: 		17
sum in binary: 			10001

Primitive Data Types

int Practice

int[] nums = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

// prints every elements in array
for(int i = 0; i < nums.length; i+=2)
{
    System.out.printf("%d\n", nums[i]);
}
0
2
4
6
8

double Practice

int x;
int y;
double casting1;
double casting2;

x = 10;
y = 15;

casting1 = (double) y / x;
casting2 = y / x;

System.out.printf("With casting: \t\t%.2f\nWithout casting: \t%.2f", casting1, casting2);
With casting: 		1.50
Without casting: 	1.00
java.io.PrintStream@16982d29

boolean Practice

boolean a = true;
boolean b = false;
boolean c = a && b;
boolean d = !(a || b);

System.out.printf("%b\n", c);
System.out.printf("%b", d);
false
false
java.io.PrintStream@41ba5a40

char Practice

char[] letters = {'H', 'E', 'L', 'L', 'O', ' ', 'W', 'O', 'R', 'L', 'D'};

// prints every element in array
for(int i = 0; i < letters.length; i++)
{
    System.out.printf("%c", letters[i]);
}
HELLO WORLD

String Practice

String message = "Hello World";
String newMessage = message.substring(6);

// full message: Hello World
System.out.printf("Before substring: \t%s\n", message);
// separates message and just prints World
System.out.printf("After substring: \t%s", newMessage);
Before substring: 	Hello World
After substring: 	World
java.io.PrintStream@16982d29

Math.random() Practice

int max = 5;
int min = 0;
int range = max - min + 1;

// creates 5 random ints between 0 and 5
for (int i = 0; i < 5; i++) {
    int rand = (int)(Math.random() * range) + min;

    System.out.printf("#%d: %d\n", i, rand);
}
#0: 2
#1: 4
#2: 4
#3: 3
#4: 3

FRQ 2018

Question #1

import java.util.ArrayList;
import java.util.Arrays;

public class FrogSimulation
{
    private int goalDistance; // distance in inches, from the starting position to the goal
    private int maxHops; //  max number of hops allowed to reach the goal

    private ArrayList <Integer> hopDistances;

    // constructor
    public FrogSimulation(int dist, int numHops)
    {
        goalDistance = dist;
        maxHops = numHops;
    }

    public void loadHopDistance(ArrayList <Integer> hopDistances)
    {
        this.hopDistances = new ArrayList <Integer> (hopDistances);
    }

    // number of hops from the frog
    private int hopDistance()
    {
        if(!hopDistances.isEmpty())
        {
            int hopDistance = hopDistances.get(0);
            hopDistances.remove(0);
            return hopDistance;
        }
        return 0;
    }

    // runs frog simulation
    public boolean simulate()
    {
        int hops = 0;
        int currentPosition = 0;

        while((currentPosition < goalDistance)
            && ((hops != maxHops)
            && (currentPosition < goalDistance))
            && (currentPosition >= 0))
        {
            hops++;
            
            currentPosition += hopDistance();
        }
        // if the frog is at the position, return true
        if((currentPosition >= goalDistance))
        {
            return true;
        }
        return false; 
    }

    // part b
    public double runSimulations (int num)
    {
        return 0.0;
    }

   // tester method
    public static void main(String[] Args)
    {
        FrogSimulation sim = new FrogSimulation(24, 5);

        // for testing
        ArrayList <Integer> frogJumps1 = new ArrayList <Integer>(Arrays.asList(5, 7, -2, 8, 6));
        ArrayList <Integer> frogJumps2 = new ArrayList <Integer>(Arrays.asList(6, 7, 6, 6));
        ArrayList <Integer> frogJumps3 = new ArrayList <Integer>(Arrays.asList(6, -6, 31));
        ArrayList <Integer> frogJumps4 = new ArrayList <Integer>(Arrays.asList(4, 2, -8));
        ArrayList <Integer> frogJumps5 = new ArrayList <Integer>(Arrays.asList(5, 4, 2, 4, 3));

        sim.loadHopDistance(frogJumps1);
        boolean goalReached = sim.simulate();
        System.out.printf("𓆏 reached goal? %b.\n", goalReached);
    }
}
FrogSimulation.main(null);
𓆏 reached goal? true.