#assignment in java
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
here is my code
import java.util.Scanner;
public class Midterms1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Define the number of rows and columns
int numRows;
int numCols;
System.out.print("Enter Number of Rows: ");
numRows = scanner.nextInt();
System.out.print("Enter Number of Columns: ");
numCols = scanner.nextInt();
int numCols2 = numCols - 1;
if (numCols % 2 == 0) {
numCols = numCols2;
}
// Create the 2D character array
char[][] matrix = new char[numRows][numCols];
// Populate the matrix with the given values
char ch = 'a';
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numCols; j++) {
matrix[i][j] = ch;
ch++;
if (ch > 'z') {
ch = 'a';
}
}
}
// Print the matrix
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numCols; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
// Print the V shape
System.out.print("V Output: ");
int midRow = numRows / 2;
int midCol = numCols / 2;
// Print the letters forming the V shape
// Print the V shape
System.out.print("V Output: ");
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numCols; j++) {
if ((i == j && i <= midRow && j <= midCol) || (j == numCols - i - 1 && i <= midRow && j >= midCol)) {
System.out.print(matrix[i][j] + " ");
}
}
}
System.out.println();
}
}