Changing / Adding Data to Arrays
I created an array with the values 1, 2, 3, 4, 5, 6, 7, 8, 9. Then I replaced the value at row 2 and column 1 with 99 to make it 1, 2, 3, 4, 5, 99, 7, 8, 9. Then I added the number 6 at the end to make it 1, 2, 10, 4, 5, 6. Finally I took a user input and added that at the end as well.
public class ArrayOperations {
public static void main(String[] args) {
// Creates an array with values 1, 2, 3, 4, 5
int[][] myArray = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
// Changes the value at index 2 to 10
myArray[2][1] = 99;
// Declares a new variable `newValue1` and assigns it the value 6
int newValue1 = 6;
// Copies the original array with an additional element
myArray = Arrays.copyOf(myArray, myArray.length + 1);
// Creates a new row in the array and assigns the value of `newValue1` to the last element
myArray[myArray.length - 1] = new int[]{newValue1};
// Get user input for the new value
Scanner scanner = new Scanner(System.in);
// Prompts the user for input
System.out.print("Enter a new value to add to the array: ");
// Reads an integer from the user
int newValue2 = scanner.nextInt();
scanner.close();
// Copies the array with an additional element
myArray = Arrays.copyOf(myArray, myArray.length + 1);
// Creates a new row in the array and assigns the value of `newValue2` to the last element
myArray[myArray.length - 1] = new int[]{newValue2};
// Print the updated array
System.out.println(Arrays.deepToString(myArray));
}
}
ArrayOperations.main(null);
Enter a new value to add to the array: [[1, 2, 3], [4, 5, 6], [7, 99, 9], [6], [19]]
Display content backwards
I started off my creating a single 3x3 array. Then I reversed it by swapping the elements at the beginning with the elements at the end. Then I outputted the array and it was printed backwards.
public class Reverse2DArray {
public static void main(String[] args) {
// Create a 2D array with 3 rows and 3 columns
int[][] array = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
// Print a message indicating that this is the original array
System.out.println("Original Array:");
// Display the content of the array
displayArray(array);
// Print a message indicating that the array is being reversed
System.out.println("\nReversed Array:");
// Reverse the content of the array
reverseArray(array);
// Display the reversed content of the array
displayArray(array);
}
// Method to reverse the content of the array
public static void reverseArray(int[][] array) {
// Iterate through each row of the array
for (int i = 0; i < array.length; i++) {
// Iterate through each element in the current row
for (int j = 0; j < array[i].length / 2; j++) {
// Swap the element at (i, j) with the element at (i, array[i].length - 1 - j)
int temp = array[i][j];
array[i][j] = array[i][array[i].length - 1 - j];
array[i][array[i].length - 1 - j] = temp;
}
}
}
// Method to display the content of the array
public static void displayArray(int[][] array) {
// Iterate through each row of the array
for (int i = 0; i < array.length; i++) {
// Iterate through each element in the current row
for (int j = 0; j < array[i].length; j++) {
// Print the element followed by a space
System.out.print(array[i][j] + " ");
}
// Move to the next line after printing each row
System.out.println();
}
}
}
Reverse2DArray.main(null);
Original Array:
1 2 3
4 5 6
7 8 9
Reversed Array:
3 2 1
6 5 4
9 8 7
Problem:
Part A:
Explaination:
This question asked me to create a 2D Array of lights with the following condition.
- Each light had a 40% chance of being turned on.
public class LightBoard {
// Declaration of a 2D array to represent the state of lights
private boolean[][] lights;
public LightBoard(int numRows, int numCols) {
// Initialize the lights array with given dimensions
lights = new boolean[numRows][numCols];
// Loop through rows
for (int r = 0; r < numRows; r++) {
// Loop through columns
for (int c = 0; c < numCols; c++) {
// Generate a random number between 0 and 1
double chance = Math.random();
// Set light to true if random number is less than 0.4
lights[r][c] = chance < 0.4;
}
}
}
public static void main(String[] args) {
// Define the number of rows for the light board
int numRows = 7;
// Define the number of columns for the light board
int numCols = 5;
// Create a new LightBoard instance
LightBoard lightBoard = new LightBoard(numRows, numCols);
// Print the initial state of the lights
// Loop through rows
for (int r = 0; r < numRows; r++) {
// Loop through columns
for (int c = 0; c < numCols; c++) {
// Print 1 if light is on, 0 if off
System.out.print(lightBoard.lights[r][c] ? "1 " : "0 ");
}
// Move to the next line after each row
System.out.println();
}
}
}
LightBoard.main(null);
1 0 0 0 0
1 0 1 1 1
0 0 0 1 0
0 1 0 0 1
1 0 0 1 0
0 0 1 1 0
0 1 0 1 0
Part B:
Explaination:
In order to answer this question, I created a method named public boolean evaluateLight
to generate a new 2D array based on the provided criteria. The rules were as follows:
- If the light is on, return false if the number of lights in its column that are on is even, including the current light.
- If the light is off, return true if the number of lights in its column that are on is divisible by three.
- Otherwise, return the light’s current status.
To achieve this, I used the existing lights array and applied the logic within the evaluateLight
method. This method traversed the array, counting the number of on lights in the same column. It then used conditional statements to determine the final status of each light based on the specified rules. The array in the output is the same array from Part A. The evaluateLight part gives the output of a given coordinate (row,column) after passing through the above three rules.
public class LightBoard {
// Declaration of a 2D array to represent the state of lights
private boolean[][] lights;
public LightBoard(int numRows, int numCols) {
// Initialize the lights array with given dimensions
lights = new boolean[numRows][numCols];
// Loop through rows
for (int r = 0; r < numRows; r++) {
// Loop through columns
for (int c = 0; c < numCols; c++) {
// Generate a random number between 0 and 1
double rnd = Math.random();
// Set light to true if random number is less than 0.4
lights[r][c] = rnd < 0.4;
}
}
}
public boolean evaluateLight(int row, int col) {
// Initialize a counter for the number of lights turned on
int numOn = 0;
// Loop through rows
for (int r = 0; r < lights.length; r++) {
// If the light in the same column is on
if (lights[r][col]) {
// Increment the counter
numOn++;
}
}
// If the light is currently on and the number of on lights in the column is even
if (lights[row][col] && numOn % 2 == 0) {
// Turn the light off
return false;
}
// If the light is currently off and the number of on lights in the column is a multiple of 3
if (!lights[row][col] && numOn % 3 == 0) {
// Turn the light on
return true;
}
// Otherwise, keep the light in its current state
return lights[row][col];
}
public static void main(String[] args) {
// Define the number of rows for the light board
int numRows = 7;
// Define the number of columns for the light board
int numCols = 5;
// Create a new LightBoard instance
LightBoard lightBoard = new LightBoard(numRows, numCols);
// Print the initial state of the lights
// Loop through rows
for (int r = 0; r < numRows; r++) {
// Loop through columns
for (int c = 0; c < numCols; c++) {
// Print 1 if light is on, 0 if off
System.out.print(lightBoard.lights[r][c] ? "1 " : "0 ");
}
// Move to the next line after each row
System.out.println();
}
// Example calls to evaluateLight
System.out.println("");
System.out.println("evaluateLight(0, 3): " + lightBoard.evaluateLight(0, 3));
System.out.println("evaluateLight(6, 0): " + lightBoard.evaluateLight(6, 0));
System.out.println("evaluateLight(4, 1): " + lightBoard.evaluateLight(4, 1));
System.out.println("evaluateLight(5, 4): " + lightBoard.evaluateLight(5, 4));
}
}
LightBoard.main(null);
1 0 1 1 1
1 0 0 1 1
0 0 1 0 0
0 1 0 1 1
0 1 1 0 0
0 0 1 0 1
1 1 0 1 1
evaluateLight(0, 3): false
evaluateLight(6, 0): true
evaluateLight(4, 1): true
evaluateLight(5, 4): true