Sept 20 Lesson Plan
Practice with FRQs and Arrays/Iteration
- Brief Review
- 2018 FRQ #1 Overview
- 2018 FRQ #1a
- 2018 FRQ #1a Solution
- 2018 FRQ #1b
- 2018 FRQ #1b Solution
- 2021 FRQ #1 Overview
- 2021 FRQ #1a
- 2021 FRQ #1b
- Array attempts
2018 FRQ #1a
Part A: The purpose is to make it so that if we call the method FrogSimulation using FrogSimulation sim = new FrogSimulation(24,5);. The simulation would have a goal at a distance of 24 inches and a maximum amount of 5 jumps. If the frog makes it to or past the goal sim, simulate will return true and if it is negative or has just not made it to the goal but is not a negative value then it will return false.
public class FrogSimulation {
private int goalDistance;
private int maxHops;
public FrogSimulation(int dist, int numHops){
goalDistance=dist;
maxHops=numHops;
}
private int hopDistance(){}
public boolean simulate(){}
public double runSimulations(int num){}
}
2018 FRQ #1a Solution
First we define location to be at zero, our starting point. Then we use a for loop so that i will continue to increase until it reaches the max number of hops and so that for as long as this happens, the frog will hop (ie. location= location+hopDistance, which means that the distance the frog jumps will be added to location every time the frog jumps in order to have the final location of the frog). After the frog has finished jumping, we used an if loop so that if the frog is at or beyond the goal then it will be true and if it is at a negative value or behind the starting value then it will return false. If the frog has used up all its jumps but has not made it to the goal or at a negative value then it will return false regardless which is done by a return false outside of the for loop.
public boolean simulate(){
int location=0;
for (int i=0; i<numHops; i++){
location +=hopDistance();
if(dist<=location){
return true;
} else if (location<0){
return false;
}
}
return false;
}
2018 FRQ #1b
Part B: We can then use the values given by the method simulate in order to finish the simulation of the frog. Prompt B is asking for us to finish the simulation and declare whether or not the frog has won the game i.e. made it to the goal, which we can find using the boolean values given by simulate, and then find the proportion of wins overall after a certain number of runs.
2018 FRQ #1b Solution
This is a method called runSimulations and basically if we call this method such as FrogSimulation run = new FrogSimulation(23); it should run the simulation 23 times and count the number of successes- which will eventually be changed into a decimal of successes/tries. We do this by setting the number of successes to zero to begin with and then we use a for loop, with basically the same idea as part a, so that the code will run for as many times as directed. Then inside of that for loop, we have an if loop so that for as long as simulate runs true then the success counter will increase and the proportion of successes/tries will be returned.
public double runSimulations(int num){
int success=o;
for(int i=0; i<num; i++ ){
if(simulate()){
success++
}
return success/num
}
}
2021 FRQ #1 Overview
The reader is given a class labeled WordMatch which stores a secret string and contains methods to compare other strings to the secret string. In order to fully complete this question, the reader will have to create two methods that fulfill the requirements listed in each part.
-
Part A: scoreGuess method → comes up with a score for each guess
-
Part B: findBetterGuess method → choose best from 2 guesses
public class WordMatch{
private String secret;
public WordMatch(String word){}
public int scoreGuess(String guess){
int result = 0;
for (int i = 0; a < secret.length(); i++){
if(secret.substring(i).indexOf(guess) == 0){
result++;
}
}
return result + guess.length() + guess.length();
}
}
public String findBetterGuess(String guess1, String guess2){
if(scoreGuess(guess1) > scoreGuess(guess2)){
return guess1;
}
if(scoreGuess(guess2) > scoreGuess(guess1)){
return guess2;
}
if(guess1.compareTo(guess2) > 0){
return guess1;
}
return guess2;
}
//Monkey thing but with dogs cuz we love dogs
class DogLoop {
//The area between class definition and the 1st method is where we keep data for object in Java
String [][] dogs; //2D Array: AP CSA Unit 8: 2D array of strings
//2D array is like a grid [x][y]
// or like a spreadsheet [row][column]
/**
* Constructor initializes a 2D array of Dogs
*/
public DogLoop() {
//Storing Data in 2D arrays
dogs = new String[][]{ //2D array above is just a name, "new" makes a container ("object")
//Dog 1
{
" Sunny ",
" __ ", //[0][0] eyes
" (___()'`;", //[0][1] chin
" /, /` ", //[0][2] body
" \\'----\\ " //[0][3] legs
},
//Dog 2
{
" Aubrey ",
" __ ", //[1][0]
" | ______/ V`-,",
" } /~~ ",
"/_)^ --,(_| "
},
//Dog 3
{
" Mari ",
" __ _ ", //[2][0]
"o'')}____//",
" `_/ )",
"(_(_/-(_/ "
},
//Dog 4
{
" Kel ",
"^..^ /", //[3][0]
"/_/|_____/ ",
" /| /| ",
" / | / | "
},
//Dog 5
{
" Basil ",
" ", //[4][0]
" ..^____/", //[4][1]
"`-. ___ )", //[4][2]
" || || " //[4][3]
},
//Dog 6
{
" Hero ",
" __", //[4][0]
"(|,--------'()'--o", //[4][1]
" (_ ___ /~''", //[4][2]
" (_)_) (_)_)" //[4][3]
},
};
}
/**
* Loop and print dogs in array
* ... repeat until you reach zero ...
*/
public void printPoem() {
//begin the poem
System.out.println();
System.out.println("Dogs in Kennel Poem in Java Loopy");
// dogs (non-primitive) defined in constructor knows its length
int dogCount = dogs.length;
int rowCount = 0;
for (int i = dogCount; i >= 1; i--) //loops through 2D array length backwards
{
//this print statement shows current count of dogs
// concatenation (+) of the loop variable and string to form a countdown message
System.out.println(i + " little dogs in the kennel, waiting to find a forever home...");
for (int col = 0; col < dogs[rowCount].length; col++) {
for (int row = 0; row < dogCount; row++) { //cycles through "cells" of 2d array
// prints specific part of the dog from the column
System.out.print(dogs[row][col] + " ");
}
//this new line gives separation between stanza of poem
System.out.println();
}
rowCount += 1;
//countdown for poem, decrementing dogCount variable by 1
dogCount -= 1;
}
//out of all the loops, prints finishing messages
System.out.println("Yay! There are no more dogs waiting in the kennel!");
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println(" THE END ");
}
/**
* A Java Driver/Test method that is the entry point for execution
*/
public static void main(String[] args) {
new DogLoop().printPoem(); //a new dog list and output in one step
}
}
DogLoop.main(null);
class playerLoop {
// The area between class definition and the 1st method is where we keep data for object in Java
String [][] players;
/**
* Constructor initializes a 2D array of s
*/
public playerLoop() {
//Storing Data in 2D arrays
players = new String[][]{
//Player 0
{
" o__",
"/| ",
"/ > o"
},
//player 1
{
" o__",
"/| ",
"/ > o"
},
//player 2
{
" o__",
"/| ",
"/ > o"
},
//player 3
{
" o__",
"/| ",
"/ > o"
},
//player 4
{
" o__",
"/| ",
"/ > o"
},
};
}
/**
* Loop and print s in array
* ... repeat until you reach zero ...
*/
public void printPoem() {
//begin the poem
System.out.println();
System.out.println("The Great Story of the Players Who Shot A Soccer Ball");
System.out.println("---------------------------------");
// players (non-primitive) defined in constructor knows its length
int playerCount = players.length;
for (int i = playerCount; i >= 1; i--) //loops through 2D array length backwards
{
//this print statement shows current count of players
// concatenation (+) of the loop variable and string to form a countdown message
System.out.println(i + " players shooting the ball...");
//how many separate parts are there in a player player?
int partCount = players[0].length;
for (int row = 0; row < partCount; row++) { //cycles through “cells” of 2d array
/*cycles through columns to print
each player part by part, will eventually print entire column*/
for (int col = 0; col < playerCount; col++) {
// prints specific part of the player from the column
System.out.print(players[col][row] + " ");
//this is new line between separate parts
}
//this new line gives separation between stanza of poem
System.out.println();
}
//countdown for poem, decrementing playerCount variable by 1
System.out.println("One missed and got benched");
playerCount -= 1;
}
//out of all the loops, prints finishing messages
System.out.println("No more players shooting in the field");
System.out.println("------------------------------------");
System.out.println(" THE END ");
}
/**
* A Java Driver/Test method that is the entry point for execution
*/
public static void main(String[] args) {
new playerLoop().printPoem(); //a new player list and output in one step
}
}
playerLoop.main(null);