-
Notifications
You must be signed in to change notification settings - Fork 0
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
task_1_5_1 #18
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
91cd6dc
Initiallization of 5th task
vlasival a556941
Task 5 factory realisation
vlasival df073fc
remove bin folder
vlasival 50cb165
Purify
vlasival 6d05975
Purify
vlasival f8c6eb8
Purify
vlasival 66165b7
Constanting variable value
vlasival dfa8ca6
Updating javadoc with checking warnings
vlasival 3902562
Adding custom exception hierarchy and splitting code by packages
vlasival cfb1a4e
Adding custom exception hierarchy and splitting code by packages, fix…
vlasival 2ea2d74
Adding custom exception hierarchy and splitting code by packages, fix…
vlasival bec8ffa
Adding custom exception hierarchy and splitting code by packages, fix…
vlasival File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
|
||
package task; | ||
|
||
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(); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
task_1_5_1/app/src/main/java/task/Exceptions/CalculationException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
task_1_5_1/app/src/main/java/task/Exceptions/IncorrectExpressionException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
task_1_5_1/app/src/main/java/task/Exceptions/LogLessZeroArgumentException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
task_1_5_1/app/src/main/java/task/Exceptions/PowLessZeroArgumentException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
task_1_5_1/app/src/main/java/task/Exceptions/SqrtLessZeroArgumentException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
task_1_5_1/app/src/main/java/task/Exceptions/ZeroDivisionException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
38
task_1_5_1/app/src/main/java/task/Factory/OperationFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package task.factory; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
20
task_1_5_1/app/src/main/java/task/operations/Addition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
28
task_1_5_1/app/src/main/java/task/operations/Division.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Дай более осмысленное имя пакету. У тебя у всех лаб одни те же названия пакетов, хотя задачи ты делаешь на разные темы