Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
lab5
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ii
INF101
25V
students
lab5
Commits
1a8ffd76
Commit
1a8ffd76
authored
3 months ago
by
Sondre Bolland
Browse files
Options
Downloads
Patches
Plain Diff
Removed Operator interface and classes dependence. Operators are hardcoded
parent
2e906771
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/main/java/no/uib/inf101/calculator/Calculator.java
+13
-37
13 additions, 37 deletions
src/main/java/no/uib/inf101/calculator/Calculator.java
src/main/java/no/uib/inf101/calculator/gui/CalculatorGUI.java
+0
-1
0 additions, 1 deletion
...main/java/no/uib/inf101/calculator/gui/CalculatorGUI.java
with
13 additions
and
38 deletions
src/main/java/no/uib/inf101/calculator/Calculator.java
+
13
−
37
View file @
1a8ffd76
package
no.uib.inf101.calculator
;
package
no.uib.inf101.calculator
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.HashMap
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Map
;
...
@@ -17,41 +18,8 @@ import no.uib.inf101.calculator.operations.Subtraction;
...
@@ -17,41 +18,8 @@ import no.uib.inf101.calculator.operations.Subtraction;
*/
*/
public
class
Calculator
{
public
class
Calculator
{
/**
* A map of operator symbols to their corresponding Operator implementations
*/
private
Map
<
String
,
Operator
>
operators
;
public
Calculator
()
{
public
Calculator
()
{
this
.
operators
=
new
HashMap
<>();
addOperators
();
}
/**
* Adds the default operators such as Addition, Subtraction, Multiplication to the calculator.
*/
private
void
addOperators
()
{
addOperator
(
new
Addition
());
addOperator
(
new
Subtraction
());
addOperator
(
new
Multiplication
());
}
/**
* Adds a single operator to the calculator.
* @param operator an implementation of the `Operator` interface
*/
private
void
addOperator
(
Operator
operator
)
{
operators
.
put
(
operator
.
getSymbol
(),
operator
);
}
/**
* Retrieves an operator by its symbol.
*
* @param symbol the symbol of the operator (e.g., "+", "-", "*")
* @return the `Operator` object corresponding to the symbol, or null if not found
*/
public
Operator
getOperator
(
String
symbol
)
{
return
operators
.
get
(
symbol
);
}
}
/**
/**
...
@@ -60,7 +28,8 @@ public class Calculator {
...
@@ -60,7 +28,8 @@ public class Calculator {
* @return a list of operator symbols
* @return a list of operator symbols
*/
*/
public
List
<
String
>
getOperatorSymbols
()
{
public
List
<
String
>
getOperatorSymbols
()
{
return
new
ArrayList
<
String
>(
operators
.
keySet
());
String
[]
operators
=
new
String
[]{
"+"
,
"-"
,
"*"
};
return
new
ArrayList
<
String
>(
Arrays
.
asList
(
operators
));
}
}
/**
/**
...
@@ -77,8 +46,6 @@ public class Calculator {
...
@@ -77,8 +46,6 @@ public class Calculator {
Expression
operand1
=
expression
.
getOperand1
();
Expression
operand1
=
expression
.
getOperand1
();
Expression
operand2
=
expression
.
getOperand2
();
Expression
operand2
=
expression
.
getOperand2
();
String
operatorSymbol
=
expression
.
getOperator
();
String
operatorSymbol
=
expression
.
getOperator
();
if
(!
operators
.
containsKey
(
operatorSymbol
))
throw
new
IllegalArgumentException
(
"The operator is not supported by the caluclator: "
+
operatorSymbol
);
return
evaluate
(
evaluate
(
operand1
),
evaluate
(
operand2
),
operatorSymbol
);
return
evaluate
(
evaluate
(
operand1
),
evaluate
(
operand2
),
operatorSymbol
);
}
}
...
@@ -93,7 +60,16 @@ public class Calculator {
...
@@ -93,7 +60,16 @@ public class Calculator {
* @throws NullPointerException if the operator is not found in the calculator
* @throws NullPointerException if the operator is not found in the calculator
*/
*/
private
double
evaluate
(
double
num1
,
double
num2
,
String
operatorSymbol
)
{
private
double
evaluate
(
double
num1
,
double
num2
,
String
operatorSymbol
)
{
return
operators
.
get
(
operatorSymbol
).
calculate
(
num1
,
num2
);
if
(
operatorSymbol
.
equals
(
"+"
))
{
return
num1
+
num2
;
}
if
(
operatorSymbol
.
equals
(
"-"
))
{
return
num1
-
num2
;
}
if
(
operatorSymbol
.
equals
(
"*"
))
{
return
num1
*
num2
;
}
throw
new
IllegalArgumentException
(
"The operator is not supported by the calculator: "
+
operatorSymbol
);
}
}
}
}
This diff is collapsed.
Click to expand it.
src/main/java/no/uib/inf101/calculator/gui/CalculatorGUI.java
+
0
−
1
View file @
1a8ffd76
...
@@ -9,7 +9,6 @@ import java.awt.GridLayout;
...
@@ -9,7 +9,6 @@ import java.awt.GridLayout;
import
java.awt.Toolkit
;
import
java.awt.Toolkit
;
import
java.awt.event.ActionEvent
;
import
java.awt.event.ActionEvent
;
import
java.awt.event.ActionListener
;
import
java.awt.event.ActionListener
;
import
java.io.FileNotFoundException
;
import
java.net.URI
;
import
java.net.URI
;
import
java.util.List
;
import
java.util.List
;
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment