From 7dca60e79fed74365dcf343d866722ea2dc9ac80 Mon Sep 17 00:00:00 2001 From: Adam Bennett Date: Fri, 7 Feb 2020 18:29:09 -0500 Subject: [PATCH 01/22] Setup junit dependencies --- bin/.gitignore | 155 ++++++++++++++++++ bin/README.md | 90 ++++++++++ bin/pom.xml | 12 ++ .../scientificcalculator/Console.class | Bin 0 -> 2787 bytes .../MainApplication.class | Bin 0 -> 1960 bytes .../TestMainApplication.class | Bin 0 -> 763 bytes pom.xml | 8 + .../scientificcalculator/Console.java | 3 +- .../TestMainApplication.java | 11 +- 9 files changed, 277 insertions(+), 2 deletions(-) create mode 100644 bin/.gitignore create mode 100644 bin/README.md create mode 100644 bin/pom.xml create mode 100644 bin/src/main/java/com/zipcodewilmington/scientificcalculator/Console.class create mode 100644 bin/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.class create mode 100644 bin/src/test/java/com/zipcodewilmington/scientific_calculator/TestMainApplication.class diff --git a/bin/.gitignore b/bin/.gitignore new file mode 100644 index 00000000..de469bb3 --- /dev/null +++ b/bin/.gitignore @@ -0,0 +1,155 @@ +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff: +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/dictionaries + +# Sensitive or high-churn files: +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.xml +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml + +# Gradle: +.idea/**/gradle.xml +.idea/**/libraries + +# CMake +cmake-build-debug/ + +# Mongo Explorer plugin: +.idea/**/mongoSettings.xml + +## File-based project format: +*.iws + +## Plugin-specific files: + +# IntelliJ +/out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff: +*.iml +.idea/** +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/dictionaries + +# Sensitive or high-churn files: +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.xml +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml + +# Gradle: +.idea/**/gradle.xml +.idea/**/libraries + +# CMake +cmake-build-debug/ + +# Mongo Explorer plugin: +.idea/**/mongoSettings.xml + +## File-based project format: +*.iws + +## Plugin-specific files: + +# IntelliJ +/out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm + # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + + # User-specific stuff: + .idea/**/workspace.xml + .idea/**/tasks.xml + .idea/dictionaries + + # Sensitive or high-churn files: + .idea/**/dataSources/ + .idea/**/dataSources.ids + .idea/**/dataSources.xml + .idea/**/dataSources.local.xml + .idea/**/sqlDataSources.xml + .idea/**/dynamic.xml + .idea/**/uiDesigner.xml + + # Gradle: + .idea/**/gradle.xml + .idea/**/libraries + + # CMake + cmake-build-debug/ + + # Mongo Explorer plugin: + .idea/**/mongoSettings.xml + + ## File-based project format: + *.iws + + ## Plugin-specific files: + + # IntelliJ + /out/ + + # mpeltonen/sbt-idea plugin + .idea_modules/ + + # JIRA plugin + atlassian-ide-plugin.xml + + # Cursive Clojure plugin + .idea/replstate.xml + + # Crashlytics plugin (for Android Studio and IntelliJ) + com_crashlytics_export_strings.xml + crashlytics.properties + crashlytics-build.properties + fabric.properties +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +.project +.classpath +.settings + + +#maven build target +target/ \ No newline at end of file diff --git a/bin/README.md b/bin/README.md new file mode 100644 index 00000000..8402302c --- /dev/null +++ b/bin/README.md @@ -0,0 +1,90 @@ +# ScientificCalculator (maven) ZCW + + +## Description + +In this project your team will build a small app to function as a calculator. This app will be built in Java, and will use the topics and techniques discussed during the week. + +Your team should work on this project in a single repository. Click the `fork` button in the top right corner to create a copy of this repository in one of your github accounts. You can go through the [GitHub forking tutorial](https://help.github.com/articles/fork-a-repo/) if you need additional practice with this. + +Then, each of the other team members should clone from the shared repository. You may want to dived the work into various classes, where each person takes responsibility for a whole class. + +## Requirements + +### Testing + +All features must be tested with Test classes. Tests must include normal behavior, and any possible error situations. Tests must have descriptive names and should be independent of each other (running or not running one test should not influence the behavior of any other test). + +### Documentation + +You must produce UML diagrams for your program. All classes (excluding test classes) must be included in the UML class diagrams. Each class should have a UML box, with variables and a list of methods. + +### Core Features + +All calculators should have the following features: + +- A state, representing the value currently displayed on the calculator (default 0) * +- Get the current number on the display * +- Clear the display * +- Change the number on the display * +- Add, subtract, multiply, and divide the value on the display by a given number +- Calculate the square (x2) and square root (√x) of the number on the display * +- Calculate variable exponentiation (xy) +- Calculate the inverse of the number on the display (1/x) * +- Invert the sign of the number on the display (switch between positive and negative) +- Update the display to `Err` if an error occurs (eg: Division by zero) * +- Errors must be cleared before any other operation can take place * + +Each operation should automatically update the display + + +### Scientific Features + +- Switch display mode (binary, octal, decimal, hexadecimal) + - `switchDisplayMode()` should rotate through the options + - `switchDisplayMode(String mode)` should set the display to the mode given +- Memory - Store up to one numeric value in memory for recall later (default to 0) * + - (`M+` key) Add the currently displayed value to the value in memory (store in memory and update display) * + - (`MC` key) Reset memory * + - (`MRC` key) Recall the current value from memory to the display * +- Trig functions + - Sine - Calculate the sine of the displayed value and display it + - Cosine - Calculate the cosine of the displayed value and display it + - Tangent - Calculate the tangent of the displayed value and display it + - Inverse Sine + - Inverse Cosine + - Inverse Tangent +- Switch trig units mode (Degrees, Radians) + - `switchUnitsMode()` should rotate through the options + - `switchUnitsMode(String mode)` should set the trig units to the type given +- Logarithmic functions + - Log + - 10x (inverse logarithm) + - Ln (natural logarithm) + - ex (inverse natural logarithm) +- Factorial function + + +### Custom Features + +In addition to the Core and Scientific features, you are required to create at least two of your own features for the calculator. They can be any two features that are not already covered and that you can implement as you see fit. These features must be properly tested. + +### Hints + +The following functions should take the displayed value (x) and updated it according to the given formula: (this may not be an exhaustive list) + +- `square()`: x2 +- `squareRoot()`: √x +- `inverse()`: 1/x +- `switchSign()`: -x +- `sine()`: sin(x) +- `cosine()`: cos(x) +- `tangent()`: tan(x) +- `inverseSine()`: sin-1(x) +- `inverseCosine()`: sin-1(x) +- `inverseTangent()`: tan-1(x) +- `factorial()`: x! (x factorial) + +## Submission + +Completed projects should be submitted by submitting a pull request against the [original repository](https://github.com/Zipcoder/ScientificCalculator.Maven). All work should be done in your team's repository. diff --git a/bin/pom.xml b/bin/pom.xml new file mode 100644 index 00000000..e7cb4f6b --- /dev/null +++ b/bin/pom.xml @@ -0,0 +1,12 @@ + + + 4.0.0 + + com.zipcodewilmington + scientific_calculator + 1.0-SNAPSHOT + + + \ No newline at end of file diff --git a/bin/src/main/java/com/zipcodewilmington/scientificcalculator/Console.class b/bin/src/main/java/com/zipcodewilmington/scientificcalculator/Console.class new file mode 100644 index 0000000000000000000000000000000000000000..7e3de06faa4c5935758ab80bdcd412b83487e2d6 GIT binary patch literal 2787 zcmcgu&2G~`5S~rbIx(d!DFq4?w&gG3$GLE#2q6kcky1cPsf0K=&L-Vr@0#6pp}YX< z6Cr^FcOD8cj@{N(9o0a@CGmP@zM1)Ec050Sefdk z<|iI{vd2DfKj30uq-bi7Ghuk2d!Flik?$I*nrl*M=`#z;1m=duzAFaJm)&jV83I!e zx!~pzfr)x!lR$Y5AuXs9m_5{erj%3_I7P~@UJJz#Zin?K#t?G!!KI*~l3kw#`T@18 z8(WN;-H_23z=@qXi8JcCLP$fqjP8Rtv}GvQl#3o$7>~b;qCQhhc<7<8WPs=Q5z2bh z@qAZn+UGvgxn4R(ArWKu(qrg24j^>BckY1E70lc@#=eus{tE3$rfHmY;2Lj>8YG8( z3^CO#AQsn&i|Y(2`vdrd2Ofk{nS}4ia6g9vyV?pupL^U;9feF`^@KJm@~}l{Ql5Gv zu0$kytj|TvK1%S}jeL{UcIbMfO)Nnz>WVm>1ULg}S4S3u8cE$@gV@!PeUVk_>}tb; z+?>YfWK{1QiKLKH+~qOB$C~{JR57hlsV7i7uHr7Y0=2fp3AdT1pS~=cEv^YXY5(h} zTqQ6W;wZuCc%|M>19_W?Eoobftpeu=d@e8($JnT&TEZq^4Wsr&1r`bT|1&HCGXrLl zEL$Rskh%H}harIOVOEE5O{ za5lc%ofI><;yJ}k1Q^(jVof%-SwX<=$VhqYIj#T%rqj)4_|deWfxArN%7Qz17g9bf zxQBwISI}miYmH072JR;smI2ouT(SVB@M&`RYYmsK2|NSX-=OlQW`BX{A9yOkY^n(; zox|tpL<`P94NrS84`=aBjBp!qOZcB4EaV{ALxfmo56+LUyO3iyb6{76i*PC9?=a%a vId-*--4(c+Ye#eJ7BhA$a4pyFdXC-Ffn5o1VwNSi1y0Nel%bCN+=YqX7ZOpa literal 0 HcmV?d00001 diff --git a/bin/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.class b/bin/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.class new file mode 100644 index 0000000000000000000000000000000000000000..0be7cc8149ed08a0b7c46735817fcba1a7445092 GIT binary patch literal 1960 zcmeHI&u`N(6n<{Ir3rKZXPn z-1(yr&vC^D?69ev5QjKU^1aWWzr5%7?fa)M0B{d(MbIGd)Eb@@OsedV9kGn-BKsgq zuE*l7%nPZePOHpvDU_2F$vMk;l{2SJwueCv%QBaoIjL0yEdnbwNzT+Xdoel`+!0v5 zFO_r;2sC^B0|Kobq>dma&^^XJHb$E^Tp-P@m&ypM^CK~)sHK#+7LwD_=us{TyG4_D zI1_X%c+L!hO2*%?si2#PPXADzo~C@0j4Lv6z@uudy){1Nxda!HGRO0 z^w0B`EjN>vz_Y>MIvlpK@O>>2*yz1JOZ(-c^ EAL@61(EtDd literal 0 HcmV?d00001 diff --git a/bin/src/test/java/com/zipcodewilmington/scientific_calculator/TestMainApplication.class b/bin/src/test/java/com/zipcodewilmington/scientific_calculator/TestMainApplication.class new file mode 100644 index 0000000000000000000000000000000000000000..e4d78d248170ce0df8df79b91c3c3fa6ce60392b GIT binary patch literal 763 zcmb_aO>Yx15FMwXNtQs`KqZc*r}VOWp%;pfs)|ZT2|YBe)Dv>NPBO)7uk6`^_+v;^ zf;&H|Vw@l~61^aC8Gk*$d47Js?!Ny3fLE~BftG+t47wl_skl}*s?feFzGAI?X+EQ^ z&@ITjB5E{_aE|(;R#rDwkq^a`Z%q`sXbad`F6P~Na(k42WlO|F+VU$!Z$y^g}P-Yo) z;YlAJ!BYWyqw&45=M?xKkCO3&F*AH#a;>+jy-?rr3#qVe>U5+#3scientific_calculator 1.0-SNAPSHOT + + + junit + junit + 4.13 + test + + \ No newline at end of file diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Console.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Console.java index 83f0e97f..d1913631 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Console.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Console.java @@ -5,6 +5,7 @@ /** * Created by leon on 2/9/18. */ +@SuppressWarnings("resource") public class Console { public static void print(String output, Object... args) { @@ -16,7 +17,7 @@ public static void println(String output, Object... args) { } public static String getStringInput(String prompt) { - Scanner scanner = new Scanner(System.in); + Scanner scanner = new Scanner(System.in); println(prompt); String userInput = scanner.nextLine(); return userInput; diff --git a/src/test/java/com/zipcodewilmington/scientific_calculator/TestMainApplication.java b/src/test/java/com/zipcodewilmington/scientific_calculator/TestMainApplication.java index 94e8d987..ea44e19a 100644 --- a/src/test/java/com/zipcodewilmington/scientific_calculator/TestMainApplication.java +++ b/src/test/java/com/zipcodewilmington/scientific_calculator/TestMainApplication.java @@ -1,7 +1,16 @@ package com.zipcodewilmington.scientific_calculator; +import org.junit.*; + /** * Created by leon on 2/9/18. */ -public class TestMainApplication { +public class TestMainApplication +{ + @Test + public void templateTestClass() + { + + } + } From c94633ac9aadd36832c1b6693abfca14c91902ea Mon Sep 17 00:00:00 2001 From: Adam Bennett Date: Fri, 7 Feb 2020 19:20:13 -0500 Subject: [PATCH 02/22] cleaned up project structure --- .gitignore | 4 +- bin/.gitignore | 155 ------------------ bin/README.md | 90 ---------- bin/pom.xml | 12 -- .../scientificcalculator/Console.class | Bin 2787 -> 0 bytes .../MainApplication.class | Bin 1960 -> 0 bytes .../TestMainApplication.class | Bin 763 -> 0 bytes .../{ => Application}/Console.java | 2 +- .../{ => Application}/MainApplication.java | 4 +- .../TestMainApplication.java | 2 +- 10 files changed, 7 insertions(+), 262 deletions(-) delete mode 100644 bin/.gitignore delete mode 100644 bin/README.md delete mode 100644 bin/pom.xml delete mode 100644 bin/src/main/java/com/zipcodewilmington/scientificcalculator/Console.class delete mode 100644 bin/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.class delete mode 100644 bin/src/test/java/com/zipcodewilmington/scientific_calculator/TestMainApplication.class rename src/main/java/com/zipcodewilmington/scientificcalculator/{ => Application}/Console.java (91%) rename src/main/java/com/zipcodewilmington/scientificcalculator/{ => Application}/MainApplication.java (86%) rename src/test/java/com/zipcodewilmington/scientific_calculator/{ => Application}/TestMainApplication.java (69%) diff --git a/.gitignore b/.gitignore index de469bb3..dd8dd225 100644 --- a/.gitignore +++ b/.gitignore @@ -150,6 +150,6 @@ fabric.properties .classpath .settings - #maven build target -target/ \ No newline at end of file +target/ +/bin diff --git a/bin/.gitignore b/bin/.gitignore deleted file mode 100644 index de469bb3..00000000 --- a/bin/.gitignore +++ /dev/null @@ -1,155 +0,0 @@ -# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm -# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 - -# User-specific stuff: -.idea/**/workspace.xml -.idea/**/tasks.xml -.idea/dictionaries - -# Sensitive or high-churn files: -.idea/**/dataSources/ -.idea/**/dataSources.ids -.idea/**/dataSources.xml -.idea/**/dataSources.local.xml -.idea/**/sqlDataSources.xml -.idea/**/dynamic.xml -.idea/**/uiDesigner.xml - -# Gradle: -.idea/**/gradle.xml -.idea/**/libraries - -# CMake -cmake-build-debug/ - -# Mongo Explorer plugin: -.idea/**/mongoSettings.xml - -## File-based project format: -*.iws - -## Plugin-specific files: - -# IntelliJ -/out/ - -# mpeltonen/sbt-idea plugin -.idea_modules/ -# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm -# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 - -# User-specific stuff: -*.iml -.idea/** -.idea/**/workspace.xml -.idea/**/tasks.xml -.idea/dictionaries - -# Sensitive or high-churn files: -.idea/**/dataSources/ -.idea/**/dataSources.ids -.idea/**/dataSources.xml -.idea/**/dataSources.local.xml -.idea/**/sqlDataSources.xml -.idea/**/dynamic.xml -.idea/**/uiDesigner.xml - -# Gradle: -.idea/**/gradle.xml -.idea/**/libraries - -# CMake -cmake-build-debug/ - -# Mongo Explorer plugin: -.idea/**/mongoSettings.xml - -## File-based project format: -*.iws - -## Plugin-specific files: - -# IntelliJ -/out/ - -# mpeltonen/sbt-idea plugin -.idea_modules/ - -# JIRA plugin -atlassian-ide-plugin.xml - -# Cursive Clojure plugin -.idea/replstate.xml - -# Crashlytics plugin (for Android Studio and IntelliJ) -com_crashlytics_export_strings.xml -crashlytics.properties -crashlytics-build.properties -fabric.properties# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm - # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 - - # User-specific stuff: - .idea/**/workspace.xml - .idea/**/tasks.xml - .idea/dictionaries - - # Sensitive or high-churn files: - .idea/**/dataSources/ - .idea/**/dataSources.ids - .idea/**/dataSources.xml - .idea/**/dataSources.local.xml - .idea/**/sqlDataSources.xml - .idea/**/dynamic.xml - .idea/**/uiDesigner.xml - - # Gradle: - .idea/**/gradle.xml - .idea/**/libraries - - # CMake - cmake-build-debug/ - - # Mongo Explorer plugin: - .idea/**/mongoSettings.xml - - ## File-based project format: - *.iws - - ## Plugin-specific files: - - # IntelliJ - /out/ - - # mpeltonen/sbt-idea plugin - .idea_modules/ - - # JIRA plugin - atlassian-ide-plugin.xml - - # Cursive Clojure plugin - .idea/replstate.xml - - # Crashlytics plugin (for Android Studio and IntelliJ) - com_crashlytics_export_strings.xml - crashlytics.properties - crashlytics-build.properties - fabric.properties -# JIRA plugin -atlassian-ide-plugin.xml - -# Cursive Clojure plugin -.idea/replstate.xml - -# Crashlytics plugin (for Android Studio and IntelliJ) -com_crashlytics_export_strings.xml -crashlytics.properties -crashlytics-build.properties -fabric.properties - -.project -.classpath -.settings - - -#maven build target -target/ \ No newline at end of file diff --git a/bin/README.md b/bin/README.md deleted file mode 100644 index 8402302c..00000000 --- a/bin/README.md +++ /dev/null @@ -1,90 +0,0 @@ -# ScientificCalculator (maven) ZCW - - -## Description - -In this project your team will build a small app to function as a calculator. This app will be built in Java, and will use the topics and techniques discussed during the week. - -Your team should work on this project in a single repository. Click the `fork` button in the top right corner to create a copy of this repository in one of your github accounts. You can go through the [GitHub forking tutorial](https://help.github.com/articles/fork-a-repo/) if you need additional practice with this. - -Then, each of the other team members should clone from the shared repository. You may want to dived the work into various classes, where each person takes responsibility for a whole class. - -## Requirements - -### Testing - -All features must be tested with Test classes. Tests must include normal behavior, and any possible error situations. Tests must have descriptive names and should be independent of each other (running or not running one test should not influence the behavior of any other test). - -### Documentation - -You must produce UML diagrams for your program. All classes (excluding test classes) must be included in the UML class diagrams. Each class should have a UML box, with variables and a list of methods. - -### Core Features - -All calculators should have the following features: - -- A state, representing the value currently displayed on the calculator (default 0) * -- Get the current number on the display * -- Clear the display * -- Change the number on the display * -- Add, subtract, multiply, and divide the value on the display by a given number -- Calculate the square (x2) and square root (√x) of the number on the display * -- Calculate variable exponentiation (xy) -- Calculate the inverse of the number on the display (1/x) * -- Invert the sign of the number on the display (switch between positive and negative) -- Update the display to `Err` if an error occurs (eg: Division by zero) * -- Errors must be cleared before any other operation can take place * - -Each operation should automatically update the display - - -### Scientific Features - -- Switch display mode (binary, octal, decimal, hexadecimal) - - `switchDisplayMode()` should rotate through the options - - `switchDisplayMode(String mode)` should set the display to the mode given -- Memory - Store up to one numeric value in memory for recall later (default to 0) * - - (`M+` key) Add the currently displayed value to the value in memory (store in memory and update display) * - - (`MC` key) Reset memory * - - (`MRC` key) Recall the current value from memory to the display * -- Trig functions - - Sine - Calculate the sine of the displayed value and display it - - Cosine - Calculate the cosine of the displayed value and display it - - Tangent - Calculate the tangent of the displayed value and display it - - Inverse Sine - - Inverse Cosine - - Inverse Tangent -- Switch trig units mode (Degrees, Radians) - - `switchUnitsMode()` should rotate through the options - - `switchUnitsMode(String mode)` should set the trig units to the type given -- Logarithmic functions - - Log - - 10x (inverse logarithm) - - Ln (natural logarithm) - - ex (inverse natural logarithm) -- Factorial function - - -### Custom Features - -In addition to the Core and Scientific features, you are required to create at least two of your own features for the calculator. They can be any two features that are not already covered and that you can implement as you see fit. These features must be properly tested. - -### Hints - -The following functions should take the displayed value (x) and updated it according to the given formula: (this may not be an exhaustive list) - -- `square()`: x2 -- `squareRoot()`: √x -- `inverse()`: 1/x -- `switchSign()`: -x -- `sine()`: sin(x) -- `cosine()`: cos(x) -- `tangent()`: tan(x) -- `inverseSine()`: sin-1(x) -- `inverseCosine()`: sin-1(x) -- `inverseTangent()`: tan-1(x) -- `factorial()`: x! (x factorial) - -## Submission - -Completed projects should be submitted by submitting a pull request against the [original repository](https://github.com/Zipcoder/ScientificCalculator.Maven). All work should be done in your team's repository. diff --git a/bin/pom.xml b/bin/pom.xml deleted file mode 100644 index e7cb4f6b..00000000 --- a/bin/pom.xml +++ /dev/null @@ -1,12 +0,0 @@ - - - 4.0.0 - - com.zipcodewilmington - scientific_calculator - 1.0-SNAPSHOT - - - \ No newline at end of file diff --git a/bin/src/main/java/com/zipcodewilmington/scientificcalculator/Console.class b/bin/src/main/java/com/zipcodewilmington/scientificcalculator/Console.class deleted file mode 100644 index 7e3de06faa4c5935758ab80bdcd412b83487e2d6..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 2787 zcmcgu&2G~`5S~rbIx(d!DFq4?w&gG3$GLE#2q6kcky1cPsf0K=&L-Vr@0#6pp}YX< z6Cr^FcOD8cj@{N(9o0a@CGmP@zM1)Ec050Sefdk z<|iI{vd2DfKj30uq-bi7Ghuk2d!Flik?$I*nrl*M=`#z;1m=duzAFaJm)&jV83I!e zx!~pzfr)x!lR$Y5AuXs9m_5{erj%3_I7P~@UJJz#Zin?K#t?G!!KI*~l3kw#`T@18 z8(WN;-H_23z=@qXi8JcCLP$fqjP8Rtv}GvQl#3o$7>~b;qCQhhc<7<8WPs=Q5z2bh z@qAZn+UGvgxn4R(ArWKu(qrg24j^>BckY1E70lc@#=eus{tE3$rfHmY;2Lj>8YG8( z3^CO#AQsn&i|Y(2`vdrd2Ofk{nS}4ia6g9vyV?pupL^U;9feF`^@KJm@~}l{Ql5Gv zu0$kytj|TvK1%S}jeL{UcIbMfO)Nnz>WVm>1ULg}S4S3u8cE$@gV@!PeUVk_>}tb; z+?>YfWK{1QiKLKH+~qOB$C~{JR57hlsV7i7uHr7Y0=2fp3AdT1pS~=cEv^YXY5(h} zTqQ6W;wZuCc%|M>19_W?Eoobftpeu=d@e8($JnT&TEZq^4Wsr&1r`bT|1&HCGXrLl zEL$Rskh%H}harIOVOEE5O{ za5lc%ofI><;yJ}k1Q^(jVof%-SwX<=$VhqYIj#T%rqj)4_|deWfxArN%7Qz17g9bf zxQBwISI}miYmH072JR;smI2ouT(SVB@M&`RYYmsK2|NSX-=OlQW`BX{A9yOkY^n(; zox|tpL<`P94NrS84`=aBjBp!qOZcB4EaV{ALxfmo56+LUyO3iyb6{76i*PC9?=a%a vId-*--4(c+Ye#eJ7BhA$a4pyFdXC-Ffn5o1VwNSi1y0Nel%bCN+=YqX7ZOpa diff --git a/bin/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.class b/bin/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.class deleted file mode 100644 index 0be7cc8149ed08a0b7c46735817fcba1a7445092..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1960 zcmeHI&u`N(6n<{Ir3rKZXPn z-1(yr&vC^D?69ev5QjKU^1aWWzr5%7?fa)M0B{d(MbIGd)Eb@@OsedV9kGn-BKsgq zuE*l7%nPZePOHpvDU_2F$vMk;l{2SJwueCv%QBaoIjL0yEdnbwNzT+Xdoel`+!0v5 zFO_r;2sC^B0|Kobq>dma&^^XJHb$E^Tp-P@m&ypM^CK~)sHK#+7LwD_=us{TyG4_D zI1_X%c+L!hO2*%?si2#PPXADzo~C@0j4Lv6z@uudy){1Nxda!HGRO0 z^w0B`EjN>vz_Y>MIvlpK@O>>2*yz1JOZ(-c^ EAL@61(EtDd diff --git a/bin/src/test/java/com/zipcodewilmington/scientific_calculator/TestMainApplication.class b/bin/src/test/java/com/zipcodewilmington/scientific_calculator/TestMainApplication.class deleted file mode 100644 index e4d78d248170ce0df8df79b91c3c3fa6ce60392b..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 763 zcmb_aO>Yx15FMwXNtQs`KqZc*r}VOWp%;pfs)|ZT2|YBe)Dv>NPBO)7uk6`^_+v;^ zf;&H|Vw@l~61^aC8Gk*$d47Js?!Ny3fLE~BftG+t47wl_skl}*s?feFzGAI?X+EQ^ z&@ITjB5E{_aE|(;R#rDwkq^a`Z%q`sXbad`F6P~Na(k42WlO|F+VU$!Z$y^g}P-Yo) z;YlAJ!BYWyqw&45=M?xKkCO3&F*AH#a;>+jy-?rr3#qVe>U5+#3 Date: Fri, 7 Feb 2020 19:21:49 -0500 Subject: [PATCH 03/22] added classes --- .../Calculator/Calculator.java | 6 ++++++ .../scientificcalculator/Utilities/Util.java | 6 ++++++ .../Calculator/TestCalculator.java | 14 ++++++++++++++ .../Utilities/TestUtilities.java | 14 ++++++++++++++ 4 files changed, 40 insertions(+) create mode 100644 src/main/java/com/zipcodewilmington/scientificcalculator/Calculator/Calculator.java create mode 100644 src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/Util.java create mode 100644 src/test/java/com/zipcodewilmington/scientific_calculator/Calculator/TestCalculator.java create mode 100644 src/test/java/com/zipcodewilmington/scientific_calculator/Utilities/TestUtilities.java diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Calculator/Calculator.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Calculator/Calculator.java new file mode 100644 index 00000000..2ac55459 --- /dev/null +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Calculator/Calculator.java @@ -0,0 +1,6 @@ +package com.zipcodewilmington.scientificcalculator.Calculator; + +public class Calculator +{ + +} diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/Util.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/Util.java new file mode 100644 index 00000000..7a4b47e7 --- /dev/null +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/Util.java @@ -0,0 +1,6 @@ +package com.zipcodewilmington.scientificcalculator.Utilities; + +public class Util +{ + +} diff --git a/src/test/java/com/zipcodewilmington/scientific_calculator/Calculator/TestCalculator.java b/src/test/java/com/zipcodewilmington/scientific_calculator/Calculator/TestCalculator.java new file mode 100644 index 00000000..f00b82b1 --- /dev/null +++ b/src/test/java/com/zipcodewilmington/scientific_calculator/Calculator/TestCalculator.java @@ -0,0 +1,14 @@ +package com.zipcodewilmington.scientific_calculator.Calculator; + +import org.junit.*; + + +public class TestCalculator +{ + @Test + public void templateTestClass() + { + + } + +} diff --git a/src/test/java/com/zipcodewilmington/scientific_calculator/Utilities/TestUtilities.java b/src/test/java/com/zipcodewilmington/scientific_calculator/Utilities/TestUtilities.java new file mode 100644 index 00000000..fd60015d --- /dev/null +++ b/src/test/java/com/zipcodewilmington/scientific_calculator/Utilities/TestUtilities.java @@ -0,0 +1,14 @@ +package com.zipcodewilmington.scientific_calculator.Utilities; + +import org.junit.*; + + +public class TestUtilities +{ + @Test + public void templateTestClass() + { + + } + +} From 35bef57b10c9438870f70363dfbf7b169378fd88 Mon Sep 17 00:00:00 2001 From: Adam Bennett Date: Fri, 7 Feb 2020 20:24:24 -0500 Subject: [PATCH 04/22] made some calculator --- .../Calculator/Calculator.java | 276 ++++++++++++++++++ .../scientificcalculator/Utilities/Util.java | 91 ++++++ 2 files changed, 367 insertions(+) diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Calculator/Calculator.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Calculator/Calculator.java index 2ac55459..436942db 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Calculator/Calculator.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Calculator/Calculator.java @@ -1,6 +1,282 @@ package com.zipcodewilmington.scientificcalculator.Calculator; +import com.zipcodewilmington.scientificcalculator.Utilities.Util; + public class Calculator { + private float trueVal; + private float storedVal; + private boolean allowNegative; + private boolean isError; + private boolean displayingStoredVal; + private DisplayMode numMode; + private TrigDisplayMode trigMode; + private String display; + + public enum DisplayMode { + BINARY, + OCTAL, + DECIMAL, + HEXADECIMAL + } + + public enum TrigDisplayMode { + DEGREES, + RADIANS + } + + public Calculator() { + this(0, 0, true, DisplayMode.DECIMAL, TrigDisplayMode.DEGREES); + } + + public Calculator(int displayVal, int storedVal, boolean allowNegatives, DisplayMode numMode, TrigDisplayMode trigMode) { + this.trueVal = displayVal; + this.storedVal = storedVal; + this.allowNegative = allowNegatives; + this.isError = false; + this.displayingStoredVal = false; + this.display = ""; + this.trigMode = trigMode; + this.numMode = numMode; + updateDisplay(); + } + + @SuppressWarnings("incomplete-switch") + public void updateDisplay() { + if (this.isError) { + this.display = "Err"; + return; + } + + float numToDisplay; + if (this.displayingStoredVal) { + numToDisplay = this.storedVal; + } + else { + numToDisplay = this.trueVal; + } + + switch (this.numMode) { + case BINARY: + this.display = "" + Integer.toBinaryString((int)numToDisplay); + return; + case HEXADECIMAL: + this.display = "" + Float.toHexString(numToDisplay); + return; + case OCTAL: + this.display = "" + Integer.toOctalString((int)numToDisplay); + return; + } + + if (!this.allowNegative && numToDisplay < 0) { + numToDisplay = 0; + } + + this.display = "" + numToDisplay; + } + + + public void clearDisplay() { + this.trueVal = 0; + this.isError = false; + this.allowNegative = true; + updateDisplay(); + } + + public void clearMemory() { + this.storedVal = 0; + } + + + // MATH //////////////////////////////////////////////////////////////////////////////////////////////// + public void add(float val) { + this.trueVal += val; + updateDisplay(); + } + + public void subtract(float val) { + this.trueVal -= val; + updateDisplay(); + } + + public void mult(float val) { + this.trueVal *= val; + updateDisplay(); + } + + public void div(float num) { + this.trueVal /= num; + updateDisplay(); + } + + public void sqRt() { + this.trueVal = Util.squareRoot(this.trueVal); + updateDisplay(); + } + + public void square() { + this.trueVal = Util.square(this.trueVal); + updateDisplay(); + } + public void pow(float exponent) { + this.trueVal = (float) Math.pow(this.trueVal, exponent); + updateDisplay(); + } + + public void inverse() { + this.trueVal = 1.0f / this.trueVal; + updateDisplay(); + } + + public void flipSign() { + this.trueVal = -this.trueVal; + updateDisplay(); + } + + public void sine() { + this.trueVal = Util.sine(this.trueVal); + if (this.trigMode == TrigDisplayMode.RADIANS) { + this.trueVal = Util.toRadians(this.trueVal); + } + } + + public void cosine() { + this.trueVal = Util.cosine(this.trueVal); + if (this.trigMode == TrigDisplayMode.RADIANS) { + this.trueVal = Util.toRadians(this.trueVal); + } + } + + public void tangent() { + this.trueVal = Util.tangent(this.trueVal); + if (this.trigMode == TrigDisplayMode.RADIANS) { + this.trueVal = Util.toRadians(this.trueVal); + } + } + + public void invSine() { + this.trueVal = Util.invSine(this.trueVal); + if (this.trigMode == TrigDisplayMode.RADIANS) { + this.trueVal = Util.toRadians(this.trueVal); + } + } + + public void invCosine() { + this.trueVal = Util.invCosine(this.trueVal); + if (this.trigMode == TrigDisplayMode.RADIANS) { + this.trueVal = Util.toRadians(this.trueVal); + } + } + + public void invTangent() { + this.trueVal = Util.invTangent(this.trueVal); + if (this.trigMode == TrigDisplayMode.RADIANS) { + this.trueVal = Util.toRadians(this.trueVal); + } + } + // END MATH /////////////////////////////////////////////////////////////////////////////////////////////// + + + // GETTERS //////////////////////////////////////////////////////////////////////////////////////////////// + public String getDisplay() { + return display; + } + + public float getDisplayVal() { + if (this.trueVal > 0.0f || this.allowNegative) { + return this.trueVal; + } + else { + return 0.0f; + } + } + + public float getStoredVal() { + return this.storedVal; + } + + public boolean isInErrorMode() { + return this.isError; + } + + public boolean allowingNegative() { + return this.allowNegative; + } + + public DisplayMode getNumMode() { + return this.numMode; + } + + public String getNumModeStr() { + switch (this.numMode) { + case BINARY: + return "Binary"; + case DECIMAL: + return "Decimal"; + case HEXADECIMAL: + return "Hexadecimal"; + case OCTAL: + return "Octal"; + default: + return "Uh.OH."; + } + } + + public TrigDisplayMode getTrigMode() { + return this.trigMode; + } + + public String getTrigModeStr() { + switch (this.trigMode) { + case DEGREES: + return "Degrees"; + case RADIANS: + return "Radians"; + default: + return "Uh.OH."; + } + } + // END GETTERS /////////////////////////////////////////////////////////////////////////////////////////// + + + // SETTERS /////////////////////////////////////////////////////////////////////////////////////////////// + public void setDisplayVal(int val) { + this.trueVal = val; + updateDisplay(); + } + + public void setAllowNegative(boolean allow) { + this.allowNegative = allow; + updateDisplay(); + } + + public void setDisplay(String newDisp) { + this.display = newDisp; + updateDisplay(); + } + + public void setError(boolean isError) { + this.isError = isError; + updateDisplay(); + } + + public void setStoredVal(int newStoredVal) { + this.storedVal = newStoredVal; + } + + public void incStoredVal(int incrementAmt) { + this.storedVal += incrementAmt; + } + + public void setDisplayMode(DisplayMode newMode) { + this.numMode = newMode; + } + + public void setTrigMode(TrigDisplayMode newMode) { + this.trigMode = newMode; + } + // END SETTERS ///////////////////////////////////////////////////////////////////////////////////////// + + } diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/Util.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/Util.java index 7a4b47e7..744aa569 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/Util.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/Util.java @@ -2,5 +2,96 @@ public class Util { + public static int add(int num1, int num2) { + return num1 + num2; + } + public static int sub(int left, int right) { + return left - right; + } + + public static int mult(int num1, int num2) { + return num1 * num2; + } + + public static float div(float numerator, float denominator) { + return numerator / denominator; + } + + public static int div(int numerator, int denominator) { + return (int)div((float)numerator, (float)denominator); + } + + public static float square(float num) { + return num * num; + } + + public static float squareRoot(float num) { + return (float) Math.sqrt(num); + } + + public static int squareRoot(int num) { + return (int)squareRoot((float)num); + } + + public static double varExp(double base, double exp) { + return Math.pow(base, exp); + } + + public static int varExp(int base, int exp) { + return (int)Math.pow((double)base, (double)exp); + } + + public static int inverse(int toInverse) { + return div(1, toInverse); + } + + public static int flipSign(int num) { + return -num; + } + + public static String returnErr() { + return "Err"; + } + + public static float sine(float num) { + return (float) Math.sin(num); + } + + public static float cosine(float num) { + return (float) Math.cos(num); + } + + public static float tangent(float num) { + return (float) Math.tan(num); + } + + public static float invSine(float num) { + return (float) Math.asin(num); + } + + public static float invCosine(float num) { + return (float) Math.acos(num); + } + + public static float invTangent(float num) { + return (float) Math.atan(num); + } + + public static float toRadians(float num) { + return (float) Math.toRadians(num); + } + + public static double log(double num) { + return Math.log(num); + } + + public static double inverseNaturalLog(double num) { + return Math.expm1(num) + 1; + } + + public static int factorial(int n) + { + return (n == 1 || n == 0) ? 1 : n * factorial(n - 1); + } } From 97669819dda2c06fe389566f42e9aa56f1015d69 Mon Sep 17 00:00:00 2001 From: Adam Bennett Date: Fri, 7 Feb 2020 21:35:25 -0500 Subject: [PATCH 05/22] Added ConsoleCommands functionality and control flow logic --- .../Application/Console.java | 20 ++- .../Application/MainApplication.java | 22 +-- .../Calculator/Calculator.java | 70 +++++----- .../Utilities/ConsoleCommands.java | 130 ++++++++++++++++++ 4 files changed, 196 insertions(+), 46 deletions(-) create mode 100644 src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/ConsoleCommands.java diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Application/Console.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Application/Console.java index 4495b24a..41fe21e0 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Application/Console.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Application/Console.java @@ -15,6 +15,10 @@ public static void print(String output, Object... args) { public static void println(String output, Object... args) { print(output + "\n", args); } + + public static void prln(String print) { + System.out.println(print); + } public static String getStringInput(String prompt) { Scanner scanner = new Scanner(System.in); @@ -24,10 +28,22 @@ public static String getStringInput(String prompt) { } public static Integer getIntegerInput(String prompt) { - return null; + Scanner scanner = new Scanner(System.in); + println(prompt); + String userInput = scanner.nextLine(); + try { + return Integer.parseInt(userInput); + } catch (NumberFormatException e) { e.printStackTrace(); } + return 0; } public static Double getDoubleInput(String prompt) { - return null; + Scanner scanner = new Scanner(System.in); + println(prompt); + String userInput = scanner.nextLine(); + try { + return Double.parseDouble(userInput); + } catch (NumberFormatException e) { e.printStackTrace(); } + return 0.0; } } diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Application/MainApplication.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Application/MainApplication.java index 1e7bb0ee..52e6400e 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Application/MainApplication.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Application/MainApplication.java @@ -1,19 +1,23 @@ package com.zipcodewilmington.scientificcalculator.Application; +import com.zipcodewilmington.scientificcalculator.Calculator.Calculator; +import com.zipcodewilmington.scientificcalculator.Utilities.ConsoleCommands; + /** * Created by leon on 2/9/18. */ public class MainApplication { + + public final static Calculator calc; + public static void main(String[] args) { - Console.println("Welcome to my calculator!"); - String s = Console.getStringInput("Enter a string"); - Integer i = Console.getIntegerInput("Enter an integer"); - Double d = Console.getDoubleInput("Enter a double."); - - - Console.println("The user input %s as a string", s); - Console.println("The user input %s as a integer", i); - Console.println("The user input %s as a d", d); + Console.println("Welcome to my calculator!"); + String input = Console.getStringInput("Enter a command to continue: "); + ConsoleCommands.runCommand(input); + } + + static { + calc = new Calculator(); } } diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Calculator/Calculator.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Calculator/Calculator.java index 436942db..3928fc80 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Calculator/Calculator.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Calculator/Calculator.java @@ -4,8 +4,8 @@ public class Calculator { - private float trueVal; - private float storedVal; + private float displayValue; + private float storedValue; private boolean allowNegative; private boolean isError; private boolean displayingStoredVal; @@ -30,8 +30,8 @@ public Calculator() { } public Calculator(int displayVal, int storedVal, boolean allowNegatives, DisplayMode numMode, TrigDisplayMode trigMode) { - this.trueVal = displayVal; - this.storedVal = storedVal; + this.displayValue = displayVal; + this.storedValue = storedVal; this.allowNegative = allowNegatives; this.isError = false; this.displayingStoredVal = false; @@ -50,10 +50,10 @@ public void updateDisplay() { float numToDisplay; if (this.displayingStoredVal) { - numToDisplay = this.storedVal; + numToDisplay = this.storedValue; } else { - numToDisplay = this.trueVal; + numToDisplay = this.displayValue; } switch (this.numMode) { @@ -77,102 +77,102 @@ public void updateDisplay() { public void clearDisplay() { - this.trueVal = 0; + this.displayValue = 0; this.isError = false; this.allowNegative = true; updateDisplay(); } public void clearMemory() { - this.storedVal = 0; + this.storedValue = 0; } // MATH //////////////////////////////////////////////////////////////////////////////////////////////// public void add(float val) { - this.trueVal += val; + this.displayValue += val; updateDisplay(); } public void subtract(float val) { - this.trueVal -= val; + this.displayValue -= val; updateDisplay(); } public void mult(float val) { - this.trueVal *= val; + this.displayValue *= val; updateDisplay(); } public void div(float num) { - this.trueVal /= num; + this.displayValue /= num; updateDisplay(); } public void sqRt() { - this.trueVal = Util.squareRoot(this.trueVal); + this.displayValue = Util.squareRoot(this.displayValue); updateDisplay(); } public void square() { - this.trueVal = Util.square(this.trueVal); + this.displayValue = Util.square(this.displayValue); updateDisplay(); } public void pow(float exponent) { - this.trueVal = (float) Math.pow(this.trueVal, exponent); + this.displayValue = (float) Math.pow(this.displayValue, exponent); updateDisplay(); } public void inverse() { - this.trueVal = 1.0f / this.trueVal; + this.displayValue = 1.0f / this.displayValue; updateDisplay(); } public void flipSign() { - this.trueVal = -this.trueVal; + this.displayValue = -this.displayValue; updateDisplay(); } public void sine() { - this.trueVal = Util.sine(this.trueVal); + this.displayValue = Util.sine(this.displayValue); if (this.trigMode == TrigDisplayMode.RADIANS) { - this.trueVal = Util.toRadians(this.trueVal); + this.displayValue = Util.toRadians(this.displayValue); } } public void cosine() { - this.trueVal = Util.cosine(this.trueVal); + this.displayValue = Util.cosine(this.displayValue); if (this.trigMode == TrigDisplayMode.RADIANS) { - this.trueVal = Util.toRadians(this.trueVal); + this.displayValue = Util.toRadians(this.displayValue); } } public void tangent() { - this.trueVal = Util.tangent(this.trueVal); + this.displayValue = Util.tangent(this.displayValue); if (this.trigMode == TrigDisplayMode.RADIANS) { - this.trueVal = Util.toRadians(this.trueVal); + this.displayValue = Util.toRadians(this.displayValue); } } public void invSine() { - this.trueVal = Util.invSine(this.trueVal); + this.displayValue = Util.invSine(this.displayValue); if (this.trigMode == TrigDisplayMode.RADIANS) { - this.trueVal = Util.toRadians(this.trueVal); + this.displayValue = Util.toRadians(this.displayValue); } } public void invCosine() { - this.trueVal = Util.invCosine(this.trueVal); + this.displayValue = Util.invCosine(this.displayValue); if (this.trigMode == TrigDisplayMode.RADIANS) { - this.trueVal = Util.toRadians(this.trueVal); + this.displayValue = Util.toRadians(this.displayValue); } } public void invTangent() { - this.trueVal = Util.invTangent(this.trueVal); + this.displayValue = Util.invTangent(this.displayValue); if (this.trigMode == TrigDisplayMode.RADIANS) { - this.trueVal = Util.toRadians(this.trueVal); + this.displayValue = Util.toRadians(this.displayValue); } } // END MATH /////////////////////////////////////////////////////////////////////////////////////////////// @@ -184,8 +184,8 @@ public String getDisplay() { } public float getDisplayVal() { - if (this.trueVal > 0.0f || this.allowNegative) { - return this.trueVal; + if (this.displayValue > 0.0f || this.allowNegative) { + return this.displayValue; } else { return 0.0f; @@ -193,7 +193,7 @@ public float getDisplayVal() { } public float getStoredVal() { - return this.storedVal; + return this.storedValue; } public boolean isInErrorMode() { @@ -242,7 +242,7 @@ public String getTrigModeStr() { // SETTERS /////////////////////////////////////////////////////////////////////////////////////////////// public void setDisplayVal(int val) { - this.trueVal = val; + this.displayValue = val; updateDisplay(); } @@ -262,11 +262,11 @@ public void setError(boolean isError) { } public void setStoredVal(int newStoredVal) { - this.storedVal = newStoredVal; + this.storedValue = newStoredVal; } public void incStoredVal(int incrementAmt) { - this.storedVal += incrementAmt; + this.storedValue += incrementAmt; } public void setDisplayMode(DisplayMode newMode) { diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/ConsoleCommands.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/ConsoleCommands.java new file mode 100644 index 00000000..ed0a352e --- /dev/null +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/ConsoleCommands.java @@ -0,0 +1,130 @@ +package com.zipcodewilmington.scientificcalculator.Utilities; + +import java.util.*; +import java.util.Map.Entry; + +import com.zipcodewilmington.scientificcalculator.Application.*; + +public class ConsoleCommands +{ + private static Map commandMap = new HashMap<>(); + + public enum Command { + CLEAR, + HELP, + INFO, + LOG, + MATH, + MODIFY, + RECALL, + RESET, + STORE, + SWITCH_DISP, + SWITCH_DISP_TRIG, + TOGGLE_NEGATIVE, + TRIG, + BAD_COMMAND + } + + static { + commandMap = new HashMap<>(); + Map tempMap = new HashMap<>(); + commandMap.put("Clear", Command.CLEAR); + commandMap.put("Help", Command.HELP); + commandMap.put("Info", Command.INFO); + commandMap.put("Log", Command.LOG); + commandMap.put("Math", Command.MATH); + commandMap.put("Modify", Command.MODIFY); + commandMap.put("Recall", Command.RECALL); + commandMap.put("Reset", Command.RESET); + commandMap.put("Store", Command.STORE); + commandMap.put("Switch mode", Command.SWITCH_DISP); + commandMap.put("Switch trig mode", Command.SWITCH_DISP_TRIG); + commandMap.put("Toggle allow negative", Command.TOGGLE_NEGATIVE); + commandMap.put("Trig", Command.TRIG); + + // Fill map with all the above commands, but in lower case and upper case (ie CLEAR/Clear/clear all will work) + for (Entry i : commandMap.entrySet()) { + tempMap.put(i.getKey().toLowerCase(), i.getValue()); + tempMap.put(i.getKey().toUpperCase(), i.getValue()); + } + for (Entry i : tempMap.entrySet()) { + commandMap.put(i.getKey(), i.getValue()); + } + } + + public static void runCommand(String input) { + if (commandMap != null && commandMap.containsKey(input)) { + run(commandMap.get(input)); + } + else { + run(Command.BAD_COMMAND); + } + } + + private static void rePrompt() { + String input = Console.getStringInput("Enter a command to continue: "); + runCommand(input); + } + + public static void run(Command cmd) { + switch (cmd) { + case BAD_COMMAND: + Console.prln("Bad command! Please enter a valid command, or enter 'Help' to view a list of all commands."); + rePrompt(); + return; + case CLEAR: + MainApplication.calc.clearDisplay(); + rePrompt(); + return; + case HELP: + Console.prln("Printing a list of all available commands: "); + Set uniques = new HashSet<>(); + for (Entry i : commandMap.entrySet()) { + uniques.add(i.getKey().toUpperCase()); + } + for (String s : uniques) { + Console.prln(s); + } + return; + case INFO: + Console.prln("This calculator was made by Adam, Matt and Aarti! We hope you find it useful."); + rePrompt(); + return; + case LOG: + rePrompt(); + return; + case MATH: + rePrompt(); + return; + case MODIFY: + rePrompt(); + return; + case RECALL: + rePrompt(); + return; + case RESET: + rePrompt(); + return; + case STORE: + rePrompt(); + return; + case SWITCH_DISP: + rePrompt(); + return; + case SWITCH_DISP_TRIG: + rePrompt(); + return; + case TOGGLE_NEGATIVE: + rePrompt(); + return; + case TRIG: + rePrompt(); + return; + default: + run(Command.BAD_COMMAND); + return; + } + } + +} From d228d9e158ee3ea4260f461231ef16b5d392e8e2 Mon Sep 17 00:00:00 2001 From: Adam Bennett Date: Fri, 7 Feb 2020 22:57:47 -0500 Subject: [PATCH 06/22] most commands working properly --- .../Application/MainApplication.java | 3 +- .../Calculator/Calculator.java | 104 +++++----- .../Utilities/ConsoleCommands.java | 185 +++++++++++++----- .../Utilities/DisplayModeCommands.java | 123 ++++++++++++ .../Utilities/MathCommands.java | 132 +++++++++++++ 5 files changed, 452 insertions(+), 95 deletions(-) create mode 100644 src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/DisplayModeCommands.java create mode 100644 src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/MathCommands.java diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Application/MainApplication.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Application/MainApplication.java index 52e6400e..43042f7b 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Application/MainApplication.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Application/MainApplication.java @@ -13,8 +13,7 @@ public class MainApplication { public static void main(String[] args) { Console.println("Welcome to my calculator!"); - String input = Console.getStringInput("Enter a command to continue: "); - ConsoleCommands.runCommand(input); + ConsoleCommands.fullPrompt(); } static { diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Calculator/Calculator.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Calculator/Calculator.java index 3928fc80..53989748 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Calculator/Calculator.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Calculator/Calculator.java @@ -37,8 +37,7 @@ public Calculator(int displayVal, int storedVal, boolean allowNegatives, Display this.displayingStoredVal = false; this.display = ""; this.trigMode = trigMode; - this.numMode = numMode; - updateDisplay(); + this.numMode = numMode; } @SuppressWarnings("incomplete-switch") @@ -79,111 +78,111 @@ public void updateDisplay() { public void clearDisplay() { this.displayValue = 0; this.isError = false; - this.allowNegative = true; - updateDisplay(); + this.allowNegative = true; } public void clearMemory() { + this.storedValue = 0; + } + + public void totalReset() { + this.displayValue = 0; this.storedValue = 0; + this.allowNegative = true; + this.isError = false; + this.numMode = DisplayMode.DECIMAL; + this.trigMode = TrigDisplayMode.DEGREES; } // MATH //////////////////////////////////////////////////////////////////////////////////////////////// public void add(float val) { - this.displayValue += val; - updateDisplay(); + this.displayValue += val; } public void subtract(float val) { - this.displayValue -= val; - updateDisplay(); + this.displayValue -= val; } public void mult(float val) { - this.displayValue *= val; - updateDisplay(); + this.displayValue *= val; } public void div(float num) { - this.displayValue /= num; - updateDisplay(); + this.displayValue /= num; } public void sqRt() { - this.displayValue = Util.squareRoot(this.displayValue); - updateDisplay(); + this.displayValue = Util.squareRoot(this.displayValue); } public void square() { - this.displayValue = Util.square(this.displayValue); - updateDisplay(); + this.displayValue = Util.square(this.displayValue); } public void pow(float exponent) { - this.displayValue = (float) Math.pow(this.displayValue, exponent); - updateDisplay(); + this.displayValue = (float) Math.pow(this.displayValue, exponent); } public void inverse() { - this.displayValue = 1.0f / this.displayValue; - updateDisplay(); + this.displayValue = 1.0f / this.displayValue; } public void flipSign() { - this.displayValue = -this.displayValue; - updateDisplay(); + this.displayValue = -this.displayValue; } public void sine() { this.displayValue = Util.sine(this.displayValue); if (this.trigMode == TrigDisplayMode.RADIANS) { this.displayValue = Util.toRadians(this.displayValue); - } + } } public void cosine() { this.displayValue = Util.cosine(this.displayValue); if (this.trigMode == TrigDisplayMode.RADIANS) { this.displayValue = Util.toRadians(this.displayValue); - } + } } public void tangent() { this.displayValue = Util.tangent(this.displayValue); if (this.trigMode == TrigDisplayMode.RADIANS) { this.displayValue = Util.toRadians(this.displayValue); - } + } } public void invSine() { this.displayValue = Util.invSine(this.displayValue); if (this.trigMode == TrigDisplayMode.RADIANS) { this.displayValue = Util.toRadians(this.displayValue); - } + } } public void invCosine() { this.displayValue = Util.invCosine(this.displayValue); if (this.trigMode == TrigDisplayMode.RADIANS) { this.displayValue = Util.toRadians(this.displayValue); - } + } } public void invTangent() { this.displayValue = Util.invTangent(this.displayValue); if (this.trigMode == TrigDisplayMode.RADIANS) { this.displayValue = Util.toRadians(this.displayValue); - } + } } // END MATH /////////////////////////////////////////////////////////////////////////////////////////////// // GETTERS //////////////////////////////////////////////////////////////////////////////////////////////// public String getDisplay() { + updateDisplay(); return display; } - public float getDisplayVal() { + public float getDisplayVal() { if (this.displayValue > 0.0f || this.allowNegative) { return this.displayValue; } @@ -192,23 +191,23 @@ public float getDisplayVal() { } } - public float getStoredVal() { + public float getStoredVal() { return this.storedValue; } - public boolean isInErrorMode() { + public boolean isInErrorMode() { return this.isError; } - public boolean allowingNegative() { + public boolean allowingNegative() { return this.allowNegative; } - public DisplayMode getNumMode() { + public DisplayMode getNumMode() { return this.numMode; } - public String getNumModeStr() { + public String getNumModeStr() { switch (this.numMode) { case BINARY: return "Binary"; @@ -223,11 +222,11 @@ public String getNumModeStr() { } } - public TrigDisplayMode getTrigMode() { + public TrigDisplayMode getTrigMode() { return this.trigMode; } - public String getTrigModeStr() { + public String getTrigModeStr() { switch (this.trigMode) { case DEGREES: return "Degrees"; @@ -242,39 +241,48 @@ public String getTrigModeStr() { // SETTERS /////////////////////////////////////////////////////////////////////////////////////////////// public void setDisplayVal(int val) { - this.displayValue = val; - updateDisplay(); + this.displayValue = val; } public void setAllowNegative(boolean allow) { - this.allowNegative = allow; - updateDisplay(); + this.allowNegative = allow; } public void setDisplay(String newDisp) { - this.display = newDisp; - updateDisplay(); + this.display = newDisp; } public void setError(boolean isError) { - this.isError = isError; - updateDisplay(); + this.isError = isError; } public void setStoredVal(int newStoredVal) { - this.storedValue = newStoredVal; + this.storedValue = newStoredVal; } public void incStoredVal(int incrementAmt) { - this.storedValue += incrementAmt; + this.storedValue += incrementAmt; } public void setDisplayMode(DisplayMode newMode) { - this.numMode = newMode; + this.numMode = newMode; } public void setTrigMode(TrigDisplayMode newMode) { - this.trigMode = newMode; + this.trigMode = newMode; + } + + public void toggleAllowNegatives() { + this.allowNegative = !this.allowNegative; + } + + public void toggleTrigMode() { + if (this.trigMode.equals(TrigDisplayMode.DEGREES)) { + this.trigMode = TrigDisplayMode.RADIANS; + } + else { + this.trigMode = TrigDisplayMode.DEGREES; + } } // END SETTERS ///////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/ConsoleCommands.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/ConsoleCommands.java index ed0a352e..1f3b101f 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/ConsoleCommands.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/ConsoleCommands.java @@ -13,17 +13,22 @@ public enum Command { CLEAR, HELP, INFO, - LOG, MATH, - MODIFY, RECALL, RESET, STORE, SWITCH_DISP, SWITCH_DISP_TRIG, TOGGLE_NEGATIVE, - TRIG, - BAD_COMMAND + BAD_COMMAND, + EXIT, + DISPLAY + } + + public enum MenuType { + CONSOLE, + MATH, + DISPLAY } static { @@ -32,16 +37,15 @@ public enum Command { commandMap.put("Clear", Command.CLEAR); commandMap.put("Help", Command.HELP); commandMap.put("Info", Command.INFO); - commandMap.put("Log", Command.LOG); commandMap.put("Math", Command.MATH); - commandMap.put("Modify", Command.MODIFY); commandMap.put("Recall", Command.RECALL); commandMap.put("Reset", Command.RESET); commandMap.put("Store", Command.STORE); - commandMap.put("Switch mode", Command.SWITCH_DISP); - commandMap.put("Switch trig mode", Command.SWITCH_DISP_TRIG); - commandMap.put("Toggle allow negative", Command.TOGGLE_NEGATIVE); - commandMap.put("Trig", Command.TRIG); + commandMap.put("Switchmode", Command.SWITCH_DISP); + commandMap.put("Switchtrig", Command.SWITCH_DISP_TRIG); + commandMap.put("Togglenegative", Command.TOGGLE_NEGATIVE); + commandMap.put("Display", Command.DISPLAY); + commandMap.put("Exit", Command.EXIT); // Fill map with all the above commands, but in lower case and upper case (ie CLEAR/Clear/clear all will work) for (Entry i : commandMap.entrySet()) { @@ -53,76 +57,167 @@ public enum Command { } } - public static void runCommand(String input) { - if (commandMap != null && commandMap.containsKey(input)) { - run(commandMap.get(input)); + public static void runCommand(ArrayList args) { + if (commandMap != null && commandMap.containsKey(args.get(0))) { + run(commandMap.get(args.get(0)), args); + } + else if (args.size() > 1 && args.get(0).equals("best") && args.get(1).equals("programmer")) { + Console.prln("Nobles"); + fullPrompt(); } else { - run(Command.BAD_COMMAND); + run(Command.BAD_COMMAND, args); } } - private static void rePrompt() { - String input = Console.getStringInput("Enter a command to continue: "); - runCommand(input); + public static ArrayList prompt(MenuType type) { + String inptCmd; + switch (type) { + case CONSOLE: + inptCmd = "Enter a CONSOLE COMMAND to continue: "; + break; + case DISPLAY: + inptCmd = "Enter a DISPLAY MODE to continue: "; + break; + case MATH: + inptCmd = "Enter a MATH COMMAND to continue: "; + break; + default: + inptCmd = "Enter a CONSOLE COMMAND to continue: "; + break; + } + String input = Console.getStringInput(inptCmd); + String[] splited = input.split("\\s+"); + ArrayList argus = new ArrayList<>(); + for (String s : splited) { + argus.add(s); + } + return argus; + } + + public static void fullPrompt() { + String input = Console.getStringInput("Enter a CONSOLE COMMAND to continue: "); + String[] splited = input.split("\\s+"); + ArrayList argus = new ArrayList<>(); + for (String s : splited) { + argus.add(s); + } + while (argus.size() < 1) { + argus = ConsoleCommands.prompt(MenuType.CONSOLE); + } + ConsoleCommands.runCommand(argus); + } + + public static void promptMath() { + String input = Console.getStringInput("Enter a MATH COMMAND to continue: "); + String[] splited = input.split("\\s+"); + ArrayList argus = new ArrayList<>(); + for (String s : splited) { + argus.add(s); + } + while (argus.size() < 1) { + argus = ConsoleCommands.prompt(MenuType.MATH); + } + ConsoleCommands.runCommand(argus); + } + + public static void promptDispSwitch() { + String input = Console.getStringInput("Enter a DISPLAY MODE to continue: "); + String[] splited = input.split("\\s+"); + ArrayList argus = new ArrayList<>(); + for (String s : splited) { + argus.add(s); + } + while (argus.size() < 1) { + argus = ConsoleCommands.prompt(MenuType.DISPLAY); + } + ConsoleCommands.runCommand(argus); } - public static void run(Command cmd) { - switch (cmd) { + public static void run(Command cmd, ArrayList args) { + switch (cmd) + { case BAD_COMMAND: Console.prln("Bad command! Please enter a valid command, or enter 'Help' to view a list of all commands."); - rePrompt(); + fullPrompt(); return; case CLEAR: MainApplication.calc.clearDisplay(); - rePrompt(); + run(Command.DISPLAY, null); return; case HELP: Console.prln("Printing a list of all available commands: "); - Set uniques = new HashSet<>(); + ArrayList uniques = new ArrayList<>(); for (Entry i : commandMap.entrySet()) { - uniques.add(i.getKey().toUpperCase()); + if (!uniques.contains(i.getKey().toUpperCase())) { + uniques.add(i.getKey().toUpperCase()); + } } + Collections.sort(uniques); for (String s : uniques) { Console.prln(s); } + fullPrompt(); return; case INFO: - Console.prln("This calculator was made by Adam, Matt and Aarti! We hope you find it useful."); - rePrompt(); - return; - case LOG: - rePrompt(); - return; - case MATH: - rePrompt(); - return; - case MODIFY: - rePrompt(); + Console.prln("This calculator was made by Aarti, Adam, and Matt! We hope you find it useful."); + fullPrompt(); return; case RECALL: - rePrompt(); + Console.prln("Stored Memory Value: " + MainApplication.calc.getStoredVal()); + fullPrompt(); return; case RESET: - rePrompt(); + MainApplication.calc.totalReset(); + Console.prln("Reset all calculator values! Display mode is now DECIMAL/DEGREES."); + run(Command.DISPLAY, null); return; case STORE: - rePrompt(); - return; - case SWITCH_DISP: - rePrompt(); + if (args.size() > 1) { + try { + int inc = Integer.parseInt(args.get(1)); + MainApplication.calc.incStoredVal(inc); + Console.prln("Stored " + inc + " in memory"); + } catch (NumberFormatException e) { + //e.printStackTrace(); + Console.prln("That's not a number..."); + } + } + else { + Console.prln("Nothing to store. Please enter a second argument!"); + } + fullPrompt(); return; case SWITCH_DISP_TRIG: - rePrompt(); + MainApplication.calc.toggleTrigMode(); + Console.prln("Trig Mode switched to " + MainApplication.calc.getTrigModeStr()); + fullPrompt(); return; case TOGGLE_NEGATIVE: - rePrompt(); + MainApplication.calc.toggleAllowNegatives(); + if (MainApplication.calc.allowingNegative()) { + Console.prln("Calculator is allowing negative numbers again."); + } + else { + Console.prln("Calculator will no longer show negative numbers."); + } + fullPrompt(); + return; + case EXIT: + System.exit(0); return; - case TRIG: - rePrompt(); + case DISPLAY: + Console.prln("Value: " + MainApplication.calc.getDisplay()); + fullPrompt(); + return; + case MATH: + //TODO + return; + case SWITCH_DISP: + DisplayModeCommands.fullPrompt(); return; default: - run(Command.BAD_COMMAND); + run(Command.BAD_COMMAND, null); return; } } diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/DisplayModeCommands.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/DisplayModeCommands.java new file mode 100644 index 00000000..0aa423d3 --- /dev/null +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/DisplayModeCommands.java @@ -0,0 +1,123 @@ +package com.zipcodewilmington.scientificcalculator.Utilities; + +import java.util.*; +import java.util.Map.Entry; + +import com.zipcodewilmington.scientificcalculator.Application.*; +import com.zipcodewilmington.scientificcalculator.Calculator.Calculator.DisplayMode; +import com.zipcodewilmington.scientificcalculator.Utilities.ConsoleCommands.Command; + +public class DisplayModeCommands +{ + private static Map commandMap = new HashMap<>(); + + public enum Mode { + BINARY, + OCTAL, + DECIMAL, + HEXADECIMAL, + RETURN, + HELP + } + + + + static { + commandMap = new HashMap<>(); + Map tempMap = new HashMap<>(); + commandMap.put("Binary", Mode.BINARY); + commandMap.put("Octal", Mode.OCTAL); + commandMap.put("Decimal", Mode.DECIMAL); + commandMap.put("Hexadecimal", Mode.HEXADECIMAL); + commandMap.put("Return", Mode.RETURN); + commandMap.put("Help", Mode.HELP); + + // Fill map with all the above commands, but in lower case and upper case (ie CLEAR/Clear/clear all will work) + for (Entry i : commandMap.entrySet()) { + tempMap.put(i.getKey().toLowerCase(), i.getValue()); + tempMap.put(i.getKey().toUpperCase(), i.getValue()); + } + for (Entry i : tempMap.entrySet()) { + commandMap.put(i.getKey(), i.getValue()); + } + } + + public static void runCommand(ArrayList args) { + if (commandMap != null && commandMap.containsKey(args.get(0))) { + run(commandMap.get(args.get(0))); + } + else { + run(Mode.RETURN); + } + } + + public static ArrayList prompt() { + String input = Console.getStringInput("Enter a DISPLAY MODE to continue: "); + String[] splited = input.split("\\s+"); + ArrayList argus = new ArrayList<>(); + for (String s : splited) { + argus.add(s); + } + return argus; + } + + public static void fullPrompt() { + String input = Console.getStringInput("Enter a DISPLAY MODE to continue: "); + String[] splited = input.split("\\s+"); + ArrayList argus = new ArrayList<>(); + for (String s : splited) { + argus.add(s); + } + while (argus.size() < 1) { + argus = DisplayModeCommands.prompt(); + } + DisplayModeCommands.runCommand(argus); + } + + public static void run(Mode cmd) { + switch (cmd) + { + case BINARY: + MainApplication.calc.setDisplayMode(DisplayMode.BINARY); + Console.prln("Calculator switched to BINARY mode."); + ConsoleCommands.run(Command.DISPLAY, null); + return; + case DECIMAL: + MainApplication.calc.setDisplayMode(DisplayMode.DECIMAL); + Console.prln("Calculator switched to DECIMAL mode."); + ConsoleCommands.run(Command.DISPLAY, null); + return; + case HEXADECIMAL: + MainApplication.calc.setDisplayMode(DisplayMode.HEXADECIMAL); + Console.prln("Calculator switched to HEXADECIMAL mode."); + ConsoleCommands.run(Command.DISPLAY, null); + return; + case OCTAL: + MainApplication.calc.setDisplayMode(DisplayMode.OCTAL); + Console.prln("Calculator switched to OCTAL mode."); + ConsoleCommands.run(Command.DISPLAY, null); + return; + case RETURN: + ConsoleCommands.fullPrompt(); + return; + case HELP: + Console.prln("Printing a list of all available commands in this menu: "); + ArrayList uniques = new ArrayList<>(); + for (Entry i : commandMap.entrySet()) { + if (!uniques.contains(i.getKey().toUpperCase())) { + uniques.add(i.getKey().toUpperCase()); + } + } + Collections.sort(uniques); + for (String s : uniques) { + Console.prln(s); + } + fullPrompt(); + return; + default: + fullPrompt(); + return; + } + } + +} diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/MathCommands.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/MathCommands.java new file mode 100644 index 00000000..a6160838 --- /dev/null +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/MathCommands.java @@ -0,0 +1,132 @@ +package com.zipcodewilmington.scientificcalculator.Utilities; + +import java.util.*; +import java.util.Map.Entry; + +import com.zipcodewilmington.scientificcalculator.Application.*; + +public class MathCommands +{ + private static Map commandMap = new HashMap<>(); + + public enum Command { + ADD, + SUBTRACT, + DIVIDE, + MULTIPLY, + SQRT, + SQUARE, + INVERSE, + VAR_EXP, + FLIP_SIGN, + HELP, + RETURN + } + + + + static { + commandMap = new HashMap<>(); + Map tempMap = new HashMap<>(); + commandMap.put("Add", Command.ADD); + commandMap.put("Subtract", Command.SUBTRACT); + commandMap.put("Minus", Command.SUBTRACT); + commandMap.put("Mult", Command.MULTIPLY); + commandMap.put("Multiply", Command.MULTIPLY); + commandMap.put("Divide", Command.DIVIDE); + commandMap.put("Sqrt", Command.SQRT); + commandMap.put("Square", Command.SQUARE); + commandMap.put("Inverse", Command.INVERSE); + commandMap.put("Varexp", Command.VAR_EXP); + commandMap.put("Signflip", Command.FLIP_SIGN); + commandMap.put("Help", Command.HELP); + commandMap.put("Return", Command.RETURN); + // Fill map with all the above commands, but in lower case and upper case (ie CLEAR/Clear/clear all will work) + for (Entry i : commandMap.entrySet()) { + tempMap.put(i.getKey().toLowerCase(), i.getValue()); + tempMap.put(i.getKey().toUpperCase(), i.getValue()); + } + for (Entry i : tempMap.entrySet()) { + commandMap.put(i.getKey(), i.getValue()); + } + } + + public static void runCommand(ArrayList args) { + if (commandMap != null && commandMap.containsKey(args.get(0))) { + run(commandMap.get(args.get(0)), args); + } + else { + run(Command.RETURN, args); + } + } + + public static ArrayList prompt() { + String input = Console.getStringInput("Enter a MATH COMMAND to continue: "); + String[] splited = input.split("\\s+"); + ArrayList argus = new ArrayList<>(); + for (String s : splited) { + argus.add(s); + } + return argus; + } + + public static void fullPrompt() { + String input = Console.getStringInput("Enter a MATH COMMAND to continue: "); + String[] splited = input.split("\\s+"); + ArrayList argus = new ArrayList<>(); + for (String s : splited) { + argus.add(s); + } + while (argus.size() < 1) { + argus = MathCommands.prompt(); + } + MathCommands.runCommand(argus); + } + + public static void run(Command cmd, ArrayList args) { + switch (cmd) + { + case ADD: + if (args.size() > 1) { + int sum = 0; + try { + for (int i = 1; i < args.size() - 1; i++) { + sum += Integer.parseInt(args.get(i)); + } + MainApplication.calc.add(sum); + ConsoleCommands.run(ConsoleCommands.Command.DISPLAY, null); + } catch (NumberFormatException e) { + //e.printStackTrace(); + Console.prln("That's not a number..."); + } + } + else { + Console.prln("Not enough arguments!"); + } + break; + case DIVIDE: + break; + case FLIP_SIGN: + break; + case HELP: + break; + case INVERSE: + break; + case MULTIPLY: + break; + case RETURN: + break; + case SQRT: + break; + case SQUARE: + break; + case SUBTRACT: + break; + case VAR_EXP: + break; + default: + break; + } + } + +} From 81e5fb729d1e9f82ab2d82286a2214b546794d4b Mon Sep 17 00:00:00 2001 From: Adam Bennett Date: Sat, 8 Feb 2020 00:19:15 -0500 Subject: [PATCH 07/22] finished most math commands --- .../Calculator/Calculator.java | 14 + .../Utilities/ConsoleCommands.java | 116 ++++++--- .../Utilities/DisplayModeCommands.java | 4 +- .../Utilities/MathCommands.java | 242 +++++++++++++++--- 4 files changed, 291 insertions(+), 85 deletions(-) diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Calculator/Calculator.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Calculator/Calculator.java index 53989748..a4dfc870 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Calculator/Calculator.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Calculator/Calculator.java @@ -109,6 +109,11 @@ public void mult(float val) { } public void div(float num) { + if (num == 0) { + this.isError = true; + updateDisplay(); + return; + } this.displayValue /= num; } @@ -125,6 +130,11 @@ public void pow(float exponent) { } public void inverse() { + if (this.displayValue == 0.0f) { + this.isError = true; + updateDisplay(); + return; + } this.displayValue = 1.0f / this.displayValue; } @@ -132,6 +142,10 @@ public void flipSign() { this.displayValue = -this.displayValue; } + public void factorial() { + this.displayValue = Util.factorial((int) this.displayValue); + } + public void sine() { this.displayValue = Util.sine(this.displayValue); if (this.trigMode == TrigDisplayMode.RADIANS) { diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/ConsoleCommands.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/ConsoleCommands.java index 1f3b101f..50d60f7b 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/ConsoleCommands.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/ConsoleCommands.java @@ -28,36 +28,41 @@ public enum Command { public enum MenuType { CONSOLE, MATH, - DISPLAY + DISPLAY, + ERROR } - static { - commandMap = new HashMap<>(); - Map tempMap = new HashMap<>(); - commandMap.put("Clear", Command.CLEAR); - commandMap.put("Help", Command.HELP); - commandMap.put("Info", Command.INFO); - commandMap.put("Math", Command.MATH); - commandMap.put("Recall", Command.RECALL); - commandMap.put("Reset", Command.RESET); - commandMap.put("Store", Command.STORE); - commandMap.put("Switchmode", Command.SWITCH_DISP); - commandMap.put("Switchtrig", Command.SWITCH_DISP_TRIG); - commandMap.put("Togglenegative", Command.TOGGLE_NEGATIVE); - commandMap.put("Display", Command.DISPLAY); - commandMap.put("Exit", Command.EXIT); + public static void fullPrompt() { - // Fill map with all the above commands, but in lower case and upper case (ie CLEAR/Clear/clear all will work) - for (Entry i : commandMap.entrySet()) { - tempMap.put(i.getKey().toLowerCase(), i.getValue()); - tempMap.put(i.getKey().toUpperCase(), i.getValue()); - } - for (Entry i : tempMap.entrySet()) { - commandMap.put(i.getKey(), i.getValue()); + // If in error mode, we are forcing a clear of the display + if (MainApplication.calc.isInErrorMode()) { + errorPrompt(); + return; } + + // Otherwise, get some input for a console command + String input = Console.getStringInput("Enter a CONSOLE COMMAND to continue: "); + + // Split the input into an array of seperate strings (this splits by spaces) + String[] splited = input.split("\\s+"); + + // Save the strings to an arraylist so we can pass it around more easily + ArrayList argus = new ArrayList<>(); + for (String s : splited) { + argus.add(s); + } + + // Loop to check for input + // If input is not entered, this waits for more + while (argus.size() < 1) { + argus = prompt(MenuType.CONSOLE); + } + + // Once we have at least 1 string of input, run a command + runCommand(argus); } - - public static void runCommand(ArrayList args) { + + public static void runCommand(ArrayList args) { if (commandMap != null && commandMap.containsKey(args.get(0))) { run(commandMap.get(args.get(0)), args); } @@ -82,6 +87,8 @@ public static ArrayList prompt(MenuType type) { case MATH: inptCmd = "Enter a MATH COMMAND to continue: "; break; + case ERROR: + inptCmd = "Error Mode - Please Clear the Display: "; default: inptCmd = "Enter a CONSOLE COMMAND to continue: "; break; @@ -95,20 +102,19 @@ public static ArrayList prompt(MenuType type) { return argus; } - public static void fullPrompt() { - String input = Console.getStringInput("Enter a CONSOLE COMMAND to continue: "); - String[] splited = input.split("\\s+"); - ArrayList argus = new ArrayList<>(); - for (String s : splited) { - argus.add(s); - } - while (argus.size() < 1) { - argus = ConsoleCommands.prompt(MenuType.CONSOLE); + public static void errorPrompt() { + String input = Console.getStringInput("Error Mode - Please Clear the Display: "); + while (!input.toLowerCase().equals("clear")) { + input = Console.getStringInput("Error Mode - Please Clear the Display: "); } - ConsoleCommands.runCommand(argus); + run(Command.CLEAR, null); } public static void promptMath() { + if (MainApplication.calc.isInErrorMode()) { + errorPrompt(); + return; + } String input = Console.getStringInput("Enter a MATH COMMAND to continue: "); String[] splited = input.split("\\s+"); ArrayList argus = new ArrayList<>(); @@ -116,12 +122,16 @@ public static void promptMath() { argus.add(s); } while (argus.size() < 1) { - argus = ConsoleCommands.prompt(MenuType.MATH); + argus = prompt(MenuType.MATH); } - ConsoleCommands.runCommand(argus); + runCommand(argus); } public static void promptDispSwitch() { + if (MainApplication.calc.isInErrorMode()) { + errorPrompt(); + return; + } String input = Console.getStringInput("Enter a DISPLAY MODE to continue: "); String[] splited = input.split("\\s+"); ArrayList argus = new ArrayList<>(); @@ -129,9 +139,9 @@ public static void promptDispSwitch() { argus.add(s); } while (argus.size() < 1) { - argus = ConsoleCommands.prompt(MenuType.DISPLAY); + argus = prompt(MenuType.DISPLAY); } - ConsoleCommands.runCommand(argus); + runCommand(argus); } public static void run(Command cmd, ArrayList args) { @@ -211,7 +221,7 @@ public static void run(Command cmd, ArrayList args) { fullPrompt(); return; case MATH: - //TODO + MathCommands.fullPrompt(); return; case SWITCH_DISP: DisplayModeCommands.fullPrompt(); @@ -222,4 +232,30 @@ public static void run(Command cmd, ArrayList args) { } } + static { + commandMap = new HashMap<>(); + Map tempMap = new HashMap<>(); + commandMap.put("Clear", Command.CLEAR); + commandMap.put("Help", Command.HELP); + commandMap.put("Info", Command.INFO); + commandMap.put("Math", Command.MATH); + commandMap.put("Recall", Command.RECALL); + commandMap.put("Reset", Command.RESET); + commandMap.put("Store", Command.STORE); + commandMap.put("Switchmode", Command.SWITCH_DISP); + commandMap.put("Switchtrig", Command.SWITCH_DISP_TRIG); + commandMap.put("Togglenegative", Command.TOGGLE_NEGATIVE); + commandMap.put("Display", Command.DISPLAY); + commandMap.put("Exit", Command.EXIT); + + // Fill map with all the above commands, but in lower case and upper case (ie CLEAR/Clear/clear all will work) + for (Entry i : commandMap.entrySet()) { + tempMap.put(i.getKey().toLowerCase(), i.getValue()); + tempMap.put(i.getKey().toUpperCase(), i.getValue()); + } + for (Entry i : tempMap.entrySet()) { + commandMap.put(i.getKey(), i.getValue()); + } + } + } diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/DisplayModeCommands.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/DisplayModeCommands.java index 0aa423d3..344a7c78 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/DisplayModeCommands.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/DisplayModeCommands.java @@ -19,9 +19,7 @@ public enum Mode { RETURN, HELP } - - - + static { commandMap = new HashMap<>(); Map tempMap = new HashMap<>(); diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/MathCommands.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/MathCommands.java index a6160838..0bea9662 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/MathCommands.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/MathCommands.java @@ -2,6 +2,7 @@ import java.util.*; import java.util.Map.Entry; +import java.util.concurrent.ThreadLocalRandom; import com.zipcodewilmington.scientificcalculator.Application.*; @@ -20,14 +21,34 @@ public enum Command { VAR_EXP, FLIP_SIGN, HELP, - RETURN + RETURN, + FACTORIAL, + SINE, + COSINE, + TANGENT, + INV_SINE, + INV_COSINE, + INV_TANGENT, + LOG, + INV_LOG, + NATURAL_LOG, + INV_NAT_LOG, + RANDOM_NUM } - - static { commandMap = new HashMap<>(); Map tempMap = new HashMap<>(); + commandMap.put("Sin", Command.SINE); + commandMap.put("Cos", Command.COSINE); + commandMap.put("Tan", Command.TANGENT); + commandMap.put("Arcsin", Command.INV_SINE); + commandMap.put("Arccos", Command.INV_COSINE); + commandMap.put("Arctan", Command.INV_TANGENT); + commandMap.put("Log", Command.LOG); + commandMap.put("InvLog", Command.INV_LOG); + commandMap.put("NaturalLog", Command.NATURAL_LOG); + commandMap.put("InvNatLog", Command.INV_NAT_LOG); commandMap.put("Add", Command.ADD); commandMap.put("Subtract", Command.SUBTRACT); commandMap.put("Minus", Command.SUBTRACT); @@ -41,6 +62,8 @@ public enum Command { commandMap.put("Signflip", Command.FLIP_SIGN); commandMap.put("Help", Command.HELP); commandMap.put("Return", Command.RETURN); + commandMap.put("Factorial", Command.FACTORIAL); + commandMap.put("Random", Command.RANDOM_NUM); // Fill map with all the above commands, but in lower case and upper case (ie CLEAR/Clear/clear all will work) for (Entry i : commandMap.entrySet()) { tempMap.put(i.getKey().toLowerCase(), i.getValue()); @@ -71,6 +94,10 @@ public static ArrayList prompt() { } public static void fullPrompt() { + if (MainApplication.calc.isInErrorMode()) { + ConsoleCommands.errorPrompt(); + return; + } String input = Console.getStringInput("Enter a MATH COMMAND to continue: "); String[] splited = input.split("\\s+"); ArrayList argus = new ArrayList<>(); @@ -84,49 +111,180 @@ public static void fullPrompt() { } public static void run(Command cmd, ArrayList args) { - switch (cmd) - { - case ADD: - if (args.size() > 1) { - int sum = 0; - try { - for (int i = 1; i < args.size() - 1; i++) { - sum += Integer.parseInt(args.get(i)); - } - MainApplication.calc.add(sum); - ConsoleCommands.run(ConsoleCommands.Command.DISPLAY, null); - } catch (NumberFormatException e) { - //e.printStackTrace(); - Console.prln("That's not a number..."); + switch (cmd) { + case ADD: + if (args.size() > 1) { + int sum = 0; + try { + for (int i = 1; i < args.size(); i++) { + sum += Integer.parseInt(args.get(i)); } + MainApplication.calc.add(sum); + reprompt(); + } catch (NumberFormatException e) { + //e.printStackTrace(); + Console.prln("That's not a number..."); + } + } + else { + Console.prln("Not enough arguments!"); + } + fullPrompt(); + return; + case COSINE: + //TODO + case DIVIDE: + if (args.size() > 1) { + try { + for (int i = 1; i < args.size(); i++) { + int denom = Integer.parseInt(args.get(i)); + MainApplication.calc.div(denom); + } + reprompt(); + } catch (NumberFormatException e) { + //e.printStackTrace(); + Console.prln("That's not a number..."); + } + } + else { + Console.prln("Not enough arguments!"); + } + fullPrompt(); + return; + case FACTORIAL: + MainApplication.calc.factorial(); + reprompt(); + return; + case FLIP_SIGN: + MainApplication.calc.flipSign(); + reprompt(); + return; + case HELP: + Console.prln("Printing a list of all available commands in this menu: "); + ArrayList uniques = new ArrayList<>(); + for (Entry i : commandMap.entrySet()) { + if (!uniques.contains(i.getKey().toUpperCase())) { + uniques.add(i.getKey().toUpperCase()); } - else { - Console.prln("Not enough arguments!"); + } + Collections.sort(uniques); + for (String s : uniques) { + Console.prln(s); + } + fullPrompt(); + return; + case INVERSE: + MainApplication.calc.inverse(); + reprompt(); + break; + case INV_COSINE: + //TODO + case INV_LOG: + //TODO + case INV_NAT_LOG: + //TODO + case INV_SINE: + //TODO + case INV_TANGENT: + //TODO + case LOG: + //TODO + case MULTIPLY: + if (args.size() > 1) { + try { + for (int i = 1; i < args.size(); i++) { + MainApplication.calc.mult(Integer.parseInt(args.get(i))); + } + reprompt(); + } catch (NumberFormatException e) { + //e.printStackTrace(); + Console.prln("That's not a number..."); } - break; - case DIVIDE: - break; - case FLIP_SIGN: - break; - case HELP: - break; - case INVERSE: - break; - case MULTIPLY: - break; - case RETURN: - break; - case SQRT: - break; - case SQUARE: - break; - case SUBTRACT: - break; - case VAR_EXP: - break; - default: - break; + } + else { + Console.prln("Not enough arguments!"); + } + fullPrompt(); + return; + case NATURAL_LOG: + //TODO + case RANDOM_NUM: + if (args.size() > 2 && args.size() < 4) { + try { + int lower = Integer.parseInt(args.get(1)); + int higher = Integer.parseInt(args.get(2)); + int rand = ThreadLocalRandom.current().nextInt(lower, higher + 1); + Console.prln("Randomly generated number (" + lower + "-" + higher + "): " + rand); + fullPrompt(); + } catch (NumberFormatException e) { + //e.printStackTrace(); + Console.prln("That's not a number..."); + } + } + else { + Console.prln("Wrong amount of arguments! Expecting 2 numbers, lower and upper bound (inclusive)."); + } + fullPrompt(); + return; + case RETURN: + ConsoleCommands.fullPrompt(); + return; + case SINE: + //TODO + case SQRT: + MainApplication.calc.sqRt(); + reprompt(); + break; + case SQUARE: + MainApplication.calc.square(); + reprompt(); + break; + case SUBTRACT: + if (args.size() > 1) { + try { + for (int i = 1; i < args.size(); i++) { + MainApplication.calc.subtract(Integer.parseInt(args.get(i))); + } + reprompt(); + } catch (NumberFormatException e) { + //e.printStackTrace(); + Console.prln("That's not a number..."); + } + } + else { + Console.prln("Not enough arguments!"); + } + fullPrompt(); + return; + case TANGENT: + //TODO + case VAR_EXP: + if (args.size() > 1) { + try { + for (int i = 1; i < args.size(); i++) { + MainApplication.calc.pow(Integer.parseInt(args.get(i))); + } + reprompt(); + } catch (NumberFormatException e) { + //e.printStackTrace(); + Console.prln("That's not a number..."); + } + } + else { + Console.prln("Not enough arguments!"); + } + fullPrompt(); + return; + default: + fullPrompt(); + return; } } + private static void reprompt() { + Console.prln("Value: " + MainApplication.calc.getDisplay()); + fullPrompt(); + return; + } + } From 67737ed1d1308cea7993020c9e829f3f04222dea Mon Sep 17 00:00:00 2001 From: Adam Bennett Date: Sat, 8 Feb 2020 00:40:44 -0500 Subject: [PATCH 08/22] added some tests --- .../Application/Console.java | 49 ----------- .../Application/MainApplication.java | 4 +- .../Calculator/Calculator.java | 4 + .../Utilities/ConsoleCommands.java | 40 ++++----- .../Utilities/DisplayModeCommands.java | 16 ++-- .../Utilities/MathCommands.java | 36 ++++---- .../scientificcalculator/Utilities/Util.java | 86 ++++++++++--------- .../Application/TestMainApplication.java | 16 ---- .../Calculator/TestCalculator.java | 58 ++++++++++++- .../Utilities/TestUtilities.java | 14 --- 10 files changed, 151 insertions(+), 172 deletions(-) delete mode 100644 src/main/java/com/zipcodewilmington/scientificcalculator/Application/Console.java delete mode 100644 src/test/java/com/zipcodewilmington/scientific_calculator/Application/TestMainApplication.java delete mode 100644 src/test/java/com/zipcodewilmington/scientific_calculator/Utilities/TestUtilities.java diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Application/Console.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Application/Console.java deleted file mode 100644 index 41fe21e0..00000000 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Application/Console.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.zipcodewilmington.scientificcalculator.Application; - -import java.util.Scanner; - -/** - * Created by leon on 2/9/18. - */ -@SuppressWarnings("resource") -public class Console { - - public static void print(String output, Object... args) { - System.out.printf(output, args); - } - - public static void println(String output, Object... args) { - print(output + "\n", args); - } - - public static void prln(String print) { - System.out.println(print); - } - - public static String getStringInput(String prompt) { - Scanner scanner = new Scanner(System.in); - println(prompt); - String userInput = scanner.nextLine(); - return userInput; - } - - public static Integer getIntegerInput(String prompt) { - Scanner scanner = new Scanner(System.in); - println(prompt); - String userInput = scanner.nextLine(); - try { - return Integer.parseInt(userInput); - } catch (NumberFormatException e) { e.printStackTrace(); } - return 0; - } - - public static Double getDoubleInput(String prompt) { - Scanner scanner = new Scanner(System.in); - println(prompt); - String userInput = scanner.nextLine(); - try { - return Double.parseDouble(userInput); - } catch (NumberFormatException e) { e.printStackTrace(); } - return 0.0; - } -} diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Application/MainApplication.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Application/MainApplication.java index 43042f7b..2ba1abb4 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Application/MainApplication.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Application/MainApplication.java @@ -1,7 +1,7 @@ package com.zipcodewilmington.scientificcalculator.Application; import com.zipcodewilmington.scientificcalculator.Calculator.Calculator; -import com.zipcodewilmington.scientificcalculator.Utilities.ConsoleCommands; +import com.zipcodewilmington.scientificcalculator.Utilities.*; /** * Created by leon on 2/9/18. @@ -12,7 +12,7 @@ public class MainApplication { public static void main(String[] args) { - Console.println("Welcome to my calculator!"); + Util.println("Welcome to my calculator!"); ConsoleCommands.fullPrompt(); } diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Calculator/Calculator.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Calculator/Calculator.java index a4dfc870..6094a61c 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Calculator/Calculator.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Calculator/Calculator.java @@ -29,6 +29,10 @@ public Calculator() { this(0, 0, true, DisplayMode.DECIMAL, TrigDisplayMode.DEGREES); } + public Calculator(int displayVal) { + this(displayVal, 0, true, DisplayMode.DECIMAL, TrigDisplayMode.DEGREES); + } + public Calculator(int displayVal, int storedVal, boolean allowNegatives, DisplayMode numMode, TrigDisplayMode trigMode) { this.displayValue = displayVal; this.storedValue = storedVal; diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/ConsoleCommands.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/ConsoleCommands.java index 50d60f7b..267c7589 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/ConsoleCommands.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/ConsoleCommands.java @@ -41,7 +41,7 @@ public static void fullPrompt() { } // Otherwise, get some input for a console command - String input = Console.getStringInput("Enter a CONSOLE COMMAND to continue: "); + String input = Util.getStringInput("Enter a CONSOLE COMMAND to continue: "); // Split the input into an array of seperate strings (this splits by spaces) String[] splited = input.split("\\s+"); @@ -67,7 +67,7 @@ public static void runCommand(ArrayList args) { run(commandMap.get(args.get(0)), args); } else if (args.size() > 1 && args.get(0).equals("best") && args.get(1).equals("programmer")) { - Console.prln("Nobles"); + Util.prln("Nobles"); fullPrompt(); } else { @@ -93,7 +93,7 @@ public static ArrayList prompt(MenuType type) { inptCmd = "Enter a CONSOLE COMMAND to continue: "; break; } - String input = Console.getStringInput(inptCmd); + String input = Util.getStringInput(inptCmd); String[] splited = input.split("\\s+"); ArrayList argus = new ArrayList<>(); for (String s : splited) { @@ -103,9 +103,9 @@ public static ArrayList prompt(MenuType type) { } public static void errorPrompt() { - String input = Console.getStringInput("Error Mode - Please Clear the Display: "); + String input = Util.getStringInput("Error Mode - Please Clear the Display: "); while (!input.toLowerCase().equals("clear")) { - input = Console.getStringInput("Error Mode - Please Clear the Display: "); + input = Util.getStringInput("Error Mode - Please Clear the Display: "); } run(Command.CLEAR, null); } @@ -115,7 +115,7 @@ public static void promptMath() { errorPrompt(); return; } - String input = Console.getStringInput("Enter a MATH COMMAND to continue: "); + String input = Util.getStringInput("Enter a MATH COMMAND to continue: "); String[] splited = input.split("\\s+"); ArrayList argus = new ArrayList<>(); for (String s : splited) { @@ -132,7 +132,7 @@ public static void promptDispSwitch() { errorPrompt(); return; } - String input = Console.getStringInput("Enter a DISPLAY MODE to continue: "); + String input = Util.getStringInput("Enter a DISPLAY MODE to continue: "); String[] splited = input.split("\\s+"); ArrayList argus = new ArrayList<>(); for (String s : splited) { @@ -148,7 +148,7 @@ public static void run(Command cmd, ArrayList args) { switch (cmd) { case BAD_COMMAND: - Console.prln("Bad command! Please enter a valid command, or enter 'Help' to view a list of all commands."); + Util.prln("Bad command! Please enter a valid command, or enter 'Help' to view a list of all commands."); fullPrompt(); return; case CLEAR: @@ -156,7 +156,7 @@ public static void run(Command cmd, ArrayList args) { run(Command.DISPLAY, null); return; case HELP: - Console.prln("Printing a list of all available commands: "); + Util.prln("Printing a list of all available commands: "); ArrayList uniques = new ArrayList<>(); for (Entry i : commandMap.entrySet()) { if (!uniques.contains(i.getKey().toUpperCase())) { @@ -165,21 +165,21 @@ public static void run(Command cmd, ArrayList args) { } Collections.sort(uniques); for (String s : uniques) { - Console.prln(s); + Util.prln(s); } fullPrompt(); return; case INFO: - Console.prln("This calculator was made by Aarti, Adam, and Matt! We hope you find it useful."); + Util.prln("This calculator was made by Aarti, Adam, and Matt! We hope you find it useful."); fullPrompt(); return; case RECALL: - Console.prln("Stored Memory Value: " + MainApplication.calc.getStoredVal()); + Util.prln("Stored Memory Value: " + MainApplication.calc.getStoredVal()); fullPrompt(); return; case RESET: MainApplication.calc.totalReset(); - Console.prln("Reset all calculator values! Display mode is now DECIMAL/DEGREES."); + Util.prln("Reset all calculator values! Display mode is now DECIMAL/DEGREES."); run(Command.DISPLAY, null); return; case STORE: @@ -187,29 +187,29 @@ public static void run(Command cmd, ArrayList args) { try { int inc = Integer.parseInt(args.get(1)); MainApplication.calc.incStoredVal(inc); - Console.prln("Stored " + inc + " in memory"); + Util.prln("Stored " + inc + " in memory"); } catch (NumberFormatException e) { //e.printStackTrace(); - Console.prln("That's not a number..."); + Util.prln("That's not a number..."); } } else { - Console.prln("Nothing to store. Please enter a second argument!"); + Util.prln("Nothing to store. Please enter a second argument!"); } fullPrompt(); return; case SWITCH_DISP_TRIG: MainApplication.calc.toggleTrigMode(); - Console.prln("Trig Mode switched to " + MainApplication.calc.getTrigModeStr()); + Util.prln("Trig Mode switched to " + MainApplication.calc.getTrigModeStr()); fullPrompt(); return; case TOGGLE_NEGATIVE: MainApplication.calc.toggleAllowNegatives(); if (MainApplication.calc.allowingNegative()) { - Console.prln("Calculator is allowing negative numbers again."); + Util.prln("Calculator is allowing negative numbers again."); } else { - Console.prln("Calculator will no longer show negative numbers."); + Util.prln("Calculator will no longer show negative numbers."); } fullPrompt(); return; @@ -217,7 +217,7 @@ public static void run(Command cmd, ArrayList args) { System.exit(0); return; case DISPLAY: - Console.prln("Value: " + MainApplication.calc.getDisplay()); + Util.prln("Value: " + MainApplication.calc.getDisplay()); fullPrompt(); return; case MATH: diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/DisplayModeCommands.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/DisplayModeCommands.java index 344a7c78..d87c2667 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/DisplayModeCommands.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/DisplayModeCommands.java @@ -50,7 +50,7 @@ public static void runCommand(ArrayList args) { } public static ArrayList prompt() { - String input = Console.getStringInput("Enter a DISPLAY MODE to continue: "); + String input = Util.getStringInput("Enter a DISPLAY MODE to continue: "); String[] splited = input.split("\\s+"); ArrayList argus = new ArrayList<>(); for (String s : splited) { @@ -60,7 +60,7 @@ public static ArrayList prompt() { } public static void fullPrompt() { - String input = Console.getStringInput("Enter a DISPLAY MODE to continue: "); + String input = Util.getStringInput("Enter a DISPLAY MODE to continue: "); String[] splited = input.split("\\s+"); ArrayList argus = new ArrayList<>(); for (String s : splited) { @@ -77,29 +77,29 @@ public static void run(Mode cmd) { { case BINARY: MainApplication.calc.setDisplayMode(DisplayMode.BINARY); - Console.prln("Calculator switched to BINARY mode."); + Util.prln("Calculator switched to BINARY mode."); ConsoleCommands.run(Command.DISPLAY, null); return; case DECIMAL: MainApplication.calc.setDisplayMode(DisplayMode.DECIMAL); - Console.prln("Calculator switched to DECIMAL mode."); + Util.prln("Calculator switched to DECIMAL mode."); ConsoleCommands.run(Command.DISPLAY, null); return; case HEXADECIMAL: MainApplication.calc.setDisplayMode(DisplayMode.HEXADECIMAL); - Console.prln("Calculator switched to HEXADECIMAL mode."); + Util.prln("Calculator switched to HEXADECIMAL mode."); ConsoleCommands.run(Command.DISPLAY, null); return; case OCTAL: MainApplication.calc.setDisplayMode(DisplayMode.OCTAL); - Console.prln("Calculator switched to OCTAL mode."); + Util.prln("Calculator switched to OCTAL mode."); ConsoleCommands.run(Command.DISPLAY, null); return; case RETURN: ConsoleCommands.fullPrompt(); return; case HELP: - Console.prln("Printing a list of all available commands in this menu: "); + Util.prln("Printing a list of all available commands in this menu: "); ArrayList uniques = new ArrayList<>(); for (Entry i : commandMap.entrySet()) { if (!uniques.contains(i.getKey().toUpperCase())) { @@ -108,7 +108,7 @@ public static void run(Mode cmd) { } Collections.sort(uniques); for (String s : uniques) { - Console.prln(s); + Util.prln(s); } fullPrompt(); return; diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/MathCommands.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/MathCommands.java index 0bea9662..fccaed62 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/MathCommands.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/MathCommands.java @@ -84,7 +84,7 @@ public static void runCommand(ArrayList args) { } public static ArrayList prompt() { - String input = Console.getStringInput("Enter a MATH COMMAND to continue: "); + String input = Util.getStringInput("Enter a MATH COMMAND to continue: "); String[] splited = input.split("\\s+"); ArrayList argus = new ArrayList<>(); for (String s : splited) { @@ -98,7 +98,7 @@ public static void fullPrompt() { ConsoleCommands.errorPrompt(); return; } - String input = Console.getStringInput("Enter a MATH COMMAND to continue: "); + String input = Util.getStringInput("Enter a MATH COMMAND to continue: "); String[] splited = input.split("\\s+"); ArrayList argus = new ArrayList<>(); for (String s : splited) { @@ -123,11 +123,11 @@ public static void run(Command cmd, ArrayList args) { reprompt(); } catch (NumberFormatException e) { //e.printStackTrace(); - Console.prln("That's not a number..."); + Util.prln("That's not a number..."); } } else { - Console.prln("Not enough arguments!"); + Util.prln("Not enough arguments!"); } fullPrompt(); return; @@ -143,11 +143,11 @@ public static void run(Command cmd, ArrayList args) { reprompt(); } catch (NumberFormatException e) { //e.printStackTrace(); - Console.prln("That's not a number..."); + Util.prln("That's not a number..."); } } else { - Console.prln("Not enough arguments!"); + Util.prln("Not enough arguments!"); } fullPrompt(); return; @@ -160,7 +160,7 @@ public static void run(Command cmd, ArrayList args) { reprompt(); return; case HELP: - Console.prln("Printing a list of all available commands in this menu: "); + Util.prln("Printing a list of all available commands in this menu: "); ArrayList uniques = new ArrayList<>(); for (Entry i : commandMap.entrySet()) { if (!uniques.contains(i.getKey().toUpperCase())) { @@ -169,7 +169,7 @@ public static void run(Command cmd, ArrayList args) { } Collections.sort(uniques); for (String s : uniques) { - Console.prln(s); + Util.prln(s); } fullPrompt(); return; @@ -198,11 +198,11 @@ public static void run(Command cmd, ArrayList args) { reprompt(); } catch (NumberFormatException e) { //e.printStackTrace(); - Console.prln("That's not a number..."); + Util.prln("That's not a number..."); } } else { - Console.prln("Not enough arguments!"); + Util.prln("Not enough arguments!"); } fullPrompt(); return; @@ -214,15 +214,15 @@ public static void run(Command cmd, ArrayList args) { int lower = Integer.parseInt(args.get(1)); int higher = Integer.parseInt(args.get(2)); int rand = ThreadLocalRandom.current().nextInt(lower, higher + 1); - Console.prln("Randomly generated number (" + lower + "-" + higher + "): " + rand); + Util.prln("Randomly generated number (" + lower + "-" + higher + "): " + rand); fullPrompt(); } catch (NumberFormatException e) { //e.printStackTrace(); - Console.prln("That's not a number..."); + Util.prln("That's not a number..."); } } else { - Console.prln("Wrong amount of arguments! Expecting 2 numbers, lower and upper bound (inclusive)."); + Util.prln("Wrong amount of arguments! Expecting 2 numbers, lower and upper bound (inclusive)."); } fullPrompt(); return; @@ -248,11 +248,11 @@ public static void run(Command cmd, ArrayList args) { reprompt(); } catch (NumberFormatException e) { //e.printStackTrace(); - Console.prln("That's not a number..."); + Util.prln("That's not a number..."); } } else { - Console.prln("Not enough arguments!"); + Util.prln("Not enough arguments!"); } fullPrompt(); return; @@ -267,11 +267,11 @@ public static void run(Command cmd, ArrayList args) { reprompt(); } catch (NumberFormatException e) { //e.printStackTrace(); - Console.prln("That's not a number..."); + Util.prln("That's not a number..."); } } else { - Console.prln("Not enough arguments!"); + Util.prln("Not enough arguments!"); } fullPrompt(); return; @@ -282,7 +282,7 @@ public static void run(Command cmd, ArrayList args) { } private static void reprompt() { - Console.prln("Value: " + MainApplication.calc.getDisplay()); + Util.prln("Value: " + MainApplication.calc.getDisplay()); fullPrompt(); return; } diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/Util.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/Util.java index 744aa569..a8cdd272 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/Util.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/Util.java @@ -1,27 +1,10 @@ package com.zipcodewilmington.scientificcalculator.Utilities; +import java.util.Scanner; + +@SuppressWarnings("resource") public class Util { - public static int add(int num1, int num2) { - return num1 + num2; - } - - public static int sub(int left, int right) { - return left - right; - } - - public static int mult(int num1, int num2) { - return num1 * num2; - } - - public static float div(float numerator, float denominator) { - return numerator / denominator; - } - - public static int div(int numerator, int denominator) { - return (int)div((float)numerator, (float)denominator); - } - public static float square(float num) { return num * num; } @@ -33,27 +16,7 @@ public static float squareRoot(float num) { public static int squareRoot(int num) { return (int)squareRoot((float)num); } - - public static double varExp(double base, double exp) { - return Math.pow(base, exp); - } - - public static int varExp(int base, int exp) { - return (int)Math.pow((double)base, (double)exp); - } - - public static int inverse(int toInverse) { - return div(1, toInverse); - } - - public static int flipSign(int num) { - return -num; - } - - public static String returnErr() { - return "Err"; - } - + public static float sine(float num) { return (float) Math.sin(num); } @@ -93,5 +56,44 @@ public static double inverseNaturalLog(double num) { public static int factorial(int n) { return (n == 1 || n == 0) ? 1 : n * factorial(n - 1); - } + } + + public static Double getDoubleInput(String prompt) { + Scanner scanner = new Scanner(System.in); + Util.println(prompt); + String userInput = scanner.nextLine(); + try { + return Double.parseDouble(userInput); + } catch (NumberFormatException e) { e.printStackTrace(); } + return 0.0; + } + + public static Integer getIntegerInput(String prompt) { + Scanner scanner = new Scanner(System.in); + Util.println(prompt); + String userInput = scanner.nextLine(); + try { + return Integer.parseInt(userInput); + } catch (NumberFormatException e) { e.printStackTrace(); } + return 0; + } + + public static String getStringInput(String prompt) { + Scanner scanner = new Scanner(System.in); + Util.println(prompt); + String userInput = scanner.nextLine(); + return userInput; + } + + public static void print(String output, Object... args) { + System.out.printf(output, args); + } + + public static void println(String output, Object... args) { + print(output + "\n", args); + } + + public static void prln(String print) { + System.out.println(print); + } } diff --git a/src/test/java/com/zipcodewilmington/scientific_calculator/Application/TestMainApplication.java b/src/test/java/com/zipcodewilmington/scientific_calculator/Application/TestMainApplication.java deleted file mode 100644 index 50528e82..00000000 --- a/src/test/java/com/zipcodewilmington/scientific_calculator/Application/TestMainApplication.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.zipcodewilmington.scientific_calculator.Application; - -import org.junit.*; - -/** - * Created by leon on 2/9/18. - */ -public class TestMainApplication -{ - @Test - public void templateTestClass() - { - - } - -} diff --git a/src/test/java/com/zipcodewilmington/scientific_calculator/Calculator/TestCalculator.java b/src/test/java/com/zipcodewilmington/scientific_calculator/Calculator/TestCalculator.java index f00b82b1..a8a4ccd2 100644 --- a/src/test/java/com/zipcodewilmington/scientific_calculator/Calculator/TestCalculator.java +++ b/src/test/java/com/zipcodewilmington/scientific_calculator/Calculator/TestCalculator.java @@ -1,14 +1,66 @@ package com.zipcodewilmington.scientific_calculator.Calculator; -import org.junit.*; +import static org.junit.Assert.assertEquals; +import org.junit.Test; + +import com.zipcodewilmington.scientificcalculator.Calculator.Calculator; +import com.zipcodewilmington.scientificcalculator.Calculator.Calculator.*; public class TestCalculator { @Test - public void templateTestClass() - { + public void TestCalculatorInit() { + Calculator calc = new Calculator(); + assertEquals(0, (int)calc.getDisplayVal()); + } + + @Test + public void TestClearDisplay() { + Calculator calc = new Calculator(); + calc.add(10); + assertEquals(10, (int)calc.getDisplayVal()); + calc.clearDisplay(); + assertEquals(0, (int)calc.getDisplayVal()); + } + + @Test + public void TestStoringMemory() { + Calculator calc = new Calculator(); + calc.setStoredVal(10); + assertEquals(10, (int)calc.getStoredVal()); + calc.clearMemory(); + assertEquals(0, (int)calc.getStoredVal()); + } + + @Test + public void TestReset() { + Calculator calc = new Calculator(); + calc.add(15); + calc.setStoredVal(10); + assertEquals(10, (int)calc.getStoredVal()); + assertEquals(15, (int)calc.getDisplayVal()); + calc.totalReset(); + assertEquals(0, (int)calc.getDisplayVal()); + assertEquals(0, (int)calc.getStoredVal()); + assertEquals(DisplayMode.DECIMAL, calc.getNumMode()); + assertEquals(TrigDisplayMode.DEGREES, calc.getTrigMode()); + } + + @Test + public void TestAllowNegatives() { + Calculator calc = new Calculator(); + calc.toggleAllowNegatives(); + assertEquals(false, calc.allowingNegative()); + calc.add(-100); + assertEquals("0.0", calc.getDisplay()); + assertEquals(0, (int)calc.getDisplayVal()); + calc.toggleAllowNegatives(); + assertEquals("-100.0", calc.getDisplay()); + assertEquals(-100, (int)calc.getDisplayVal()); } + + } diff --git a/src/test/java/com/zipcodewilmington/scientific_calculator/Utilities/TestUtilities.java b/src/test/java/com/zipcodewilmington/scientific_calculator/Utilities/TestUtilities.java deleted file mode 100644 index fd60015d..00000000 --- a/src/test/java/com/zipcodewilmington/scientific_calculator/Utilities/TestUtilities.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.zipcodewilmington.scientific_calculator.Utilities; - -import org.junit.*; - - -public class TestUtilities -{ - @Test - public void templateTestClass() - { - - } - -} From 0aef8aa5b1d81e89731a968f43cddfdf1f2d2bd3 Mon Sep 17 00:00:00 2001 From: Adam Bennett Date: Sat, 8 Feb 2020 00:53:16 -0500 Subject: [PATCH 09/22] cleanup utilities --- .../Utilities/MathCommands.java | 13 ++++++++++++- .../scientificcalculator/Utilities/Util.java | 2 +- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/MathCommands.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/MathCommands.java index fccaed62..b846e459 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/MathCommands.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/MathCommands.java @@ -33,7 +33,8 @@ public enum Command { INV_LOG, NATURAL_LOG, INV_NAT_LOG, - RANDOM_NUM + RANDOM_NUM, + CLEAR } static { @@ -59,11 +60,17 @@ public enum Command { commandMap.put("Square", Command.SQUARE); commandMap.put("Inverse", Command.INVERSE); commandMap.put("Varexp", Command.VAR_EXP); + commandMap.put("Power", Command.VAR_EXP); + commandMap.put("Pow", Command.VAR_EXP); + commandMap.put("Exponent", Command.VAR_EXP); + commandMap.put("Exp", Command.VAR_EXP); commandMap.put("Signflip", Command.FLIP_SIGN); + commandMap.put("Flipsign", Command.FLIP_SIGN); commandMap.put("Help", Command.HELP); commandMap.put("Return", Command.RETURN); commandMap.put("Factorial", Command.FACTORIAL); commandMap.put("Random", Command.RANDOM_NUM); + commandMap.put("Clear", Command.CLEAR); // Fill map with all the above commands, but in lower case and upper case (ie CLEAR/Clear/clear all will work) for (Entry i : commandMap.entrySet()) { tempMap.put(i.getKey().toLowerCase(), i.getValue()); @@ -275,6 +282,10 @@ public static void run(Command cmd, ArrayList args) { } fullPrompt(); return; + case CLEAR: + MainApplication.calc.clearDisplay(); + reprompt(); + return; default: fullPrompt(); return; diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/Util.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/Util.java index a8cdd272..500826c7 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/Util.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/Util.java @@ -55,7 +55,7 @@ public static double inverseNaturalLog(double num) { public static int factorial(int n) { - return (n == 1 || n == 0) ? 1 : n * factorial(n - 1); + return (n == 1 || n == 0) ? 1 : n * factorial(n - 1); } public static Double getDoubleInput(String prompt) { From a462fec80641d19d91a3938ec26da7ccc7095089 Mon Sep 17 00:00:00 2001 From: Adam Bennett Date: Sat, 8 Feb 2020 11:26:09 -0500 Subject: [PATCH 10/22] cleaned up logic, added some trig commands --- .../Calculator/Calculator.java | 35 ++-- .../Utilities/ConsoleCommands.java | 55 +++--- .../Utilities/DisplayModeCommands.java | 40 ++-- .../Utilities/MathCommands.java | 182 ++++++++++-------- .../scientificcalculator/Utilities/Util.java | 19 +- 5 files changed, 179 insertions(+), 152 deletions(-) diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Calculator/Calculator.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Calculator/Calculator.java index 6094a61c..7c70c7c7 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Calculator/Calculator.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Calculator/Calculator.java @@ -8,7 +8,6 @@ public class Calculator private float storedValue; private boolean allowNegative; private boolean isError; - private boolean displayingStoredVal; private DisplayMode numMode; private TrigDisplayMode trigMode; private String display; @@ -38,7 +37,6 @@ public Calculator(int displayVal, int storedVal, boolean allowNegatives, Display this.storedValue = storedVal; this.allowNegative = allowNegatives; this.isError = false; - this.displayingStoredVal = false; this.display = ""; this.trigMode = trigMode; this.numMode = numMode; @@ -51,14 +49,7 @@ public void updateDisplay() { return; } - float numToDisplay; - if (this.displayingStoredVal) { - numToDisplay = this.storedValue; - } - else { - numToDisplay = this.displayValue; - } - + float numToDisplay = this.displayValue; switch (this.numMode) { case BINARY: this.display = "" + Integer.toBinaryString((int)numToDisplay); @@ -71,11 +62,11 @@ public void updateDisplay() { return; } - if (!this.allowNegative && numToDisplay < 0) { - numToDisplay = 0; + if (!this.allowNegative && numToDisplay < 0.0f) { + numToDisplay = 0.0f; } - this.display = "" + numToDisplay; + this.display = "" + (float)numToDisplay; } @@ -191,6 +182,20 @@ public void invTangent() { this.displayValue = Util.toRadians(this.displayValue); } } + + public void log() { + this.displayValue = Util.log(this.displayValue); + if (this.trigMode == TrigDisplayMode.RADIANS) { + this.displayValue = Util.toRadians(this.displayValue); + } + } + + public void invNatLog() { + this.displayValue = Util.inverseNaturalLog(this.displayValue); + if (this.trigMode == TrigDisplayMode.RADIANS) { + this.displayValue = Util.toRadians(this.displayValue); + } + } // END MATH /////////////////////////////////////////////////////////////////////////////////////////////// @@ -278,8 +283,8 @@ public void setStoredVal(int newStoredVal) { this.storedValue = newStoredVal; } - public void incStoredVal(int incrementAmt) { - this.storedValue += incrementAmt; + public void incStoredVal(float inc) { + this.storedValue += inc; } public void setDisplayMode(DisplayMode newMode) { diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/ConsoleCommands.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/ConsoleCommands.java index 267c7589..de36b3aa 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/ConsoleCommands.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/ConsoleCommands.java @@ -3,7 +3,7 @@ import java.util.*; import java.util.Map.Entry; -import com.zipcodewilmington.scientificcalculator.Application.*; +import com.zipcodewilmington.scientificcalculator.Application.MainApplication; public class ConsoleCommands { @@ -63,10 +63,13 @@ public static void fullPrompt() { } public static void runCommand(ArrayList args) { - if (commandMap != null && commandMap.containsKey(args.get(0))) { - run(commandMap.get(args.get(0)), args); + if (commandMap != null && commandMap.containsKey(args.get(0).toLowerCase())) { + run(commandMap.get(args.get(0).toLowerCase()), args); } - else if (args.size() > 1 && args.get(0).equals("best") && args.get(1).equals("programmer")) { + else if (MathCommands.commandExists(args.get(0).toLowerCase())) { + MathCommands.runCommand(args); + } + else if (args.size() > 1 && args.get(0).toLowerCase().equals("best") && args.get(1).toLowerCase().equals("programmer")) { Util.prln("Nobles"); fullPrompt(); } @@ -156,12 +159,10 @@ public static void run(Command cmd, ArrayList args) { run(Command.DISPLAY, null); return; case HELP: - Util.prln("Printing a list of all available commands: "); + Util.prln("Printing a list of all available commands in this menu: "); ArrayList uniques = new ArrayList<>(); - for (Entry i : commandMap.entrySet()) { - if (!uniques.contains(i.getKey().toUpperCase())) { - uniques.add(i.getKey().toUpperCase()); - } + for (Entry i : commandMap.entrySet()) { + uniques.add(i.getKey().toUpperCase()); } Collections.sort(uniques); for (String s : uniques) { @@ -185,7 +186,7 @@ public static void run(Command cmd, ArrayList args) { case STORE: if (args.size() > 1) { try { - int inc = Integer.parseInt(args.get(1)); + float inc = Float.parseFloat(args.get(1)); MainApplication.calc.incStoredVal(inc); Util.prln("Stored " + inc + " in memory"); } catch (NumberFormatException e) { @@ -234,28 +235,18 @@ public static void run(Command cmd, ArrayList args) { static { commandMap = new HashMap<>(); - Map tempMap = new HashMap<>(); - commandMap.put("Clear", Command.CLEAR); - commandMap.put("Help", Command.HELP); - commandMap.put("Info", Command.INFO); - commandMap.put("Math", Command.MATH); - commandMap.put("Recall", Command.RECALL); - commandMap.put("Reset", Command.RESET); - commandMap.put("Store", Command.STORE); - commandMap.put("Switchmode", Command.SWITCH_DISP); - commandMap.put("Switchtrig", Command.SWITCH_DISP_TRIG); - commandMap.put("Togglenegative", Command.TOGGLE_NEGATIVE); - commandMap.put("Display", Command.DISPLAY); - commandMap.put("Exit", Command.EXIT); - - // Fill map with all the above commands, but in lower case and upper case (ie CLEAR/Clear/clear all will work) - for (Entry i : commandMap.entrySet()) { - tempMap.put(i.getKey().toLowerCase(), i.getValue()); - tempMap.put(i.getKey().toUpperCase(), i.getValue()); - } - for (Entry i : tempMap.entrySet()) { - commandMap.put(i.getKey(), i.getValue()); - } + commandMap.put("clear", Command.CLEAR); + commandMap.put("help", Command.HELP); + commandMap.put("info", Command.INFO); + commandMap.put("math", Command.MATH); + commandMap.put("recall", Command.RECALL); + commandMap.put("reset", Command.RESET); + commandMap.put("store", Command.STORE); + commandMap.put("switchmode", Command.SWITCH_DISP); + commandMap.put("switchtrig", Command.SWITCH_DISP_TRIG); + commandMap.put("togglenegative", Command.TOGGLE_NEGATIVE); + commandMap.put("display", Command.DISPLAY); + commandMap.put("exit", Command.EXIT); } } diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/DisplayModeCommands.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/DisplayModeCommands.java index d87c2667..42404c9c 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/DisplayModeCommands.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/DisplayModeCommands.java @@ -20,29 +20,9 @@ public enum Mode { HELP } - static { - commandMap = new HashMap<>(); - Map tempMap = new HashMap<>(); - commandMap.put("Binary", Mode.BINARY); - commandMap.put("Octal", Mode.OCTAL); - commandMap.put("Decimal", Mode.DECIMAL); - commandMap.put("Hexadecimal", Mode.HEXADECIMAL); - commandMap.put("Return", Mode.RETURN); - commandMap.put("Help", Mode.HELP); - - // Fill map with all the above commands, but in lower case and upper case (ie CLEAR/Clear/clear all will work) - for (Entry i : commandMap.entrySet()) { - tempMap.put(i.getKey().toLowerCase(), i.getValue()); - tempMap.put(i.getKey().toUpperCase(), i.getValue()); - } - for (Entry i : tempMap.entrySet()) { - commandMap.put(i.getKey(), i.getValue()); - } - } - public static void runCommand(ArrayList args) { - if (commandMap != null && commandMap.containsKey(args.get(0))) { - run(commandMap.get(args.get(0))); + if (commandMap != null && commandMap.containsKey(args.get(0).toLowerCase())) { + run(commandMap.get(args.get(0).toLowerCase())); } else { run(Mode.RETURN); @@ -101,10 +81,8 @@ public static void run(Mode cmd) { case HELP: Util.prln("Printing a list of all available commands in this menu: "); ArrayList uniques = new ArrayList<>(); - for (Entry i : commandMap.entrySet()) { - if (!uniques.contains(i.getKey().toUpperCase())) { - uniques.add(i.getKey().toUpperCase()); - } + for (Entry i : commandMap.entrySet()) { + uniques.add(i.getKey().toUpperCase()); } Collections.sort(uniques); for (String s : uniques) { @@ -118,4 +96,14 @@ public static void run(Mode cmd) { } } + static { + commandMap = new HashMap<>(); + commandMap.put("binary", Mode.BINARY); + commandMap.put("octal", Mode.OCTAL); + commandMap.put("decimal", Mode.DECIMAL); + commandMap.put("hexadecimal", Mode.HEXADECIMAL); + commandMap.put("return", Mode.RETURN); + commandMap.put("help", Mode.HELP); + } + } diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/MathCommands.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/MathCommands.java index b846e459..f1fec34e 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/MathCommands.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/MathCommands.java @@ -4,7 +4,7 @@ import java.util.Map.Entry; import java.util.concurrent.ThreadLocalRandom; -import com.zipcodewilmington.scientificcalculator.Application.*; +import com.zipcodewilmington.scientificcalculator.Application.MainApplication; public class MathCommands { @@ -34,56 +34,21 @@ public enum Command { NATURAL_LOG, INV_NAT_LOG, RANDOM_NUM, - CLEAR + CLEAR, + TOGGLE_NEGATIVE, + DISPLAY } - - static { - commandMap = new HashMap<>(); - Map tempMap = new HashMap<>(); - commandMap.put("Sin", Command.SINE); - commandMap.put("Cos", Command.COSINE); - commandMap.put("Tan", Command.TANGENT); - commandMap.put("Arcsin", Command.INV_SINE); - commandMap.put("Arccos", Command.INV_COSINE); - commandMap.put("Arctan", Command.INV_TANGENT); - commandMap.put("Log", Command.LOG); - commandMap.put("InvLog", Command.INV_LOG); - commandMap.put("NaturalLog", Command.NATURAL_LOG); - commandMap.put("InvNatLog", Command.INV_NAT_LOG); - commandMap.put("Add", Command.ADD); - commandMap.put("Subtract", Command.SUBTRACT); - commandMap.put("Minus", Command.SUBTRACT); - commandMap.put("Mult", Command.MULTIPLY); - commandMap.put("Multiply", Command.MULTIPLY); - commandMap.put("Divide", Command.DIVIDE); - commandMap.put("Sqrt", Command.SQRT); - commandMap.put("Square", Command.SQUARE); - commandMap.put("Inverse", Command.INVERSE); - commandMap.put("Varexp", Command.VAR_EXP); - commandMap.put("Power", Command.VAR_EXP); - commandMap.put("Pow", Command.VAR_EXP); - commandMap.put("Exponent", Command.VAR_EXP); - commandMap.put("Exp", Command.VAR_EXP); - commandMap.put("Signflip", Command.FLIP_SIGN); - commandMap.put("Flipsign", Command.FLIP_SIGN); - commandMap.put("Help", Command.HELP); - commandMap.put("Return", Command.RETURN); - commandMap.put("Factorial", Command.FACTORIAL); - commandMap.put("Random", Command.RANDOM_NUM); - commandMap.put("Clear", Command.CLEAR); - // Fill map with all the above commands, but in lower case and upper case (ie CLEAR/Clear/clear all will work) - for (Entry i : commandMap.entrySet()) { - tempMap.put(i.getKey().toLowerCase(), i.getValue()); - tempMap.put(i.getKey().toUpperCase(), i.getValue()); - } - for (Entry i : tempMap.entrySet()) { - commandMap.put(i.getKey(), i.getValue()); + + public static boolean commandExists(String cmd) { + if (commandMap != null && commandMap.containsKey(cmd.toLowerCase())) { + return true; } + return false; } - + public static void runCommand(ArrayList args) { - if (commandMap != null && commandMap.containsKey(args.get(0))) { - run(commandMap.get(args.get(0)), args); + if (commandMap != null && commandMap.containsKey(args.get(0).toLowerCase())) { + run(commandMap.get(args.get(0).toLowerCase()), args); } else { run(Command.RETURN, args); @@ -116,15 +81,22 @@ public static void fullPrompt() { } MathCommands.runCommand(argus); } + + + private static void reprompt() { + Util.prln("Value: " + MainApplication.calc.getDisplay()); + fullPrompt(); + return; + } public static void run(Command cmd, ArrayList args) { switch (cmd) { case ADD: if (args.size() > 1) { - int sum = 0; + float sum = 0; try { for (int i = 1; i < args.size(); i++) { - sum += Integer.parseInt(args.get(i)); + sum += Float.parseFloat(args.get(i)); } MainApplication.calc.add(sum); reprompt(); @@ -138,13 +110,15 @@ public static void run(Command cmd, ArrayList args) { } fullPrompt(); return; - case COSINE: - //TODO + case COSINE: + MainApplication.calc.cosine(); + reprompt(); + return; case DIVIDE: if (args.size() > 1) { try { for (int i = 1; i < args.size(); i++) { - int denom = Integer.parseInt(args.get(i)); + float denom = Float.parseFloat(args.get(i)); MainApplication.calc.div(denom); } reprompt(); @@ -169,10 +143,8 @@ public static void run(Command cmd, ArrayList args) { case HELP: Util.prln("Printing a list of all available commands in this menu: "); ArrayList uniques = new ArrayList<>(); - for (Entry i : commandMap.entrySet()) { - if (!uniques.contains(i.getKey().toUpperCase())) { - uniques.add(i.getKey().toUpperCase()); - } + for (Entry i : commandMap.entrySet()) { + uniques.add(i.getKey().toUpperCase()); } Collections.sort(uniques); for (String s : uniques) { @@ -185,22 +157,30 @@ public static void run(Command cmd, ArrayList args) { reprompt(); break; case INV_COSINE: - //TODO - case INV_LOG: - //TODO + MainApplication.calc.invCosine(); + reprompt(); + return; case INV_NAT_LOG: - //TODO + MainApplication.calc.invNatLog(); + reprompt(); + return; case INV_SINE: - //TODO + MainApplication.calc.invSine(); + reprompt(); + return; case INV_TANGENT: - //TODO + MainApplication.calc.invTangent(); + reprompt(); + return; case LOG: - //TODO + MainApplication.calc.log(); + reprompt(); + return; case MULTIPLY: if (args.size() > 1) { try { for (int i = 1; i < args.size(); i++) { - MainApplication.calc.mult(Integer.parseInt(args.get(i))); + MainApplication.calc.mult(Float.parseFloat(args.get(i))); } reprompt(); } catch (NumberFormatException e) { @@ -213,8 +193,6 @@ public static void run(Command cmd, ArrayList args) { } fullPrompt(); return; - case NATURAL_LOG: - //TODO case RANDOM_NUM: if (args.size() > 2 && args.size() < 4) { try { @@ -225,7 +203,7 @@ public static void run(Command cmd, ArrayList args) { fullPrompt(); } catch (NumberFormatException e) { //e.printStackTrace(); - Util.prln("That's not a number..."); + Util.prln("Only integers accepted as arguments."); } } else { @@ -237,7 +215,9 @@ public static void run(Command cmd, ArrayList args) { ConsoleCommands.fullPrompt(); return; case SINE: - //TODO + MainApplication.calc.sine(); + reprompt(); + return; case SQRT: MainApplication.calc.sqRt(); reprompt(); @@ -250,7 +230,7 @@ public static void run(Command cmd, ArrayList args) { if (args.size() > 1) { try { for (int i = 1; i < args.size(); i++) { - MainApplication.calc.subtract(Integer.parseInt(args.get(i))); + MainApplication.calc.subtract(Float.parseFloat(args.get(i))); } reprompt(); } catch (NumberFormatException e) { @@ -264,12 +244,14 @@ public static void run(Command cmd, ArrayList args) { fullPrompt(); return; case TANGENT: - //TODO + MainApplication.calc.tangent(); + reprompt(); + return; case VAR_EXP: if (args.size() > 1) { try { for (int i = 1; i < args.size(); i++) { - MainApplication.calc.pow(Integer.parseInt(args.get(i))); + MainApplication.calc.pow(Float.parseFloat(args.get(i))); } reprompt(); } catch (NumberFormatException e) { @@ -286,16 +268,66 @@ public static void run(Command cmd, ArrayList args) { MainApplication.calc.clearDisplay(); reprompt(); return; + case TOGGLE_NEGATIVE: + MainApplication.calc.toggleAllowNegatives(); + if (MainApplication.calc.allowingNegative()) { + Util.prln("Calculator is allowing negative numbers again."); + } + else { + Util.prln("Calculator will no longer show negative numbers."); + } + reprompt(); + return; + case DISPLAY: + Util.prln("Value: " + MainApplication.calc.getDisplay()); + fullPrompt(); + return; + case INV_LOG: + //TODO + case NATURAL_LOG: + //TODO default: fullPrompt(); return; } } + - private static void reprompt() { - Util.prln("Value: " + MainApplication.calc.getDisplay()); - fullPrompt(); - return; + static { + commandMap = new HashMap<>(); + commandMap.put("sin", Command.SINE); + commandMap.put("cos", Command.COSINE); + commandMap.put("tan", Command.TANGENT); + commandMap.put("arcsin", Command.INV_SINE); + commandMap.put("arccos", Command.INV_COSINE); + commandMap.put("arctan", Command.INV_TANGENT); + commandMap.put("log", Command.LOG); + commandMap.put("invlog", Command.INV_LOG); + commandMap.put("naturallog", Command.NATURAL_LOG); + commandMap.put("invnatlog", Command.INV_NAT_LOG); + commandMap.put("add", Command.ADD); + commandMap.put("subtract", Command.SUBTRACT); + commandMap.put("minus", Command.SUBTRACT); + commandMap.put("mult", Command.MULTIPLY); + commandMap.put("multiply", Command.MULTIPLY); + commandMap.put("divide", Command.DIVIDE); + commandMap.put("sqrt", Command.SQRT); + commandMap.put("square", Command.SQUARE); + commandMap.put("inverse", Command.INVERSE); + commandMap.put("varexp", Command.VAR_EXP); + commandMap.put("power", Command.VAR_EXP); + commandMap.put("pow", Command.VAR_EXP); + commandMap.put("exponent", Command.VAR_EXP); + commandMap.put("exp", Command.VAR_EXP); + commandMap.put("signflip", Command.FLIP_SIGN); + commandMap.put("flipsign", Command.FLIP_SIGN); + commandMap.put("help", Command.HELP); + commandMap.put("return", Command.RETURN); + commandMap.put("factorial", Command.FACTORIAL); + commandMap.put("random", Command.RANDOM_NUM); + commandMap.put("clear", Command.CLEAR); + commandMap.put("togglenegative", Command.TOGGLE_NEGATIVE); + commandMap.put("display", Command.DISPLAY); } } diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/Util.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/Util.java index 500826c7..8f40ad29 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/Util.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/Util.java @@ -45,18 +45,29 @@ public static float toRadians(float num) { return (float) Math.toRadians(num); } - public static double log(double num) { - return Math.log(num); + public static float log(float num) { + return (float) Math.log(num); } - public static double inverseNaturalLog(double num) { - return Math.expm1(num) + 1; + public static float inverseNaturalLog(float num) { + return (float) (Math.expm1(num) + 1); } public static int factorial(int n) { return (n == 1 || n == 0) ? 1 : n * factorial(n - 1); } + + public static Float getFloatInput(String prompt) { + Scanner scanner = new Scanner(System.in); + Util.println(prompt); + String userInput = scanner.nextLine(); + try { + return Float.parseFloat(userInput); + } catch (NumberFormatException e) { e.printStackTrace(); } + return 0.0f; + } + public static Double getDoubleInput(String prompt) { Scanner scanner = new Scanner(System.in); From 281f6605c8add4029fe1f122ee1bc69144d3f5f4 Mon Sep 17 00:00:00 2001 From: Matthew Ascone Date: Sat, 8 Feb 2020 13:47:29 -0500 Subject: [PATCH 11/22] Changed store functionality --- .../Utilities/ConsoleCommands.java | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/ConsoleCommands.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/ConsoleCommands.java index de36b3aa..d5ae237b 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/ConsoleCommands.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/ConsoleCommands.java @@ -184,19 +184,8 @@ public static void run(Command cmd, ArrayList args) { run(Command.DISPLAY, null); return; case STORE: - if (args.size() > 1) { - try { - float inc = Float.parseFloat(args.get(1)); - MainApplication.calc.incStoredVal(inc); - Util.prln("Stored " + inc + " in memory"); - } catch (NumberFormatException e) { - //e.printStackTrace(); - Util.prln("That's not a number..."); - } - } - else { - Util.prln("Nothing to store. Please enter a second argument!"); - } + MainApplication.calc.incStoredVal(MainApplication.calc.getDisplayVal()); + Util.prln("Stored " + MainApplication.calc.getDisplayVal() + " in memory"); fullPrompt(); return; case SWITCH_DISP_TRIG: From 78edf6bc7a04e28ba3d9c735057905644aa98dcc Mon Sep 17 00:00:00 2001 From: Nyoxide Date: Sat, 8 Feb 2020 17:35:32 -0500 Subject: [PATCH 12/22] new tests --- .../Calculator/Calculator.java | 11 ++++++-- .../Calculator/TestCalculator.java | 27 ++++++++++++++++++- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Calculator/Calculator.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Calculator/Calculator.java index 7c70c7c7..728923cd 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Calculator/Calculator.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Calculator/Calculator.java @@ -61,6 +61,11 @@ public void updateDisplay() { this.display = "" + Integer.toOctalString((int)numToDisplay); return; } + + if (this.trigMode == TrigDisplayMode.RADIANS) { + this.display = "" + Math.toRadians(numToDisplay); + return; + } if (!this.allowNegative && numToDisplay < 0.0f) { numToDisplay = 0.0f; @@ -288,11 +293,13 @@ public void incStoredVal(float inc) { } public void setDisplayMode(DisplayMode newMode) { - this.numMode = newMode; + this.numMode = newMode; + updateDisplay(); } public void setTrigMode(TrigDisplayMode newMode) { - this.trigMode = newMode; + this.trigMode = newMode; + updateDisplay(); } public void toggleAllowNegatives() { diff --git a/src/test/java/com/zipcodewilmington/scientific_calculator/Calculator/TestCalculator.java b/src/test/java/com/zipcodewilmington/scientific_calculator/Calculator/TestCalculator.java index a8a4ccd2..17abfe33 100644 --- a/src/test/java/com/zipcodewilmington/scientific_calculator/Calculator/TestCalculator.java +++ b/src/test/java/com/zipcodewilmington/scientific_calculator/Calculator/TestCalculator.java @@ -1,6 +1,7 @@ package com.zipcodewilmington.scientific_calculator.Calculator; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; import org.junit.Test; @@ -61,6 +62,30 @@ public void TestAllowNegatives() { assertEquals(-100, (int)calc.getDisplayVal()); } - + @Test + public void TestDisplayModes() { + Calculator calc = new Calculator(); + calc.add(4); + calc.setDisplayMode(DisplayMode.BINARY); + assertEquals("100", calc.getDisplay()); + calc.setDisplayMode(DisplayMode.DECIMAL); + assertNotEquals("100", calc.getDisplay()); + } + + @Test + public void TestTrigModes() { + Calculator calc = new Calculator(); + calc.add(180); + calc.setTrigMode(TrigDisplayMode.RADIANS); + assertEquals("3.141592653589793", calc.getDisplay()); + } + + @Test + public void TestSqrt() { + Calculator calc = new Calculator(); + calc.add(9); + calc.sqRt(); + assertEquals(3, (int)calc.getDisplayVal()); + } } From 92c2c26e144fb95218e9f89b5238070a740e00e9 Mon Sep 17 00:00:00 2001 From: asconem Date: Sat, 8 Feb 2020 18:02:49 -0500 Subject: [PATCH 13/22] Added more passing test cases --- .../Calculator/TestCalculator.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/test/java/com/zipcodewilmington/scientific_calculator/Calculator/TestCalculator.java b/src/test/java/com/zipcodewilmington/scientific_calculator/Calculator/TestCalculator.java index 17abfe33..1f174233 100644 --- a/src/test/java/com/zipcodewilmington/scientific_calculator/Calculator/TestCalculator.java +++ b/src/test/java/com/zipcodewilmington/scientific_calculator/Calculator/TestCalculator.java @@ -88,4 +88,28 @@ public void TestSqrt() { assertEquals(3, (int)calc.getDisplayVal()); } + @Test + public void TestSquare() { + Calculator calc = new Calculator(); + calc.add(9); + calc.square(); + assertEquals(81, (int)calc.getDisplayVal()); + } + + @Test + public void TestPow() { + Calculator calc = new Calculator(); + calc.add(3); + calc.pow(3); + assertEquals(27, (int)calc.getDisplayVal()); + } + + @Test + public void FlipSignTest() { + Calculator calc = new Calculator(); + calc.add(5); + calc.flipSign(); + assertEquals(-5, (int)calc.getDisplayVal()); + } + } From 0e8b384d8b4c41779f6efc43963782c54ba870ef Mon Sep 17 00:00:00 2001 From: Adam Bennett Date: Sat, 8 Feb 2020 22:43:07 -0500 Subject: [PATCH 14/22] new test cases --- .../Calculator/TestCalculator.java | 185 ++++++++++++++++-- 1 file changed, 173 insertions(+), 12 deletions(-) diff --git a/src/test/java/com/zipcodewilmington/scientific_calculator/Calculator/TestCalculator.java b/src/test/java/com/zipcodewilmington/scientific_calculator/Calculator/TestCalculator.java index 1f174233..484910ea 100644 --- a/src/test/java/com/zipcodewilmington/scientific_calculator/Calculator/TestCalculator.java +++ b/src/test/java/com/zipcodewilmington/scientific_calculator/Calculator/TestCalculator.java @@ -1,7 +1,6 @@ package com.zipcodewilmington.scientific_calculator.Calculator; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotEquals; import org.junit.Test; @@ -61,16 +60,6 @@ public void TestAllowNegatives() { assertEquals("-100.0", calc.getDisplay()); assertEquals(-100, (int)calc.getDisplayVal()); } - - @Test - public void TestDisplayModes() { - Calculator calc = new Calculator(); - calc.add(4); - calc.setDisplayMode(DisplayMode.BINARY); - assertEquals("100", calc.getDisplay()); - calc.setDisplayMode(DisplayMode.DECIMAL); - assertNotEquals("100", calc.getDisplay()); - } @Test public void TestTrigModes() { @@ -78,6 +67,11 @@ public void TestTrigModes() { calc.add(180); calc.setTrigMode(TrigDisplayMode.RADIANS); assertEquals("3.141592653589793", calc.getDisplay()); + calc.setDisplayVal(27); + calc.setTrigMode(TrigDisplayMode.RADIANS); + assertEquals("0.47123889803846897", calc.getDisplay()); + calc.setTrigMode(TrigDisplayMode.DEGREES); + assertEquals(27.0, calc.getDisplayVal(), 0.001); } @Test @@ -105,11 +99,178 @@ public void TestPow() { } @Test - public void FlipSignTest() { + public void TestFlipSign() { Calculator calc = new Calculator(); calc.add(5); calc.flipSign(); assertEquals(-5, (int)calc.getDisplayVal()); } + + @Test + public void TestFactorial() { + Calculator calc = new Calculator(); + calc.setDisplayVal(1); + calc.factorial(); + assertEquals(1, (int)calc.getDisplayVal()); + + calc.setDisplayVal(2); + calc.factorial(); + assertEquals(2, (int)calc.getDisplayVal()); + + calc.setDisplayVal(3); + calc.factorial(); + assertEquals(6, (int)calc.getDisplayVal()); + + calc.setDisplayVal(4); + calc.factorial(); + assertEquals(24, (int)calc.getDisplayVal()); + + calc.setDisplayVal(5); + calc.factorial(); + assertEquals(120, (int)calc.getDisplayVal()); + + calc.setDisplayVal(6); + calc.factorial(); + assertEquals(720, (int)calc.getDisplayVal()); + + calc.setDisplayVal(7); + calc.factorial(); + assertEquals(5040, (int)calc.getDisplayVal()); + + calc.setDisplayVal(8); + calc.factorial(); + assertEquals(40320, (int)calc.getDisplayVal()); + + calc.setDisplayVal(9); + calc.factorial(); + assertEquals(362880, (int)calc.getDisplayVal()); + + calc.setDisplayVal(10); + calc.factorial(); + assertEquals(3628800, (int)calc.getDisplayVal()); + + calc.setDisplayVal(11); + calc.factorial(); + assertEquals(39916800, (int)calc.getDisplayVal()); + + calc.setDisplayVal(12); + calc.factorial(); + assertEquals(479001600, (int)calc.getDisplayVal()); + + calc.setDisplayVal(13); + calc.factorial(); + assertEquals((int)Float.MAX_VALUE, (int)calc.getDisplayVal()); + + calc.setDisplayVal(14); + calc.factorial(); + assertEquals((int)Float.MAX_VALUE, (int)calc.getDisplayVal()); + + calc.setDisplayVal(15); + calc.factorial(); + assertEquals((int)Float.MAX_VALUE, (int)calc.getDisplayVal()); + + calc.setDisplayVal(9999); + calc.factorial(); + assertEquals((int)Float.MAX_VALUE, (int)calc.getDisplayVal()); + } + + @Test + public void TestSine() { + Calculator calc = new Calculator(); + calc.add(10.0f); + calc.sin(); + assertEquals(-0.544021, calc.getDisplayVal(), 0.001); + } + + + @Test + public void TestCosine() { + Calculator calc = new Calculator(); + calc.add(10.0f); + calc.cosine(); + assertEquals(-0.839072, calc.getDisplayVal(), 0.001); + } + + @Test + public void TestTangent() { + Calculator calc = new Calculator(); + calc.add(10.0f); + calc.tangent(); + assertEquals(0.648361, calc.getDisplayVal(), 0.001); + } + + @Test + public void TestInvSin() { + Calculator calc = new Calculator(); + calc.add(0.33f); + calc.invSin(); + assertEquals(0.336304, calc.getDisplayVal(), 0.001); + } + + @Test + public void TestInvCosine() { + Calculator calc = new Calculator(); + calc.add(0.33f); + calc.invCosine(); + assertEquals(1.234493, calc.getDisplayVal(), 0.001); + } + + @Test + public void TestInvTan() { + Calculator calc = new Calculator(); + calc.add(0.33f); + calc.invTangent(); + assertEquals(0.318748, calc.getDisplayVal(), 0.001); + } + + @Test + public void TestLog() { + Calculator calc = new Calculator(); + calc.add(10); + calc.log(); + assertEquals(2.302585092994045684, calc.getDisplayVal(), 0.001); + } + + @Test + public void TestInvNatLog() { + Calculator calc = new Calculator(); + calc.add(10); + calc.invNatLog(); + assertEquals(22026.465, calc.getDisplayVal(), 0.001); + + calc.setDisplayVal(7); + calc.invNatLog(); + assertEquals(1096.633, calc.getDisplayVal(), 0.001); + } + + @Test + public void TestBinaryMode() { + Calculator calc = new Calculator(); + calc.add(4); + calc.setDisplayMode(DisplayMode.BINARY); + assertEquals("100", calc.getDisplay()); + calc.setDisplayMode(DisplayMode.DECIMAL); + assertEquals("4.0", calc.getDisplay()); + } + + @Test + public void TestOctalMode() { + Calculator calc = new Calculator(); + calc.add(12); + calc.setDisplayMode(DisplayMode.OCTAL); + assertEquals("14", calc.getDisplay()); + calc.setDisplayMode(DisplayMode.DECIMAL); + assertEquals("12.0", calc.getDisplay()); + } + + @Test + public void TestHexMode() { + Calculator calc = new Calculator(); + calc.add(22); + calc.setDisplayMode(DisplayMode.HEXADECIMAL); + assertEquals(Integer.toHexString(22), calc.getDisplay()); + calc.setDisplayMode(DisplayMode.DECIMAL); + assertEquals("22.0", calc.getDisplay()); + } } From 81b6279f178b485e5ba5c9cd632db335915e632b Mon Sep 17 00:00:00 2001 From: Adam Bennett Date: Sat, 8 Feb 2020 22:43:23 -0500 Subject: [PATCH 15/22] fixed trig mode bug --- .../Calculator/Calculator.java | 45 ++++++------------- 1 file changed, 13 insertions(+), 32 deletions(-) diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Calculator/Calculator.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Calculator/Calculator.java index 728923cd..3ba30dda 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Calculator/Calculator.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Calculator/Calculator.java @@ -55,7 +55,7 @@ public void updateDisplay() { this.display = "" + Integer.toBinaryString((int)numToDisplay); return; case HEXADECIMAL: - this.display = "" + Float.toHexString(numToDisplay); + this.display = "" + Integer.toHexString((int)numToDisplay); return; case OCTAL: this.display = "" + Integer.toOctalString((int)numToDisplay); @@ -143,63 +143,44 @@ public void flipSign() { } public void factorial() { - this.displayValue = Util.factorial((int) this.displayValue); + if (this.displayValue < 13) { + this.displayValue = Util.factorial((int) this.displayValue); + } + else { + this.displayValue = Float.MAX_VALUE; + } } - public void sine() { - this.displayValue = Util.sine(this.displayValue); - if (this.trigMode == TrigDisplayMode.RADIANS) { - this.displayValue = Util.toRadians(this.displayValue); - } + public void sin() { + this.displayValue = Util.sine(this.displayValue); } public void cosine() { - this.displayValue = Util.cosine(this.displayValue); - if (this.trigMode == TrigDisplayMode.RADIANS) { - this.displayValue = Util.toRadians(this.displayValue); - } + this.displayValue = Util.cosine(this.displayValue); } public void tangent() { - this.displayValue = Util.tangent(this.displayValue); - if (this.trigMode == TrigDisplayMode.RADIANS) { - this.displayValue = Util.toRadians(this.displayValue); - } + this.displayValue = Util.tangent(this.displayValue); } - public void invSine() { - this.displayValue = Util.invSine(this.displayValue); - if (this.trigMode == TrigDisplayMode.RADIANS) { - this.displayValue = Util.toRadians(this.displayValue); - } + public void invSin() { + this.displayValue = Util.invSine(this.displayValue); } public void invCosine() { this.displayValue = Util.invCosine(this.displayValue); - if (this.trigMode == TrigDisplayMode.RADIANS) { - this.displayValue = Util.toRadians(this.displayValue); - } } public void invTangent() { this.displayValue = Util.invTangent(this.displayValue); - if (this.trigMode == TrigDisplayMode.RADIANS) { - this.displayValue = Util.toRadians(this.displayValue); - } } public void log() { this.displayValue = Util.log(this.displayValue); - if (this.trigMode == TrigDisplayMode.RADIANS) { - this.displayValue = Util.toRadians(this.displayValue); - } } public void invNatLog() { this.displayValue = Util.inverseNaturalLog(this.displayValue); - if (this.trigMode == TrigDisplayMode.RADIANS) { - this.displayValue = Util.toRadians(this.displayValue); - } } // END MATH /////////////////////////////////////////////////////////////////////////////////////////////// From a70136ad93a1855cda301c290601cccd61b99aab Mon Sep 17 00:00:00 2001 From: Adam Bennett Date: Sat, 8 Feb 2020 22:43:40 -0500 Subject: [PATCH 16/22] cleaned up commands logic --- .../Utilities/ConsoleCommands.java | 3 + .../Utilities/DisplayModeCommands.java | 7 ++ .../Utilities/MathCommands.java | 86 +++++++++++++++---- 3 files changed, 80 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/ConsoleCommands.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/ConsoleCommands.java index d5ae237b..1225325c 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/ConsoleCommands.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/ConsoleCommands.java @@ -69,6 +69,9 @@ public static void runCommand(ArrayList args) { else if (MathCommands.commandExists(args.get(0).toLowerCase())) { MathCommands.runCommand(args); } + else if (DisplayModeCommands.commandExists(args.get(0).toLowerCase())) { + DisplayModeCommands.runCommand(args); + } else if (args.size() > 1 && args.get(0).toLowerCase().equals("best") && args.get(1).toLowerCase().equals("programmer")) { Util.prln("Nobles"); fullPrompt(); diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/DisplayModeCommands.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/DisplayModeCommands.java index 42404c9c..0fdc7c9e 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/DisplayModeCommands.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/DisplayModeCommands.java @@ -19,6 +19,13 @@ public enum Mode { RETURN, HELP } + + public static boolean commandExists(String cmd) { + if (commandMap != null && commandMap.containsKey(cmd.toLowerCase())) { + return true; + } + return false; + } public static void runCommand(ArrayList args) { if (commandMap != null && commandMap.containsKey(args.get(0).toLowerCase())) { diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/MathCommands.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/MathCommands.java index f1fec34e..c72426a9 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/MathCommands.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/MathCommands.java @@ -36,7 +36,9 @@ public enum Command { RANDOM_NUM, CLEAR, TOGGLE_NEGATIVE, - DISPLAY + DISPLAY, + EXIT, + RANDOM_FLOAT } public static boolean commandExists(String cmd) { @@ -165,7 +167,7 @@ public static void run(Command cmd, ArrayList args) { reprompt(); return; case INV_SINE: - MainApplication.calc.invSine(); + MainApplication.calc.invSin(); reprompt(); return; case INV_TANGENT: @@ -195,27 +197,32 @@ public static void run(Command cmd, ArrayList args) { return; case RANDOM_NUM: if (args.size() > 2 && args.size() < 4) { - try { - int lower = Integer.parseInt(args.get(1)); - int higher = Integer.parseInt(args.get(2)); - int rand = ThreadLocalRandom.current().nextInt(lower, higher + 1); - Util.prln("Randomly generated number (" + lower + "-" + higher + "): " + rand); - fullPrompt(); - } catch (NumberFormatException e) { - //e.printStackTrace(); - Util.prln("Only integers accepted as arguments."); + handleRandomNum(args, new ArrayList()); + } + else if (args.size() > 2) { + ArrayList excluded = new ArrayList<>(); + for (int i = 3; i < args.size(); i++) { + try { + excluded.add(Integer.parseInt(args.get(i))); + } catch (NumberFormatException e) { Util.prln("Expects extra numbers as additional arguments. Numbers after the initial bound arguments will be excluded from random generation."); } } + handleRandomNum(args, excluded); } else { - Util.prln("Wrong amount of arguments! Expecting 2 numbers, lower and upper bound (inclusive)."); + Util.prln("Wrong amount of arguments! Expecting at least 2 numbers - lower and upper bound (inclusive). \n You may also include extra numbers to exclude from random generation."); } fullPrompt(); return; + case RANDOM_FLOAT: + float rand = ThreadLocalRandom.current().nextFloat(); + Util.prln("Randomly generated float (0-1): " + rand); + fullPrompt(); + return; case RETURN: ConsoleCommands.fullPrompt(); return; case SINE: - MainApplication.calc.sine(); + MainApplication.calc.sin(); reprompt(); return; case SQRT: @@ -286,13 +293,56 @@ public static void run(Command cmd, ArrayList args) { //TODO case NATURAL_LOG: //TODO + case EXIT: + System.exit(0); default: fullPrompt(); return; } } - + private static void handleRandomNum(ArrayList args, ArrayList excludedNums) { + int lower; + int higher; + int timeOut = 1000; + boolean generated = false; + try { + lower = Integer.parseInt(args.get(1)); + try { + higher = Integer.parseInt(args.get(2)); + generated = true; + } catch (NumberFormatException e) { + Util.prln("Only integers accepted as arguments."); + lower = 1; + higher = 100; + generated = false; + } + } catch (NumberFormatException e) { + Util.prln("Only integers accepted as arguments."); + lower = 1; + higher = 100; + generated = false; + } + if (generated) { + int rand = ThreadLocalRandom.current().nextInt(lower, higher + 1); + while (excludedNums.contains(rand) && timeOut > 0) { + rand = ThreadLocalRandom.current().nextInt(lower, higher + 1); + timeOut--; + } + Util.prln("Randomly generated number (" + lower + "-" + higher + "): " + rand); + fullPrompt(); + } + else { + int rand = ThreadLocalRandom.current().nextInt(lower, higher + 1); + while (excludedNums.contains(rand) && timeOut > 0) { + rand = ThreadLocalRandom.current().nextInt(lower, higher + 1); + timeOut--; + } + Util.prln("Randomly generated number (1-100)[default]: " + rand); + fullPrompt(); + } + } + static { commandMap = new HashMap<>(); commandMap.put("sin", Command.SINE); @@ -302,8 +352,6 @@ public static void run(Command cmd, ArrayList args) { commandMap.put("arccos", Command.INV_COSINE); commandMap.put("arctan", Command.INV_TANGENT); commandMap.put("log", Command.LOG); - commandMap.put("invlog", Command.INV_LOG); - commandMap.put("naturallog", Command.NATURAL_LOG); commandMap.put("invnatlog", Command.INV_NAT_LOG); commandMap.put("add", Command.ADD); commandMap.put("subtract", Command.SUBTRACT); @@ -328,6 +376,12 @@ public static void run(Command cmd, ArrayList args) { commandMap.put("clear", Command.CLEAR); commandMap.put("togglenegative", Command.TOGGLE_NEGATIVE); commandMap.put("display", Command.DISPLAY); + commandMap.put("exit", Command.EXIT); + commandMap.put("randomfloat", Command.RANDOM_FLOAT); + + // TODO + //commandMap.put("invlog", Command.INV_LOG); + //commandMap.put("naturallog", Command.NATURAL_LOG); } } From 829961d9704699e6257be5f314e6687cba48a6af Mon Sep 17 00:00:00 2001 From: Adam Bennett Date: Sat, 8 Feb 2020 22:55:04 -0500 Subject: [PATCH 17/22] fixed some try/catch logic errors --- .../Utilities/MathCommands.java | 72 ++++++++----------- 1 file changed, 31 insertions(+), 41 deletions(-) diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/MathCommands.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/MathCommands.java index c72426a9..725fc873 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/MathCommands.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/MathCommands.java @@ -96,16 +96,14 @@ public static void run(Command cmd, ArrayList args) { case ADD: if (args.size() > 1) { float sum = 0; - try { - for (int i = 1; i < args.size(); i++) { + for (int i = 1; i < args.size(); i++) { + try { sum += Float.parseFloat(args.get(i)); - } - MainApplication.calc.add(sum); - reprompt(); - } catch (NumberFormatException e) { - //e.printStackTrace(); - Util.prln("That's not a number..."); + } catch (NumberFormatException e) {} } + MainApplication.calc.add(sum); + reprompt(); + return; } else { Util.prln("Not enough arguments!"); @@ -117,17 +115,15 @@ public static void run(Command cmd, ArrayList args) { reprompt(); return; case DIVIDE: - if (args.size() > 1) { - try { - for (int i = 1; i < args.size(); i++) { - float denom = Float.parseFloat(args.get(i)); + if (args.size() > 1) { + for (int i = 1; i < args.size(); i++) { + try { + float denom = Float.parseFloat(args.get(i)); MainApplication.calc.div(denom); - } - reprompt(); - } catch (NumberFormatException e) { - //e.printStackTrace(); - Util.prln("That's not a number..."); - } + } catch (NumberFormatException e) { } + } + reprompt(); + return; } else { Util.prln("Not enough arguments!"); @@ -180,15 +176,13 @@ public static void run(Command cmd, ArrayList args) { return; case MULTIPLY: if (args.size() > 1) { - try { - for (int i = 1; i < args.size(); i++) { + for (int i = 1; i < args.size(); i++) { + try { MainApplication.calc.mult(Float.parseFloat(args.get(i))); - } - reprompt(); - } catch (NumberFormatException e) { - //e.printStackTrace(); - Util.prln("That's not a number..."); + } catch (NumberFormatException e) { } } + reprompt(); + return; } else { Util.prln("Not enough arguments!"); @@ -235,15 +229,13 @@ else if (args.size() > 2) { break; case SUBTRACT: if (args.size() > 1) { - try { - for (int i = 1; i < args.size(); i++) { + for (int i = 1; i < args.size(); i++) { + try { MainApplication.calc.subtract(Float.parseFloat(args.get(i))); - } - reprompt(); - } catch (NumberFormatException e) { - //e.printStackTrace(); - Util.prln("That's not a number..."); - } + } catch (NumberFormatException e) { } + } + reprompt(); + return; } else { Util.prln("Not enough arguments!"); @@ -256,15 +248,13 @@ else if (args.size() > 2) { return; case VAR_EXP: if (args.size() > 1) { - try { - for (int i = 1; i < args.size(); i++) { + for (int i = 1; i < args.size(); i++) { + try { MainApplication.calc.pow(Float.parseFloat(args.get(i))); - } - reprompt(); - } catch (NumberFormatException e) { - //e.printStackTrace(); - Util.prln("That's not a number..."); - } + } catch (NumberFormatException e) { } + } + reprompt(); + return; } else { Util.prln("Not enough arguments!"); From e7765a1afcc42c92e25d32a06ca5247d5872e978 Mon Sep 17 00:00:00 2001 From: Nyoxide Date: Sun, 9 Feb 2020 15:05:36 -0500 Subject: [PATCH 18/22] f --- .../scientificcalculator/Utilities/ConsoleCommands.java | 7 +++++++ .../Utilities/DisplayModeCommands.java | 6 ++++++ .../scientificcalculator/Utilities/MathCommands.java | 6 ++++++ 3 files changed, 19 insertions(+) diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/ConsoleCommands.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/ConsoleCommands.java index 1225325c..9a41e94f 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/ConsoleCommands.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/ConsoleCommands.java @@ -31,6 +31,13 @@ public enum MenuType { DISPLAY, ERROR } + + public static boolean commandExists(String cmd) { + if (commandMap != null && commandMap.containsKey(cmd.toLowerCase())) { + return true; + } + return false; + } public static void fullPrompt() { diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/DisplayModeCommands.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/DisplayModeCommands.java index 0fdc7c9e..74627606 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/DisplayModeCommands.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/DisplayModeCommands.java @@ -31,6 +31,12 @@ public static void runCommand(ArrayList args) { if (commandMap != null && commandMap.containsKey(args.get(0).toLowerCase())) { run(commandMap.get(args.get(0).toLowerCase())); } + else if (MathCommands.commandExists(args.get(0).toLowerCase())) { + MathCommands.runCommand(args); + } + else if (DisplayModeCommands.commandExists(args.get(0).toLowerCase())) { + DisplayModeCommands.runCommand(args); + } else { run(Mode.RETURN); } diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/MathCommands.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/MathCommands.java index 725fc873..4eb644ff 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/MathCommands.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/MathCommands.java @@ -52,6 +52,12 @@ public static void runCommand(ArrayList args) { if (commandMap != null && commandMap.containsKey(args.get(0).toLowerCase())) { run(commandMap.get(args.get(0).toLowerCase()), args); } + else if (ConsoleCommands.commandExists(args.get(0).toLowerCase())) { + ConsoleCommands.runCommand(args); + } + else if (DisplayModeCommands.commandExists(args.get(0).toLowerCase())) { + DisplayModeCommands.runCommand(args); + } else { run(Command.RETURN, args); } From c9ce96b24ef75f07697e8f95526fa7f77eba66b9 Mon Sep 17 00:00:00 2001 From: Adam Bennett Date: Thu, 20 Feb 2020 21:19:16 -0500 Subject: [PATCH 19/22] started expression implementation --- .../Utilities/ConsoleCommands.java | 2 +- .../Utilities/DisplayModeCommands.java | 2 +- .../Utilities/MathCommands.java | 44 +++++++++++++++++-- 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/ConsoleCommands.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/ConsoleCommands.java index 9a41e94f..9191b8cd 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/ConsoleCommands.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/ConsoleCommands.java @@ -7,7 +7,7 @@ public class ConsoleCommands { - private static Map commandMap = new HashMap<>(); + private static Map commandMap = new HashMap(); public enum Command { CLEAR, diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/DisplayModeCommands.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/DisplayModeCommands.java index 74627606..328eb65f 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/DisplayModeCommands.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/DisplayModeCommands.java @@ -9,7 +9,7 @@ public class DisplayModeCommands { - private static Map commandMap = new HashMap<>(); + private static Map commandMap = new HashMap(); public enum Mode { BINARY, diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/MathCommands.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/MathCommands.java index 4eb644ff..0bc664de 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/MathCommands.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/MathCommands.java @@ -8,7 +8,7 @@ public class MathCommands { - private static Map commandMap = new HashMap<>(); + private static Map commandMap = new HashMap(); public enum Command { ADD, @@ -38,7 +38,8 @@ public enum Command { TOGGLE_NEGATIVE, DISPLAY, EXIT, - RANDOM_FLOAT + RANDOM_FLOAT, + EXPRESSION } public static boolean commandExists(String cmd) { @@ -291,11 +292,46 @@ else if (args.size() > 2) { //TODO case EXIT: System.exit(0); + case EXPRESSION: + if (args.size() > 1) { + args.remove(0); + } + Double result = handleExpression(args); + System.out.println("Result: " + result); + fullPrompt(); + return; default: fullPrompt(); return; } } + + private static Double handleExpression(ArrayList args) { + Stack ops = new Stack(); + Stack vals = new Stack(); + + for (int i = 0; i < args.size(); i++) { + String s = args.remove(i); + if (s.equals("(")) ; + else if (s.equals("+")) ops.push(s); + else if (s.equals("-")) ops.push(s); + else if (s.equals("*")) ops.push(s); + else if (s.equals("/")) ops.push(s); + else if (s.equals("sqrt")) ops.push(s); + else if (s.equals(")")) { + String op = ops.pop(); + double v = vals.pop(); + if (op.equals("+")) v = vals.pop() + v; + else if (op.equals("-")) v = vals.pop() - v; + else if (op.equals("*")) v = vals.pop() * v; + else if (op.equals("/")) v = vals.pop() / v; + else if (op.equals("sqrt")) v = Math.sqrt(v); + vals.push(v); + } + else vals.push(Double.parseDouble(s)); + } + return vals.pop(); + } private static void handleRandomNum(ArrayList args, ArrayList excludedNums) { int lower; @@ -374,7 +410,9 @@ private static void handleRandomNum(ArrayList args, ArrayList e commandMap.put("display", Command.DISPLAY); commandMap.put("exit", Command.EXIT); commandMap.put("randomfloat", Command.RANDOM_FLOAT); - + commandMap.put("expression", Command.EXPRESSION); + + // TODO //commandMap.put("invlog", Command.INV_LOG); //commandMap.put("naturallog", Command.NATURAL_LOG); From 8821471a1c1f94d79d1c624548af3d78a9edc948 Mon Sep 17 00:00:00 2001 From: Adam Bennett Date: Thu, 20 Feb 2020 21:40:34 -0500 Subject: [PATCH 20/22] added expression command --- .../Utilities/MathCommands.java | 32 ++++++++----------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/MathCommands.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/MathCommands.java index 0bc664de..1c9f0c0f 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/MathCommands.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/MathCommands.java @@ -296,8 +296,10 @@ else if (args.size() > 2) { if (args.size() > 1) { args.remove(0); } - Double result = handleExpression(args); - System.out.println("Result: " + result); + try { + Double result = handleExpression(args); + System.out.println("Result: " + result); + } catch (NumberFormatException e) { System.out.println("Improper input. Expected an expression."); } fullPrompt(); return; default: @@ -306,31 +308,23 @@ else if (args.size() > 2) { } } + // Copied from my boy Dijkstra private static Double handleExpression(ArrayList args) { - Stack ops = new Stack(); - Stack vals = new Stack(); - + Stack ops = new Stack(); Stack vals = new Stack(); for (int i = 0; i < args.size(); i++) { - String s = args.remove(i); - if (s.equals("(")) ; - else if (s.equals("+")) ops.push(s); - else if (s.equals("-")) ops.push(s); - else if (s.equals("*")) ops.push(s); - else if (s.equals("/")) ops.push(s); - else if (s.equals("sqrt")) ops.push(s); + String s = args.get(i); + if (s.equals("(")) {} + else if (s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/") || s.equals("sqrt")) { ops.push(s); } else if (s.equals(")")) { - String op = ops.pop(); - double v = vals.pop(); - if (op.equals("+")) v = vals.pop() + v; + String op = ops.pop(); double v = vals.pop(); + if (op.equals("+")) v = vals.pop() + v; else if (op.equals("-")) v = vals.pop() - v; else if (op.equals("*")) v = vals.pop() * v; else if (op.equals("/")) v = vals.pop() / v; else if (op.equals("sqrt")) v = Math.sqrt(v); vals.push(v); - } - else vals.push(Double.parseDouble(s)); - } - return vals.pop(); + } else vals.push(Double.parseDouble(s)); + } return vals.pop(); } private static void handleRandomNum(ArrayList args, ArrayList excludedNums) { From 8249d0a9c8046181643d2eae0ccb0592df50e7ad Mon Sep 17 00:00:00 2001 From: Adam Bennett Date: Thu, 20 Feb 2020 22:24:43 -0500 Subject: [PATCH 21/22] added expression command tests, fixed bugs --- .../Utilities/MathCommands.java | 6 +- .../Utilities/MathCommandsTest.java | 102 ++++++++++++++++++ 2 files changed, 105 insertions(+), 3 deletions(-) create mode 100644 src/test/java/com/zipcodewilmington/scientificcalculator/Utilities/MathCommandsTest.java diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/MathCommands.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/MathCommands.java index 1c9f0c0f..18be6d42 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/MathCommands.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/MathCommands.java @@ -309,10 +309,10 @@ else if (args.size() > 2) { } // Copied from my boy Dijkstra - private static Double handleExpression(ArrayList args) { + public static Double handleExpression(ArrayList args) { Stack ops = new Stack(); Stack vals = new Stack(); - for (int i = 0; i < args.size(); i++) { - String s = args.get(i); + for (int i = 0; i < args.size();) { + String s = args.remove(i); if (s.equals("(")) {} else if (s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/") || s.equals("sqrt")) { ops.push(s); } else if (s.equals(")")) { diff --git a/src/test/java/com/zipcodewilmington/scientificcalculator/Utilities/MathCommandsTest.java b/src/test/java/com/zipcodewilmington/scientificcalculator/Utilities/MathCommandsTest.java new file mode 100644 index 00000000..8322b470 --- /dev/null +++ b/src/test/java/com/zipcodewilmington/scientificcalculator/Utilities/MathCommandsTest.java @@ -0,0 +1,102 @@ +package com.zipcodewilmington.scientificcalculator.Utilities; + + +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.util.ArrayList; +import java.util.Random; + +import static org.junit.Assert.*; + + +public class MathCommandsTest { + + @Test + public void TestHandleExpression() { + ArrayList input = new ArrayList<>(); + input.add("("); + input.add("("); + input.add("22"); + input.add("+"); + input.add("24"); + input.add(")"); + input.add("/"); + input.add("2"); + input.add(")"); + Double expected = 23.0; + Double actual = MathCommands.handleExpression(input); + assertEquals(expected, actual); + } + + @Test + public void TestHandleExpressionExampleA() { + ArrayList input = new ArrayList<>(); + input.add("("); + input.add("1"); + input.add("+"); + input.add("("); + input.add("("); + input.add("2"); + input.add("+"); + input.add("3"); + input.add(")"); + input.add("*"); + input.add("("); + input.add("4"); + input.add("*"); + input.add("5"); + input.add(")"); + input.add(")"); + input.add(")"); + Double expected = 101.0; + Double actual = MathCommands.handleExpression(input); + assertEquals(expected, actual); + } + + @Test + public void TestHandleExpressionExampleB() { + ArrayList input = new ArrayList<>(); + input.add("("); + input.add("("); + input.add("1"); + input.add("+"); + input.add("sqrt"); + input.add("("); + input.add("5"); + input.add(")"); + input.add(")"); + input.add("/"); + input.add("2.0"); + input.add(")"); + Double expected = 1.618033988749895; + Double actual = MathCommands.handleExpression(input); + assertEquals(expected, actual); + } + + @Test + public void TestHandleExpressionWeirdInput() { + ArrayList input = new ArrayList<>(); + input.add("("); + input.add("1"); + input.add("+"); + input.add("("); + input.add("("); + input.add("2"); + input.add("3"); + input.add("+"); + input.add(")"); + input.add("("); + input.add("4"); + input.add("5"); + input.add("*"); + input.add(")"); + input.add("*"); + input.add(")"); + input.add("+"); + input.add(")"); + Double expected = 101.0; + Double actual = MathCommands.handleExpression(input); + assertEquals(expected, actual); + } +} From 9a8810442d52af30274b375a1dcad14b1a6f338c Mon Sep 17 00:00:00 2001 From: Adam Bennett Date: Thu, 20 Feb 2020 22:27:23 -0500 Subject: [PATCH 22/22] added brackets to expression code, Dijkstra is clearly a lunatic --- .../scientificcalculator/Utilities/MathCommands.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/MathCommands.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/MathCommands.java index 18be6d42..2ed4c3f6 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/MathCommands.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Utilities/MathCommands.java @@ -317,11 +317,11 @@ public static Double handleExpression(ArrayList args) { else if (s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/") || s.equals("sqrt")) { ops.push(s); } else if (s.equals(")")) { String op = ops.pop(); double v = vals.pop(); - if (op.equals("+")) v = vals.pop() + v; - else if (op.equals("-")) v = vals.pop() - v; - else if (op.equals("*")) v = vals.pop() * v; - else if (op.equals("/")) v = vals.pop() / v; - else if (op.equals("sqrt")) v = Math.sqrt(v); + if (op.equals("+")) { v = vals.pop() + v; } + else if (op.equals("-")) { v = vals.pop() - v; } + else if (op.equals("*")) { v = vals.pop() * v; } + else if (op.equals("/")) { v = vals.pop() / v; } + else if (op.equals("sqrt")) { v = Math.sqrt(v); } vals.push(v); } else vals.push(Double.parseDouble(s)); } return vals.pop();