Skip to content
This repository was archived by the owner on Dec 28, 2024. It is now read-only.

Commit b0448db

Browse files
authored
Merge pull request #665 from Kr1sM0L/main
Лабораторные работы №22-30 и 32 (частично GUI)
2 parents 8e094e2 + 9498509 commit b0448db

File tree

66 files changed

+987
-5
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+987
-5
lines changed

students/23K0362/23K0362-p20/src/main/java/mirea/lab20/task123/Animal.java renamed to students/23K0362/23K0362-p20/src/main/java/ru/mirea/lab20/task123/Animal.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package ru.mirea.lab20.task1_3;
1+
package mirea.lab20.task123;
22

33
import java.io.Serializable;
44

students/23K0362/23K0362-p20/src/main/java/mirea/lab20/task123/GenericClass.java renamed to students/23K0362/23K0362-p20/src/main/java/ru/mirea/lab20/task123/GenericClass.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package ru.mirea.lab20.task1_3;
1+
package mirea.lab20.task123;
22

33
/*
44
1. Создать обобщенный класс с тремя параметрами (T, V, K).
@@ -58,4 +58,4 @@ public void printNamesClasses() {
5858
public String toString() {
5959
return "GenericClass{" + "tend=" + tend + ", value=" + value + ", key=" + key + '}';
6060
}
61-
}
61+
}

students/23K0362/23K0362-p20/src/main/java/mirea/lab20/task123/TestGenericClass.java renamed to students/23K0362/23K0362-p20/src/main/java/ru/mirea/lab20/task123/TestGenericClass.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
package ru.mirea.lab20.task1_3;
1+
package mirea.lab20.task123;
22

3-
public class testGenericClass {
3+
abstract class TestGenericClass {
44
public static void main(String[] args) {
55
GenericClass<String, Animal, Integer> genericClassExample = new GenericClass<>("Example",
66
new Animal("Жужелик", "Собака", 5), 5);
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package mirea.lab20.task4;
2+
3+
/*
4+
Написать класс Калькулятор
5+
(необобщенный), который содержит обобщенные статические методы -
6+
sum, multiply, divide, subtraction. Параметры этих методов - два числа
7+
разного типа, над которыми должна быть произведена операция.
8+
*/
9+
10+
public class Calculator {
11+
public <T extends Number, S extends Number> double sum(T firstValue, S secondValue) {
12+
return firstValue.doubleValue() + secondValue.doubleValue();
13+
}
14+
15+
public <T extends Number, S extends Number> double divide(T firstValue, S secondValue) {
16+
return firstValue.doubleValue() - secondValue.doubleValue();
17+
}
18+
19+
public <T extends Number, S extends Number> double multiply(T firstValue, S secondValue) {
20+
return firstValue.doubleValue() * secondValue.doubleValue();
21+
}
22+
23+
public <T extends Number, S extends Number> double substraction(T firstValue, S secondValue) {
24+
try {
25+
if (secondValue.doubleValue() == 0) {
26+
throw new NullPointerException("Must be not 0 values in denominator.");
27+
}
28+
return firstValue.doubleValue() / secondValue.doubleValue();
29+
} catch (Exception e) {
30+
System.out.println(e.getMessage());
31+
return 0.0;
32+
}
33+
}
34+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package mirea.lab20.task4;
2+
3+
/*
4+
4. Написать обобщенный класс MinMax, который содержит
5+
методы для нахождения минимального и максимального элемента
6+
массива. Массив является переменной класса. Массив должен
7+
передаваться в класс через конструктор. Написать класс Калькулятор
8+
(необобщенный), который содержит обобщенные статические методы -
9+
sum, multiply, divide, subtraction. Параметры этих методов - два числа
10+
разного типа, над которыми должна быть произведена операция.
11+
*/
12+
13+
public class FindMaxMin<T extends Comparable<T>> {
14+
private final T[] arrayValues;
15+
16+
public FindMaxMin(T[] arrayValues) {
17+
this.arrayValues = arrayValues;
18+
}
19+
20+
public T findMin() {
21+
try {
22+
if (arrayValues == null || arrayValues.length == 0) {
23+
throw new IllegalArgumentException("Must values in input array.");
24+
}
25+
T minValue = arrayValues[0];
26+
for (int i = 1; i < arrayValues.length; i++) {
27+
if (arrayValues[i].compareTo(minValue) < 0) {
28+
minValue = arrayValues[i];
29+
}
30+
}
31+
return minValue;
32+
} catch (Exception e) {
33+
System.out.println(e.getMessage());
34+
return null;
35+
}
36+
}
37+
38+
public T findMax() {
39+
try {
40+
if (arrayValues == null || arrayValues.length == 0) {
41+
throw new IllegalArgumentException("Must values in input array.");
42+
}
43+
T maxValue = arrayValues[0];
44+
for (int i = 1; i < arrayValues.length; i++) {
45+
if (arrayValues[i].compareTo(maxValue) > 0) {
46+
maxValue = arrayValues[i];
47+
}
48+
}
49+
return maxValue;
50+
} catch (Exception e) {
51+
System.out.println(e.getMessage());
52+
return null;
53+
}
54+
}
55+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package mirea.lab20.task4;
2+
3+
abstract class Test {
4+
public static void main(String[] args) {
5+
String[] strMass = {"Dof", "", "jkdsjkjds"};
6+
Integer[] integerMass = {1, 2345, 34567, -87654, 0, 43};
7+
FindMaxMin<String> findMxMnStr = new FindMaxMin<>(strMass);
8+
System.out.println("Max value > \"" + findMxMnStr.findMax() + "\", min value > \"" + findMxMnStr.findMin() + "\"");
9+
FindMaxMin<Integer> findMxMnInt = new FindMaxMin<>(integerMass);
10+
System.out.println("Max value > " + findMxMnInt.findMax() + ", min value > " + findMxMnInt.findMin());
11+
/*
12+
Max value > "jkdsjkjds", min value > ""
13+
Max value > 34567, min value > -87654
14+
*/
15+
Calculator calc = new Calculator();
16+
System.out.println("Sum: " + calc.sum(23456.3454, 234));
17+
System.out.println("Multiply: " + calc.multiply(6.3454, -3456));
18+
System.out.println("Divide: " + calc.divide(2354, -45.345));
19+
System.out.println("Subtraction: " + calc.substraction(236.3454, 0));
20+
System.out.println("Subtraction: " + calc.substraction(236.3454, 54));
21+
/*
22+
Sum: 23690.3454
23+
Multiply: -21929.7024
24+
Divide: 2399.345
25+
Must be not 0 values in denominator.
26+
Subtraction: 0.0
27+
Subtraction: 4.376766666666667
28+
*/
29+
}
30+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package mirea.lab20.task5;
2+
3+
/*
4+
5. Написать класс Matrix, на основе обобщенного типа,
5+
реализовать операции с матрицами
6+
*/
7+
8+
import java.util.Arrays;
9+
10+
public class Matrix<T extends Number> {
11+
private T[][] matrix;
12+
private int rows;
13+
private int columns;
14+
15+
@SuppressWarnings("unchecked")
16+
public Matrix(int rows, int columns) {
17+
this.rows = rows;
18+
this.columns = columns;
19+
this.matrix = (T[][]) new Number[rows][columns];
20+
}
21+
22+
public void setValue(int row, int column, T value) {
23+
try {
24+
if (row >= this.rows || column >= this.columns) {
25+
throw new IllegalArgumentException("Must be input row < " + rows + ", and input column < " + columns);
26+
}
27+
this.matrix[row][column] = value;
28+
} catch (Exception e) {
29+
System.out.println(e.getMessage());
30+
}
31+
}
32+
33+
public T getValue(int row, int column) {
34+
try {
35+
if (row >= this.rows || column >= this.columns) {
36+
throw new IllegalArgumentException("Must be input row < " + rows + ", and input column < " + columns);
37+
}
38+
return this.matrix[row][column];
39+
} catch (Exception e) {
40+
System.out.println(e.getMessage());
41+
return null;
42+
}
43+
}
44+
45+
public Matrix<T> add(Matrix<T> otherMatrix) {
46+
try {
47+
if (otherMatrix.rows != this.rows || otherMatrix.columns != this.columns) {
48+
throw new IllegalArgumentException("Matrices must have equal dimensions.");
49+
}
50+
Matrix<T> resultMatrix = new Matrix<>(this.rows, otherMatrix.columns);
51+
for (int i = 0; i < this.rows; i++) {
52+
for (int j = 0; j < this.columns; j++) {
53+
resultMatrix.setValue(i, j, (T) Double.valueOf(this.matrix[i][j].doubleValue()
54+
+ otherMatrix.getValue(i, j).doubleValue()));
55+
}
56+
}
57+
return resultMatrix;
58+
} catch (Exception e) {
59+
System.out.println(e.getMessage());
60+
return null;
61+
}
62+
}
63+
64+
public Matrix<T> divide(Matrix<T> otherMatrix) {
65+
try {
66+
if (otherMatrix.rows != this.rows || otherMatrix.columns != this.columns) {
67+
throw new IllegalArgumentException("Matrices must have equal dimensions.");
68+
}
69+
Matrix<T> resultMatrix = new Matrix<>(this.rows, otherMatrix.columns);
70+
for (int i = 0; i < this.rows; i++) {
71+
for (int j = 0; j < this.columns; j++) {
72+
resultMatrix.setValue(i, j, (T) Double.valueOf(this.matrix[i][j].doubleValue()
73+
- otherMatrix.getValue(i, j).doubleValue()));
74+
}
75+
}
76+
return resultMatrix;
77+
} catch (Exception e) {
78+
System.out.println(e.getMessage());
79+
return null;
80+
}
81+
}
82+
83+
public Matrix<T> multiply(Matrix<T> otherMatrix) {
84+
if (this.columns != otherMatrix.rows) {
85+
throw new IllegalArgumentException("Matrices must have compatible dimensions for multiplication.");
86+
}
87+
88+
Matrix<T> resultMatrix = new Matrix<>(this.rows, otherMatrix.columns);
89+
for (int i = 0; i < this.rows; i++) {
90+
for (int j = 0; j < otherMatrix.columns; j++) {
91+
double sum = 0;
92+
for (int k = 0; k < this.columns; k++) {
93+
sum += this.getValue(i, k).doubleValue() * otherMatrix.getValue(k, j).doubleValue();
94+
}
95+
resultMatrix.setValue(i, j, (T) Double.valueOf(sum));
96+
}
97+
}
98+
return resultMatrix;
99+
}
100+
101+
@Override
102+
public String toString() {
103+
return Arrays.deepToString(matrix);
104+
}
105+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package mirea.lab20.task5;
2+
3+
abstract class Test {
4+
public static void main(String[] args) {
5+
Matrix<Double> mat1 = new Matrix<>(2, 2);
6+
mat1.setValue(0, 0, 3.0);
7+
mat1.setValue(0, 1, 1.0);
8+
mat1.setValue(1, 0, -5.0);
9+
mat1.setValue(1, 1, 4.0);
10+
11+
Matrix<Double> mat2 = new Matrix<>(2, 2);
12+
mat2.setValue(0, 0, 2.0);
13+
mat2.setValue(0, 1, -6.0);
14+
mat2.setValue(1, 0, 2.0);
15+
mat2.setValue(1, 1, 1.0);
16+
17+
Matrix<Double> sumMatrix = mat1.add(mat2);
18+
System.out.println("Sum:\n" + sumMatrix);
19+
Matrix<Double> divideMatrix = mat1.divide(mat2);
20+
System.out.println("Divide:\n" + divideMatrix);
21+
Matrix<Double> multiplyMatrix = mat1.multiply(mat2);
22+
System.out.println("Multiply:\n" + multiplyMatrix);
23+
}
24+
}

0 commit comments

Comments
 (0)