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

Лабораторные 1-32 #644

Merged
merged 5 commits into from
Dec 13, 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
1 change: 0 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@
<module>students/23K0186</module>
<module>students/23K0755</module>
<module>students/23K1292</module>

</modules>
<dependencies>
<dependency>
Expand Down
13 changes: 13 additions & 0 deletions students/23K0368/23K0368-p21/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?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>23K0368</artifactId>
<groupId>ru.mirea.practice</groupId>
<version>2024.1</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>23K0368-p21</artifactId>
<description>Стирание типов в Java</description>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package ru.mirea.practice.s0000001.prog1;

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

public abstract class Main {
public static List<String> convertArrayToList(String[] array) {
return Arrays.asList(array);
}

// Метод для конвертации массива чисел в список
public static List<Integer> convertArrayToList(Integer[] array) {
return Arrays.asList(array);
}

public static void main(String[] args) {
String[] stringArray = {"apple", "banana", "cherry"};
Integer[] intArray = {1, 2, 3, 4, 5};

List<String> stringList = convertArrayToList(stringArray);
List<Integer> intList = convertArrayToList(intArray);

System.out.println("Список строк: " + stringList);
System.out.println("Список чисел: " + intList);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package ru.mirea.practice.s0000001.prog2;

import java.util.Arrays;

public class AllType {
private final Object[] array;

public AllType(Object[] array) {
this.array = array;
}

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

public abstract class Main {
public static void main(String[] args) {
Integer[] mas1 = {1, 2, 3, 4};
AllType intmas1 = new AllType(mas1);
System.out.println("Integer array: " + intmas1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package ru.mirea.practice.s0000001.prog3;

import java.util.Arrays;

public class AllType<T> {
private T[] array;

public AllType(T[] array) {
this.array = array;
}

public T getElement(int index) {
if (index >= 0 && index < array.length) {
return array[index];
} else {
throw new IndexOutOfBoundsException("Index is out of bounds");
}
}

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

public abstract class Main {
public static void main(String[] args) {
AllType<Integer> intAllType = new AllType<>(new Integer[]{1, 2, 3, 4, 5});
System.out.println("Integer array: " + intAllType);

AllType<String> stringAllType = new AllType<>(new String[]{"apple", "banana", "cherry"});
System.out.println("String array: " + stringAllType);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package ru.mirea.practice.s0000001.prog4;

import java.util.Arrays;

public class AllType<T> {
private T[] array;

public AllType(T[] array) {
this.array = array;
}

public T getElement(int index) {
if (index >= 0 && index < array.length) {
return array[index];
} else {
throw new IndexOutOfBoundsException("Index is out of bounds");
}
}

public int getLength() {
return array.length;
}

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

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

public abstract class Main {
public static List<Object> getDirectoryContents(String directorypath) {
File directory = new File(directorypath);
List<Object> contents = new ArrayList<>();

if (directory.isDirectory()) {
File[] files = directory.listFiles();
if (files != null) {
contents.addAll(Arrays.asList(files));
}
} else {
System.out.println("Указанный путь ошибочен");
throw new IllegalStateException("Invalid");
}

return contents;
}

public static void print(AllType<Object> list) {
System.out.println("Первые 5 элементов содержимого каталога:");
for (int i = 0; i < Math.min(5, list.getLength()); i++) {
System.out.println((i + 1) + ". " + list.getElement(i));
}
}

public static void main(String[] args) {
String path = "Path";
try {
List<Object> content = getDirectoryContents(path);

AllType<Object> directoryContents = new AllType<>(content.toArray());
print(directoryContents);
} catch (IllegalStateException e) {
System.out.println("Ошибка: " + e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package ru.mirea.practice.s0000001.prog5;

import java.util.List;
import java.util.Map;
import java.util.Set;

public abstract class Main {
public static void main(String[] args) {
List<Integer> list = Solution.newArrayList(1, 2, 3, 4);
System.out.println("Integer elements(ArrayList)" + list);

Set<String> set = Solution.newHashSet("T90", "T80", "T54", "T72Б3");
System.out.println("String elements(HashSet)" + set);

Map<String, Integer> map = Solution.newHashMap(
new Pair<>("T90", 90),
new Pair<>("T80", 80),
new Pair<>("T54", 54),
new Pair<>("T72Б3", 72)
);
System.out.println("Map<String, Integer>" + map);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package ru.mirea.practice.s0000001.prog5;

public class Pair<K, V> {
private K k;
private V v;

public Pair(K k, V v) {
this.k = k;
this.v = v;
}

public K getK() {
return k;
}

public V getV() {
return v;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package ru.mirea.practice.s0000001.prog5;

import java.util.Map;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public abstract class Solution<T, K, V> {
public static <T> List<T> newArrayList(T... elements) {
List<T> list = new ArrayList<>();
for (T element : elements) {
list.add(element);
}
return list;
}

public static <T> Set<T> newHashSet(T... elements) {
Set<T> set = new HashSet<>();
for (T element : elements) {
set.add(element);
}
return set;
}

public static <K, V> Map<K, V> newHashMap(Pair<K,V>... entries) {
Map<K, V> map = new HashMap<>();
for (Pair<K,V> entry : entries) {
map.put(entry.getK(), entry.getV());
}
return map;
}

}
13 changes: 13 additions & 0 deletions students/23K0368/23K0368-p22/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?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>23K0368</artifactId>
<groupId>ru.mirea.practice</groupId>
<version>2024.1</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>23K0368-p22</artifactId>
<description>Абстрактные типы данных. Стек</description>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package ru.mirea.practice.s23k0368;

import java.util.Stack;

public abstract class Main {
public static double evaluateRpn(String expression) {
Stack<Double> stack = new Stack<>();
String[] tokens = expression.split("\\s+"); // Разделяем выражение по пробелам

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();
if (divisor == 0) {
throw new ArithmeticException("Деление на ноль.");
}
stack.push(stack.pop() / divisor);
break;
default:
stack.push(Double.parseDouble(token));
break;
}
}

if (stack.size() != 1) {
throw new IllegalArgumentException("Некорректное выражение.");
}

return stack.pop();
}

public static void main(String[] args) {
String expression = "3 4 + 2 * 7 /";
try {
double result = evaluateRpn(expression);
System.out.println("Результат: " + result);
} catch (Exception e) {
System.out.println("Ошибка: " + e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package ru.mirea.practice.s23k0368.prog2;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

// Controller
class CalculatorController implements ActionListener {
private CalculatorView view;
private CalculatorModel model;

public CalculatorController(CalculatorView view, CalculatorModel model) {
this.view = view;
this.model = model;
}

@Override
public void actionPerformed(ActionEvent e) {
String expression = view.getInputExpression();
String result = model.evaluateRpn(expression);
view.setResult(result);
}
}
Loading
Loading