Data Type
Data Types Hacks
- Explore teacher's code:
- Answer what are Methods and Control Structures
- Explore AP FRQ that teaches us about Methods and Control Structures FRQ
- Look at Diverse Arrays, Matrix in Teacher code and see if you think this is Methods and Control structures.
- Look at Diverse Arrays, Matrix in Teacher code an see if you thing this fits Data Types.
- Math.random is covered in Number, this Teacher code associated with random is critical knowledge when taking the AP Exam. Random numbers in range like 7 to 9 is very important.
- Review DoNothingByValue, what is key knowledge here?
- Review IntByReference, what is key knowledge here?
- Review Menu code. Try, Catch, Runnable are used to control program execution. See if there is a way to get this to work in Jupyter Notebooks.
- Define "Method and Control Structures". To the Teacher, the Menu Code has the most work of methodDataTypes files that is related to the "Methods and Control Structures" topic. Such exploration would begin by asking "describe Java Methods and Control structures". Are instances of MenuRow and Runnable data types, control structures? Does Driver have control structures, enumerate them.
- Explore AP Classroom:
Answer what are Methods and Control Structures
Methods and Control Structures(AP Classroom):
Method signatures
- visibility modifier
- return type
- name
- parameter list
Types of methods: void methods - not return a value return methods - have to have "return", do return a value
Methods and Control Structures(chatGPT):
Methods are blocks of code that perform specific tasks and can be called and executed whenever necessary. They are used to organize code into reusable modules and can take in input parameters and return output values.
Control structures are used to control the flow of execution within a program. They determine when certain code blocks should be executed or skipped based on specific conditions or user input. Common control structures include conditional statements, loops, and switch statements. They are used to make decisions and repeat operations in a program.
Look at Diverse Arrays, Matrix in Teacher code and see if you think this is Methods and Control structures.
Diverse Arrays and Matrix are not methods and control structures. But in the code provided that contained DiverseArray and Matrix classes, it used several Methods and Control structures. Like Methods: arraySum, rowSums, isDiverse. Control Structures: for-loop and if-statement.
Look at Diverse Arrays, Matrix in Teacher code an see if you thing this fits Data Types.
In the teacher code provided, the diverse arrays and matrix are both defined as 2D Array, and array is a collection of data of the same type which is one of the reference data type. So in my case, both are fit the data type.
Math.random is covered in Number, this Teacher code associated with random is critical knowledge when taking the AP Exam. Random numbers in range like 7 to 9 is very important.
Math.random provided number between 0 and 1(not inclusive), so if we want to use it to provide a random number between 7 to 9, we can make it like that:
7 + (int)(2 * math.random()) + (int)(2 * math.random())
Review Menu code. Try, Catch, Runnable are used to control program execution. See if there is a way to get this to work in Jupyter Notebooks.
It does not work out, I think the reason why is because we didn't have the package com.nighthawk.hacks.methodsDataTypes; in our directory which makes the whole project break down.
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
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
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");
}
}
}
}
Define "Method and Control Structures". To the Teacher, the Menu Code has the most work of methodDataTypes files that is related to the "Methods and Control Structures" topic. Such exploration would begin by asking "describe Java Methods and Control structures". Are instances of MenuRow and Runnable data types, control structures? Does Driver have control structures, enumerate them.
Menu code that related to the "Methods and Control Structures" topic:
- Are instances of MenuRow and Runnable data types, control structures?
The instances of MenuRow and Runnable are data type, not control structures.
It used HashMap to define the instances of MenuRow as Integer which is a data type
Code: Map<Integer, MenuRow> menu = new HashMap<>();
And for the Runnable, because it is used as an instance variable in the class and also used as input of the method, so it most likely a data type.
Code1:
String title;
Runnable action;
Code2: public MenuRow(String title, Runnable action)
- Does Driver have control structures, enumerate them. Yes it does have control structures:
- while loop
Explore AP Classroom:
Look at 1 unique FRQ per team member on AP Classroom that goes over Methods and Control Structures. Provide me 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 Ten minutes. This option could use your PBL project and concepts if they were tailored to Teaching.
2021 FRQ1-WordMatch
This question involves the WordMatch class, which stores a secret String and provides methods that compare other strings and provides methods that compare other strings to the secret String. You will write two methods in the WordMatch class.
// 2021 FRQ1 - WordMatch
public class WordMatch{ // Create a class called WordMatch
private String secret; // Private instance variable, always use private on AP Test;
public WordMatch(String word){ //Constructor, set String secret as the input String word
secret = word;
}
public int scoreGuess(String guess){ // part a, return method
int count = 0; //Create a counter int value that stores how many times String guess appears in String secret
for (int i=0; i<(secret.length()-(guess.length()-1)); i++){
if (secret.substring(i, i+guess.length()).equals(guess)){ //for loop, and if-statement check how many times String guess appears in String secret
count++;
}
}
return count * guess.length() * guess.length();
}
public String findBetterGuess(String guess1, String guess2){ //part2, return method
String betterGuess = ""; //Set a String value to be returned as the method been called.
if (scoreGuess(guess1) > scoreGuess(guess2)){ //compare the two inputs by using if-statement and first method that we created above
betterGuess += guess1;
}
else if (scoreGuess(guess2) > scoreGuess(guess1)){
betterGuess += guess2;
}
else if (scoreGuess(guess1) == scoreGuess(guess2)){ // if the two String value inputs are qual to each other, then compare them alphabetically
if (guess1.compareTo(guess2)>0){
betterGuess += guess1;
}
else{
betterGuess += guess2;
}
}
return betterGuess;
}
public static void main(String[] args) { // tester method
WordMatch tester = new WordMatch("Tianbin");
System.out.println(tester.scoreGuess("in"));
System.out.println(tester.findBetterGuess("in", "i"));
}
}
WordMatch.main(null);
Data type we used in the FRQ:
Primitive data type: int Reference data type: String
I was confused about the data type of String and I checked Mr.M's data type lesson but it didn't talk about any of it. So I asked chatGPT, and that the result I got:
The String class is considered a reference type because it is a class that encapsulates a sequence of characters, and it is defined in the Java standard library. However, the String class is special in that it is often treated like a primitive type in Java, due to the fact that it is immutable and has some unique properties.
Methods we used:
return methods: scoreGuess and findBetterGuess, which should return a value when you called the methods.
void methods: main, the tester method
compareTo() - compare alphabetically between two Strings, return 0 if String is equal to its String input and return a value greater 0 if String is greater than the String it compare to, and return a value less than 0 if String is less than the String it compare to.
ex. guess1.compareTo(guess2)>0, if String guess1 is greater than guess2
substring() - return the letters between two indexes in a String, the end index is not inclusive.