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

Commit b8134f3

Browse files
authored
Merge pull request #654 from K-Roma/Practic-27-30
Practic 27-30
2 parents ecfc688 + 050163f commit b8134f3

File tree

68 files changed

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

68 files changed

+1894
-1
lines changed

students/23K0354/23K0354-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>23K0354</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0354-p21</artifactId>
12+
<description> Стирание типов в Джава </description>
13+
</project>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package ru.mirea.practices0000001.task1;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
public abstract class Converter {
7+
public static <T> List<T> convertArrayToList(T[] array) {
8+
return Arrays.asList(array);
9+
}
10+
11+
public static void main(String[] args) {
12+
String[] stringArray = {"a", "b", "c"};
13+
List<String> stringList = convertArrayToList(stringArray);
14+
System.out.println("Список строк: " + stringList);
15+
16+
Integer[] intArray = {1, 2, 3};
17+
List<Integer> intList = convertArrayToList(intArray);
18+
System.out.println("Список чисел: " + intList);
19+
}
20+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package ru.mirea.practices0000001.task4;
2+
3+
import java.io.File;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
public abstract class DirectoryUtils {
8+
public static List<String> getDirectoryContents(String path) {
9+
File directory = new File(path);
10+
List<String> fileList = new ArrayList<>();
11+
12+
if (directory.isDirectory()) {
13+
for (File file : directory.listFiles()) {
14+
fileList.add(file.getName());
15+
}
16+
}
17+
18+
return fileList;
19+
}
20+
21+
public static void main(String[] args) {
22+
List<String> contents = getDirectoryContents("/Users/viktoriapermakova/Desktop/Java");
23+
contents.stream().limit(5).forEach(System.out::println);
24+
}
25+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package ru.mirea.practices0000001.task5;
2+
3+
import java.util.List;
4+
import java.util.Map;
5+
import java.util.Set;
6+
7+
public abstract class Main {
8+
public static void main(String[] args) {
9+
List<Integer> list = Solution.newArrayList(1, 2, 3, 4, 5);
10+
System.out.println("ArrayList: " + list);
11+
12+
Set<String> set = Solution.newHashSet("a", "b", "c");
13+
System.out.println("HashSet: " + set);
14+
15+
Map<String, Integer> map = Solution.newHashMap("key1", 100);
16+
System.out.println("HashMap: " + map);
17+
}
18+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package ru.mirea.practices0000001.task5;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.HashSet;
6+
import java.util.List;
7+
import java.util.Map;
8+
import java.util.Set;
9+
10+
public abstract class Solution {
11+
public static <T> List<T> newArrayList(T... elements) {
12+
List<T> list = new ArrayList<>();
13+
for (T element : elements) {
14+
list.add(element);
15+
}
16+
return list;
17+
}
18+
19+
public static <T> Set<T> newHashSet(T... elements) {
20+
Set<T> set = new HashSet<>();
21+
for (T element : elements) {
22+
set.add(element);
23+
}
24+
return set;
25+
}
26+
27+
public static <K, V> Map<K, V> newHashMap(K key, V value) {
28+
Map<K, V> map = new HashMap<>();
29+
map.put(key, value);
30+
return map;
31+
}
32+
}

students/23K0354/23K0354-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>23K0354</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0354-p22</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.practices0000001.task1;
2+
3+
import java.util.Scanner;
4+
import java.util.Stack;
5+
6+
public abstract class RpnCalculator {
7+
8+
public static double evaluate(String expression) {
9+
Stack<Double> stack = new Stack<>();
10+
11+
String[] tokens = expression.split(" ");
12+
13+
for (String token : tokens) {
14+
switch (token) {
15+
case "+":
16+
stack.push(stack.pop() + stack.pop());
17+
break;
18+
case "-":
19+
double subtrahend = stack.pop();
20+
double minuend = stack.pop();
21+
stack.push(minuend - subtrahend);
22+
break;
23+
case "*":
24+
stack.push(stack.pop() * stack.pop());
25+
break;
26+
case "/":
27+
double divisor = stack.pop();
28+
double dividend = stack.pop();
29+
stack.push(dividend / divisor);
30+
break;
31+
default:
32+
stack.push(Double.parseDouble(token));
33+
break;
34+
}
35+
}
36+
37+
return stack.pop();
38+
}
39+
40+
public static void main(String[] args) {
41+
try (Scanner scanner = new Scanner(System.in)) {
42+
System.out.print("Введите выражение в обратной польской нотации (RPN): ");
43+
44+
String expression = scanner.nextLine();
45+
46+
if (expression != null && !expression.trim().isEmpty()) {
47+
try {
48+
double result = evaluate(expression);
49+
System.out.println("Результат: " + result);
50+
} catch (Exception e) {
51+
System.out.println("Ошибка: " + e.getMessage());
52+
}
53+
} else {
54+
System.out.println("Вы не ввели выражение.");
55+
}
56+
}
57+
}
58+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package ru.mirea.practices0000001.task2;
2+
3+
import javax.swing.JButton;
4+
import java.awt.event.ActionEvent;
5+
import java.awt.event.ActionListener;
6+
7+
public class CalculatorController {
8+
private final CalculatorModel model;
9+
private final CalculatorView view;
10+
11+
public CalculatorController(CalculatorModel model, CalculatorView view) {
12+
this.model = model;
13+
this.view = view;
14+
15+
this.view.addCalculateListener(new CalculateListener());
16+
this.view.addNumberButtonListener(new NumberButtonListener());
17+
this.view.addOperationButtonListener(new OperationButtonListener());
18+
}
19+
20+
private class CalculateListener implements ActionListener {
21+
@Override
22+
public void actionPerformed(ActionEvent e) {
23+
String input = view.getInput();
24+
try {
25+
double result = model.evaluate(input);
26+
view.setResult("Результат: " + result);
27+
} catch (Exception ex) {
28+
view.setResult("Ошибка: " + ex.getMessage());
29+
}
30+
}
31+
}
32+
33+
private class NumberButtonListener implements ActionListener {
34+
@Override
35+
public void actionPerformed(ActionEvent e) {
36+
JButton source = (JButton) e.getSource();
37+
view.setInput(view.getInput() + source.getText());
38+
}
39+
}
40+
41+
private class OperationButtonListener implements ActionListener {
42+
@Override
43+
public void actionPerformed(ActionEvent e) {
44+
JButton source = (JButton) e.getSource();
45+
String operation = source.getText();
46+
if ("C".equals(operation)) {
47+
view.setInput("");
48+
} else {
49+
view.setInput(view.getInput() + " " + operation + " ");
50+
}
51+
}
52+
}
53+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package ru.mirea.practices0000001.task2;
2+
3+
import java.util.Stack;
4+
5+
public class CalculatorModel {
6+
public double evaluate(String expression) {
7+
Stack<Double> stack = new Stack<>();
8+
String[] tokens = expression.trim().split("\\s+");
9+
10+
for (String token : tokens) {
11+
switch (token) {
12+
case "+":
13+
stack.push(stack.pop() + stack.pop());
14+
break;
15+
case "-":
16+
double subtrahend = stack.pop();
17+
double minuend = stack.pop();
18+
stack.push(minuend - subtrahend);
19+
break;
20+
case "*":
21+
stack.push(stack.pop() * stack.pop());
22+
break;
23+
case "/":
24+
double divisor = stack.pop();
25+
double dividend = stack.pop();
26+
stack.push(dividend / divisor);
27+
break;
28+
default:
29+
stack.push(Double.parseDouble(token));
30+
break;
31+
}
32+
}
33+
return stack.pop();
34+
}
35+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package ru.mirea.practices0000001.task2;
2+
3+
import javax.swing.JButton;
4+
import javax.swing.JFrame;
5+
import javax.swing.JLabel;
6+
import javax.swing.JPanel;
7+
import javax.swing.JTextArea;
8+
import javax.swing.JTextField;
9+
import java.awt.GridLayout;
10+
import java.awt.event.ActionListener;
11+
12+
public class CalculatorView {
13+
private final JTextField inputField;
14+
private final JTextArea resultArea;
15+
private final JButton[] numberButtons;
16+
private final JButton[] operationButtons;
17+
private final JButton calculateButton;
18+
private final String[] operations = {"+", "-", "*", "/", "C"};
19+
20+
public CalculatorView() {
21+
final JFrame frame = new JFrame("RPN Calculator");
22+
inputField = new JTextField(20);
23+
resultArea = new JTextArea(10, 30);
24+
calculateButton = new JButton("Вычислить");
25+
26+
numberButtons = new JButton[10];
27+
for (int i = 0; i < 10; i++) {
28+
numberButtons[i] = new JButton(String.valueOf(i));
29+
}
30+
31+
operationButtons = new JButton[operations.length];
32+
for (int i = 0; i < operations.length; i++) {
33+
operationButtons[i] = new JButton(operations[i]);
34+
}
35+
36+
JPanel panel = new JPanel();
37+
panel.setLayout(new GridLayout(5, 4));
38+
39+
panel.add(new JLabel("Введите выражение (RPN):"));
40+
panel.add(inputField);
41+
panel.add(calculateButton);
42+
panel.add(resultArea);
43+
44+
for (JButton numberButton : numberButtons) {
45+
panel.add(numberButton);
46+
}
47+
48+
for (JButton operationButton : operationButtons) {
49+
panel.add(operationButton);
50+
}
51+
52+
resultArea.setEditable(false);
53+
frame.add(panel);
54+
frame.setSize(400, 400);
55+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
56+
frame.setVisible(true);
57+
}
58+
59+
public String getInput() {
60+
return inputField.getText();
61+
}
62+
63+
public void setInput(String input) {
64+
inputField.setText(input);
65+
}
66+
67+
public void setResult(String result) {
68+
resultArea.setText(result);
69+
}
70+
71+
public void addCalculateListener(ActionListener listenForCalcButton) {
72+
calculateButton.addActionListener(listenForCalcButton);
73+
}
74+
75+
public void addNumberButtonListener(ActionListener listener) {
76+
for (JButton numberButton : numberButtons) {
77+
numberButton.addActionListener(listener);
78+
}
79+
}
80+
81+
public void addOperationButtonListener(ActionListener listener) {
82+
for (JButton operationButton : operationButtons) {
83+
operationButton.addActionListener(listener);
84+
}
85+
}
86+
}

0 commit comments

Comments
 (0)