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

Commit 12d838a

Browse files
authored
Merge pull request #704 from sunlight28/main
Лабораторные 21-30 + 32
2 parents f06e525 + 11a40f2 commit 12d838a

File tree

55 files changed

+1530
-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.

55 files changed

+1530
-0
lines changed

students/23K0087/23K0087-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>23K0087</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0087-p21</artifactId>
12+
<description>Двадцать первое задание</description>
13+
</project>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package ru.mirea.practice.s23k0087.task1;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
public abstract class ArrayToList {
7+
8+
public static <E> List<E> convertArrayToList(E[] array) {
9+
return Arrays.asList(array);
10+
}
11+
12+
public static void main(String[] args) {
13+
String[] arrayStr = {"Прибыл", "Сатору", "Годжо"};
14+
List<String> listStr = convertArrayToList(arrayStr);
15+
System.out.println("List String: " + listStr);
16+
17+
Integer[] arrayInt = {2, 0, 3, 1};
18+
List<Integer> listInt = convertArrayToList(arrayInt);
19+
System.out.println("List Integer: " + listInt);
20+
}
21+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package ru.mirea.practice.s23k0087.tasks2and3;
2+
3+
public class GenericArr<E> {
4+
private final E[] array;
5+
6+
public GenericArr(E[] elements) {
7+
this.array = elements;
8+
}
9+
10+
public E get(int index) {
11+
12+
if (index >= 0 && index < array.length) {
13+
return array[index];
14+
} else {
15+
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + array.length);
16+
}
17+
}
18+
19+
public void set(int index, E element) {
20+
if (index >= 0 && index < array.length) {
21+
array[index] = element;
22+
} else {
23+
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + array.length);
24+
}
25+
}
26+
27+
public E getElementByIndex(int index) {
28+
if (index < 0 || index >= array.length) {
29+
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + array.length);
30+
}
31+
return array[index];
32+
}
33+
34+
@Override
35+
public String toString() {
36+
StringBuilder sb = new StringBuilder("[");
37+
for (int i = 0; i < array.length; i++) {
38+
sb.append(array[i]);
39+
if (i < array.length - 1) {
40+
sb.append(", ");
41+
}
42+
}
43+
sb.append("]");
44+
return sb.toString();
45+
}
46+
47+
public int size() {
48+
return array.length;
49+
}
50+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package ru.mirea.practice.s23k0087.tasks2and3;
2+
3+
public abstract class Tester {
4+
public static void main(String[] args) {
5+
String[] arrayStr = {"Volkswagen", "Das", "Auto"};
6+
GenericArr<String> stringGenericArr = new GenericArr<>(arrayStr);
7+
System.out.println(stringGenericArr);
8+
9+
Long[] arrayLong = {2031L, 911922L, 1234567890L};
10+
GenericArr<Long> longGenericArr = new GenericArr<>(arrayLong);
11+
System.out.println(longGenericArr);
12+
13+
Double[] arrayDouble = {20.31, 0.19293912, 0.41413, 0.221};
14+
GenericArr<Double> doubleGenericArr = new GenericArr<>(arrayDouble);
15+
System.out.println(doubleGenericArr);
16+
}
17+
}

students/23K0087/23K0087-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>23K0087</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0087-p22</artifactId>
12+
<description>Двадцать второе задание</description>
13+
</project>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package ru.mirea.practice.s23k0087.task1;
2+
3+
import java.util.Stack;
4+
5+
public abstract class Calculator {
6+
private static double applyOperation(double a, double b, String operator) {
7+
switch (operator) {
8+
case "+":
9+
return a + b;
10+
case "-":
11+
return a - b;
12+
case "*":
13+
return a * b;
14+
case "/":
15+
if (b == 0) {
16+
throw new ArithmeticException("Деление на ноль");
17+
}
18+
return a / b;
19+
default:
20+
throw new IllegalArgumentException("Недопустимый оператор: " + operator);
21+
}
22+
}
23+
24+
private static boolean isOperator(String token) {
25+
return "+".equals(token) || "-".equals(token) || "*".equals(token) || "/".equals(token);
26+
}
27+
28+
public static double calculate(String expression) {
29+
Stack<Double> stack = new Stack<>();
30+
String[] tokens = expression.split("\\s+");
31+
for (String token : tokens) {
32+
if (isOperator(token)) {
33+
double b = stack.pop();
34+
double a = stack.pop();
35+
stack.push(applyOperation(a, b, token));
36+
} else {
37+
stack.push(Double.parseDouble(token));
38+
}
39+
}
40+
return stack.pop();
41+
}
42+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package ru.mirea.practice.s23k0087.task1;
2+
3+
public abstract class Tester {
4+
public static void main(String[] args) {
5+
String expression = "4 8 - 2 / 7 *";
6+
System.out.println("Calculating ((4 - 8) / 2) * 7");
7+
double result = Calculator.calculate(expression);
8+
System.out.println("Result: " + result);
9+
}
10+
}

students/23K0087/23K0087-p23/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>23K0087</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0087-p23</artifactId>
12+
<description>Двадцать третье задание</description>
13+
</project>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package ru.mirea.practice.s23k0087.task1;
2+
3+
public final class ArrayQueue {
4+
5+
private Object[] queue = new Object[16];
6+
private int head = 0;
7+
private int tail = 0;
8+
private int size = 0;
9+
10+
public static ArrayQueue createQueue() {
11+
return new ArrayQueue();
12+
}
13+
14+
public void enqueue(Object element) {
15+
ensureCapacity(size + 1);
16+
queue[tail] = element;
17+
tail = (tail + 1) % queue.length;
18+
size++;
19+
}
20+
21+
public Object getFirstElement() {
22+
return queue[head];
23+
}
24+
25+
public Object dequeue() {
26+
Object result;
27+
result = queue[head];
28+
queue[head] = null;
29+
head = (head + 1) % queue.length;
30+
size--;
31+
return result;
32+
}
33+
34+
public int size() {
35+
return size;
36+
}
37+
38+
public boolean isEmpty() {
39+
return size == 0;
40+
}
41+
42+
public void clear() {
43+
queue = new Object[16];
44+
head = tail = size = 0;
45+
}
46+
47+
private void ensureCapacity(int capacity) {
48+
if (capacity > queue.length) {
49+
Object[] newQueue = new Object[queue.length * 2];
50+
for (int i = 0; i < size; i++) {
51+
newQueue[i] = queue[(head + i) % queue.length];
52+
}
53+
queue = newQueue;
54+
head = 0;
55+
tail = size;
56+
}
57+
}
58+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package ru.mirea.practice.s23k0087.task1;
2+
3+
public final class ArrayQueueAdt {
4+
5+
private Object[] queue = new Object[16];
6+
private int head = 0;
7+
private int tail = 0;
8+
private int size = 0;
9+
10+
public static void enqueue(ArrayQueueAdt queueAdt, Object element) {
11+
ensureCapacity(queueAdt, queueAdt.size + 1);
12+
queueAdt.queue[queueAdt.tail] = element;
13+
queueAdt.tail = (queueAdt.tail + 1) % queueAdt.queue.length;
14+
queueAdt.size++;
15+
}
16+
17+
public static Object getFirstElement(ArrayQueueAdt queueAdt) {
18+
return queueAdt.queue[queueAdt.head];
19+
}
20+
21+
public static Object dequeue(ArrayQueueAdt queueAdt) {
22+
Object result;
23+
result = queueAdt.queue[queueAdt.head];
24+
queueAdt.queue[queueAdt.head] = null;
25+
queueAdt.head = (queueAdt.head + 1) % queueAdt.queue.length;
26+
queueAdt.size--;
27+
return result;
28+
}
29+
30+
public static int size(ArrayQueueAdt queueAdt) {
31+
return queueAdt.size;
32+
}
33+
34+
public static boolean isEmpty(ArrayQueueAdt queueAdt) {
35+
return queueAdt.size == 0;
36+
}
37+
38+
public static void clear(ArrayQueueAdt queueAdt) {
39+
queueAdt.queue = new Object[16];
40+
queueAdt.head = queueAdt.tail = queueAdt.size = 0;
41+
}
42+
43+
private static void ensureCapacity(ArrayQueueAdt queueAdt, int capacity) {
44+
if (capacity > queueAdt.queue.length) {
45+
Object[] newQueue = new Object[queueAdt.queue.length * 2];
46+
for (int i = 0; i < queueAdt.size; i++) {
47+
newQueue[i] = queueAdt.queue[(queueAdt.head + i) % queueAdt.queue.length];
48+
}
49+
queueAdt.queue = newQueue;
50+
queueAdt.head = 0;
51+
queueAdt.tail = queueAdt.size;
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)