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

Commit 4c6dce0

Browse files
authored
Merge pull request #663 from Kostya507/main
Лабораторная 21-32
2 parents 3d46a57 + ea5a113 commit 4c6dce0

File tree

78 files changed

+2135
-0
lines changed

Some content is hidden

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

78 files changed

+2135
-0
lines changed

students/23K0145/23K0145-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>23K0145</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0145-p21</artifactId>
12+
<description>21 практика</description>
13+
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package ru.mirea.practice.s23k0145;
2+
3+
public final class Main {
4+
5+
private Main() {
6+
7+
}
8+
9+
public static void main(String[] args) {
10+
System.out.println("двадцать первая практическая работа!");
11+
}
12+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package ru.mirea.practice.s23k0145.lab1;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
public abstract class ArrayToListConverter {
7+
8+
public static <T> List<T> toList(T[] array) {
9+
if (array == null || array.length == 0) {
10+
throw new IllegalArgumentException("Массив не должен быть пустым или null");
11+
}
12+
return Arrays.asList(array);
13+
}
14+
15+
public static void main(String[] args) {
16+
String[] fruits = {"слон", "мышь", "заяц"};
17+
List<String> fruitList = toList(fruits);
18+
System.out.println("Список фруктов: " + fruitList);
19+
20+
Double[] numbers = {1.1, 2.2, 3.3, 4.4};
21+
List<Double> numberList = toList(numbers);
22+
System.out.println("Список чисел: " + numberList);
23+
24+
Character[] letters = {'A', 'B', 'C'};
25+
List<Character> letterList = toList(letters);
26+
System.out.println("Список букв: " + letterList);
27+
}
28+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package ru.mirea.practice.s23k0145.lab2;
2+
3+
import java.util.Arrays;
4+
5+
public class FlexibleArray<T> {
6+
private Object[] data;
7+
private int count;
8+
9+
public FlexibleArray(int initialCapacity) {
10+
if (initialCapacity <= 0) {
11+
throw new IllegalArgumentException("Начальный размер должен быть положительным");
12+
}
13+
data = new Object[initialCapacity];
14+
count = 0;
15+
}
16+
17+
public void add(T element) {
18+
ensureCapacity();
19+
data[count++] = element;
20+
}
21+
22+
@SuppressWarnings("unchecked")
23+
public T get(int index) {
24+
if (index < 0 || index >= count) {
25+
throw new IndexOutOfBoundsException("Индекс: " + index + ", Размер: " + count);
26+
}
27+
return (T) data[index];
28+
}
29+
30+
public int size() {
31+
return count;
32+
}
33+
34+
private void ensureCapacity() {
35+
if (count == data.length) {
36+
data = Arrays.copyOf(data, data.length * 2);
37+
}
38+
}
39+
40+
@Override
41+
public String toString() {
42+
return Arrays.toString(Arrays.copyOf(data, count));
43+
}
44+
45+
public static void main(String[] args) {
46+
FlexibleArray<Object> array = new FlexibleArray<>(5);
47+
48+
array.add(42);
49+
array.add(123456789L);
50+
array.add("Привет");
51+
array.add(3.1415);
52+
53+
System.out.println("Содержимое массива: " + array);
54+
System.out.println("Элемент по индексу 1: " + array.get(1));
55+
System.out.println("Размер массива: " + array.size());
56+
}
57+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package ru.mirea.practice.s23k0145.lab3;
2+
3+
import java.util.Objects;
4+
5+
public abstract class ElementRetriever {
6+
7+
// Обобщенный метод для получения элемента массива
8+
public static <T> T fetchElement(T[] array, int index) {
9+
Objects.requireNonNull(array, "Массив не должен быть null");
10+
if (index < 0 || index >= array.length) {
11+
throw new IllegalArgumentException("Некорректный индекс: " + index);
12+
}
13+
return array[index];
14+
}
15+
16+
public static void main(String[] args) {
17+
// Пример с массивом строк
18+
String[] fruits = {"яблоко", "банан", "вишня"};
19+
System.out.println("Элемент на позиции 1: " + fetchElement(fruits, 1));
20+
21+
// Пример с массивом чисел
22+
Integer[] numbers = {10, 20, 30, 40};
23+
System.out.println("Элемент на позиции 2: " + fetchElement(numbers, 2));
24+
25+
// Пример с массивом символов
26+
Character[] letters = {'A', 'B', 'C'};
27+
System.out.println("Элемент на позиции 0: " + fetchElement(letters, 0));
28+
}
29+
}

students/23K0145/23K0145-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>23K0145</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0145-p22</artifactId>
12+
<description>22 практика</description>
13+
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package ru.mirea.practice.s23k0145;
2+
3+
public final class Main {
4+
5+
private Main() {
6+
7+
}
8+
9+
public static void main(String[] args) {
10+
System.out.println("двадцать вторая практическая работа!");
11+
}
12+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package ru.mirea.practice.s23k0145.lab1;
2+
3+
public abstract class FactorialCalculator {
4+
public static void main(String[] args) {
5+
int number = 5; // Пример числа для вычисления факториала
6+
long factorial = calculateFactorial(number);
7+
System.out.println("Факториал числа " + number + " = " + factorial);
8+
}
9+
10+
// Метод для вычисления факториала с использованием цикла
11+
public static long calculateFactorial(int num) {
12+
long result = 1;
13+
for (int i = 2; i <= num; i++) {
14+
result *= i;
15+
}
16+
return result;
17+
}
18+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package ru.mirea.practice.s23k0145.lab1;
2+
3+
public abstract class HarmonicSeries {
4+
public static void main(String[] args) {
5+
// Печать первых 10 чисел гармонического ряда
6+
for (int i = 1; i <= 10; i++) {
7+
double harmonicNumber = 1.0 / i;
8+
System.out.printf("Гармоническое число(%d) = %.3f\n", i, harmonicNumber);
9+
}
10+
}
11+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package ru.mirea.practice.s23k0145.lab1;
2+
3+
import java.util.Scanner;
4+
import java.util.Deque;
5+
import java.util.ArrayDeque;
6+
7+
public final class RpnCalculator {
8+
9+
private RpnCalculator() {
10+
throw new UnsupportedOperationException("Utility class");
11+
}
12+
13+
public static void main(String[] args) {
14+
try (Scanner scanner = new Scanner(System.in)) {
15+
System.out.println("Введите выражение в обратной польской записи (например, '2 3 +'):");
16+
String input = scanner.nextLine();
17+
18+
try {
19+
double result = calculateRpn(input);
20+
System.out.println("Результат: " + result);
21+
} catch (Exception e) {
22+
System.out.println("Ошибка: " + e.getMessage());
23+
}
24+
}
25+
}
26+
27+
public static double calculateRpn(String expression) {
28+
Deque<Double> stack = new ArrayDeque<>();
29+
String[] tokens = expression.trim().split("\\s+");
30+
31+
for (String token : tokens) {
32+
switch (token) {
33+
case "+":
34+
checkStackSize(stack, 2);
35+
stack.push(stack.pop() + stack.pop());
36+
break;
37+
case "-":
38+
checkStackSize(stack, 2);
39+
double b = stack.pop();
40+
double a = stack.pop();
41+
stack.push(a - b);
42+
break;
43+
case "*":
44+
checkStackSize(stack, 2);
45+
stack.push(stack.pop() * stack.pop());
46+
break;
47+
case "/":
48+
checkStackSize(stack, 2);
49+
double divisor = stack.pop();
50+
if (divisor == 0) {
51+
throw new ArithmeticException("Деление на ноль.");
52+
}
53+
double dividend = stack.pop();
54+
stack.push(dividend / divisor);
55+
break;
56+
default:
57+
stack.push(parseNumber(token));
58+
break;
59+
}
60+
}
61+
62+
if (stack.size() != 1) {
63+
throw new IllegalArgumentException("Некорректное выражение.");
64+
}
65+
return stack.pop();
66+
}
67+
68+
private static double parseNumber(String token) {
69+
try {
70+
return Double.parseDouble(token);
71+
} catch (NumberFormatException e) {
72+
throw new IllegalArgumentException("Неверный ввод: '" + token + "'.", e);
73+
}
74+
}
75+
76+
private static void checkStackSize(Deque<Double> stack, int requiredSize) {
77+
if (stack.size() < requiredSize) {
78+
throw new IllegalArgumentException("Недостаточно операндов в стеке для выполнения операции.");
79+
}
80+
}
81+
}
82+

0 commit comments

Comments
 (0)