diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..afc8050 Binary files /dev/null and b/.DS_Store differ diff --git a/.gitignore b/.gitignore index de469bb..50ffdd9 100644 --- a/.gitignore +++ b/.gitignore @@ -152,4 +152,6 @@ fabric.properties #maven build target -target/ \ No newline at end of file +target/ + +.DS_Store \ No newline at end of file diff --git a/calc-uml.png b/calc-uml.png new file mode 100644 index 0000000..54cb352 Binary files /dev/null and b/calc-uml.png differ diff --git a/pom.xml b/pom.xml index e7cb4f6..e2a2063 100644 --- a/pom.xml +++ b/pom.xml @@ -7,6 +7,24 @@ com.zipcodewilmington scientific_calculator 1.0-SNAPSHOT + + + junit + junit + 4.13.1 + test + + + org.junit.jupiter + junit-jupiter + RELEASE + test + + + + 18 + 18 + \ No newline at end of file diff --git a/src/.DS_Store b/src/.DS_Store new file mode 100644 index 0000000..c08d70c Binary files /dev/null and b/src/.DS_Store differ diff --git a/src/main/.DS_Store b/src/main/.DS_Store new file mode 100644 index 0000000..069ccdf Binary files /dev/null and b/src/main/.DS_Store differ diff --git a/src/main/java/.DS_Store b/src/main/java/.DS_Store new file mode 100644 index 0000000..360a3bc Binary files /dev/null and b/src/main/java/.DS_Store differ diff --git a/src/main/java/com/.DS_Store b/src/main/java/com/.DS_Store new file mode 100644 index 0000000..3cace59 Binary files /dev/null and b/src/main/java/com/.DS_Store differ diff --git a/src/main/java/com/zipcodewilmington/.DS_Store b/src/main/java/com/zipcodewilmington/.DS_Store new file mode 100644 index 0000000..58a4e0f Binary files /dev/null and b/src/main/java/com/zipcodewilmington/.DS_Store differ diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/.DS_Store b/src/main/java/com/zipcodewilmington/scientificcalculator/.DS_Store new file mode 100644 index 0000000..7dce267 Binary files /dev/null and b/src/main/java/com/zipcodewilmington/scientificcalculator/.DS_Store differ diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/CalcDisplayMode.java b/src/main/java/com/zipcodewilmington/scientificcalculator/CalcDisplayMode.java new file mode 100644 index 0000000..70e0ea5 --- /dev/null +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/CalcDisplayMode.java @@ -0,0 +1,39 @@ +package com.zipcodewilmington.scientificcalculator; + +public enum CalcDisplayMode { + DECIMAL(1, "DEC"), + BINARY(2, "BIN"), + HEXADECIMAL(3, "HEX"), + OCTAL(4, "OCT"); + + int modeNum; + String abbrev; + + CalcDisplayMode(int modeNum, String abbrev) { + this.modeNum = modeNum; + this.abbrev = abbrev; + } + + public int getModeNum() { + return modeNum; + } + + public String getAbbrev() { + return abbrev; + } + public int nextMode() { + if (this.modeNum == 4) { + return this.DECIMAL.modeNum; + } + return this.modeNum + 1; + } + + public static CalcDisplayMode getModeByNum(int num) { + for (CalcDisplayMode mode : values()) { + if (mode.modeNum == num) { + return mode; + } + } + return null; + } +} \ No newline at end of file diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/CalcGUI.java b/src/main/java/com/zipcodewilmington/scientificcalculator/CalcGUI.java new file mode 100644 index 0000000..f92aa1f --- /dev/null +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/CalcGUI.java @@ -0,0 +1,596 @@ +package com.zipcodewilmington.scientificcalculator; + +import javax.swing.*; +import javax.xml.crypto.dsig.spec.DigestMethodParameterSpec; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.math.BigInteger; +import java.text.NumberFormat; + +public class CalcGUI extends ScientificCalc { + private JFrame frame; + private JTextField field; + private double tmp1, tmp2, result, currVal; + private String operation; + private Boolean flagOverwrite; + private Boolean flagHasOverwritten; + private Boolean operatorLastInput; + private Boolean flagIsError; + + public CalcGUI() { + super(); + tmp1 = 0; + tmp2 = 0; + result = 0; + operation = ""; + flagOverwrite = false; + flagHasOverwritten = false; + operatorLastInput = false; + flagIsError = false; + init(); + } + + private void init() { + // init frame + frame = new JFrame("Zip Calc"); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setPreferredSize(new Dimension(400, 600)); + frame.setBackground(new Color(239, 232, 232)); + + // init field + this.field = new JTextField(); + field.setPreferredSize(new Dimension(400, 80)); + field.setText(""); + field.setEditable(false); + field.setHorizontalAlignment(SwingConstants.RIGHT); + field.setFont(new Font("Roboto", Font.BOLD, 30)); + frame.add(field, BorderLayout.NORTH); + //=========================== Number pad panel ========================= + // number panel + JPanel calcPanel = new JPanel(); + //calcPanel.setPreferredSize(new Dimension(200, 200)); + calcPanel.setLayout(new GridLayout(7, 3)); + + Action opActionListener = new AbstractAction() { + @Override + public void actionPerformed(ActionEvent e) { + if (flagIsError) { + return; + } + JButton button = (JButton) e.getSource(); + String op = button.getText(); + if (op.equals("xʸ")) { + op = "pow"; + } + if (operatorLastInput) { + operation = op; + operatorLastInput = true; + return; + } else if (operation != "") { + tmp1 = handleOperation(operation, tmp1, Double.valueOf(field.getText())); + field.setText(convertForDisplayMode(tmp1)); + } else { + tmp1 = Double.valueOf(field.getText()); + } + operation = op; + operatorLastInput = true; + flagOverwrite = true; + flagHasOverwritten = false; + } + }; + + JButton bMPlus = new JButton("M+"); + bMPlus.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (flagIsError) { + return; + } + setMemory(addition(getMemory(), Double.valueOf(field.getText()))); + } + }); + JButton bMC = new JButton("MC"); + bMC.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (flagIsError) { + return; + } + setMemory(0); + } + }); + JButton bMRC = new JButton("MRC"); + bMRC.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (flagIsError) { + return; + } + field.setText(String.valueOf(getMemory())); + } + }); + JButton bSquare = new JButton("x²"); + bSquare.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (flagIsError) { + return; + } + double x = Double.valueOf(field.getText()); + field.setText(String.valueOf(squared(x))); + } + }); + JButton bSRoot = new JButton("√x"); + bSRoot.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (flagIsError) { + return; + } + double x = Double.valueOf(field.getText()); + field.setText(String.valueOf(squareRoot(x))); + } + }); + JButton bClear = new JButton("C"); + bClear.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + field.setText("0"); + tmp1= 0; + tmp2= 0; + operation = ""; + result = 0; + flagHasOverwritten = false; + flagOverwrite = true; + flagIsError = false; + } + }); + JButton bExpo = new JButton("xʸ"); + bExpo.addActionListener(opActionListener); + // Division by 0 possible! + JButton bInv = new JButton("1/x"); + bInv.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (flagIsError) { + return; + } + double x = Double.valueOf(field.getText()); + if (x == 0) { + field.setText("ERR"); + flagIsError = true; + return; + } + field.setText(String.valueOf(inverse(x))); + } + }); + JButton bAbs = new JButton("|x|"); + bAbs.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (flagIsError) { + return; + } + double x = Double.valueOf(field.getText()); + field.setText(String.valueOf(abs(x))); + } + }); + + JButton b0 = new JButton("0"); + JButton b1 = new JButton("1"); + JButton b2 = new JButton("2"); + JButton b3 = new JButton("3"); + JButton b4 = new JButton("4"); + JButton b5 = new JButton("5"); + JButton b6 = new JButton("6"); + JButton b7 = new JButton("7"); + JButton b8 = new JButton("8"); + JButton b9 = new JButton("9"); + JButton bDot = new JButton("."); + bDot.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (flagIsError) { + return; + } + String fieldTxt = field.getText(); + if (!fieldTxt.contains(".")) { + field.setText(fieldTxt.concat(".")); + } + } + }); + JButton bPlusMinus = new JButton("+/-"); + bPlusMinus.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (flagIsError) { + return; + } + double x = Double.valueOf(field.getText()); + field.setText(String.valueOf(invert(x))); + } + }); + + calcPanel.add(bMPlus); + calcPanel.add(bMC); + calcPanel.add(bMRC); + calcPanel.add(bSquare); + calcPanel.add(bSRoot); + calcPanel.add(bClear); + calcPanel.add(bExpo); + calcPanel.add(bInv); + calcPanel.add(bAbs); + calcPanel.add(b7); + calcPanel.add(b8); + calcPanel.add(b9); + calcPanel.add(b4); + calcPanel.add(b5); + calcPanel.add(b6); + calcPanel.add(b1); + calcPanel.add(b2); + calcPanel.add(b3); + calcPanel.add(bPlusMinus); + calcPanel.add(b0); + calcPanel.add(bDot); + + + // ======================== Operator/Right panel ====================== + JPanel opPanel = new JPanel(); + //opPanel.setPreferredSize(new Dimension(70, 200)); + opPanel.setLayout(new GridLayout(7, 1)); + + JButton bDiv = new JButton("÷"); + JButton bMul = new JButton("x"); + JButton bMinus = new JButton("-"); + JButton bPlus = new JButton("+"); + JButton bEqual = new JButton("="); + JButton bMod = new JButton("mod"); + JButton bBackSpace = new JButton("←"); + bBackSpace.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (flagIsError) { + return; + } + String txt = field.getText(); + if (txt.length() == 1) { + field.setText("0"); + flagOverwrite = true; + flagHasOverwritten = false; + } else if (txt.charAt(txt.length() - 2) == '.') { + field.setText(txt.substring(0, txt.length() - 2)); + } else { + field.setText(txt.substring(0, txt.length() - 1)); + } + } + }); + // listener for number pad buttons + Action numActionListener = new AbstractAction() { + @Override + public void actionPerformed(ActionEvent e) { + if (flagIsError) { + return; + } + JButton button = (JButton) e.getSource(); + if (flagOverwrite && !flagHasOverwritten) { + field.setText(button.getText()); + flagHasOverwritten = true; + } else { + field.setText(field.getText().concat(button.getText())); + } + operatorLastInput = false; + } + }; + + b0.addActionListener(numActionListener); + b1.addActionListener(numActionListener); + b2.addActionListener(numActionListener); + b3.addActionListener(numActionListener); + b4.addActionListener(numActionListener); + b5.addActionListener(numActionListener); + b6.addActionListener(numActionListener); + b7.addActionListener(numActionListener); + b8.addActionListener(numActionListener); + b9.addActionListener(numActionListener); + bDiv.addActionListener(opActionListener); + bPlus.addActionListener(opActionListener); + bMinus.addActionListener(opActionListener); + bMod.addActionListener(opActionListener); + bMul.addActionListener(opActionListener); + bEqual.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (flagIsError) { + return; + } + tmp2 = Double.valueOf(field.getText()); + switch (operation) { + case "+": + result = addition(tmp1, tmp2); + tmp1 = result; + field.setText(convertForDisplayMode(result)); + break; + case "-": + result = subtraction(tmp1, tmp2); + tmp1 = result; + field.setText(convertForDisplayMode(result)); + break; + case "÷": + result = division(tmp1, tmp2); + tmp1 = result; + field.setText(convertForDisplayMode(result)); + break; + case "x": + result = multiplication(tmp1, tmp2); + tmp1 = result; + field.setText(convertForDisplayMode(result)); + break; + case "mod": + result = tmp1 % tmp2; + tmp1 = result; + field.setText(convertForDisplayMode(result)); + break; + case "pow": + result = exponent(tmp1, tmp2); + tmp1 = result; + field.setText(convertForDisplayMode(result)); + } + // reset operation and flag + operation = ""; + flagOverwrite = true; + flagHasOverwritten = false; + if (Double.isNaN(result)) { + flagIsError = true; + field.setText("ERR"); + } + } + }); + + + + opPanel.add(bBackSpace); + opPanel.add(bMod); + opPanel.add(bDiv); + opPanel.add(bMul); + opPanel.add(bMinus); + opPanel.add(bPlus); + opPanel.add(bEqual); + + // ==================== Left Panel ========================== + JPanel leftPanel = new JPanel(); + leftPanel.setLayout(new GridLayout(7, 2)); + + JButton bLog = new JButton("Log"); + bLog.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (flagIsError) { + return; + } + double x = Double.valueOf(field.getText()); + field.setText(String.valueOf(log(x))); + } + }); + JButton bILog = new JButton("10ˣ"); + bILog.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (flagIsError) { + return; + } + double x = Double.valueOf(field.getText()); + field.setText(String.valueOf(invLog(x))); + } + }); + JButton bINatLog = new JButton("eˣ"); + bINatLog.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (flagIsError) { + return; + } + double x = Double.valueOf(field.getText()); + field.setText(String.valueOf(invNatLog(x))); + } + }); + JButton bNatLog = new JButton("Ln"); + bNatLog.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (flagIsError) { + return; + } + double x = Double.valueOf(field.getText()); + field.setText(String.valueOf(natLog(x))); + } + }); + JButton bFactorial = new JButton("x!"); + bFactorial.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (flagIsError) { + return; + } + double x = Double.valueOf(field.getText()); + field.setText(String.valueOf(factorial(x))); + } + }); + JButton bSin = new JButton("sin"); + bSin.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (flagIsError) { + return; + } + double x = Double.valueOf(field.getText()); + field.setText(String.valueOf(sin(x))); + } + }); + JButton bCos = new JButton("cos"); + bCos.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (flagIsError) { + return; + } + double x = Double.valueOf(field.getText()); + field.setText(String.valueOf(cos(x))); + } + }); + JButton bTan = new JButton("tan"); + bTan.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (flagIsError) { + return; + } + double x = Double.valueOf(field.getText()); + field.setText(String.valueOf(tan(x))); + } + }); + JButton bISin = new JButton("sin⁻¹"); + bISin.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (flagIsError) { + return; + } + double x = Double.valueOf(field.getText()); + field.setText(String.valueOf(invSin(x))); + } + }); + JButton bITan = new JButton("tan⁻¹"); + bITan.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (flagIsError) { + return; + } + double x = Double.valueOf(field.getText()); + field.setText(String.valueOf(invTan(x))); + } + }); + JButton bICos = new JButton("cos⁻¹"); + bICos.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (flagIsError) { + return; + } + double x = Double.valueOf(field.getText()); + field.setText(String.valueOf(invCos(x))); + } + }); + JButton bDisplayMode = new JButton("Dec"); + bDisplayMode.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (flagIsError) { + return; + } + switchDisplayMode(); + bDisplayMode.setText(getDisplayMode()); + } + }); + JButton bTrigMode = new JButton("Deg"); + bTrigMode.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (flagIsError) { + return; + } + switchUnitsMode(); + bTrigMode.setText(TrigUnit.valueOf(getTrigMode()).getAbbrev()); + } + }); + JButton bPi = new JButton("π"); + bPi.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (flagIsError) { + return; + } + field.setText(String.valueOf(Math.PI)); + } + }); + + leftPanel.add(bLog); + leftPanel.add(bILog); + leftPanel.add(bINatLog); + leftPanel.add(bNatLog); + leftPanel.add(bFactorial); + leftPanel.add(bSin); + leftPanel.add(bCos); + leftPanel.add(bTan); + leftPanel.add(bISin); + leftPanel.add(bITan); + leftPanel.add(bICos); + leftPanel.add(bDisplayMode); + leftPanel.add(bTrigMode); + leftPanel.add(bPi); + //================================================================== + + + + frame.add(calcPanel); + frame.add(opPanel, BorderLayout.EAST); + frame.add(leftPanel, BorderLayout.WEST); + frame.pack(); + frame.setVisible(true); + + + } + + private double handleOperation(String operation, double x, double y) { + switch (operation) { + case "+": + return addition(x, y); + case "-": + return subtraction(x, y); + case "÷": + return division(x, y); + case "x": + return multiplication(x, y); + case "mod": + return x % y; + case "pow": + return exponent(x, y); + default: + return 0; + } + } + + private String convertForDisplayMode(double num) { + String display = getDisplayMode(); + switch(display) { + case "DEC": { + return String.valueOf(num); + } + case "BIN": { + return Long.toBinaryString(Double.doubleToRawLongBits(num)); + } + case "OCT": { + return Long.toOctalString(Double.doubleToRawLongBits(num)); + } + case "HEX": { + return Double.toHexString(num); + } + } + return String.valueOf(num); + } + + private Double toDouble(String numStr) { + String displayMode = getDisplayMode(); + switch(displayMode) { + case "BIN": + return Double.longBitsToDouble(new BigInteger(numStr, 2).longValue()); + case "OCT": + return Double.valueOf(Integer.parseInt(numStr,8)); + case "HEX": + return Double.longBitsToDouble(new BigInteger(numStr, 16).longValue()); + } + return Double.valueOf(0); + } +} + diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Calculations.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Calculations.java new file mode 100644 index 0000000..7c1dcc9 --- /dev/null +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Calculations.java @@ -0,0 +1,68 @@ +package com.zipcodewilmington.scientificcalculator; + + +public class Calculations { + + public static Double addition(double value1, double value2) { + double sum = value1 + value2; + return sum; + + } + + public static Double subtraction(double value1, double value2) { + double difference = value1 - value2; + return difference; + } + + public static Double multiplication(double value1, double value2) { + double product = value1 * value2; + return product; + } + + public static Double division(double value1, double value2) { + if (value2 == 0) { + return Double.NaN; + } + double quotient = value1 / value2; + return quotient; + } + + public static Double squared(double value1) { + double sq = value1 * value1; + return sq; + } + + public static Double squareRoot(double value1) { + + double sqrt = Math.sqrt(value1); + return sqrt; + } + + public static Double exponent(double value1, double value2) { + double exp = Math.pow(value1, value2); + return exp; + } + + public static Double inverse(double value1) { + if (value1 == 0) { + return Double.NaN; + } + double ins = 1 / value1; + return ins; + } + + public static Double invert(double value1) { + double inv = -1 * value1; + if (value1 == 0) { + return (double) 0; + } else { + return inv; + + } + } + + + public static void clear() { + System.out.println(""); + } +} diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Console.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Console.java index 83f0e97..8802be7 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Console.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Console.java @@ -23,10 +23,20 @@ public static String getStringInput(String prompt) { } public static Integer getIntegerInput(String prompt) { - return null; + Scanner scanner = new Scanner(System.in); + println(prompt); + Integer userInput = scanner.nextInt(); + return userInput; } public static Double getDoubleInput(String prompt) { - return null; + Scanner scanner = new Scanner(System.in); + println(prompt); + Double userInput = scanner.nextDouble(); + return userInput; } + + } + + diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java b/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java index 5f42132..1108189 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java @@ -1,11 +1,85 @@ package com.zipcodewilmington.scientificcalculator; +import javax.swing.*; +import java.util.Scanner; + /** * Created by leon on 2/9/18. */ public class MainApplication { + public static void main(String[] args) { - Console.println("Welcome to my calculator!"); + + SwingUtilities.invokeLater(new Runnable() { + public void run() { + new CalcGUI(); + } + }); + /* + boolean exit = false; + + do { + Scanner scanner = new Scanner(System.in); + Console.println("Welcome to my calculator! \nThe current value is 0"); + double value1 = Console.getDoubleInput("Enter first number"); + char operations; + Console.println("Select your operation: \n 0. Clear display \n 1. + \n 2. - \n 3. * \n 4. /" + + "\n 5. SQ \n 6. SQRT \n 7. EXP \n 8. Inverse \n 9. INV \n "); + double value2; + operations = scanner.next().charAt(0); + + switch (operations) { + case '1': + value2 = Console.getDoubleInput("Please enter a second number to add: "); + Calculations.addition(value1, value2); + break; + + case '2': + value2 = Console.getDoubleInput("Please enter a second number to subtract: "); + Calculations.subtraction(value1, value2); + break; + + case '3': + value2 = Console.getDoubleInput("Please enter a second number to multiply: "); + Calculations.multiplication(value1, value2); + break; + + case '4': + value2 = Console.getDoubleInput("Please enter a second number to divide by: "); + while (value2 == 0) { + Console.println("Err"); + value2 = Console.getDoubleInput("Please enter a second number that isn't 0: "); + } + Calculations.division(value1, value2); + break; + + case '5': + Calculations.squared(value1); + break; + + case '6': + Calculations.squareRoot(value1); + break; + + case '7': + value2 = Console.getDoubleInput("Please enter an exponent "); + Calculations.exponent(value1, value2); + break; + + case '8': + Calculations.inverse(value1); + break; + + case '9': + Calculations.invert(value1); + + break; + + case '0': + Calculations.clear(); + break; + + /* String s = Console.getStringInput("Enter a string"); Integer i = Console.getIntegerInput("Enter an integer"); Double d = Console.getDoubleInput("Enter a double."); @@ -13,5 +87,10 @@ public static void main(String[] args) { 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); + } + } while (!exit); + + } + */ } } diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/ScientificCalc.java b/src/main/java/com/zipcodewilmington/scientificcalculator/ScientificCalc.java new file mode 100644 index 0000000..b761aeb --- /dev/null +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/ScientificCalc.java @@ -0,0 +1,158 @@ +package com.zipcodewilmington.scientificcalculator; + +enum TrigUnit { + DEGREES("DEG"), + RADIANS("RAD"); + + String abbrev; + + private TrigUnit(String abbrev) { + this.abbrev = abbrev; + } + + public String getAbbrev() { + return abbrev; + } + protected TrigUnit nextMode() { + if (this.name() == "DEGREES") { + return RADIANS; + } else { + return DEGREES; + } + } + +} + +public class ScientificCalc extends Calculations { + protected CalcDisplayMode mode; + private double memory; + private TrigUnit trigUnit; + + public ScientificCalc() { + this.memory = 0; + this.mode = CalcDisplayMode.DECIMAL; + this.trigUnit = TrigUnit.DEGREES; + } + + protected void switchDisplayMode() { + setDisplayMode(CalcDisplayMode.getModeByNum(mode.nextMode())); + } + + protected void switchDisplayMode(String mode) { + this.mode = CalcDisplayMode.valueOf(mode); + } + + protected void mPlusKey() {} + + protected void mCKey() { + memory = 0; + } + + protected double mRCKey() { + return memory; + } + + protected void switchUnitsMode() { + trigUnit = trigUnit.nextMode(); + } + + protected void switchUnitsMode(String mode) { + trigUnit = TrigUnit.valueOf(mode); + } + + public String getDisplayMode() { + return mode.getAbbrev(); + } + public void setDisplayMode(CalcDisplayMode mode) { + this.mode = mode; + } + + public String getTrigMode() { + return trigUnit.name(); + } + + public double getMemory() { + return memory; + } + + public void setMemory(double x) { + this.memory = x; + } + + private double convertRadTrigUnit(double x) { + if (getTrigMode() == "DEGREES") { + return Math.toRadians(x); + } + return x; + } + + private double convertDegTrigUnit(double x) { + if (getTrigMode() == "RADIANS") { + return Math.toDegrees(x); + } + return x; + } + + public double sin(double x) { + double convertedNum = convertDegTrigUnit(x); + return convertDegTrigUnit(Math.sin(convertedNum)); + } + + public double cos(double x) { + double convertedNum = convertDegTrigUnit(x); + return convertDegTrigUnit(Math.cos(convertedNum)); + } + + public double tan(double x) { + double convertedNum = convertDegTrigUnit(x); + return convertDegTrigUnit(Math.tan(convertedNum)); + } + + public double invSin(double x) { + double convertedNum = convertDegTrigUnit(x); + return convertDegTrigUnit(Math.asin(convertedNum)); + } + + public double invCos(double x) { + double convertedNum = convertDegTrigUnit(x); + return convertDegTrigUnit(Math.acos(convertedNum)); + } + + public double invTan(double x) { + double convertedNum = convertDegTrigUnit(x); + return convertDegTrigUnit(Math.atan(convertedNum)); + } + + public static double log(double x) { + return Math.log10(x); + } + + public static double invLog(double x) { + return Math.pow(10, x); + } + + public static double natLog(double x) { + return Math.log(x); + } + + public static double invNatLog(double x) { + return Math.pow(Math.E, x); + } + + public static double pi() { + return Math.PI; + } + + public static double abs(double x) { + return Math.abs(x); + } + + public static double factorial(double x) { + double product = 1; + for (double i = x; i > 0; i--) { + product *= i; + } + return product; + } + +} \ No newline at end of file diff --git a/src/test/.DS_Store b/src/test/.DS_Store new file mode 100644 index 0000000..55c1fcb Binary files /dev/null and b/src/test/.DS_Store differ diff --git a/src/test/java/.DS_Store b/src/test/java/.DS_Store new file mode 100644 index 0000000..35a54bf Binary files /dev/null and b/src/test/java/.DS_Store differ diff --git a/src/test/java/com/.DS_Store b/src/test/java/com/.DS_Store new file mode 100644 index 0000000..fdd76ec Binary files /dev/null and b/src/test/java/com/.DS_Store differ diff --git a/src/test/java/com/zipcodewilmington/.DS_Store b/src/test/java/com/zipcodewilmington/.DS_Store new file mode 100644 index 0000000..b9a69af Binary files /dev/null and b/src/test/java/com/zipcodewilmington/.DS_Store differ diff --git a/src/test/java/com/zipcodewilmington/scientific_calculator/TestMainApplication.java b/src/test/java/com/zipcodewilmington/scientific_calculator/TestMainApplication.java deleted file mode 100644 index 94e8d98..0000000 --- a/src/test/java/com/zipcodewilmington/scientific_calculator/TestMainApplication.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.zipcodewilmington.scientific_calculator; - -/** - * Created by leon on 2/9/18. - */ -public class TestMainApplication { -} diff --git a/src/test/java/com/zipcodewilmington/scientific_calculator/TestScientificFunctions.java b/src/test/java/com/zipcodewilmington/scientific_calculator/TestScientificFunctions.java new file mode 100644 index 0000000..2262dc9 --- /dev/null +++ b/src/test/java/com/zipcodewilmington/scientific_calculator/TestScientificFunctions.java @@ -0,0 +1,199 @@ +package com.zipcodewilmington.scientific_calculator; + +import com.zipcodewilmington.scientificcalculator.ScientificCalc; +import org.junit.Assert; +import org.junit.Before; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + + +/** + * Created by leon on 2/9/18. + */ +public class TestScientificFunctions { + private ScientificCalc objSciCalcTest; + + @Before + public void setUp() { + objSciCalcTest = new ScientificCalc(); + } + + @Test + public void testCos() { + objSciCalcTest = new ScientificCalc(); + double randomNum = (Math.random() * 10); + double expectedResult = Math.cos(randomNum); + double result = objSciCalcTest.cos(randomNum); + Assert.assertEquals(expectedResult, result, 0.000000d); + } + + @Test + public void testCosHard() { + objSciCalcTest = new ScientificCalc(); + double expectedResult = Math.cos(1); + double result = objSciCalcTest.cos(1); + Assert.assertEquals(expectedResult, result, 0.000000d); + } + + @Test + public void testCosHard2() { + objSciCalcTest = new ScientificCalc(); + double expectedResult = Math.cos(2); + double result = objSciCalcTest.cos(2); + Assert.assertEquals(expectedResult, result, 0.000000d); + } + @Test + public void testCosNotEqual() { + objSciCalcTest = new ScientificCalc(); + double expectedResult = Math.cos(1); + double result = objSciCalcTest.cos(2); + Assert.assertNotEquals(expectedResult, result, 0.000000d); + } + + @Test + public void testSine(){ + objSciCalcTest = new ScientificCalc(); + double randomNum = (Math.random() * 10); + double expectedResult = Math.sin(randomNum); + double result = objSciCalcTest.sin(randomNum); + Assert.assertEquals(expectedResult, result, 0.000000d); + } + + @Test + public void testSineHard() { + objSciCalcTest = new ScientificCalc(); + double expectedResult = Math.sin(2); + double result = objSciCalcTest.sin(2); + Assert.assertEquals(expectedResult, result, 0.000000d); + } + @Test + public void testSinNotEqual() { + objSciCalcTest = new ScientificCalc(); + double expectedResult = Math.sin(1); + double result = objSciCalcTest.sin(2); + Assert.assertNotEquals(expectedResult, result, 0.000000d); + } + + @Test + public void testTan() { + objSciCalcTest = new ScientificCalc(); + double randomNum = (Math.random() * 10); + double expectedResult = Math.tan(randomNum); + double result = objSciCalcTest.tan(randomNum); + Assert.assertEquals(expectedResult, result, 0.000000d); + } + + @Test + public void testTanHard() { + objSciCalcTest = new ScientificCalc(); + double expectedResult = Math.tan(2); + double result = objSciCalcTest.tan(2); + Assert.assertEquals(expectedResult, result, 0.000000d); + } + @Test + public void testTanNotEqual() { + objSciCalcTest = new ScientificCalc(); + double expectedResult = Math.tan(1); + double result = objSciCalcTest.tan(2); + Assert.assertNotEquals(expectedResult, result, 0.000000d); + } + + @Test + public void testInvSine() { + objSciCalcTest = new ScientificCalc(); + double randomNum = Math.random(); + double expectedResult = Math.asin(randomNum); + double result = objSciCalcTest.invSin(randomNum); + Assert.assertEquals(expectedResult, result, 0.000000d); + } + + @Test + public void testInvCos() { + objSciCalcTest = new ScientificCalc(); + double randomNum = Math.random(); + double expectedResult = Math.acos(randomNum); + double result = objSciCalcTest.invCos(randomNum); + Assert.assertEquals(expectedResult, result, 0.000000d); + } + + @Test + public void testInvTan() { + objSciCalcTest = new ScientificCalc(); + double randomNum = Math.random(); + double expectedResult = Math.atan(randomNum); + double result = objSciCalcTest.invTan(randomNum); + Assert.assertEquals(expectedResult, result, 0.000000d); + } + + @Test + public void testLog() { + double randomNum = (Math.random() * 10); + double expectedResult = Math.log10(randomNum); + double result = objSciCalcTest.log(randomNum); + Assert.assertEquals(expectedResult, result, 0.000000d); + } + + @Test + public void testInvLog() { + double randomNum = (Math.random() * 10); + double expectedResult = Math.pow(10, randomNum); + double result = objSciCalcTest.invLog(randomNum); + Assert.assertEquals(expectedResult, result, 0.000000d); + } + + @Test + public void testLnFunc() { + double randomNum = (Math.random() * 10); + double expectedResult = Math.log(randomNum); + double result = objSciCalcTest.natLog(randomNum); + Assert.assertEquals(expectedResult, result, 0.000000d); + } + + @Test + public void testinvLnFunc() { + double randomNum = (Math.random() * 10); + //I don't know if this is the right math method + double expectedResult = Math.pow(Math.E, randomNum); + double result = objSciCalcTest.invNatLog(randomNum); + Assert.assertEquals(expectedResult, result, 0.000000d); + } + + @Test + public void abs() { + double testNum = 8; + double expectedResult = (Math.abs(testNum)); + double result = objSciCalcTest.abs(testNum); + Assert.assertEquals(expectedResult, result, 0.00000000d); + } + + @Test + public void factorial() { + long testNum = 8; + long expectedResult = 40320; + double result = objSciCalcTest.factorial(testNum); + Assert.assertEquals(expectedResult, result, 0.00000000d); + } + + @Test + public void factorialFail() { + long testNum = 8; + long expectedResult = 94032; + double result = objSciCalcTest.factorial(3); + Assert.assertNotEquals(expectedResult, result); + } + + @Test + public void Pi() { + long expectedResult = (long) Math.PI; + long result = (long) objSciCalcTest.pi(); + Assert.assertEquals(expectedResult, result, 0.00000000d); + } +} + + + + + + + + diff --git a/src/test/java/com/zipcodewilmington/scientificcalculator/CalcDisplayModeTest.java b/src/test/java/com/zipcodewilmington/scientificcalculator/CalcDisplayModeTest.java new file mode 100644 index 0000000..02a4bf5 --- /dev/null +++ b/src/test/java/com/zipcodewilmington/scientificcalculator/CalcDisplayModeTest.java @@ -0,0 +1,30 @@ +package com.zipcodewilmington.scientificcalculator; + +import org.junit.Test; + +import static org.junit.Assert.*; + +public class CalcDisplayModeTest { + + @Test + public void testGetModeNum() { + + } + + @Test + public void testNextMode() { + + } + + @Test + public void testGetModeByNum() { + } + + @Test + public void testValues() { + } + + @Test + public void testValueOf() { + } +} \ No newline at end of file diff --git a/src/test/java/com/zipcodewilmington/scientificcalculator/CalculationsTest.java b/src/test/java/com/zipcodewilmington/scientificcalculator/CalculationsTest.java new file mode 100644 index 0000000..84be02b --- /dev/null +++ b/src/test/java/com/zipcodewilmington/scientificcalculator/CalculationsTest.java @@ -0,0 +1,85 @@ +package com.zipcodewilmington.scientificcalculator; + +import static org.junit.Assert.*; + +public class CalculationsTest { + + @org.junit.Test + public void addition() { + assertEquals(Double.valueOf(1) , Calculations.addition(.5 , .5)); + assertEquals(Double.valueOf(10) , Calculations.addition(5 , 5)); + assertEquals(Double.valueOf(100) , Calculations.addition(87 , 13)); + assertEquals(Double.valueOf(1000) , Calculations.addition(460 , 540)); + + } + + @org.junit.Test + public void subtraction() { + assertEquals(Double.valueOf(1), Calculations.subtraction(7, 6)); + assertEquals(Double.valueOf(10), Calculations.subtraction(12, 2)); + assertEquals(Double.valueOf(100), Calculations.subtraction(140, 40)); + assertEquals(Double.valueOf(1000), Calculations.subtraction(1500, 500)); + } + + @org.junit.Test + public void multiplication() { + assertEquals(Double.valueOf(1), Calculations.multiplication(1, 1)); + assertEquals(Double.valueOf(10), Calculations.multiplication(5, 2)); + assertEquals(Double.valueOf(100), Calculations.multiplication(5, 20)); + assertEquals(Double.valueOf(1000), Calculations.multiplication(50, 20)); + } + + @org.junit.Test + public void division() { + assertEquals(Double.valueOf(1), Calculations.division(2, 2)); + assertEquals(Double.valueOf(10), Calculations.division(250, 25)); + assertEquals(Double.valueOf(100), Calculations.division(400, 4)); + assertEquals(Double.valueOf(1000), Calculations.division(10000, 10)); + + } + + @org.junit.Test + public void squared() { + assertEquals(Double.valueOf(25), Calculations.squared(5)); + assertEquals(Double.valueOf(225), Calculations.squared(15)); + assertEquals(Double.valueOf(2500), Calculations.squared(50)); + assertEquals(Double.valueOf(10000), Calculations.squared(100)); + } + + @org.junit.Test + public void squareRoot() { + assertEquals(Double.valueOf(5), Calculations.squareRoot(25)); + assertEquals(Double.valueOf(15), Calculations.squareRoot(225)); + assertEquals(Double.valueOf(50), Calculations.squareRoot(2500)); + assertEquals(Double.valueOf(100), Calculations.squareRoot(10000)); + } + + @org.junit.Test + public void exponent() { + assertEquals(Double.valueOf(1), Calculations.exponent(1, 1)); + assertEquals(Double.valueOf(27),Calculations.exponent(3, 3)); + assertEquals(Double.valueOf(729), Calculations.exponent(9, 3)); + assertEquals(Double.valueOf(1000),Calculations.exponent(10, 3)); + + } + + @org.junit.Test + public void inverse() { + assertEquals(Double.valueOf(.1), Calculations.inverse(10)); + assertEquals(Double.valueOf(.002), Calculations.inverse(500)); + assertEquals(Double.valueOf(1), Calculations.inverse(1)); + assertEquals(Double.valueOf(.5), Calculations.inverse(2)); + } + + @org.junit.Test + public void invert() { + assertEquals(Double.valueOf(0), Calculations.invert(0)); + assertEquals(Double.valueOf(-10), Calculations.invert(10)); + assertEquals(Double.valueOf(-729), Calculations.invert(729)); + assertEquals(Double.valueOf(-1000), Calculations.invert(1000)); + } + + @org.junit.Test + public void clear() { + } +} \ No newline at end of file diff --git a/src/test/java/com/zipcodewilmington/scientificcalculator/ScientificCalcTest.java b/src/test/java/com/zipcodewilmington/scientificcalculator/ScientificCalcTest.java new file mode 100644 index 0000000..51ee46e --- /dev/null +++ b/src/test/java/com/zipcodewilmington/scientificcalculator/ScientificCalcTest.java @@ -0,0 +1,14 @@ +package com.zipcodewilmington.scientificcalculator; + +import org.junit.Test; +import static org.junit.Assert.*; + +public class ScientificCalcTest { + + @Test + public void testFactorial() { + ScientificCalc calc = new ScientificCalc(); + assertEquals(120, calc.factorial(5)); + assertEquals(3628800, calc.factorial(10)); + } +}