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
}
}
57 changes: 57 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,57 @@

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.exceptions.IncorrectExpressionException;
import task.factory.OperationFactory;
import task.operations.Operation;

/**
* 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 = OperationFactory.create(substrings[i]);
stack.push(op.apply(stack));
} else {
try {
stack.push(Double.parseDouble(substrings[i]));
} catch (NumberFormatException e) {
throw new IncorrectExpressionException();
}
}
}
return stack.pop();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package task.exceptions;

/**
* Custom exception class for handling calculating exceptions.
*/
public class CalculationException extends ArithmeticException {

/**
* Constructs a CalculationException without parameters.
*/
public CalculationException() {
super();
}

/**
* Constructs a CalculationException with the specified error message.
*
* @param message is a detailed message.
*/
public CalculationException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package task.exceptions;

/**
* Custom exception class for handling incorrect expression.
*/
public class IncorrectExpressionException extends CalculationException {

/**
* Constructs a IncorrectExpressionException without parameters.
*/
public IncorrectExpressionException() {
super();
}

/**
* Constructs a IncorrectExpressionException with the specified error message.
*
* @param message is a detailed message.
*/
public IncorrectExpressionException(String message) {
super(message);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package task.exceptions;

/**
* Custom exception class for handling incorrect log arguments.
*/
public class LogLessZeroArgumentException extends CalculationException {
/**
* Constructs a LogLessZeroArgumentException without parameters.
*/
public LogLessZeroArgumentException() {
super();
}

/**
* Constructs a LogLessZeroArgumentException with the specified error message.
*
* @param message is a detailed message.
*/
public LogLessZeroArgumentException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package task.exceptions;

/**
* Custom exception class for handling incorrect argument in pow method.
*/
public class PowLessZeroArgumentException extends CalculationException {
/**
* Constructs a PowLessZeroArgumentException without parameters.
*/
public PowLessZeroArgumentException() {
super();
}

/**
* Constructs a PowLessZeroArgumentException with the specified error message.
*
* @param message is a detailed message.
*/
public PowLessZeroArgumentException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package task.exceptions;

/**
* Custom exception class for handling incorrect argument in sqrt method.
*/
public class SqrtLessZeroArgumentException extends CalculationException {
/**
* Constructs a SqrtLessZeroArgumentException without parameters.
*/
public SqrtLessZeroArgumentException() {
super();
}

/**
* Constructs a SqrtLessZeroArgumentException with the specified error message.
*
* @param message is a detailed message.
*/
public SqrtLessZeroArgumentException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package task.exceptions;

/**
* Custom exception class for handling division by zero.
*/
public class ZeroDivisionException extends CalculationException {

/**
* Constructs a ZeroDivisionException without parameters.
*/
public ZeroDivisionException() {
super();
}

/**
* Constructs a ZeroDivisionException with the specified error message.
*
* @param message is a detailed message.
*/
public ZeroDivisionException(String message) {
super(message);
}
}
38 changes: 38 additions & 0 deletions task_1_5_1/app/src/main/java/task/Factory/OperationFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package task.factory;

Choose a reason for hiding this comment

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

Почему ты решил перенести фабрику в отдельный пакет?


import task.operations.Addition;
import task.operations.Cos;
import task.operations.Division;
import task.operations.Log;
import task.operations.Multiplication;
import task.operations.Operation;
import task.operations.Pow;
import task.operations.Sin;
import task.operations.Sqrt;
import task.operations.Substraction;

/**
* Factory class create objects of arithmetical operations classes.
*/
public class OperationFactory {
/**
* 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;
}
}
}
20 changes: 20 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,20 @@
package task.operations;

import java.util.EmptyStackException;
import java.util.Stack;
import task.exceptions.IncorrectExpressionException;


/**
* 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 IncorrectExpressionException();
}
}
}
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.exceptions.IncorrectExpressionException;

/**
* 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 IncorrectExpressionException();
}
}
}
28 changes: 28 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,28 @@
package task.operations;

import java.util.EmptyStackException;
import java.util.Stack;
import task.Calculator;
import task.exceptions.IncorrectExpressionException;
import task.exceptions.ZeroDivisionException;

/**
* 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 IncorrectExpressionException();
}
if (Math.abs(op2) < Calculator.zeroValue) {
throw new ZeroDivisionException();
}
return op1 / op2;
}
}
Loading
Loading