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

Лабораторные 21-32 #699

Merged
merged 1 commit into from
Dec 14, 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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ru.mirea.practice.s23k0755.task1;
package ru.mirea.practice.s23k0755;

import java.util.Arrays;
import java.util.Random;
Expand Down Expand Up @@ -49,4 +49,4 @@ public static void main(String[] args) {
throw new RuntimeException(e);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ru.mirea.practice.s23k0755.task1;
package ru.mirea.practice.s23k0755;

class Magazine implements Printable {
private String title;
Expand All @@ -18,4 +18,4 @@ public static void printMagazines(Printable[] printables) {
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ru.mirea.practice.s23k0755.task1;
package ru.mirea.practice.s23k0755;

interface Printable {
String getTitle();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ru.mirea.practice.s23k0755.task1;
package ru.mirea.practice.s23k0755;

public abstract class TestMagazine {
public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ru.mirea.practice.s23k0755.task1;
package ru.mirea.practice.s23k0755;

public class Exception {
public void exceptionDemo() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ru.mirea.practice.s23k0755.task1;
package ru.mirea.practice.s23k0755;

public class Exception1 {
public void exceptionDemo() {
Expand Down
13 changes: 13 additions & 0 deletions students/23K0755/23K0755-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>23K0755</artifactId>
<groupId>ru.mirea.practice</groupId>
<version>2024.1</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>23K0755-p21</artifactId>
<description>Массивы</description>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package ru.mirea.practice.s23k0755.task1;

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

public abstract class Converter {

public static <T> List<T> arrayToList(T[] array) {
return new ArrayList<>(Arrays.asList(array));
}

public static void main(String[] args) {
String[] stringArray = {"Петя", "Вася", "Федя"};
List<String> stringList = arrayToList(stringArray);
System.out.println("String list: " + stringList);

Integer[] intArray = {1, 2, 3, 4, 5};
List<Integer> intList = arrayToList(intArray);
System.out.println("Integer list: " + intList);
}
}

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

public class GenericArray<T> {
private T[] elements;

public GenericArray(T[] elements) {
this.elements = elements;
}

public T get(int index) {
if (index >= 0 && index < elements.length) {
return elements[index];
} else {
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + elements.length);
}
}

public void set(int index, T element) {
if (index >= 0 && index < elements.length) {
elements[index] = element;
} else {
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + elements.length);
}
}

public int size() {
return elements.length;
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder("[");
for (int i = 0; i < elements.length; i++) {
sb.append(elements[i]);
if (i < elements.length - 1) {
sb.append(", ");
}
}
sb.append("]");
return sb.toString();
}

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

String[] stringArray = {"Петя", "Вася", "Федя"};
GenericArray<String> stringStorage = new GenericArray<>(stringArray);
System.out.println("String array: " + stringStorage);
}
}

13 changes: 13 additions & 0 deletions students/23K0755/23K0755-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>23K0755</artifactId>
<groupId>ru.mirea.practice</groupId>
<version>2024.1</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>23K0755-p22</artifactId>
<description>Массивы</description>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package ru.mirea.practice.s23k0755;

public class RpnCalculator {
private Stack stack;

public RpnCalculator(int size) {
stack = new Stack(size);
}

public double evaluate(String expression) {
String[] strings = expression.split(" ");
for (String element : strings) {
try {
float f = Float.parseFloat(element);
stack.push(f);
} catch (NumberFormatException e) {
performOperation(element);
}
}
return stack.pop();
}

private void performOperation(String operator) {
float secondOperand = stack.pop();
float firstOperand = stack.pop();
switch (operator) {
case "+":
stack.push(firstOperand + secondOperand);
break;
case "-":
stack.push(firstOperand - secondOperand);
break;
case "*":
stack.push(firstOperand * secondOperand);
break;
case "/":
if (secondOperand == 0) {
throw new ArithmeticException("Ошибка: Деление на ноль");
}
stack.push(firstOperand / secondOperand);
break;
default:
throw new IllegalArgumentException("Недопустимый оператор: " + operator);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package ru.mirea.practice.s23k0755;

class Stack {
// store elements of stack
private float[] arr;
// represent top of stack
private int top;
// total capacity of the stack
private int capacity;

// Creating a stack
Stack(int size) {
// initialize the array
// initialize the stack variables
arr = new float[size];
capacity = size;
top = -1;
}

// push elements to the top of stack
public void push(float x) {
if (isFull()) {
System.out.println("Stack OverFlow");
// terminates the program
System.exit(1);
}
// insert element on top of stack
System.out.println("Inserting " + x);
arr[++top] = x;
}

// pop elements from top of stack
public float pop() {
// if stack is empty
// no element to pop
if (isEmpty()) {
System.out.println("STACK EMPTY");
// terminates the program
System.exit(1);
}
// pop element from top of stack
return arr[top--];
}

// return size of the stack
public int getSize() {
return top + 1;
}

// check if the stack is empty
public Boolean isEmpty() {
return top == -1;
}

// check if the stack is full
public Boolean isFull() {
return top == capacity - 1;
}

// display elements of stack
public void printStack() {
for (int i = 0; i <= top; i++) {
System.out.print(arr[i] + ", ");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ru.mirea.practice.s23k0755;

public abstract class Test {
public static void main(String[] args) {
RpnCalculator calculator = new RpnCalculator(10);
System.out.println("16.1 3.9 + = " + calculator.evaluate("16.1 3.9 +"));
System.out.println("8 4 - = " + calculator.evaluate("8 4 -"));
System.out.println("28 0.5 * = " + calculator.evaluate("28 0.5 *"));
System.out.println("7 2 / = " + calculator.evaluate("7 2 /"));
System.out.println("3 4 + 2 * = " + calculator.evaluate("3 4 + 2 *"));

try {
System.out.println("38 0 / = " + calculator.evaluate("38 0 /"));
} catch (ArithmeticException e) {
System.out.println(e.getMessage());
}
}
}
13 changes: 13 additions & 0 deletions students/23K0755/23K0755-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>23K0755</artifactId>
<groupId>ru.mirea.practice</groupId>
<version>2024.1</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>23K0755-p23</artifactId>
<description>Массивы</description>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package ru.mirea.practice.s23k0755.task1;

public class ArrayQueue {
private double[] elements;
private int head;
private int tail;
private int size;

public ArrayQueue(int capacity) {
this.elements = new double[capacity];
this.head = 0;
this.tail = 0;
this.size = 0;
}

public void enqueue(double value) {
if (size == elements.length) {
throw new IllegalStateException("Очередь переполнена");
}
elements[tail] = value;
tail = (tail + 1) % elements.length;
size++;
}

public double dequeue() {
if (isEmpty()) {
throw new IllegalStateException("Очередь пуста");
}
double value = elements[head];
head = (head + 1) % elements.length;
size--;
return value;
}

public double element() {
if (isEmpty()) {
throw new IllegalStateException("Очередь пуста");
}
return elements[head];
}

public int size() {
return size;
}

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

public void clear() {
head = 0;
tail = 0;
size = 0;
}
}
Loading
Loading