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

Commit 8e094e2

Browse files
authored
Merge pull request #667 from PogoNight/features/labs
Labs 21-32
2 parents 7ef7370 + 03fcca7 commit 8e094e2

File tree

92 files changed

+2429
-1
lines changed

Some content is hidden

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

92 files changed

+2429
-1
lines changed

students/23K0339/23K0339-p21/pom.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<artifactId>23K0339</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0339-p21</artifactId>
12+
<description>Практическая работа №21</description>
13+
</project>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package ru.mirea.practice.lab21.t1;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
abstract class ArrayToListConverter {
8+
public static <T> List<T> arrayToList(T[] array) {
9+
return new ArrayList<>(Arrays.asList(array));
10+
}
11+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package ru.mirea.practice.lab21.t1;
2+
3+
import java.util.List;
4+
5+
6+
abstract class ConverterTest {
7+
public static void main(String[] args) {
8+
String[] stringArray = {"три", "два", "раз"};
9+
List<String> stringList = ArrayToListConverter.arrayToList(stringArray);
10+
System.out.println("Список строк: " + stringList);
11+
12+
Integer[] intArray = {1, 2, 3, 4, 5};
13+
List<Integer> intList = ArrayToListConverter.arrayToList(intArray);
14+
System.out.println("Список чисел: " + intList);
15+
}
16+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package ru.mirea.practice.lab21.t2;
2+
3+
public class GenericArray<T> {
4+
private T[] array;
5+
6+
public GenericArray(T[] array) {
7+
this.array = array;
8+
}
9+
10+
public T[] getArray() {
11+
return array;
12+
}
13+
14+
public void setArray(T[] array) {
15+
this.array = array;
16+
}
17+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package ru.mirea.practice.lab21.t2;
2+
3+
abstract class GenericTest {
4+
public static void main(String[] args) {
5+
String[] stringArray = {"Два","Ква","Ма"};
6+
GenericArray<String> genericStringArray = new GenericArray<>(stringArray);
7+
System.out.println("Массив строк: " + String.join(", ", genericStringArray.getArray()));
8+
9+
}
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package ru.mirea.practice.lab21.t3;
2+
3+
abstract class ArrayElementRetriever {
4+
public static <T> T getElement(T[] array, int index) {
5+
if (index < 0 || index >= array.length) {
6+
throw new IndexOutOfBoundsException("Индекс выходит за пределы массива");
7+
}
8+
return array[index];
9+
}
10+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package ru.mirea.practice.lab21.t3;
2+
3+
abstract class RetrieverTest {
4+
public static void main(String[] args) {
5+
String[] stringArray = {"буль", "ба", "завр"};
6+
Integer[] intArray = {1, 2, 3, 4, 5};
7+
System.out.println("Элемент массива строк по индексу 1: "
8+
+ ArrayElementRetriever.getElement(stringArray, 1));
9+
System.out.println("Элемент массива чисел по индексу 3: "
10+
+ ArrayElementRetriever.getElement(intArray, 3));
11+
}
12+
}

students/23K0339/23K0339-p22/pom.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<artifactId>23K0339</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0339-p22</artifactId>
12+
<description>Практическая работа №22</description>
13+
</project>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package ru.mirea.practice.lab22.calculator;
2+
3+
import java.util.Stack;
4+
5+
public class RpNCalculator {
6+
public double evaluate(String expression) {
7+
if (expression.trim().isEmpty()) {
8+
throw new IllegalArgumentException("Некорректное выражение: пустая строка");
9+
}
10+
11+
Stack<Double> stack = new Stack<>();
12+
String[] tokens = expression.split("\\s+");
13+
14+
for (String token : tokens) {
15+
if (isNumeric(token)) {
16+
stack.push(Double.parseDouble(token));
17+
} else {
18+
if (stack.size() < 2) {
19+
throw new IllegalArgumentException("Недостаточно операндов для операции: " + token);
20+
}
21+
double b = stack.pop();
22+
double a = stack.pop();
23+
stack.push(applyOperation(token, a, b));
24+
}
25+
}
26+
27+
if (stack.size() != 1) {
28+
throw new IllegalArgumentException("Некорректное выражение: " + expression);
29+
}
30+
31+
return stack.pop();
32+
}
33+
34+
private boolean isNumeric(String str) {
35+
try {
36+
Double.parseDouble(str);
37+
return true;
38+
} catch (NumberFormatException e) {
39+
return false;
40+
}
41+
}
42+
43+
private double applyOperation(String operator, double a, double b) {
44+
switch (operator) {
45+
case "+":
46+
return a + b;
47+
case "-":
48+
return a - b;
49+
case "*":
50+
return a * b;
51+
case "/":
52+
if (b == 0) {
53+
throw new ArithmeticException("Деление на ноль");
54+
}
55+
return a / b;
56+
default:
57+
throw new IllegalArgumentException("Некорректный оператор: " + operator);
58+
}
59+
}
60+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package ru.mirea.practice.lab22.calculator;
2+
3+
abstract class RpNCalculatorTest {
4+
public static void main(String[] args) {
5+
RpNCalculator calculator = new RpNCalculator();
6+
7+
try {
8+
test(calculator.evaluate("2 3 +") == 5, "Тест 1: 2 3 +");
9+
test(calculator.evaluate("5 1 2 + 4 * + 3 -") == 14, "Тест 2: 5 1 2 + 4 * + 3 -");
10+
11+
try {
12+
calculator.evaluate("4 0 /");
13+
System.out.println("Тест 3: Ошибка не выброшена при делении на ноль");
14+
} catch (ArithmeticException e) {
15+
test("Деление на ноль".equals(e.getMessage()), "Тест 3: Деление на ноль");
16+
}
17+
18+
try {
19+
calculator.evaluate("1 2 + *");
20+
System.out.println("Тест 4: Ошибка не выброшена при недостатке операндов");
21+
} catch (IllegalArgumentException e) {
22+
test(e.getMessage().contains("Недостаточно операндов"), "Тест 4: Недостаточно операндов");
23+
}
24+
25+
try {
26+
calculator.evaluate("2 3 &");
27+
System.out.println("Тест 5: Ошибка не выброшена при некорректном операторе");
28+
} catch (IllegalArgumentException e) {
29+
test(e.getMessage().contains("Некорректный оператор"), "Тест 5: Некорректный оператор");
30+
}
31+
32+
try {
33+
calculator.evaluate("");
34+
System.out.println("Тест 6: Ошибка не выброшена при пустом выражении");
35+
} catch (IllegalArgumentException e) {
36+
test(e.getMessage().contains("Некорректное выражение"), "Тест 6: Пустое выражение");
37+
}
38+
39+
System.out.println("Все тесты пройдены успешно!");
40+
} catch (AssertionError e) {
41+
System.err.println("Тест провален: " + e.getMessage());
42+
}
43+
}
44+
45+
private static void test(boolean condition, String testName) {
46+
if (!condition) {
47+
throw new AssertionError(testName + " не пройден");
48+
}
49+
System.out.println(testName + " пройден");
50+
}
51+
}
52+

0 commit comments

Comments
 (0)