FRQ #1
import java.lang.Math;
public class NumberCube {
public int toss() {
return 1 + (int)(Math.random() * 6);
}
}
a) cubeTosses
method
Solution:
import java.lang.Math;
public class NumberCube {
public int toss() {
return 1 + (int)(Math.random() * 6);
}
// Begin solution to problem
public int[] getCubeTosses(NumberCube cube, int numTosses) {
// Initialize an array of integers with length equal to numTosses
int[] results = new int[numTosses];
// Loop for numTosses
for (int i = 0; i < numTosses; i ++) {
// Set each value to a cube roll; must be done this way because Java arrays are FIXED IN SIZE
results[i] = cube.toss();
};
return results;
}
// End soluton to problem
};
// Create new object for testing
NumberCube dice = new NumberCube();
// Set results to a variable, because otherwise it returns a memory address/pointer
int[] list = dice.getCubeTosses(dice, 5);
// Print each item in the list
for (int item : list) {
System.out.println(item);
}
5
6
1
2
3
CollegeBoard Grade:
- 1 for correctly constructing arrays
- 2.5 for functionality
- .5 for return
b) getLongestRun
method
Solution:
import java.lang.Math;
public class NumberCube {
public int toss() {
return 1 + (int)(Math.random() * 6);
}
public int[] getCubeTosses(NumberCube cube, int numTosses) {
// Initialize an array of integers with length equal to numTosses
int[] results = new int[numTosses];
// Loop for numTosses
for (int i = 0; i < numTosses; i ++) {
// Set each value to a cube roll; must be done this way because Java arrays are FIXED IN SIZE
results[i] = cube.toss();
};
return results;
}
// Begin solution to problem
public static int getLongestRun(int[] values) {
// If there are no values, return -1
if (values.length == 0) {
return -1;
};
// Create a new integer for the maximum value, zero for default
int maxIndex = 0;
// For every value
for (int i = 0; i < values.length; i ++) {
// If the value is bigger than the current max, replace the max with it
if (values[maxIndex] < values[i]) {
maxIndex = i;
}
};
// By the end of the loop, the index of the max will be returned
return maxIndex;
};
// End solution to problem
};
// Create new object for testing
NumberCube dice = new NumberCube();
// Set results to a variable, because otherwise it returns a memory address/pointer
int[] list = dice.getCubeTosses(dice, 5);
// Print each item in the list
for (int item : list) {
System.out.println(item);
}
// Get the index of the max
dice.getLongestRun(list)
3
6
3
1
6
1
CollegeBoard Grading:
- 1 for iteration
- 1 for comparing values
- 1 checks value for each run
- 1 for identifying maximum
- 1 for returning max or -1
(Initially, I would have only gotten 2 points because my for loop did not work.)
Key Takeaways:
- Java is very similar to JavaScript (crazy, I know)
- Arrays are FIXED IN SIZE
- You must define size and data type when creating an array
- Example:
int[] arrayName = new int[length]
int[]
= list of integersarrayName
= name of the arraynew int[length]
= creates a new integer array of a length (replacelength
with an integer)
- Creating objects in Java:
- Keywords: Come before defining things
public
= can be accessed from anywhere in the code, similar tovar
private
= only accessible within the declared class, similar tolet
, incompatible withstatic
static
= used to define methods that can be used by all instances of an objectvoid
= used in place of return value type to indicate no return
- Parameters:
- Define parameter type and name for access
int
,String
,char
,float
,boolean
- Define parameter type and name for access
- Keywords: Come before defining things