FRQ4 - 2D Arrays
Relationship of FRQ 4 to Project Based learning.
2D Array Background
These are some key skills for 2D arrays.
- Nested for loops, perhaps enhanced for loops.
- Zero based counting.
- Access of x and y coordinates. Find properties at coordinates.
- sim.evaluateLight(0, 3);
- sim.evaluateLight(6, 0);
- sim.evaluateLight(4, 1);
- Perform counts on values in columns or rows.
Ideation
Sahill class of '23 discussion... For 2019 FRQ4, one idea I had was maybe turning each light into an object, since right now each element of the 2D array is simply a Boolean. Each light can have Boolean property for off/on, but also additional method to turn on the light (this would be the same as a setter, but it is more abstracted and cleaner in my opinion). An interesting idea could be to have a luminosity property and a quality property. We can start off each light object with a certain luminosity, say 100, and then decrement this luminosity based on quality value. For example, if quality is 1, then the luminosity decreases by 1 every minute (if the light is on). We can then have an API endpoint that tracks the luminosity for each light.
Code ideation does not always follow conversation. As you can see, the light board started out looking more like a color palette. This was because conversation(s) were ideas, but real purpose is yet to be found.
import java.util.HashMap;
import java.util.Map;
public class Light {
boolean on;
short red;
short green;
short blue;
short effect;
/* ANSI effects
n Name Note
0 Reset or normal All attributes off
1 Bold or increased intensity As with faint, the color change is a PC (SCO / CGA) invention.[38][better source needed]
2 Faint, decreased intensity, or dim May be implemented as a light font weight like bold.[39]
3 Italic Not widely supported. Sometimes treated as inverse or blink.[38]
4 Underline Style extensions exist for Kitty, VTE, mintty and iTerm2.[40][41]
5 Slow blink Sets blinking to less than 150 times per minute
6 Rapid blink MS-DOS ANSI.SYS, 150+ per minute; not widely supported
7 Reverse video or invert Swap foreground and background colors; inconsistent emulation[42]
8 Conceal or hide Not widely supported.
9 Crossed-out, or strike Characters legible but marked as if for deletion. Not supported in Terminal.app
*/
private final Map<Short, String> EFFECT = new HashMap<>();
{
// Map<"separator", not_used>
EFFECT.put((short) 0, "Normal");
EFFECT.put((short) 1, "Bold");
EFFECT.put((short) 2, "Faint");
EFFECT.put((short) 3, "Italic");
EFFECT.put((short) 4, "Underline");
EFFECT.put((short) 5, "Slow Blink");
EFFECT.put((short) 6, "Fast Blink");
EFFECT.put((short) 7, "Reverse");
EFFECT.put((short) 8, "Conceal");
EFFECT.put((short) 9, "Crossed_out");
}
/* Assign random colors and effects */
public Light() {
int maxColor = 255;
int effect = 9;
this.red = (short) (Math.random()*(maxColor+1));
this.green = (short) (Math.random()*(maxColor+1));
this.blue = (short) (Math.random()*(maxColor+1));
this.effect = (short) (Math.random()*(effect+1));
}
public String getEffectTitle() {
return EFFECT.get(this.effect);
}
public String getRGB() {
return ( "#" +
String.format("%02X", this.red) +
String.format("%02X", this.green) +
String.format("%02X", this.blue)
);
}
/* toString output as key/values */
public String toString() {
return( "{" +
"\"red\": " + red + "," +
"\"green\": " + green + "," +
"\"blue\": " + blue + "," +
"\"effect\": " + "\"" + EFFECT.get(effect) + "\"" +
"}" );
}
public boolean isOn() {
return on;
}
public void setOn(boolean on) {
this.on = on;
}
public short getRed() {
return red;
}
public short getGreen() {
return green;
}
public short getBlue() {
return blue;
}
public short getEffect() {
return effect;
}
static public void main(String[] args) {
// create and display LightBoard
Light light = new Light();
System.out.println(light); // use toString() method
}
}
Light.main(null);
public class LightBoard {
private Light[][] lights;
/* Initialize LightBoard and Lights */
public LightBoard(int numRows, int numCols) {
this.lights = new Light[numRows][numCols];
// 2D array nested loops, used for initialization
for (int row = 0; row < numRows; row++) {
for (int col = 0; col < numCols; col++) {
lights[row][col] = new Light(); // each cell needs to be constructed
}
}
}
/* Output is intended for API key/values */
public String toString() {
String outString = "[";
// 2D array nested loops, used for reference
for (int row = 0; row < lights.length; row++) {
for (int col = 0; col < lights[row].length; col++) {
outString +=
// data
"{" +
"\"row\": " + row + "," +
"\"column\": " + col + "," +
"\"light\": " + lights[row][col] + // extract toString data
"}," ;
}
}
// remove last comma, newline, add square bracket, reset color
outString = outString.substring(0,outString.length() - 1) + "]";
return outString;
}
/* Output is intended for Terminal, effects added to output */
public String toTerminal() {
String outString = "[";
// 2D array nested loops, used for reference
for (int row = 0; row < lights.length; row++) {
for (int col = 0; col < lights[row].length; col++) {
outString +=
// reset
"\033[m" +
// color
"\033[38;2;" +
lights[row][col].getRed() + ";" + // set color using getters
lights[row][col].getGreen() + ";" +
lights[row][col].getBlue() + ";" +
lights[row][col].getEffect() + "m" +
// data, extract custom getters
"{" +
"\"" + "RGB\": " + "\"" + lights[row][col].getRGB() + "\"" +
"," +
"\"" + "Effect\": " + "\"" + lights[row][col].getEffectTitle() + "\"" +
"}," +
// newline
"\n" ;
}
}
// remove last comma, newline, add square bracket, reset color
outString = outString.substring(0,outString.length() - 2) + "\033[m" + "]";
return outString;
}
/* Output is intended for Terminal, draws color palette */
public String toColorPalette() {
// block sizes
final int ROWS = 5;
final int COLS = 10;
// Build large string for entire color palette
String outString = "";
// find each row
for (int row = 0; row < lights.length; row++) {
// repeat each row for block size
for (int i = 0; i < ROWS; i++) {
// find each column
for (int col = 0; col < lights[row].length; col++) {
// repeat each column for block size
for (int j = 0; j < COLS; j++) {
// print single character, except at midpoint print color code
String c = (i == (int) (ROWS / 2) && j == (int) (COLS / 2) )
? lights[row][col].getRGB()
: (j == (int) (COLS / 2)) // nested ternary
? " ".repeat(lights[row][col].getRGB().length())
: " ";
outString +=
// reset
"\033[m" +
// color
"\033[38;2;" +
lights[row][col].getRed() + ";" +
lights[row][col].getGreen() + ";" +
lights[row][col].getBlue() + ";" +
"7m" +
// color code or blank character
c +
// reset
"\033[m";
}
}
outString += "\n";
}
}
// remove last comma, newline, add square bracket, reset color
outString += "\033[m";
return outString;
}
static public void main(String[] args) {
// create and display LightBoard
LightBoard lightBoard = new LightBoard(5, 5);
System.out.println(lightBoard); // use toString() method
System.out.println(lightBoard.toTerminal());
System.out.println(lightBoard.toColorPalette());
}
}
LightBoard lightBoard = new LightBoard(5, 5);
System.out.println(lightBoard); // use toString() method
LightBoard lightBoard = new LightBoard(5, 5);
System.out.println(lightBoard.toTerminal());
LightBoard lightBoard = new LightBoard(5, 5);
System.out.println(lightBoard.toColorPalette());
Hacks
There are many ideas and possibilities with 2D arrays.
Link- College Board Skill: Build vocab from unit 8, incorporate vocab and definitions with code examples
Basic:- Java skill. Complete College Board FRQ4. Can be found here:-
Java skill. Establish key concepts and code examples on 2D arrays using FRQ4 and code above. Build vocab and add a new terminal concept.
-
Examples:
- add usability for on/off or effects
- edit size of 2d array
- change size of each color palette cell
- Input color values instead of setting values using Math.random
-
Extra (+++):
- Create API endpoints similar to previous FRQ's, use postman or curl to test
Advanced:
- Full stack skill. Think about Frontend, Backend use case to make the existing color board into more of a light board show. Perhaps on a timer to change Frontend color board to be a light show, with all the CSS and HTML possibilities. Ideate something of your own, break away from board.