mirror of
https://github.com/ASDjonok/OOP_IO-2x_2023.git
synced 2026-04-02 21:41:49 +03:00
Compare commits
8 Commits
896fc1f4d4
...
master_bef
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6fc3cc244e | ||
|
|
3bbb7c8e54 | ||
|
|
1af299f8f7 | ||
|
|
4924bbe62f | ||
|
|
29e63163a4 | ||
|
|
399941844f | ||
|
|
f385bff73d | ||
|
|
6d8171ad4c |
4
.gitignore
vendored
4
.gitignore
vendored
@@ -1,2 +1,2 @@
|
||||
Lab 1/Lab_1.class
|
||||
Lab 2/Lab_2.class
|
||||
# Project exclude paths
|
||||
/out/
|
||||
@@ -1,41 +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);
|
||||
|
||||
input.close();
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
107
Lab 2/Lab_2.java
107
Lab 2/Lab_2.java
@@ -1,107 +0,0 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
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;
|
||||
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