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

Commit a75d52c

Browse files
authored
Merge pull request #674 from Kermesina/main
Практические №21-32
2 parents 0ff9e51 + 8f2ec26 commit a75d52c

File tree

50 files changed

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

50 files changed

+1342
-0
lines changed

students/23K0093/23K0093-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>23K0093</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0093-p21</artifactId>
12+
<description>Афанасьева</description>
13+
</project>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package ru.mirea.practice.s23k0093;
2+
3+
public abstract class ArrayElementRetriever {
4+
public static <T> T getElementAtIndex(T[] array, int index) {
5+
if (index < 0 || index >= array.length) {
6+
throw new IndexOutOfBoundsException("Индекс вне границ массива");
7+
}
8+
return array[index];
9+
}
10+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package ru.mirea.practice.s23k0093;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public abstract class ArrayToListConverter {
7+
public static <T> List<T> convertArrayToList(T[] array) {
8+
List<T> list = new ArrayList<>();
9+
for (T element : array) {
10+
list.add(element);
11+
}
12+
return list;
13+
}
14+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package ru.mirea.practice.s23k0093;
2+
3+
import java.util.List;
4+
5+
public abstract class TestMethod {
6+
public static void main(String[] args) {
7+
8+
String[] stringArray = {"Я", "люблю", "спать"};
9+
List<String> stringList = ArrayToListConverter.convertArrayToList(stringArray);
10+
System.out.println("Список строк: " + stringList);
11+
12+
String element = ArrayElementRetriever.getElementAtIndex(stringArray, 1); // получаем 'banana'
13+
System.out.println("Элемент по индексу 1: " + element);
14+
15+
Integer[] intArray = {1, 2, 3, 4, 5};
16+
List<Integer> intList = ArrayToListConverter.convertArrayToList(intArray);
17+
System.out.println("Список чисел: " + intList);
18+
19+
Integer num = ArrayElementRetriever.getElementAtIndex(intArray, 3); // получаем '4'
20+
System.out.println("Элемент по индексу 3: " + num);
21+
}
22+
}

students/23K0093/23K0093-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>23K0093</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0093-p22</artifactId>
12+
<description>Афанасьева</description>
13+
</project>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package ru.mirea.practice.s23k0093;
2+
3+
public class RpnCalculator {
4+
private Stack stack;
5+
6+
public RpnCalculator(int size) {
7+
stack = new Stack(size);
8+
}
9+
10+
public double evaluate(String expression) {
11+
String[] strings = expression.split(" ");
12+
for (String element : strings) {
13+
try {
14+
float f = Float.parseFloat(element);
15+
stack.push(f);
16+
} catch (NumberFormatException e) {
17+
performOperation(element);
18+
}
19+
}
20+
return stack.pop();
21+
}
22+
23+
private void performOperation(String operator) {
24+
float secondOperand = stack.pop();
25+
float firstOperand = stack.pop();
26+
switch (operator) {
27+
case "+":
28+
stack.push(firstOperand + secondOperand);
29+
break;
30+
case "-":
31+
stack.push(firstOperand - secondOperand);
32+
break;
33+
case "*":
34+
stack.push(firstOperand * secondOperand);
35+
break;
36+
case "/":
37+
if (secondOperand == 0) {
38+
throw new ArithmeticException("Ошибка: Деление на ноль");
39+
}
40+
stack.push(firstOperand / secondOperand);
41+
break;
42+
default:
43+
throw new IllegalArgumentException("Недопустимый оператор: " + operator);
44+
}
45+
}
46+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package ru.mirea.practice.s23k0093;
2+
3+
class Stack {
4+
// store elements of stack
5+
private float[] arr;
6+
// represent top of stack
7+
private int top;
8+
// total capacity of the stack
9+
private int capacity;
10+
11+
// Creating a stack
12+
Stack(int size) {
13+
// initialize the array
14+
// initialize the stack variables
15+
arr = new float[size];
16+
capacity = size;
17+
top = -1;
18+
}
19+
20+
// push elements to the top of stack
21+
public void push(float x) {
22+
if (isFull()) {
23+
System.out.println("Stack OverFlow");
24+
// terminates the program
25+
System.exit(1);
26+
}
27+
// insert element on top of stack
28+
System.out.println("Inserting " + x);
29+
arr[++top] = x;
30+
}
31+
32+
// pop elements from top of stack
33+
public float pop() {
34+
// if stack is empty
35+
// no element to pop
36+
if (isEmpty()) {
37+
System.out.println("STACK EMPTY");
38+
// terminates the program
39+
System.exit(1);
40+
}
41+
// pop element from top of stack
42+
return arr[top--];
43+
}
44+
45+
// return size of the stack
46+
public int getSize() {
47+
return top + 1;
48+
}
49+
50+
// check if the stack is empty
51+
public Boolean isEmpty() {
52+
return top == -1;
53+
}
54+
55+
// check if the stack is full
56+
public Boolean isFull() {
57+
return top == capacity - 1;
58+
}
59+
60+
// display elements of stack
61+
public void printStack() {
62+
for (int i = 0; i <= top; i++) {
63+
System.out.print(arr[i] + ", ");
64+
}
65+
}
66+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package ru.mirea.practice.s23k0093;
2+
3+
public abstract class Test {
4+
public static void main(String[] args) {
5+
RpnCalculator calculator = new RpnCalculator(10);
6+
System.out.println("16.1 3.9 + = " + calculator.evaluate("16.1 3.9 +"));
7+
System.out.println("8 4 - = " + calculator.evaluate("8 4 -"));
8+
System.out.println("28 0.5 * = " + calculator.evaluate("28 0.5 *"));
9+
System.out.println("7 2 / = " + calculator.evaluate("7 2 /"));
10+
System.out.println("3 4 + 2 * = " + calculator.evaluate("3 4 + 2 *"));
11+
12+
try {
13+
System.out.println("38 0 / = " + calculator.evaluate("38 0 /"));
14+
} catch (ArithmeticException e) {
15+
System.out.println(e.getMessage());
16+
}
17+
}
18+
}

students/23K0093/23K0093-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>23K0093</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0093-p23</artifactId>
12+
<description>Афанасьева</description>
13+
</project>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package ru.mirea.practice.s23k0093;
2+
3+
public abstract class AbstractQueue<T> implements Queue<T> {
4+
5+
protected int size;
6+
7+
@Override
8+
public boolean isEmpty() {
9+
return size == 0;
10+
}
11+
12+
@Override
13+
public int size() {
14+
return size;
15+
}
16+
}

0 commit comments

Comments
 (0)