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

Лабораторная работа 21-30 #713

Merged
merged 2 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions students/23K0818/23K0818-p21/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>23K0818</artifactId>
<groupId>ru.mirea.practice</groupId>
<version>2024.1</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>23K0818-p21</artifactId>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package ru.mirea.practice.s0000001.task1;

import java.util.Arrays;
import java.util.List;

public abstract class ArrayToListConverter {


public static List<String> convertStringArrayToList(String[] array) {
return Arrays.asList(array);
}


public static List<Integer> convertIntegerArrayToList(Integer[] array) {
return Arrays.asList(array);
}

public static void main(String[] args) {

String[] stringArray = {"apple", "banana", "cherry"};
List<String> stringList = convertStringArrayToList(stringArray);
System.out.println("Список строк: " + stringList);


Integer[] integerArray = {1, 2, 3, 4, 5};
List<Integer> integerList = convertIntegerArrayToList(integerArray);
System.out.println("Список чисел: " + integerList);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package ru.mirea.practice.s0000001.task2;

import java.util.Arrays;

public abstract class GenericArray<T> {
private Object[] array;
private int size;

public GenericArray(int capacity) {
if (capacity <= 0) {
throw new IllegalArgumentException("Capacity must be greater than 0");
}
array = new Object[capacity];
size = 0;
}

public void add(T element) {
if (size == array.length) {
array = Arrays.copyOf(array, array.length * 2);
}
array[size++] = element;
}

public T get(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException("Index out of bounds");
}
return (T) array[index];
}

public int size() {
return size;
}

public void set(int index, T element) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException("Index out of bounds");
}
array[index] = element;
}

public void remove(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException("Index out of bounds");
}
System.arraycopy(array, index + 1, array, index, size - index - 1);
size--;
}

public boolean contains(T element) {
for (int i = 0; i < size; i++) {
if (array[i].equals(element)) {
return true;
}
}
return false;
}

public void clear() {
array = new Object[array.length];
size = 0;
}

@Override
public String toString() {
return Arrays.toString(Arrays.copyOf(array, size));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package ru.mirea.practice.s0000001.task4;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public abstract class DirectoryContent {

public static void main(String[] args) {
String directoryPath = "your_directory_path_here";
List<String> directoryContents = getDirectoryContents(directoryPath);

for (int i = 0; i < Math.min(5, directoryContents.size()); i++) {
System.out.println(directoryContents.get(i));
}
}

public static List<String> getDirectoryContents(String directoryPath) {
List<String> contents = new ArrayList<>();
File directory = new File(directoryPath);

if (directory.exists() && directory.isDirectory()) {
File[] files = directory.listFiles();
if (files != null) {
for (File file : files) {
contents.add(file.getName());
}
}
}
return contents;
}
}
12 changes: 12 additions & 0 deletions students/23K0818/23K0818-p22/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>23K0818</artifactId>
<groupId>ru.mirea.practice</groupId>
<version>2024.1</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>23K0818-p22</artifactId>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package ru.mirea.practice.s0000001.task1;

import java.util.Stack;

public abstract class RpnCalculator {

public static double evaluaterpn(String[] tokens) {
Stack<Double> stack = new Stack<>();

for (String token : tokens) {
switch (token) {
case "+":
stack.push(stack.pop() + stack.pop());
break;
case "-":
double subtrahend = stack.pop();
stack.push(stack.pop() - subtrahend);
break;
case "*":
stack.push(stack.pop() * stack.pop());
break;
case "/":
double divisor = stack.pop();
stack.push(stack.pop() / divisor);
break;
default:
stack.push(Double.parseDouble(token));
break;
}
}
return stack.pop();
}

public static void main(String[] args) {
String[] expression = {"2", "3", "+", "4", "5", "*", "+"}; // (2 + 3) + (4 * 5)
System.out.println("Result: " + evaluaterpn(expression));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ru.mirea.practice.s0000001.task2;

public abstract class CalculatorApp {
public static void main(String[] args) {
CalculatorModel model = new CalculatorModel();
CalculatorView view = new CalculatorView();
new CalculatorController(model, view);

view.setVisible(true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package ru.mirea.practice.s0000001.task2;

import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class CalculatorController {
public CalculatorController(CalculatorModel model, CalculatorView view) {
view.addButtonListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JButton source = (JButton) e.getSource();
String buttonText = source.getText();

if ("=".equals(buttonText)) {
String expression = view.getDisplayText();
String result = model.calculate(expression);
view.setDisplayText(result);
} else {
view.setDisplayText(view.getDisplayText() + buttonText + " ");
}
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package ru.mirea.practice.s0000001.task2;

import java.util.Stack;

public class CalculatorModel {

public String calculate(String expression) {
Stack<Double> stack = new Stack<>();

String[] tokens = expression.split(" ");

try {
for (String token : tokens) {
if (isNumber(token)) {
stack.push(Double.parseDouble(token));
} else {
double b = stack.pop();
double a = stack.pop();
switch (token) {
case "+":
stack.push(a + b);
break;
case "-":
stack.push(a - b);
break;
case "*":
stack.push(a * b);
break;
case "/":
stack.push(a / b);
break;
default:
throw new IllegalArgumentException("Недопустимая операция");
}
}
}
return String.valueOf(stack.pop());
} catch (Exception e) {
return "Ошибка";
}
}

private boolean isNumber(String token) {
try {
Double.parseDouble(token);
return true;
} catch (NumberFormatException e) {
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package ru.mirea.practice.s0000001.task2;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionListener;

public class CalculatorView extends JFrame {
private final JTextField display = new JTextField();

public CalculatorView() {
setTitle("Калькулятор");
setSize(400, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new BorderLayout());

display.setEditable(false);
display.setHorizontalAlignment(JTextField.RIGHT);
add(display, BorderLayout.NORTH);

JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(4, 4));

String[] buttons = {"7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0", ".", "=", "+"};
for (String text : buttons) {
JButton button = new JButton(text);
buttonPanel.add(button);
}

add(buttonPanel, BorderLayout.CENTER);
}

public String getDisplayText() {
return display.getText();
}

public void setDisplayText(String text) {
display.setText(text);
}

public void addButtonListener(ActionListener listener) {
for (Component component : ((JPanel) getContentPane().getComponent(1)).getComponents()) {
if (component instanceof JButton) {
((JButton) component).addActionListener(listener);
}
}
}
}
12 changes: 12 additions & 0 deletions students/23K0818/23K0818-p23/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>23K0818</artifactId>
<groupId>ru.mirea.practice</groupId>
<version>2024.1</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>23K0818-p23</artifactId>
</project>
Loading
Loading