Senior
/*1. Fill a 10x10 matrix 2. Output to console as shown below Implement through two separate methods | [phone]| | [phone]| | [phone]| | [phone]| | [phone]| | [phone]| | [phone]| | [phone]| | [phone]| | [phone]| */
sobes.tech AI
Answer from AI
Below is a Java example with two methods: one fills a 10x10 matrix with the text "[phone]" in each cell, and the other prints it to the console in a format where each row of the matrix is displayed as a line with vertical bars and spaces, as in the example.
public class PhoneMatrix {
private static final int SIZE = 10;
private String[][] matrix = new String[SIZE][SIZE];
// Method to fill the matrix
public void fillMatrix() {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
matrix[i][j] = "[phone]";
}
}
}
// Method to print the matrix
public void printMatrix() {
for (int i = 0; i < SIZE; i++) {
StringBuilder sb = new StringBuilder();
sb.append("|");
for (int j = 0; j < SIZE; j++) {
sb.append(" ").append(matrix[i][j]);
}
sb.append("|");
System.out.println(sb.toString());
}
}
public static void main(String[] args) {
PhoneMatrix pm = new PhoneMatrix();
pm.fillMatrix();
pm.printMatrix();
}
}
The output will look like:
| [phone] [phone] [phone] [phone] [phone] [phone] [phone] [phone] [phone] [phone]|
| [phone] [phone] [phone] [phone] [phone] [phone] [phone] [phone] [phone] [phone]|
... (total 10 lines)
If you need to output only one element per line, as in the example, you can modify the printMatrix method to print one element per line.