Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

task_1_5_1 #18

Merged
merged 12 commits into from
Dec 15, 2023
9 changes: 9 additions & 0 deletions task_1_5_1/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#
# https://help.github.com/articles/dealing-with-line-endings/
#
# Linux start script should use lf
/gradlew text eol=lf

# These are Windows script files and should use crlf
*.bat text eol=crlf

7 changes: 7 additions & 0 deletions task_1_5_1/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Ignore Gradle project-specific cache directory
.gradle

# Ignore Gradle build output directory
build

bin
50 changes: 50 additions & 0 deletions task_1_5_1/app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* This file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Java application project to get you started.
* For more details on building Java & JVM projects, please refer to https://docs.gradle.org/8.3/userguide/building_java_projects.html in the Gradle documentation.
*/

plugins {
// Apply the application plugin to add support for building a CLI application in Java.
id 'application'
id 'jacoco'
}

repositories {
// Use Maven Central for resolving dependencies.
mavenCentral()
}

dependencies {
// Use JUnit Jupiter for testing.
testImplementation 'org.junit.jupiter:junit-jupiter:5.9.3'

testRuntimeOnly 'org.junit.platform:junit-platform-launcher'

// This dependency is used by the application.
implementation 'com.google.guava:guava:32.1.1-jre'
}

// Apply a specific Java toolchain to ease working on different environments.
java {
toolchain {
languageVersion = JavaLanguageVersion.of(20)
}
}

application {
// Define the main class for the application.
mainClass = 'task.App'
}

tasks.named('test') {
// Use JUnit Platform for unit tests.
useJUnitPlatform()
}

jacocoTestReport {
reports {
xml.required = true
}
}
15 changes: 15 additions & 0 deletions task_1_5_1/app/src/main/java/task/CalculationException.java
galinabykova marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package task;

/**
* Custom exception class for handling calculating exceptions.
*/
public class CalculationException extends ArithmeticException {
/**
* Constructs a CalculationException with the specified error message.
*
* @param message is a detailed message.
*/
public CalculationException(String message) {
super(message);
}
}
56 changes: 56 additions & 0 deletions task_1_5_1/app/src/main/java/task/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@

package task;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Дай более осмысленное имя пакету. У тебя у всех лаб одни те же названия пакетов, хотя задачи ты делаешь на разные темы


import java.util.Stack;
import task.operations.Operation;
import task.operations.OprationFactory;

/**
* A class that implements methods for calculating the value of an arithmetic expression.
*/
public class Calculator {
/**
* Equals to zero value using in functionalities class.
*/
public static double zeroValue = 0.000000001;

/**
* Method-helper to determine operator.
*
* @param elem parsed token.
* @return true if this is an operator.
*/
private static boolean isOperator(String elem) {
String ops = "sin cos log pow sqrt + - * /";
if (ops.indexOf(elem) != -1) {
return true;
}
return false;
}

/**
* Static method realises calculating.
*
* @param input input string of expression.
* @return result of expression if possible.
*/
public static double calculate(String input) {
String[] substrings = input.split(" ");

Stack<Double> stack = new Stack<>();

for (int i = substrings.length - 1; i >= 0; i--) {
if (isOperator(substrings[i])) {
Operation op = OprationFactory.create(substrings[i]);
stack.push(op.apply(stack));
} else {
try {
stack.push(Double.parseDouble(substrings[i]));
} catch (NumberFormatException e) {
throw new CalculationException("Incorrect or non-existent operator!");
}
}
}
return stack.pop();
}
}
19 changes: 19 additions & 0 deletions task_1_5_1/app/src/main/java/task/operations/Addition.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package task.operations;

import java.util.EmptyStackException;
import java.util.Stack;
import task.CalculationException;

/**
* Class implements addition of two numbers in stack.
*/
public final class Addition implements Operation {
@Override
public double apply(Stack<Double> stack) {
try {
return stack.pop() + stack.pop();
} catch (EmptyStackException e) {
throw new CalculationException("Incorrect expression!");
}
}
}
19 changes: 19 additions & 0 deletions task_1_5_1/app/src/main/java/task/operations/Cos.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package task.operations;

import java.util.EmptyStackException;
import java.util.Stack;
import task.CalculationException;

/**
* Class implements cosinus calculations.
*/
public final class Cos implements Operation {
@Override
public double apply(Stack<Double> stack) {
try {
return Math.cos(stack.pop());
} catch (EmptyStackException e) {
throw new CalculationException("Incorrect expression!");
}
}
}
27 changes: 27 additions & 0 deletions task_1_5_1/app/src/main/java/task/operations/Division.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package task.operations;

import java.util.EmptyStackException;
import java.util.Stack;
import task.CalculationException;
import task.Calculator;

/**
* Class implements division of two numbers in stack.
*/
public final class Division implements Operation {
@Override
public double apply(Stack<Double> stack) {
double op1;
double op2;
try {
op1 = stack.pop();
op2 = stack.pop();
} catch (EmptyStackException e) {
throw new CalculationException("Incorrect expression!");
}
if (Math.abs(op2) < Calculator.zeroValue) {
throw new CalculationException("Division by zero!");
}
return op1 / op2;
}
}
25 changes: 25 additions & 0 deletions task_1_5_1/app/src/main/java/task/operations/Log.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package task.operations;

import java.util.EmptyStackException;
import java.util.Stack;
import task.CalculationException;
import task.Calculator;

/**
* Class implements log calculations.
*/
public final class Log implements Operation {
@Override
public double apply(Stack<Double> stack) {
double op;
try {
op = stack.pop();
} catch (EmptyStackException e) {
throw new CalculationException("Incorrect expression!");
}
if (op < Calculator.zeroValue) {
throw new CalculationException("The argument of the log function is less than zero!");
}
return Math.log(op);
}
}
19 changes: 19 additions & 0 deletions task_1_5_1/app/src/main/java/task/operations/Multiplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package task.operations;

import java.util.EmptyStackException;
import java.util.Stack;
import task.CalculationException;

/**
* Class implements multiplication of two numbers in stack.
*/
public final class Multiplication implements Operation {
@Override
public double apply(Stack<Double> stack) {
try {
return stack.pop() * stack.pop();
} catch (EmptyStackException e) {
throw new CalculationException("Incorrect expression!");
}
}
}
17 changes: 17 additions & 0 deletions task_1_5_1/app/src/main/java/task/operations/Operation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package task.operations;

import java.util.Stack;

/**
* Sealed interface (contract) that brings together all operations.
*/
public sealed interface Operation permits Addition, Substraction, Multiplication,
Division, Sin, Log, Cos, Sqrt, Pow {
/**
* Abstract method implies the implementation of the functionality of applying an operation.
*
* @param stack stack of input numbers.
* @return result of operation.
*/
public double apply(Stack<Double> stack);
}
27 changes: 27 additions & 0 deletions task_1_5_1/app/src/main/java/task/operations/OprationFactory.java
galinabykova marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package task.operations;

/**
* Factory class create objects of arithmetical operations classes.
*/
public class OprationFactory {
galinabykova marked this conversation as resolved.
Show resolved Hide resolved
/**
* Method creates class instances.
*
* @param token operation.
* @return wrapped class of arithmetical operation.
*/
public static Operation create(String token) {
switch (token) {
case "+": return new Addition();
case "-": return new Substraction();
case "*": return new Multiplication();
case "/": return new Division();
case "sin": return new Sin();
case "cos": return new Cos();
case "log": return new Log();
case "sqrt": return new Sqrt();
case "pow": return new Pow();
default: return null;
}
}
}
27 changes: 27 additions & 0 deletions task_1_5_1/app/src/main/java/task/operations/Pow.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package task.operations;

import java.util.EmptyStackException;
import java.util.Stack;
import task.CalculationException;
import task.Calculator;

/**
* Class implements pow calculating.
*/
public final class Pow implements Operation {
@Override
public double apply(Stack<Double> stack) {
double op1;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Плохой нейминг

double op2;
try {
op1 = stack.pop();
op2 = stack.pop();
} catch (EmptyStackException e) {
throw new CalculationException("Incorrect expression!");
}
if (op1 < Calculator.zeroValue) {
throw new CalculationException("The base of the pow function is less than zero!");
}
return Math.pow(op1, op2);
}
}
20 changes: 20 additions & 0 deletions task_1_5_1/app/src/main/java/task/operations/Sin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package task.operations;

import java.util.EmptyStackException;
import java.util.Stack;
import task.CalculationException;

/**
* Class implements sinus calculations.
*/
public final class Sin implements Operation {
@Override
public double apply(Stack<Double> stack) {
try {
return Math.sin(stack.pop());
} catch (EmptyStackException e) {
throw new CalculationException("Incorrect expression!");
}
}

}
25 changes: 25 additions & 0 deletions task_1_5_1/app/src/main/java/task/operations/Sqrt.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package task.operations;

import java.util.EmptyStackException;
import java.util.Stack;
import task.CalculationException;
import task.Calculator;

/**
* Class implements sqrt calculations.
*/
public final class Sqrt implements Operation {
@Override
public double apply(Stack<Double> stack) {
double op;
try {
op = stack.pop();
} catch (EmptyStackException e) {
throw new CalculationException("Incorrect expression!");
}
if (op < Calculator.zeroValue) {
throw new CalculationException("The argument of the sqrt function is less than zero!");
}
return Math.sqrt(op);
}
}
19 changes: 19 additions & 0 deletions task_1_5_1/app/src/main/java/task/operations/Substraction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package task.operations;

import java.util.EmptyStackException;
import java.util.Stack;
import task.CalculationException;

/**
* Class implements substraction of two numbers in stack.
*/
public final class Substraction implements Operation {
@Override
public double apply(Stack<Double> stack) {
try {
return stack.pop() - stack.pop();
} catch (EmptyStackException e) {
throw new CalculationException("Incorrect expression");
}
}
}
Loading
Loading