2023-05-08 10:44:41 +03:00
|
|
|
|
package OOP.Java.lab_2;
|
|
|
|
|
|
2023-03-23 09:10:55 +02:00
|
|
|
|
import java.util.Scanner;
|
|
|
|
|
|
2023-05-07 15:19:06 +03:00
|
|
|
|
public class lab_2 {
|
2023-03-23 09:10:55 +02:00
|
|
|
|
|
2023-05-03 10:32:23 +03:00
|
|
|
|
public static short protectedInput(String inputPrompt, String errorMessage, Scanner input) {
|
|
|
|
|
short read_variable;
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
try {
|
|
|
|
|
System.out.println();
|
|
|
|
|
System.out.print(inputPrompt);
|
|
|
|
|
|
|
|
|
|
read_variable = input.nextShort();
|
|
|
|
|
break;
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
System.out.println(errorMessage);
|
|
|
|
|
input.nextLine();
|
|
|
|
|
}
|
|
|
|
|
} while (true);
|
|
|
|
|
|
|
|
|
|
return read_variable;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-23 09:10:55 +02:00
|
|
|
|
public static String format(int number) {
|
|
|
|
|
int width = String.valueOf(number).length() + 1;
|
|
|
|
|
|
2023-05-08 10:44:41 +03:00
|
|
|
|
return "|%" + width + "d ";
|
2023-03-23 09:10:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static double average(short[] row) {
|
|
|
|
|
short sum = 0;
|
|
|
|
|
|
|
|
|
|
for (short element : row) {
|
|
|
|
|
sum += element;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-08 10:44:41 +03:00
|
|
|
|
return (double) sum / row.length;
|
2023-03-23 09:10:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
2023-05-08 10:44:41 +03:00
|
|
|
|
short rows, columns;
|
2023-05-03 10:39:06 +03:00
|
|
|
|
|
2023-03-23 09:10:55 +02:00
|
|
|
|
Scanner input = new Scanner(System.in);
|
|
|
|
|
|
2023-05-08 10:44:41 +03:00
|
|
|
|
final short a = protectedInput("Input a constant to multiply a matrix by: ",
|
2023-05-03 10:39:06 +03:00
|
|
|
|
"A constant must be a short-data type integer, try again.", input);
|
|
|
|
|
|
2023-03-23 09:10:55 +02:00
|
|
|
|
System.out.println();
|
|
|
|
|
System.out.println("Input size of the matrix.");
|
|
|
|
|
|
|
|
|
|
do {
|
2023-05-03 10:39:06 +03:00
|
|
|
|
rows = protectedInput("Rows: ",
|
|
|
|
|
"A number of rows must be a short-data type integer, try again.", input);
|
2023-03-23 09:10:55 +02:00
|
|
|
|
} while (rows <= 0);
|
2023-05-03 10:39:06 +03:00
|
|
|
|
|
2023-03-23 09:10:55 +02:00
|
|
|
|
do {
|
2023-05-03 10:39:06 +03:00
|
|
|
|
columns = protectedInput("Columns: ",
|
|
|
|
|
"A number of columns must be a short-data type integer, try again.", input);
|
2023-03-23 09:10:55 +02:00
|
|
|
|
} while (columns <= 0);
|
2023-05-03 10:39:06 +03:00
|
|
|
|
|
2023-05-03 10:32:23 +03:00
|
|
|
|
input.close();
|
2023-03-23 09:10:55 +02:00
|
|
|
|
|
|
|
|
|
short[][] matrix_B = new short[rows][columns];
|
|
|
|
|
|
|
|
|
|
System.out.println();
|
|
|
|
|
System.out.println("Matrix B:");
|
|
|
|
|
|
2023-05-08 10:44:41 +03:00
|
|
|
|
String format = format(rows * columns);
|
2023-03-23 09:10:55 +02:00
|
|
|
|
|
|
|
|
|
for (short i = 0; i < rows; i++) {
|
|
|
|
|
for (short j = 0; j < columns; j++) {
|
|
|
|
|
matrix_B[i][j] = (short) ((i + 1) * (j + 1));
|
|
|
|
|
|
|
|
|
|
System.out.printf(format, matrix_B[i][j]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
System.out.println("|");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
System.out.println();
|
|
|
|
|
System.out.println("Matrix a×B:");
|
|
|
|
|
|
|
|
|
|
format = format(rows * columns * a);
|
|
|
|
|
|
2023-05-03 10:32:23 +03:00
|
|
|
|
for (short i = 0; i < matrix_B.length; i++) {
|
|
|
|
|
for (short j = 0; j < matrix_B[i].length; j++) {
|
2023-05-08 10:44:41 +03:00
|
|
|
|
matrix_B[i][j] *= (a);
|
2023-03-23 09:10:55 +02:00
|
|
|
|
|
|
|
|
|
System.out.printf(format, matrix_B[i][j]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
System.out.println("|");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
System.out.println();
|
|
|
|
|
System.out.println("Averages of each row:");
|
|
|
|
|
|
|
|
|
|
for (short[] row : matrix_B) {
|
|
|
|
|
System.out.println(average(row));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|