2D ARRAYS Unit 8/Vocab Integration
Arrays HW
import java.util.Scanner;
//Create a class for 2D array learning
public class Array{
public int[][] numbers = {{1,3,5},{7,9,11},{13,15,18},{21,23,25}};
//method1
public void numArray() {
System.out.println("\nArray: ");
int[][] numbers = {{1,3,5},{7,9,11},{13,15,18},{21,23,25}};
for(int i = 0;i<numbers.length;i++){
for(int j = 0; j < numbers[i].length;j++){
System.out.print(numbers[i][j]+" ");
}
System.out.println();
}
}
public void reversedArray(){
System.out.println("\nReversed 2dArray: ");
for(int i=numbers.length-1;i>=0;i--){
for(int j = 0; j <numbers[i].length;j++){
System.out.print(numbers[i][j]+" ");
}
System.out.println();
}
}
public void inputValue(){
int[][] numbers = {{1,3,5},{7,9,11},{13,15,18},{21,23,25}};
System.out.println("\n");
Scanner scanner = new Scanner(System.in); {
System.out.print("Row: ");
int inputRow = scanner.nextInt();
System.out.println(inputRow);
System.out.print("Column: ");
int inputColumn = scanner.nextInt();
System.out.println(inputColumn);
System.out.println("Row, Column: " + inputRow + ", " + inputColumn);
// System.out.println("\n");
System.out.println(numbers[inputRow][inputColumn]);
}
System.out.println();
}
public void findProd(){
int[][] numbers = {{1,3,5},{7,9,11},{13,15,18},{21,23,25}};
// System.out.println("\n");
int sumofProd = 0;
for(int i = 0; i<numbers.length; i++) {
int product = 1;
for(int j = 0; j < numbers[i].length; j++) {
product = product * numbers[i][j];
sumofProd += product;
}
}
System.out.println("product sum: " + sumofProd);
}
}
Array twoD = new Array();
twoD.numArray();
twoD.reversedArray();
twoD.inputValue();
twoD.findProd();
Casting, specifically for division: In a 2D array, you can use casting to convert the elements in the array from one data type to another. For example, if you have a 2D array of double values, you can use casting to convert them to int values and then perform integer division on the elements.
Wrapper classes: Wrapper classes, such as Integer and Double, are used to store primitive data types (such as int and double) as objects. This can be useful in a 2D array when you want to store elements of different data types in the same array.
Concatenation: Concatenation is the process of combining two or more strings into a single string. In a 2D array, you can use concatenation to combine the elements of the array into a single string. For example, if you have a 2D array of strings, you can use concatenation to create a single string that contains all the elements of the array.
Math class: The Math class in Java provides a variety of methods for performing mathematical operations, such as trigonometric functions, logarithms, and random number generation. In a 2D array, you can use the Math class to generate random numbers and perform other mathematical operations on the elements of the array.
Compound boolean expressions: A compound boolean expression is a logical expression that consists of two or more sub-expressions joined by logical operators, such as AND and OR. In a 2D array, you can use compound boolean expressions to perform complex operations on the elements of the array, such as searching for a specific element or sorting the array.
Truth tables: A truth table is a mathematical table that shows the truth values of a logical expression for all possible combinations of its variables. In a 2D array, you can use truth tables to evaluate the truth values of complex expressions involving the elements of the array.
Comparing numbers: In a 2D array, you can use comparison operators, such as >, <, and ==, to compare the elements of the array to each other or to a specified value. For example, you can use a for loop to iterate over the elements of the array and compare each element to a specified threshold value to determine whether it is greater than, less than, or equal to the threshold.
Comparing strings: In a 2D array, you can use the equals() method to compare the elements of the array to each other or to a specified string. For example, you can use a for loop to iterate over the elements of the array and compare each element to a specified search string to determine whether they are equal.
Comparing objects: In a 2D array, you can use the compareTo() method to compare the elements of the array to each other or to a specified object. For example, you can use a for loop to iterate over the elements of the array and compare each element to a specified target object to determine whether they are equal or whether one is greater than or less than the other.
for loops: In a 2D array, you can use a for loop to iterate over the elements of the array and perform operations on them. For example, you can use a for loop to search for a specific element in the array or to sort the elements of the array in ascending or descending order.
while loops: In a 2D array, you can use a while loop to iterate over the elements of the array and perform operations on them. For example, you can use a while loop to search for a specific element in the array or to sort the elements of the array in ascending or descending order.
Nested loops: In a 2D array, you can use nested loops to iterate over the elements of the array and perform operations on them. For example, you can use a nested loop to search for a specific element in the array or to sort the elements of the array in ascending or descending order.
Classes: In a 2D array, you can use a class to define the data type of the elements of the array. For example, you can create a Point class to represent a point in 2D space, and then create a 2D array of Point objects to store a set of points.
De Morgan's law: In a 2D array, you can use De Morgan's law to manipulate logical expressions involving the elements of the array. For example, if you have a 2D array of boolean values, you can use De Morgan's law to convert an expression involving the AND and OR operators into an equivalent expression involving the NOT and XOR operators.
public class DeMorganExample {
public static void main(String[] args) {
// Create a 2D array of boolean values
boolean[][] array = new boolean[][] {
{true, false, true},
{false, true, false},
{true, false, true}
};
// Use De Morgan's law to convert an expression involving the AND and OR operators
// into an equivalent expression involving the NOT and XOR operators
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
boolean a = array[i][j];
boolean b = array[i][j + 1];
boolean c = array[i + 1][j];
boolean d = array[i + 1][j + 1];
boolean e = !(a || b) && !(c || d);
}
}
}
}
public class Example {
public static void main(String[] args) {
// Create a 2D array of strings
String[][] array = new String[][] {
{"cat", "dog", "bird"},
{"apple", "banana", "cherry"},
{"red", "green", "blue"}
};
// Call the tester method
test(array);
}
// Tester method that generates output
public static void test(String[][] array) {
// Use a for loop to iterate over the elements of the array
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
// Compare the current element to a specified string
if (array[i][j].equals("cat")) {
// Use De Morgan's law to convert an expression involving the AND and OR operators
// into an equivalent expression involving the NOT and XOR operators
boolean a = !array[i][j].equals("dog");
boolean b = !array[i][j].equals("bird");
boolean c = !(a || b);
// Use concatenation to combine the elements of the array into a single string
String s = array[i][0] + ", " + array[i][1] + ", " + array[i][2];
// Create a Point class to represent a point in 2D space
class Point {
int x;
int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public String toString() {
return "(" + x + ", " + y + ")";
}
}
// Create a Point object using the current indices of the array
Point p = new Point(i, j);
// Print the resulting values
System.out.println("Element: " + array[i][j]);
System.out.println("De Morgan's law: " + c);
System.out.println("Concatenated string: " + s);
System.out.println("Point: " + p);
}
}
}
}
}