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;
|
|
|
|
|
String format = "|%" + width + "d ";
|
|
|
|
|
|
|
|
|
|
return format;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static double average(short[] row) {
|
|
|
|
|
short sum = 0;
|
|
|
|
|
|
|
|
|
|
for (short element : row) {
|
|
|
|
|
sum += element;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double result = sum / row.length;
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
2023-04-26 14:37:46 +03:00
|
|
|
|
short a, rows = 0, columns = 0;
|
2023-03-23 09:10:55 +02:00
|
|
|
|
String format;
|
2023-05-03 10:39:06 +03:00
|
|
|
|
|
2023-03-23 09:10:55 +02:00
|
|
|
|
Scanner input = new Scanner(System.in);
|
|
|
|
|
|
2023-05-03 10:39:06 +03:00
|
|
|
|
a = protectedInput("Input a constant to multipy a matrix by: ",
|
|
|
|
|
"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:");
|
|
|
|
|
|
|
|
|
|
format = format(rows * columns);
|
|
|
|
|
|
|
|
|
|
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-03-23 09:10:55 +02:00
|
|
|
|
matrix_B[i][j] *= (short) (a);
|
|
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|