mirror of
https://github.com/ASDjonok/OOP_IO-2x_2023.git
synced 2026-04-21 21:34:35 +03:00
Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 6fc3cc244e | |||
| 3bbb7c8e54 | |||
| 1af299f8f7 | |||
| 4924bbe62f | |||
| 29e63163a4 | |||
| 399941844f | |||
| f385bff73d | |||
| 6d8171ad4c |
@@ -0,0 +1,2 @@
|
||||
# Project exclude paths
|
||||
/out/
|
||||
@@ -1,39 +0,0 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Lab_1 {
|
||||
public static void main(String[] args) {
|
||||
int n, m, a, b;
|
||||
float s = 0;
|
||||
|
||||
Scanner input = new Scanner(System.in);
|
||||
|
||||
System.out.print("Enter n: ");
|
||||
n = input.nextInt();
|
||||
|
||||
System.out.print("Enter m: ");
|
||||
m = input.nextInt();
|
||||
|
||||
do {
|
||||
System.out.print("Enter a (can't be above n): ");
|
||||
a = input.nextInt();
|
||||
} while (a > n);
|
||||
|
||||
do {
|
||||
System.out.print("Enter b (can't be above m): ");
|
||||
b = input.nextInt();
|
||||
} while (b > m);
|
||||
|
||||
if ((a <= 0) && (n >= 0)) {
|
||||
System.out.println("Error, division by zero, shutting down!");
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
for (int i = a; i < n; i++) {
|
||||
for (int j = b; j < m; j++) {
|
||||
s += i * j / i;
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("S = " + s);
|
||||
}
|
||||
}
|
||||
@@ -1,108 +0,0 @@
|
||||
import java.util.Scanner;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class Lab_2 {
|
||||
|
||||
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) {
|
||||
short a, rows = 0, columns = 0, width;
|
||||
String format;
|
||||
|
||||
Scanner input = new Scanner(System.in);
|
||||
|
||||
do {
|
||||
try {
|
||||
System.out.println();
|
||||
System.out.print("Input a constant to multipy a matrix by: ");
|
||||
|
||||
a = input.nextShort();
|
||||
break;
|
||||
} catch (Exception e) {
|
||||
System.out.println("A constant must be a short-data type integer, try again.");
|
||||
input.nextLine();
|
||||
}
|
||||
} while (true);
|
||||
|
||||
System.out.println();
|
||||
System.out.println("Input size of the matrix.");
|
||||
|
||||
do {
|
||||
try {
|
||||
System.out.print("Rows: ");
|
||||
rows = input.nextShort();
|
||||
} catch (Exception e) {
|
||||
System.out.println("A number of rows must be a short-data type integer, try again.");
|
||||
input.nextLine();
|
||||
}
|
||||
} while (rows <= 0);
|
||||
|
||||
do {
|
||||
try {
|
||||
System.out.print("Columns: ");
|
||||
columns = input.nextShort();
|
||||
} catch (Exception e) {
|
||||
System.out.println("A number of columns must be a short-data type integer, try again.");
|
||||
input.nextLine();
|
||||
}
|
||||
} while (columns <= 0);
|
||||
|
||||
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);
|
||||
|
||||
for (short i = 0; i < rows; i++) {
|
||||
for (short j = 0; j < columns; j++) {
|
||||
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));
|
||||
}
|
||||
|
||||
input.close();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user