Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Created a working calculator #92

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,6 @@ fabric.properties
.classpath
.settings


#maven build target
target/
target/
/bin
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,13 @@
<artifactId>scientific_calculator</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.zipcodewilmington.scientificcalculator.Application;

import com.zipcodewilmington.scientificcalculator.Calculator.Calculator;
import com.zipcodewilmington.scientificcalculator.Utilities.*;

/**
* Created by leon on 2/9/18.
*/
public class MainApplication {

public final static Calculator calc;

public static void main(String[] args) {

Util.println("Welcome to my calculator!");
ConsoleCommands.fullPrompt();
}

static {
calc = new Calculator();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,301 @@
package com.zipcodewilmington.scientificcalculator.Calculator;

import com.zipcodewilmington.scientificcalculator.Utilities.Util;

public class Calculator
{
private float displayValue;
private float storedValue;
private boolean allowNegative;
private boolean isError;
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) {
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;
this.allowNegative = allowNegatives;
this.isError = false;
this.display = "";
this.trigMode = trigMode;
this.numMode = numMode;
}

@SuppressWarnings("incomplete-switch")
public void updateDisplay() {
if (this.isError) {
this.display = "Err";
return;
}

float numToDisplay = this.displayValue;
switch (this.numMode) {
case BINARY:
this.display = "" + Integer.toBinaryString((int)numToDisplay);
return;
case HEXADECIMAL:
this.display = "" + Integer.toHexString((int)numToDisplay);
return;
case OCTAL:
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;
}

this.display = "" + (float)numToDisplay;
}


public void clearDisplay() {
this.displayValue = 0;
this.isError = false;
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;
}

public void subtract(float val) {
this.displayValue -= val;
}

public void mult(float val) {
this.displayValue *= val;
}

public void div(float num) {
if (num == 0) {
this.isError = true;
updateDisplay();
return;
}
this.displayValue /= num;
}

public void sqRt() {
this.displayValue = Util.squareRoot(this.displayValue);
}

public void square() {
this.displayValue = Util.square(this.displayValue);
}

public void pow(float exponent) {
this.displayValue = (float) Math.pow(this.displayValue, exponent);
}

public void inverse() {
if (this.displayValue == 0.0f) {
this.isError = true;
updateDisplay();
return;
}
this.displayValue = 1.0f / this.displayValue;
}

public void flipSign() {
this.displayValue = -this.displayValue;
}

public void factorial() {
if (this.displayValue < 13) {
this.displayValue = Util.factorial((int) this.displayValue);
}
else {
this.displayValue = Float.MAX_VALUE;
}
}

public void sin() {
this.displayValue = Util.sine(this.displayValue);
}

public void cosine() {
this.displayValue = Util.cosine(this.displayValue);
}

public void tangent() {
this.displayValue = Util.tangent(this.displayValue);
}

public void invSin() {
this.displayValue = Util.invSine(this.displayValue);
}

public void invCosine() {
this.displayValue = Util.invCosine(this.displayValue);
}

public void invTangent() {
this.displayValue = Util.invTangent(this.displayValue);
}

public void log() {
this.displayValue = Util.log(this.displayValue);
}

public void invNatLog() {
this.displayValue = Util.inverseNaturalLog(this.displayValue);
}
// END MATH ///////////////////////////////////////////////////////////////////////////////////////////////


// GETTERS ////////////////////////////////////////////////////////////////////////////////////////////////
public String getDisplay() {
updateDisplay();
return display;
}

public float getDisplayVal() {
if (this.displayValue > 0.0f || this.allowNegative) {
return this.displayValue;
}
else {
return 0.0f;
}
}

public float getStoredVal() {
return this.storedValue;
}

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.displayValue = val;
}

public void setAllowNegative(boolean allow) {
this.allowNegative = allow;
}

public void setDisplay(String newDisp) {
this.display = newDisp;
}

public void setError(boolean isError) {
this.isError = isError;
}

public void setStoredVal(int newStoredVal) {
this.storedValue = newStoredVal;
}

public void incStoredVal(float inc) {
this.storedValue += inc;
}

public void setDisplayMode(DisplayMode newMode) {
this.numMode = newMode;
updateDisplay();
}

public void setTrigMode(TrigDisplayMode newMode) {
this.trigMode = newMode;
updateDisplay();
}

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 /////////////////////////////////////////////////////////////////////////////////////////


}

This file was deleted.

Loading