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

Commit 9a87f77

Browse files
authored
Merge pull request #713 from Vertyshka/features/lab21-30
Лабораторная работа 21-30
2 parents 23066fa + a0a2e1b commit 9a87f77

File tree

64 files changed

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

64 files changed

+2386
-0
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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>23K0818</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0818-p21</artifactId>
12+
</project>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package ru.mirea.practice.s0000001.task1;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
public abstract class ArrayToListConverter {
7+
8+
9+
public static List<String> convertStringArrayToList(String[] array) {
10+
return Arrays.asList(array);
11+
}
12+
13+
14+
public static List<Integer> convertIntegerArrayToList(Integer[] array) {
15+
return Arrays.asList(array);
16+
}
17+
18+
public static void main(String[] args) {
19+
20+
String[] stringArray = {"apple", "banana", "cherry"};
21+
List<String> stringList = convertStringArrayToList(stringArray);
22+
System.out.println("Список строк: " + stringList);
23+
24+
25+
Integer[] integerArray = {1, 2, 3, 4, 5};
26+
List<Integer> integerList = convertIntegerArrayToList(integerArray);
27+
System.out.println("Список чисел: " + integerList);
28+
}
29+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package ru.mirea.practice.s0000001.task2;
2+
3+
import java.util.Arrays;
4+
5+
public abstract class GenericArray<T> {
6+
private Object[] array;
7+
private int size;
8+
9+
public GenericArray(int capacity) {
10+
if (capacity <= 0) {
11+
throw new IllegalArgumentException("Capacity must be greater than 0");
12+
}
13+
array = new Object[capacity];
14+
size = 0;
15+
}
16+
17+
public void add(T element) {
18+
if (size == array.length) {
19+
array = Arrays.copyOf(array, array.length * 2);
20+
}
21+
array[size++] = element;
22+
}
23+
24+
public T get(int index) {
25+
if (index < 0 || index >= size) {
26+
throw new IndexOutOfBoundsException("Index out of bounds");
27+
}
28+
return (T) array[index];
29+
}
30+
31+
public int size() {
32+
return size;
33+
}
34+
35+
public void set(int index, T element) {
36+
if (index < 0 || index >= size) {
37+
throw new IndexOutOfBoundsException("Index out of bounds");
38+
}
39+
array[index] = element;
40+
}
41+
42+
public void remove(int index) {
43+
if (index < 0 || index >= size) {
44+
throw new IndexOutOfBoundsException("Index out of bounds");
45+
}
46+
System.arraycopy(array, index + 1, array, index, size - index - 1);
47+
size--;
48+
}
49+
50+
public boolean contains(T element) {
51+
for (int i = 0; i < size; i++) {
52+
if (array[i].equals(element)) {
53+
return true;
54+
}
55+
}
56+
return false;
57+
}
58+
59+
public void clear() {
60+
array = new Object[array.length];
61+
size = 0;
62+
}
63+
64+
@Override
65+
public String toString() {
66+
return Arrays.toString(Arrays.copyOf(array, size));
67+
}
68+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package ru.mirea.practice.s0000001.task4;
2+
3+
import java.io.File;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
public abstract class DirectoryContent {
8+
9+
public static void main(String[] args) {
10+
String directoryPath = "your_directory_path_here";
11+
List<String> directoryContents = getDirectoryContents(directoryPath);
12+
13+
for (int i = 0; i < Math.min(5, directoryContents.size()); i++) {
14+
System.out.println(directoryContents.get(i));
15+
}
16+
}
17+
18+
public static List<String> getDirectoryContents(String directoryPath) {
19+
List<String> contents = new ArrayList<>();
20+
File directory = new File(directoryPath);
21+
22+
if (directory.exists() && directory.isDirectory()) {
23+
File[] files = directory.listFiles();
24+
if (files != null) {
25+
for (File file : files) {
26+
contents.add(file.getName());
27+
}
28+
}
29+
}
30+
return contents;
31+
}
32+
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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>23K0818</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0818-p22</artifactId>
12+
</project>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package ru.mirea.practice.s0000001.task1;
2+
3+
import java.util.Stack;
4+
5+
public abstract class RpnCalculator {
6+
7+
public static double evaluaterpn(String[] tokens) {
8+
Stack<Double> stack = new Stack<>();
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+
stack.push(stack.pop() - subtrahend);
18+
break;
19+
case "*":
20+
stack.push(stack.pop() * stack.pop());
21+
break;
22+
case "/":
23+
double divisor = stack.pop();
24+
stack.push(stack.pop() / divisor);
25+
break;
26+
default:
27+
stack.push(Double.parseDouble(token));
28+
break;
29+
}
30+
}
31+
return stack.pop();
32+
}
33+
34+
public static void main(String[] args) {
35+
String[] expression = {"2", "3", "+", "4", "5", "*", "+"}; // (2 + 3) + (4 * 5)
36+
System.out.println("Result: " + evaluaterpn(expression));
37+
}
38+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package ru.mirea.practice.s0000001.task2;
2+
3+
public abstract class CalculatorApp {
4+
public static void main(String[] args) {
5+
CalculatorModel model = new CalculatorModel();
6+
CalculatorView view = new CalculatorView();
7+
new CalculatorController(model, view);
8+
9+
view.setVisible(true);
10+
}
11+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package ru.mirea.practice.s0000001.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+
public CalculatorController(CalculatorModel model, CalculatorView view) {
9+
view.addButtonListener(new ActionListener() {
10+
@Override
11+
public void actionPerformed(ActionEvent e) {
12+
JButton source = (JButton) e.getSource();
13+
String buttonText = source.getText();
14+
15+
if ("=".equals(buttonText)) {
16+
String expression = view.getDisplayText();
17+
String result = model.calculate(expression);
18+
view.setDisplayText(result);
19+
} else {
20+
view.setDisplayText(view.getDisplayText() + buttonText + " ");
21+
}
22+
}
23+
});
24+
}
25+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package ru.mirea.practice.s0000001.task2;
2+
3+
import java.util.Stack;
4+
5+
public class CalculatorModel {
6+
7+
public String calculate(String expression) {
8+
Stack<Double> stack = new Stack<>();
9+
10+
String[] tokens = expression.split(" ");
11+
12+
try {
13+
for (String token : tokens) {
14+
if (isNumber(token)) {
15+
stack.push(Double.parseDouble(token));
16+
} else {
17+
double b = stack.pop();
18+
double a = stack.pop();
19+
switch (token) {
20+
case "+":
21+
stack.push(a + b);
22+
break;
23+
case "-":
24+
stack.push(a - b);
25+
break;
26+
case "*":
27+
stack.push(a * b);
28+
break;
29+
case "/":
30+
stack.push(a / b);
31+
break;
32+
default:
33+
throw new IllegalArgumentException("Недопустимая операция");
34+
}
35+
}
36+
}
37+
return String.valueOf(stack.pop());
38+
} catch (Exception e) {
39+
return "Ошибка";
40+
}
41+
}
42+
43+
private boolean isNumber(String token) {
44+
try {
45+
Double.parseDouble(token);
46+
return true;
47+
} catch (NumberFormatException e) {
48+
return false;
49+
}
50+
}
51+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package ru.mirea.practice.s0000001.task2;
2+
3+
import javax.swing.JButton;
4+
import javax.swing.JFrame;
5+
import javax.swing.JPanel;
6+
import javax.swing.JTextField;
7+
import java.awt.BorderLayout;
8+
import java.awt.Component;
9+
import java.awt.GridLayout;
10+
import java.awt.event.ActionListener;
11+
12+
public class CalculatorView extends JFrame {
13+
private final JTextField display = new JTextField();
14+
15+
public CalculatorView() {
16+
setTitle("Калькулятор");
17+
setSize(400, 500);
18+
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
19+
setLocationRelativeTo(null);
20+
setLayout(new BorderLayout());
21+
22+
display.setEditable(false);
23+
display.setHorizontalAlignment(JTextField.RIGHT);
24+
add(display, BorderLayout.NORTH);
25+
26+
JPanel buttonPanel = new JPanel();
27+
buttonPanel.setLayout(new GridLayout(4, 4));
28+
29+
String[] buttons = {"7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0", ".", "=", "+"};
30+
for (String text : buttons) {
31+
JButton button = new JButton(text);
32+
buttonPanel.add(button);
33+
}
34+
35+
add(buttonPanel, BorderLayout.CENTER);
36+
}
37+
38+
public String getDisplayText() {
39+
return display.getText();
40+
}
41+
42+
public void setDisplayText(String text) {
43+
display.setText(text);
44+
}
45+
46+
public void addButtonListener(ActionListener listener) {
47+
for (Component component : ((JPanel) getContentPane().getComponent(1)).getComponents()) {
48+
if (component instanceof JButton) {
49+
((JButton) component).addActionListener(listener);
50+
}
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)