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

Лабораторные работы №21-30 #683

Merged
merged 1 commit 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
13 changes: 13 additions & 0 deletions students/23K0350/23K0350-p21-retry/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>23K0350</artifactId>
<groupId>ru.mirea.practice</groupId>
<version>2024.1</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>23K0350-p21</artifactId>
<description>Стирание типов</description>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package ru.mirea.practice.s23k0350.task1;

import java.util.ArrayList;
import java.util.List;

public class AnyArray<T> {
public List<T> array;

public AnyArray() {
this.array = new ArrayList<>();
}

public void add(T item) {
array.add(item);
}


public void printElements() {
for (T item: array) {
System.out.println(item.toString());
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package ru.mirea.practice.s23k0350.task1;


import ru.mirea.practice.s23k0350.task2.TestHuman;

abstract class Main {
public static void main(String[] args) {
AnyArray<Object> testing = new AnyArray<>();
TestHuman female = new TestHuman("Natasha", 27);
testing.add(10);
testing.add("Hello");
testing.add(true);
testing.add(3.14);
testing.add(female);
System.out.println("Elements:");
testing.printElements();
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package ru.mirea.practice.s23k0350.task2;


import ru.mirea.practice.s23k0350.task1.AnyArray;

public class AnyArrayElecBug<T> extends AnyArray<T> {
public T returnElement(int index) {
return array.get(index);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ru.mirea.practice.s23k0350.task2;


abstract class Main {
public static void main(String[] args) {
AnyArrayElecBug<Object> test = new AnyArrayElecBug<>();
TestHuman male = new TestHuman("Oleg", 35);
test.add(10);
test.add(male);
test.add("Hello");
test.add(true);
test.add(3.14);
System.out.println("Elements:");
test.printElements();
System.out.println("w/ Index:\n" + test.returnElement(1));

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ru.mirea.practice.s23k0350.task2;

public class TestHuman {
String name;
int age;

public TestHuman(String name, int age) {
this.age = age;
this.name = name;
}

public String toString() {
return "Name: " + name + "\n" + "Age:" + age;
}
}
13 changes: 13 additions & 0 deletions students/23K0350/23K0350-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>23K0350</artifactId>
<groupId>ru.mirea.practice</groupId>
<version>2024.1</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>23K0350-p22</artifactId>
<description>Стек</description>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package ru.mirea.practice.s23k0350;

abstract class Main {
public static void main(String[] args) {
String expression = "2 3 * 4 5 * + ";
RpnCalculator rp = new RpnCalculator(expression);
System.out.println("Результат: " + rp.evaluate(expression));
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package ru.mirea.practice.s23k0350;

import java.util.Stack;

public class RpnCalculator {
String expression;

public RpnCalculator(String expression) {
this.expression = expression;
}

public static double evaluate(String expression) {
Stack<Double> stack = new Stack<>();
String[] tokens = expression.split(" ");

for (String token : tokens) {
if (isNumber(token)) {
stack.push(Double.parseDouble(token));
} else {
double b = stack.pop();
double a = stack.pop();
stack.push(applyOperation(a, b, token));
}
}

return stack.pop();
}

private static boolean isNumber(String token) {
try {
Double.parseDouble(token);
return true;
} catch (NumberFormatException e) {
return false;
}
}

private static double applyOperation(double a, double b, String operator) {
if ("+".equals(operator)) {
return a + b;
} else if ("-".equals(operator)) {
return a - b;
} else if ("*".equals(operator)) {
return a * b;
} else if ("/".equals(operator)) {
if (b == 0) {
throw new UnsupportedOperationException("Cannot divide by zero");
}
return a / b;
}
throw new UnsupportedOperationException("Unsupported operation: " + operator);
}
}


13 changes: 13 additions & 0 deletions students/23K0350/23K0350-p23/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>23K0350</artifactId>
<groupId>ru.mirea.practice</groupId>
<version>2024.1</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>23K0350-p23</artifactId>
<description>Очередь</description>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ru.mirea.practice.s23k0350;

public abstract class AbstractQueue<T> implements Queue<T> {
protected int size;


public boolean isEmpty() {
return size == 0;
}


public int size() {
return size;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package ru.mirea.practice.s23k0350;

public class LinkedQueue<T> extends AbstractQueue<T> {
private Node<T> head;
private Node<T> tail;
private int size;

public LinkedQueue() {
head = null;
tail = null;
size = 0;
}

public void enqueue(T element) {
Node<T> newNode = new Node<>(element);
if (tail != null) {
tail.next = newNode;
}
tail = newNode;
if (head == null) {
head = newNode;
}
size++;
}

public T dequeue() {
if (isEmpty()) {
throw new IllegalStateException("Queue is empty");
}
head = head.next;
T value = head.value;
if (head == null) {
tail = null;
}
size--;
return value;
}


public T peek() {
if (isEmpty()) {
throw new IllegalStateException("Queue is empty");
}
return head.value;
}


public boolean isEmpty() {
return size == 0;
}


public int size() {
return size;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package ru.mirea.practice.s23k0350;

class Node<T> {
T value;
Node<T> next;

Node(T value) {
this.value = value;
this.next = null;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ru.mirea.practice.s23k0350;

public interface Queue<T> {
void enqueue(T element);

T dequeue();

T peek();

boolean isEmpty();

int size();
}
13 changes: 13 additions & 0 deletions students/23K0350/23K0350-p24/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>23K0350</artifactId>
<groupId>ru.mirea.practice</groupId>
<version>2024.1</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>23K0350-p24</artifactId>
<description>Фабрики</description>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package ru.mirea.practice.s23k0350;

interface Chair {
void sit();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package ru.mirea.practice.s23k0350;

abstract class ChairFactory {
public abstract Chair createChair();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package ru.mirea.practice.s23k0350;

class Client {
public void sit(Chair chair) {
chair.sit();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package ru.mirea.practice.s23k0350;

class MagicalChair implements Chair {
@Override
public void sit() {
System.out.println("Sitting on a magical chair.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package ru.mirea.practice.s23k0350;


class MagicalChairFactory extends ChairFactory {
@Override
public Chair createChair() {
return new MagicalChair();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package ru.mirea.practice.s23k0350;

abstract class Maine {
public static void main(String[] args) {
Client client = new Client();

ChairFactory victorianFactory = new VictorianChairFactory();
Chair victorianChair = victorianFactory.createChair();
client.sit(victorianChair);

ChairFactory multifunctionalFactory = new MultifunctionalChairFactory();
Chair multifunctionalChair = multifunctionalFactory.createChair();
client.sit(multifunctionalChair);

ChairFactory magicalFactory = new MagicalChairFactory();
Chair magicalChair = magicalFactory.createChair();
client.sit(magicalChair);
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package ru.mirea.practice.s23k0350;


class MultifunctionalChair implements Chair {
@Override
public void sit() {
System.out.println("Sitting on a multifunctional chair.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package ru.mirea.practice.s23k0350;


class MultifunctionalChairFactory extends ChairFactory {
@Override
public Chair createChair() {
return new MultifunctionalChair();
}
}
Loading
Loading