From 5818c9049ef15853e59e21fb23c072506fbce9ff Mon Sep 17 00:00:00 2001 From: junior Date: Fri, 29 Oct 2021 17:23:40 -0400 Subject: [PATCH 01/29] commnit --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8402302c..1f448b54 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ ## 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. +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. From 117e31c19541131961117ff815119514766b8507 Mon Sep 17 00:00:00 2001 From: Keerthana Srinivasan Date: Fri, 29 Oct 2021 18:06:43 -0400 Subject: [PATCH 02/29] adding team --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8402302c..9b6fb50d 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,6 @@ -# ScientificCalculator (maven) ZCW +#Team Members +# ScientificCalculator (maven) ZCW ## Description From f05320e83b676f4c83931b332a057dbf0147795f Mon Sep 17 00:00:00 2001 From: Keerthana Srinivasan Date: Fri, 29 Oct 2021 18:17:16 -0400 Subject: [PATCH 03/29] team --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 9b6fb50d..0118eb2b 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ #Team Members +teAM MEMBER + # ScientificCalculator (maven) ZCW ## Description From 0ce0444ac3a5d11282dbc5633c060154833a11be Mon Sep 17 00:00:00 2001 From: Keerthana Srinivasan Date: Fri, 29 Oct 2021 18:41:56 -0400 Subject: [PATCH 04/29] test commit to keerthana brnach --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8402302c..d4678f07 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # ScientificCalculator (maven) ZCW - +# TEAM MEMBERS: ## Description From f2568de2f2a1eb0e0244de0ed8a27d047ce5d25a Mon Sep 17 00:00:00 2001 From: junior Date: Fri, 29 Oct 2021 20:58:19 -0400 Subject: [PATCH 05/29] 3rd commit --- README.md | 2 +- .../scientificcalculator/BasicCalculator.java | 32 +++++++++++++ .../scientificcalculator/MainApplication.java | 45 ++++++++++++++++++- .../scientificcalculator/ScFunction.java | 7 +++ 4 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/zipcodewilmington/scientificcalculator/BasicCalculator.java create mode 100644 src/main/java/com/zipcodewilmington/scientificcalculator/ScFunction.java diff --git a/README.md b/README.md index deb2f534..e4f516f5 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ teAM MEMBER Keerthana -# ScientificCalculator (maven) ZCW +# com.zipcodewilmington.scientificcalculator.ScientificCalculator (maven) ZCW ## Description diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/BasicCalculator.java b/src/main/java/com/zipcodewilmington/scientificcalculator/BasicCalculator.java new file mode 100644 index 00000000..de9d983a --- /dev/null +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/BasicCalculator.java @@ -0,0 +1,32 @@ +package com.zipcodewilmington.scientificcalculator; + +public class BasicCalculator { + private Double result; + + + public BasicCalculator(){ + result = 0.0; + + } + + public Double add(double num1, double num2){ + result = num1 + num2; + return result; + } + + public Double subtract(double num1, double num2){ + result = num1 - num2; + return result; + } + + public Double divide(double num1, double num2){ + result = num1 / num2; + return result; + } + + public Double multiply(double num1, double num2){ + result = num1 * num2; + return result; + } + +} diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java b/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java index 5f421325..2c32f4be 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java @@ -1,17 +1,58 @@ package com.zipcodewilmington.scientificcalculator; +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!"); + + /* 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("The user input %s as a d", d)*/ + Scanner scan = new Scanner(System.in); + + System.out.println("select\n 1) add \n 2) subtract\n 3) multiply \n 4) divison\n"); + Integer choice = scan.nextInt(); + + + + + + + System.out.println("Enter first number"); + Double input1 = scan.nextDouble(); + + System.out.println("Enter second number"); + Double input2 = scan.nextDouble(); + + + BasicCalculator calc = new BasicCalculator(); + + switch (choice){ + case 1: + System.out.println(calc.add(input1, input2)); + break; + case 2: + System.out.println(calc.subtract(input1, input2)); + break; + case 3: + System.out.println(calc.multiply(input1, input2)); + break; + case 4: + System.out.println(calc.divide(input1, input2)); + + } + + + + + } } diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/ScFunction.java b/src/main/java/com/zipcodewilmington/scientificcalculator/ScFunction.java new file mode 100644 index 00000000..0b468d75 --- /dev/null +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/ScFunction.java @@ -0,0 +1,7 @@ +package com.zipcodewilmington.scientificcalculator; + +public class ScFunction { + + + +} From b2e1ce61c58eb5afc6788759bb3c45210ad4e5a8 Mon Sep 17 00:00:00 2001 From: Ellis John Date: Fri, 29 Oct 2021 21:12:33 -0400 Subject: [PATCH 06/29] added hello --- .../scientificcalculator/MainApplication.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java b/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java index 2c32f4be..90aa3d19 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java @@ -16,6 +16,9 @@ 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)*/ + + // hello + Scanner scan = new Scanner(System.in); System.out.println("select\n 1) add \n 2) subtract\n 3) multiply \n 4) divison\n"); From 311ee2f8b62eedc1078bcb61da1f2026ae15f057 Mon Sep 17 00:00:00 2001 From: junior Date: Sat, 30 Oct 2021 06:28:15 -0400 Subject: [PATCH 07/29] added some input validation --- .../scientificcalculator/BasicCalculator.java | 9 ++ .../scientificcalculator/MainApplication.java | 113 +++++++++++++++--- 2 files changed, 108 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/BasicCalculator.java b/src/main/java/com/zipcodewilmington/scientificcalculator/BasicCalculator.java index de9d983a..6f7a4474 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/BasicCalculator.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/BasicCalculator.java @@ -28,5 +28,14 @@ public Double multiply(double num1, double num2){ result = num1 * num2; return result; } + public Double SquareRoot(Double num1) { + result = Math.sqrt(num1); + return result; + } + + public Double Square(Double num1) { + result = Math.pow(num1, 2); + return result; + } } diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java b/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java index 2c32f4be..4cd83919 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java @@ -17,13 +17,101 @@ public static void main(String[] args) { Console.println("The user input %s as a integer", i); Console.println("The user input %s as a d", d)*/ Scanner scan = new Scanner(System.in); - - System.out.println("select\n 1) add \n 2) subtract\n 3) multiply \n 4) divison\n"); - Integer choice = scan.nextInt(); - - - - + Integer choice; + + + + while(true) { // first while loop to verify valid input + + System.out.println("Please select between 1) Basic Calculator and 2) Scientific Calculator"); + choice = scan.nextInt(); + + if(choice == 1) { // Option 1 + + while (true) { // second while loop to verify valid input + System.out.println("Please select from the options below\n 1) add \n 2) subtract\n 3) multiply \n 4) divison\n"); + choice = scan.nextInt(); + + if (choice == 1 || choice == 2 || choice == 3 || choice == 4) { + break; + + } + + + else { + System.out.println("###############################"); + System.out.println("# Please enter a valid option #"); + System.out.println("###############################"); + System.out.println(); + continue; + } + + }//second loop + break; + } + + else if(choice == 2){ //Option 2 + + while (true) { // third while loop to verify valid input + System.out.println("Please select from the options below\n"+ + "1) degree\n" + + "2) natural log\n" + + "3) base log\n" + + "4) inverse log\n" + + "5) square root\n" + + "6) inverse\n" + + "7) square\n" + + "8) exponent\n" + + "9) change sign\n" + + "10) sine\n" + + "11) cos\n" + + "12) tan\n" + + "13) inverseCosine\n" + + "14) inverseTangent\n" + + "15) inverseSine\n" + + "16) radian\n" + + "17) factorial\n" + + "18) fibonacci\n"); + choice = scan.nextInt(); + + if (choice == 1 || choice == 2 || choice == 3 || choice == 4 ||choice == 5 || choice == 6 || choice == 7 || choice == 8) { + break; + + } + else { + System.out.println("###############################"); + System.out.println("# Please enter a valid option #"); + System.out.println("###############################"); + System.out.println(); + continue; + } + + }//third loop ends here + break; + } + else { + System.out.println("###############################"); + System.out.println("# Please enter a valid option #"); + System.out.println("###############################"); + System.out.println(); + continue; + + } + } // first loop + + + /*while (true) { + System.out.println("select from teh options below\n 1) add \n 2) subtract\n 3) multiply \n 4) divison\n"); + + choice = scan.nextInt(); + if (choice == 1 || choice == 2 || choice == 3 || choice == 4) { + break; + } else { + System.out.println("Please enter a valid option"); + continue; + } + + }*/ System.out.println("Enter first number"); @@ -35,24 +123,21 @@ public static void main(String[] args) { BasicCalculator calc = new BasicCalculator(); - switch (choice){ + switch (choice) { case 1: System.out.println(calc.add(input1, input2)); - break; + break; case 2: System.out.println(calc.subtract(input1, input2)); - break; + break; case 3: System.out.println(calc.multiply(input1, input2)); - break; + break; case 4: System.out.println(calc.divide(input1, input2)); } - - - } } From 26cfb8263fce1ce08be983fe4550e4a6422ae41b Mon Sep 17 00:00:00 2001 From: junior Date: Sat, 30 Oct 2021 06:39:23 -0400 Subject: [PATCH 08/29] removed unused code --- .../scientificcalculator/MainApplication.java | 22 ------------------- 1 file changed, 22 deletions(-) diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java b/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java index d86696b8..6a5a5c03 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java @@ -8,14 +8,6 @@ public class MainApplication { 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)*/ // hello @@ -103,20 +95,6 @@ else if(choice == 2){ //Option 2 } // first loop - /*while (true) { - System.out.println("select from teh options below\n 1) add \n 2) subtract\n 3) multiply \n 4) divison\n"); - - choice = scan.nextInt(); - if (choice == 1 || choice == 2 || choice == 3 || choice == 4) { - break; - } else { - System.out.println("Please enter a valid option"); - continue; - } - - }*/ - - System.out.println("Enter first number"); Double input1 = scan.nextDouble(); From bd3083a89c0e583840e217a4a7f9cfb031810d92 Mon Sep 17 00:00:00 2001 From: junior Date: Sat, 30 Oct 2021 06:57:56 -0400 Subject: [PATCH 09/29] made adjustment to the validation --- .../scientificcalculator/MainApplication.java | 69 ++++++++++--------- 1 file changed, 35 insertions(+), 34 deletions(-) diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java b/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java index 6a5a5c03..c0184771 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java @@ -10,30 +10,49 @@ public static void main(String[] args) { // hello - + BasicCalculator calc = new BasicCalculator(); Scanner scan = new Scanner(System.in); Integer choice; - - while(true) { // first while loop to verify valid input + //INPUT VALIDATION + while (true) { // first while loop to verify valid input System.out.println("Please select between 1) Basic Calculator and 2) Scientific Calculator"); choice = scan.nextInt(); - if(choice == 1) { // Option 1 + if (choice == 1) { // Option 1 (if statement will execute if value is 1) while (true) { // second while loop to verify valid input System.out.println("Please select from the options below\n 1) add \n 2) subtract\n 3) multiply \n 4) divison\n"); choice = scan.nextInt(); if (choice == 1 || choice == 2 || choice == 3 || choice == 4) { - break; + System.out.println("Enter first number"); + Double input1 = scan.nextDouble(); - } + System.out.println("Enter second number"); + Double input2 = scan.nextDouble(); + switch (choice) { + case 1: + System.out.println(calc.add(input1, input2)); + break; + case 2: + System.out.println(calc.subtract(input1, input2)); + break; + case 3: + System.out.println(calc.multiply(input1, input2)); + break; + case 4: + System.out.println(calc.divide(input1, input2)); + } + + + + break; - else { + } else { System.out.println("###############################"); System.out.println("# Please enter a valid option #"); System.out.println("###############################"); @@ -43,12 +62,10 @@ public static void main(String[] args) { }//second loop break; - } - - else if(choice == 2){ //Option 2 + } else if (choice == 2) { //Option 2 (else if statement will execute if value is 2) while (true) { // third while loop to verify valid input - System.out.println("Please select from the options below\n"+ + System.out.println("Please select from the options below\n" + "1) degree\n" + "2) natural log\n" + "3) base log\n" + @@ -69,11 +86,12 @@ else if(choice == 2){ //Option 2 "18) fibonacci\n"); choice = scan.nextInt(); - if (choice == 1 || choice == 2 || choice == 3 || choice == 4 ||choice == 5 || choice == 6 || choice == 7 || choice == 8) { + if (choice == 1 || choice == 2 || choice == 3 || choice == 4 || choice == 5 || choice == 6 || choice == 7 || choice == 8 + || choice == 9 || choice == 10 || choice == 11 || choice == 12 || choice == 13 || choice == 14 || choice == 15 + || choice == 16 || choice == 17 || choice == 18) { break; - } - else { + } else { System.out.println("###############################"); System.out.println("# Please enter a valid option #"); System.out.println("###############################"); @@ -82,9 +100,9 @@ else if(choice == 2){ //Option 2 } }//third loop ends here + break; - } - else { + } else { System.out.println("###############################"); System.out.println("# Please enter a valid option #"); System.out.println("###############################"); @@ -93,31 +111,14 @@ else if(choice == 2){ //Option 2 } } // first loop + //INPUT VALIDATION END - System.out.println("Enter first number"); - Double input1 = scan.nextDouble(); - System.out.println("Enter second number"); - Double input2 = scan.nextDouble(); - BasicCalculator calc = new BasicCalculator(); - switch (choice) { - case 1: - System.out.println(calc.add(input1, input2)); - break; - case 2: - System.out.println(calc.subtract(input1, input2)); - break; - case 3: - System.out.println(calc.multiply(input1, input2)); - break; - case 4: - System.out.println(calc.divide(input1, input2)); - } } From b1fcd00dfa2f73fb6179efa6ffe7dde0c95d316b Mon Sep 17 00:00:00 2001 From: junior Date: Sat, 30 Oct 2021 08:44:00 -0400 Subject: [PATCH 10/29] made more ajustments to input validation --- .../scientificcalculator/MainApplication.java | 59 +++++++++++++++---- 1 file changed, 48 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java b/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java index c0184771..32b73ed7 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java @@ -1,5 +1,6 @@ package com.zipcodewilmington.scientificcalculator; +import java.util.InputMismatchException; import java.util.Scanner; /** @@ -27,12 +28,53 @@ public static void main(String[] args) { System.out.println("Please select from the options below\n 1) add \n 2) subtract\n 3) multiply \n 4) divison\n"); choice = scan.nextInt(); + if (choice == 1 || choice == 2 || choice == 3 || choice == 4) { - System.out.println("Enter first number"); - Double input1 = scan.nextDouble(); - System.out.println("Enter second number"); - Double input2 = scan.nextDouble(); + Double input2; + + Double input1; + while (true) // Checks for valid input(numbers) + { + try + { + System.out.println("Please enter first number"); + input1 = scan.nextDouble(); + break; + } + catch (Exception e) + { + scan.next(); + System.out.print("That’s not " + + "a Number. Try again: \n"); + } + } + + while (true) // Checks for valid input(numbers) + { + try + { + System.out.println("Please enter second number"); + input2 = scan.nextDouble(); + break; + } + catch (Exception e) + { + scan.next(); + System.out.print("That’s not " + + "a Number. Try again: \n"); + } + } + // System.out.println("Enter first number"); + // input1 = scan.nextDouble(); + + + // System.out.println("Enter second number"); + // input2 = scan.nextDouble(); + + + + switch (choice) { case 1: System.out.println(calc.add(input1, input2)); @@ -45,11 +87,9 @@ public static void main(String[] args) { break; case 4: System.out.println(calc.divide(input1, input2)); - + break; } - - break; } else { @@ -114,12 +154,9 @@ public static void main(String[] args) { //INPUT VALIDATION END + } - - - - } } From 2997012331cc0a4b238bfe23ac395b7245b2950d Mon Sep 17 00:00:00 2001 From: Keerthana Srinivasan Date: Sat, 30 Oct 2021 12:14:57 -0400 Subject: [PATCH 11/29] added basic calc methods and updated console for integer and double --- .../scientificcalculator/BasicCalculator.java | 55 +++++++++++++------ .../scientificcalculator/Console.java | 36 +++++++++++- 2 files changed, 71 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/BasicCalculator.java b/src/main/java/com/zipcodewilmington/scientificcalculator/BasicCalculator.java index 6f7a4474..174e4d5f 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/BasicCalculator.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/BasicCalculator.java @@ -1,41 +1,60 @@ package com.zipcodewilmington.scientificcalculator; public class BasicCalculator { - private Double result; + Double currentValue; public BasicCalculator(){ - result = 0.0; - + currentValue = 0.0; } public Double add(double num1, double num2){ - result = num1 + num2; - return result; + currentValue = num1 + num2; + return currentValue ; } public Double subtract(double num1, double num2){ - result = num1 - num2; - return result; + currentValue = num1 - num2; + return currentValue ; + } + + public Double multiply(double num1, double num2){ + currentValue = num1 * num2; + return currentValue ; } public Double divide(double num1, double num2){ - result = num1 / num2; - return result; + currentValue = num1 / num2; + return currentValue ; } - public Double multiply(double num1, double num2){ - result = num1 * num2; - return result; + public Double squareRoot(Double num1) { + currentValue = Math.sqrt(num1); + return currentValue ; } - public Double SquareRoot(Double num1) { - result = Math.sqrt(num1); - return result; + + public Double square(Double num1) { + currentValue = Math.pow(num1, 2); + return currentValue ; } - public Double Square(Double num1) { - result = Math.pow(num1, 2); - return result; + public Double exponentiation(Double num1, Double num2) { + currentValue = Math.pow(num1, num2); + return currentValue; } + public Double inverse(Double num1) { + currentValue = 1 / num1; + return currentValue; + } + + public Double invertNumber(Double num1) { + currentValue = -1 * num1; + return currentValue; + } + + public Double percentage(Double num1) { + currentValue = num1 / 100; + return currentValue; + } } diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Console.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Console.java index 83f0e97f..6f1ddcd1 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Console.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Console.java @@ -23,10 +23,42 @@ public static String getStringInput(String prompt) { } public static Integer getIntegerInput(String prompt) { - return null; + + Scanner scanner = new Scanner(System.in); + println(prompt); + Integer inputValue = 0; + + while (true) { + try { + inputValue = Integer.valueOf(scanner.nextLine()); + break; + } + catch(Exception e) { + println("Invalid Integer Number!"); + println(prompt); + } + } + + return inputValue; } public static Double getDoubleInput(String prompt) { - return null; + + Scanner scanner = new Scanner(System.in); + println(prompt); + Double inputValue = 0.0; + + while (true) { + try { + inputValue = Double.valueOf(scanner.nextLine()); + break; + } + catch(Exception e) { + println("Invalid Double Number!"); + println(prompt); + } + } + + return inputValue; } } From 9d8f82eb723c9264bd86e4528c43c639c6206b83 Mon Sep 17 00:00:00 2001 From: junior Date: Sat, 30 Oct 2021 12:15:32 -0400 Subject: [PATCH 12/29] created fibonacci class and working --- pom.xml | 8 ++ .../scientificcalculator/Fibonacci.java | 31 +++++++ .../scientificcalculator/MainApplication.java | 15 ++-- .../scientificcalculator/ScFunction.java | 89 +++++++++++++++++++ .../TestScCalculator.java | 8 ++ 5 files changed, 143 insertions(+), 8 deletions(-) create mode 100644 src/main/java/com/zipcodewilmington/scientificcalculator/Fibonacci.java create mode 100644 src/test/java/com/zipcodewilmington/scientific_calculator/TestScCalculator.java diff --git a/pom.xml b/pom.xml index e7cb4f6b..d8cd763e 100644 --- a/pom.xml +++ b/pom.xml @@ -7,6 +7,14 @@ com.zipcodewilmington scientific_calculator 1.0-SNAPSHOT + + + junit + junit + 4.12 + test + + \ No newline at end of file diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Fibonacci.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Fibonacci.java new file mode 100644 index 00000000..29554b72 --- /dev/null +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Fibonacci.java @@ -0,0 +1,31 @@ +package com.zipcodewilmington.scientificcalculator; + +public class Fibonacci { + Double c=0.0, + i=0.0, + j=1.0, + next; + public Fibonacci(){ + + } + public void fib(Double num1) + { + + + if(num1>0) + { + if(c<=1) + next=c; + else + { + next=i+j; + i=j; + j=next; + } + + System.out.print(Math.round(next)+" "); + c++; + fib(--num1); + } + } +} diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java b/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java index 32b73ed7..f5257bc7 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java @@ -12,6 +12,8 @@ public static void main(String[] args) { // hello BasicCalculator calc = new BasicCalculator(); + ScFunction scientificCalc = new ScFunction(); + Fibonacci fibo = new Fibonacci(); Scanner scan = new Scanner(System.in); Integer choice; @@ -29,7 +31,7 @@ public static void main(String[] args) { choice = scan.nextInt(); - if (choice == 1 || choice == 2 || choice == 3 || choice == 4) { + if (choice == 1 || choice == 2 || choice == 3 || choice == 4 || choice == 5) { Double input2; @@ -65,13 +67,6 @@ public static void main(String[] args) { + "a Number. Try again: \n"); } } - // System.out.println("Enter first number"); - // input1 = scan.nextDouble(); - - - // System.out.println("Enter second number"); - // input2 = scan.nextDouble(); - @@ -88,6 +83,10 @@ public static void main(String[] args) { case 4: System.out.println(calc.divide(input1, input2)); break; + case 5: + fibo.fib(input1); // testing fibonacci + break; + } break; diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/ScFunction.java b/src/main/java/com/zipcodewilmington/scientificcalculator/ScFunction.java index 0b468d75..99cb03dd 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/ScFunction.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/ScFunction.java @@ -2,6 +2,95 @@ public class ScFunction { + private Double result; + + public ScFunction() { + result = 0.0; // constructor to initialize variable + } + +// public switchDisplayMode() { +// +// } +// +// public switchDisplayMode(String mode) { +// +// } + + + public double sin(double num1) { + result = Math.sin(num1); + return result; + // this works + } + + public double cosine(double num1) { + result = Math.cos(num1); + return result; + // this works + } + + + public double tangent(double num1) { + result = Math.tan(num1); + return result; + // this works + } + + + public double inverseSin(double num1) { + result = Math.asin(num1); + return result; + // this works + // num1 NEEDS TO BE BETWEEN -1 AND 1 otherwise NaN. + } + + + public double inverseCosine(double num1) { + result = Math.acos(num1); + return result; + // this works + // num1 NEEDS TO BE BETWEEN -1 AND 1 otherwise NaN + } + + + public double inverseTangent(double num1) { + result = Math.atan(num1); + return result; + // this works + // no input restrictions + } + + + public double log(double num1) { + result = Math.log(num1); + return result; + // need to revisit--not sure + } + + + public double inverseLog(double num1) { + result = Math.exp(Math.pow(10, num1)); + return result; + // inputs are traditionally known as x and y + // needs two inputs + } + + public double inverseNaturalLog(double num1) { + result = Math.pow(Math.E, num1); + return result; + + } + + //Fibonacci + + + //Factorial + public Double factorial(Double num1) { + if (num1 == 0) + return 1.00; + else + return (num1 * factorial(num1 - 1)); + } } diff --git a/src/test/java/com/zipcodewilmington/scientific_calculator/TestScCalculator.java b/src/test/java/com/zipcodewilmington/scientific_calculator/TestScCalculator.java new file mode 100644 index 00000000..014f7bea --- /dev/null +++ b/src/test/java/com/zipcodewilmington/scientific_calculator/TestScCalculator.java @@ -0,0 +1,8 @@ +package com.zipcodewilmington.scientific_calculator; + +import org.junit.Test; + +public class TestScCalculator { + + +} From 2f31465156012be6f071750f593ccc3fd8468a61 Mon Sep 17 00:00:00 2001 From: Keerthana Srinivasan Date: Sat, 30 Oct 2021 12:19:53 -0400 Subject: [PATCH 13/29] revert to original state --- .../scientificcalculator/BasicCalculator.java | 44 +++++++++---------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/BasicCalculator.java b/src/main/java/com/zipcodewilmington/scientificcalculator/BasicCalculator.java index 174e4d5f..b8235fc5 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/BasicCalculator.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/BasicCalculator.java @@ -2,59 +2,59 @@ public class BasicCalculator { - Double currentValue; + private Double result; public BasicCalculator(){ - currentValue = 0.0; + result = 0.0; } public Double add(double num1, double num2){ - currentValue = num1 + num2; - return currentValue ; + result = num1 + num2; + return result ; } public Double subtract(double num1, double num2){ - currentValue = num1 - num2; - return currentValue ; + result = num1 - num2; + return result ; } public Double multiply(double num1, double num2){ - currentValue = num1 * num2; - return currentValue ; + result = num1 * num2; + return result ; } public Double divide(double num1, double num2){ - currentValue = num1 / num2; - return currentValue ; + result = num1 / num2; + return result ; } public Double squareRoot(Double num1) { - currentValue = Math.sqrt(num1); - return currentValue ; + result = Math.sqrt(num1); + return result ; } public Double square(Double num1) { - currentValue = Math.pow(num1, 2); - return currentValue ; + result = Math.pow(num1, 2); + return result ; } public Double exponentiation(Double num1, Double num2) { - currentValue = Math.pow(num1, num2); - return currentValue; + result = Math.pow(num1, num2); + return result; } public Double inverse(Double num1) { - currentValue = 1 / num1; - return currentValue; + result = 1 / num1; + return result; } public Double invertNumber(Double num1) { - currentValue = -1 * num1; - return currentValue; + result = -1 * num1; + return result; } public Double percentage(Double num1) { - currentValue = num1 / 100; - return currentValue; + result = num1 / 100; + return result; } } From a71a1f6a7587c753e246f3d21b5b58950146d097 Mon Sep 17 00:00:00 2001 From: Keerthana Srinivasan Date: Sat, 30 Oct 2021 12:21:40 -0400 Subject: [PATCH 14/29] revert to original state --- .../scientificcalculator/BasicCalculator.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/BasicCalculator.java b/src/main/java/com/zipcodewilmington/scientificcalculator/BasicCalculator.java index b8235fc5..69f1427c 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/BasicCalculator.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/BasicCalculator.java @@ -10,32 +10,32 @@ public BasicCalculator(){ public Double add(double num1, double num2){ result = num1 + num2; - return result ; + return result; } public Double subtract(double num1, double num2){ result = num1 - num2; - return result ; + return result; } public Double multiply(double num1, double num2){ result = num1 * num2; - return result ; + return result; } public Double divide(double num1, double num2){ result = num1 / num2; - return result ; + return result; } public Double squareRoot(Double num1) { result = Math.sqrt(num1); - return result ; + return result; } public Double square(Double num1) { result = Math.pow(num1, 2); - return result ; + return result; } public Double exponentiation(Double num1, Double num2) { From 0db203711a4af6dea27c8768390c20d86aa9c2ca Mon Sep 17 00:00:00 2001 From: junior Date: Sat, 30 Oct 2021 12:50:09 -0400 Subject: [PATCH 15/29] small adj to fibonacci --- .../com/zipcodewilmington/scientificcalculator/Fibonacci.java | 2 +- .../zipcodewilmington/scientificcalculator/MainApplication.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Fibonacci.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Fibonacci.java index 29554b72..0b7e23fb 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Fibonacci.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Fibonacci.java @@ -23,7 +23,7 @@ public void fib(Double num1) j=next; } - System.out.print(Math.round(next)+" "); + System.out.print(Math.round(next)+" ");// Math.round removes decimal point from Double c++; fib(--num1); } diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java b/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java index f5257bc7..b80b6480 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java @@ -27,7 +27,7 @@ public static void main(String[] args) { if (choice == 1) { // Option 1 (if statement will execute if value is 1) while (true) { // second while loop to verify valid input - System.out.println("Please select from the options below\n 1) add \n 2) subtract\n 3) multiply \n 4) divison\n"); + System.out.println("Please select from the options below\n 1) add \n 2) subtract\n 3) multiply \n 4) divison\n 5) fibonacci\n"); choice = scan.nextInt(); From c7faef5c983d40c5ab943554d1bff60b860ba144 Mon Sep 17 00:00:00 2001 From: Keerthana Srinivasan Date: Sat, 30 Oct 2021 13:57:06 -0400 Subject: [PATCH 16/29] updated main application with basic operations --- .../scientificcalculator/Console.java | 14 +- .../scientificcalculator/MainApplication.java | 262 +++++++++--------- 2 files changed, 147 insertions(+), 129 deletions(-) diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Console.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Console.java index 6f1ddcd1..962dfb82 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Console.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Console.java @@ -15,6 +15,10 @@ public static void println(String output, Object... args) { print(output + "\n", args); } + public static void displayValue(double currentValue) { + Console.println(">>>>>>>> Display Value: %s <<<<<<<<<< \n", currentValue); + } + public static String getStringInput(String prompt) { Scanner scanner = new Scanner(System.in); println(prompt); @@ -22,7 +26,7 @@ public static String getStringInput(String prompt) { return userInput; } - public static Integer getIntegerInput(String prompt) { + public static Integer getIntegerInput(String prompt, int rangeStart, int rangeStop) { Scanner scanner = new Scanner(System.in); println(prompt); @@ -31,7 +35,13 @@ public static Integer getIntegerInput(String prompt) { while (true) { try { inputValue = Integer.valueOf(scanner.nextLine()); - break; + if (inputValue >= rangeStart && inputValue <= rangeStop) { + break; + } + else { + println("Invalid Choice!"); + println(prompt); + } } catch(Exception e) { println("Invalid Integer Number!"); diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java b/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java index b80b6480..47d2724b 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java @@ -7,104 +7,94 @@ * Created by leon on 2/9/18. */ public class MainApplication { - public static void main(String[] args) { + public static void main(String[] args) { - // hello - BasicCalculator calc = new BasicCalculator(); + // Initialize all required fields + BasicCalculator basicCalc = new BasicCalculator(); ScFunction scientificCalc = new ScFunction(); Fibonacci fibo = new Fibonacci(); - Scanner scan = new Scanner(System.in); - Integer choice; - - - //INPUT VALIDATION - while (true) { // first while loop to verify valid input - - System.out.println("Please select between 1) Basic Calculator and 2) Scientific Calculator"); - choice = scan.nextInt(); - - if (choice == 1) { // Option 1 (if statement will execute if value is 1) - - while (true) { // second while loop to verify valid input - System.out.println("Please select from the options below\n 1) add \n 2) subtract\n 3) multiply \n 4) divison\n 5) fibonacci\n"); - choice = scan.nextInt(); - - - if (choice == 1 || choice == 2 || choice == 3 || choice == 4 || choice == 5) { - - Double input2; - - Double input1; - while (true) // Checks for valid input(numbers) - { - try - { - System.out.println("Please enter first number"); - input1 = scan.nextDouble(); - break; - } - catch (Exception e) - { - scan.next(); - System.out.print("That’s not " - + "a Number. Try again: \n"); - } - } - - while (true) // Checks for valid input(numbers) - { - try - { - System.out.println("Please enter second number"); - input2 = scan.nextDouble(); - break; - } - catch (Exception e) - { - scan.next(); - System.out.print("That’s not " - + "a Number. Try again: \n"); - } - } - - - - switch (choice) { - case 1: - System.out.println(calc.add(input1, input2)); - break; - case 2: - System.out.println(calc.subtract(input1, input2)); - break; - case 3: - System.out.println(calc.multiply(input1, input2)); - break; - case 4: - System.out.println(calc.divide(input1, input2)); - break; - case 5: - fibo.fib(input1); // testing fibonacci - break; - - } - - break; - - } else { - System.out.println("###############################"); - System.out.println("# Please enter a valid option #"); - System.out.println("###############################"); - System.out.println(); - continue; - } - - }//second loop - break; - } else if (choice == 2) { //Option 2 (else if statement will execute if value is 2) - - while (true) { // third while loop to verify valid input - System.out.println("Please select from the options below\n" + + + double currentValue; + int calculatorType; + + // Print Welcome Note + Console.println("Welcome to Our Calculator!"); + + // Get Calculator Type + calculatorType = Console.getIntegerInput("Please select between 1) Basic Calculator and 2) Scientific Calculator", 1, 2); + + // Get First Number to start + currentValue = Console.getDoubleInput("Please enter the number"); + Console.displayValue(currentValue); + + double operandValue = 0.0; + + // Creating infinite loop until user choose to exit + while (true) { + + if (calculatorType == 1) { + + int choice = Console.getIntegerInput("Please select from the options below\n " + + "1) Add \n " + + "2) Subtract\n " + + "3) Multiply \n " + + "4) Divison\n " + + "5) Invert\n" + + "6) Percentage\n" + + "7) Switch to Scientific Calculator\n" + + "8) Clear\n" + + "9) Exit", 1, 9); + + if (choice >= 1 && choice <= 4) { + operandValue = Console.getDoubleInput("Please enter the operand number"); + } + + switch (choice) { + case 1: + currentValue = basicCalc.add(currentValue, operandValue); + Console.displayValue(currentValue); + break; + case 2: + currentValue = basicCalc.subtract(currentValue, operandValue); + Console.displayValue(currentValue); + break; + case 3: + currentValue = basicCalc.multiply(currentValue, operandValue); + Console.displayValue(currentValue); + break; + case 4: + currentValue = basicCalc.divide(currentValue, operandValue); + Console.displayValue(currentValue); + break; + case 5: + currentValue = basicCalc.invertNumber(currentValue); + Console.displayValue(currentValue); + break; + case 6: + currentValue = basicCalc.percentage(currentValue); + Console.displayValue(currentValue); + break; + case 7: + calculatorType = 2; + Console.println("Switching to Scientific Calculator"); + Console.displayValue(currentValue); + break; + case 8: + currentValue = 0.0; + Console.displayValue(currentValue); + currentValue = Console.getDoubleInput("Please enter the number"); + Console.displayValue(currentValue); + break; + } + + if (choice == 9) { + break; + } + } + else { + + int choice = Console.getIntegerInput("Please select from the options below\n " + "1) degree\n" + "2) natural log\n" + "3) base log\n" + @@ -122,40 +112,58 @@ public static void main(String[] args) { "15) inverseSine\n" + "16) radian\n" + "17) factorial\n" + - "18) fibonacci\n"); - choice = scan.nextInt(); - - if (choice == 1 || choice == 2 || choice == 3 || choice == 4 || choice == 5 || choice == 6 || choice == 7 || choice == 8 - || choice == 9 || choice == 10 || choice == 11 || choice == 12 || choice == 13 || choice == 14 || choice == 15 - || choice == 16 || choice == 17 || choice == 18) { + "18) fibonacci\n"+ + "19) Switch to Scientific Calculator" + + "20) Clear" + + "21) Exit", 1, 21); + switch (choice) { + case 1: + // break; + case 2: + // + break; + case 3: + // + break; + case 4: + // + break; + case 5: + currentValue = basicCalc.squareRoot(currentValue); + Console.displayValue(currentValue); + break; + case 6: + currentValue = basicCalc.inverse(currentValue); + Console.displayValue(currentValue); + break; + case 7: + currentValue = basicCalc.square(currentValue); + Console.displayValue(currentValue); + break; + case 18: + fibo.fib(currentValue); + currentValue = Console.getDoubleInput("Please enter the number"); + Console.displayValue(currentValue); + break; + case 19: + // + break; + case 20: + calculatorType = 1; + break; + case 21: + currentValue = 0.0; + Console.displayValue(currentValue); + currentValue = Console.getDoubleInput("Please enter the number"); + Console.displayValue(currentValue); + break; + } - } else { - System.out.println("###############################"); - System.out.println("# Please enter a valid option #"); - System.out.println("###############################"); - System.out.println(); - continue; - } - - }//third loop ends here - - break; - } else { - System.out.println("###############################"); - System.out.println("# Please enter a valid option #"); - System.out.println("###############################"); - System.out.println(); - continue; - + if (choice == 21) { + break; + } } - } // first loop - //INPUT VALIDATION END - - + } } - - - - -} +} \ No newline at end of file From e5bc7da5833735fffa557a27e6afd4c3f5b850fd Mon Sep 17 00:00:00 2001 From: Keerthana Srinivasan Date: Sat, 30 Oct 2021 14:02:37 -0400 Subject: [PATCH 17/29] updated main application with basic operations --- .../scientificcalculator/MainApplication.java | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java b/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java index 47d2724b..b862c70c 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java @@ -113,8 +113,8 @@ public static void main(String[] args) { "16) radian\n" + "17) factorial\n" + "18) fibonacci\n"+ - "19) Switch to Scientific Calculator" + - "20) Clear" + + "19) Switch to Scientific Calculator\n" + + "20) Clear\n" + "21) Exit", 1, 21); switch (choice) { case 1: @@ -147,12 +147,9 @@ public static void main(String[] args) { Console.displayValue(currentValue); break; case 19: - // - break; - case 20: calculatorType = 1; break; - case 21: + case 20: currentValue = 0.0; Console.displayValue(currentValue); currentValue = Console.getDoubleInput("Please enter the number"); @@ -165,5 +162,7 @@ public static void main(String[] args) { } } } + + Console.println("Exit! Thank you"); } } \ No newline at end of file From a718dec4c72366f967dec9209734e2cc9c7a40eb Mon Sep 17 00:00:00 2001 From: junior Date: Sat, 30 Oct 2021 14:11:27 -0400 Subject: [PATCH 18/29] added Unit Tests --- .../TestScCalculator.java | 252 +++++++++++++++++- 1 file changed, 251 insertions(+), 1 deletion(-) diff --git a/src/test/java/com/zipcodewilmington/scientific_calculator/TestScCalculator.java b/src/test/java/com/zipcodewilmington/scientific_calculator/TestScCalculator.java index 014f7bea..4f5ff702 100644 --- a/src/test/java/com/zipcodewilmington/scientific_calculator/TestScCalculator.java +++ b/src/test/java/com/zipcodewilmington/scientific_calculator/TestScCalculator.java @@ -1,8 +1,258 @@ package com.zipcodewilmington.scientific_calculator; + +import com.zipcodewilmington.scientificcalculator.BasicCalculator; +import com.zipcodewilmington.scientificcalculator.ScFunction; +import org.junit.Assert; import org.junit.Test; public class TestScCalculator { -} + + ScFunction scientificCal = new ScFunction(); + BasicCalculator basicCalc = new BasicCalculator(); + @Test + public void positiveintegerAdderTest() { + Double assumed = 2.; + + Double actual = basicCalc.add(1,1); + Assert.assertEquals(assumed, actual); + } + + @Test + public void NegativeIntegerAdderTest() { + Double assumed = -200.; + Double actual = basicCalc.add(0., -200.); + Assert.assertEquals(assumed, actual); + } + + @Test + public void SubtractionTestPositiveReturn() { + Double assumed = 3.; + Double actual = basicCalc.subtract(5., 2.); + Assert.assertEquals(assumed, actual); + + } + + @Test + public void SubtractionTestZeroReturn() { + Double assumed = 0.; + Double actual = basicCalc.subtract(3., 3.); + Assert.assertEquals(assumed, actual); + } + + @Test + public void SubtractionTestNegativeReturn() { + Double assumed = -1.; + Double actual = basicCalc.subtract(5., 6.); + Assert.assertEquals(assumed, actual); + } + + @Test + public void MultiplicationPositiveReturn() { + Double assumed = 40.; + Double actual = basicCalc.multiply(20., 2.); + Assert.assertEquals(assumed, actual); + } + + @Test + public void MultiplicationZeroReturn() { + Double assumed = 0.; + Double actual = basicCalc.multiply(0., 5.); + Assert.assertEquals(assumed, actual); + } + + @Test + public void MultiplicationNegativeReturn() { + Double assumed = -9.; + Double actual = basicCalc.multiply(-3., 3.); + Assert.assertEquals(assumed, actual); + } + + @Test + public void MultiplicationBothNegativeNumbers() { + Double assumed = 9.; + Double actual = basicCalc.multiply(-3., -3.); + Assert.assertEquals(assumed, actual); + } + + @Test + public void DivisionPositiveReturnWholeNumber() { + Double assumed = 10.; + Double actual = basicCalc.divide(20., 2.); + Assert.assertEquals(assumed, actual); + } + + @Test + public void DivisionPositiveReturnDecimal() { + Double assumed = 2.5; + Double actual = basicCalc.divide(5., 2.); + Assert.assertEquals(assumed, actual); + } + + @Test + public void squareRootReturnPositiveWholeNumber() { + Double assumed = 4.; + Double actual = basicCalc.squareRoot(16.); + Assert.assertEquals(assumed, actual); + } + + @Test + public void squareRootReturnPositiveDecimal() { + Double assumed = 4.358898943540674; + Double actual = basicCalc.squareRoot(19.); + Assert.assertEquals(assumed, actual); + } + + @Test + public void SquareReturnPositiveWholeNumber() { + Double assumed = 100.; + Double actual = basicCalc.square(10.); + Assert.assertEquals(assumed, actual); + } + + @Test + public void SquareReturnNegativeWholeNumber() { + Double assumed = 25.; + Double actual = basicCalc.square(-5.); + Assert.assertEquals(assumed, actual); + } + + @Test + public void SquareReturnPositiveDecimal() { + Double assumed = 42.25; + Double actual = basicCalc.square(6.5); + Assert.assertEquals(assumed, actual); + } + + @Test + public void SquareReturnNegativeDecimal() { + Double assumed = 30.25; + Double actual = basicCalc.square(-5.5); + Assert.assertEquals(assumed, actual); + } + + /* @Test + public void ChangeSignFromPositive() { + Double assumed = -5.; + Double actual = ScFunction.changeSign(5.); + Assert.assertEquals(assumed, actual); + } + + @Test + public void ChangeSignFromNegative() { + Double assumed = 5.; + Double actual = ScFunction.changeSign(-5.); + Assert.assertEquals(assumed, actual); + }*/ + + @Test + public void SinePositiveNumber() { + Double assumed = 0.9129452507276277; + Double actual = scientificCal.sin(20.); + Assert.assertEquals(assumed, actual); + } + + @Test + public void SineNegativeNumber() { + Double assumed = 0.5365729180004349; + Double actual = scientificCal.sin(-12.); + Assert.assertEquals(assumed, actual); + } + + @Test + public void SineZeroNumber() { + Double assumed = 0.; + Double actual = scientificCal.sin(0.); + Assert.assertEquals(assumed, actual); + } + + + @Test + public void CosineNegativeNumber() { + Double assumed = -0.7596879128588213; + Double actual = scientificCal.cosine(-15.); + Assert.assertEquals(assumed, actual); + } + + @Test + public void CosinePositiveNumber() { + Double assumed = -0.7596879128588213; + Double actual = scientificCal.cosine(-15.); + Assert.assertEquals(assumed, actual); + } + + @Test + public void CosineZeroNumber() { + Double assumed = 1.; + Double actual = scientificCal.cosine(0.); + Assert.assertEquals(assumed, actual); + } + + @Test + public void TangentZeroNumber() { + Double assumed = 0.; + Double actual = scientificCal.tangent(0.); + Assert.assertEquals(assumed, actual); + } + + @Test + public void TangentPositiveNumber() { + Double assumed = -0.8559934009085188; + Double actual = scientificCal.tangent(15.); + Assert.assertEquals(assumed, actual); + } + + @Test + public void TangentNegativeNumber() { + Double assumed = 0.8559934009085188; + Double actual = scientificCal.tangent(-15.); + Assert.assertEquals(assumed, actual); + } + + @Test + public void ExponentPositiveNumbers() { + Double assumed = 4.; + Double actual = basicCalc.exponentiation(2., 2.); + Assert.assertEquals(assumed, actual); + } + + @Test + public void ExponentNegativeNumbers() { + Double assumed = 4.; + Double actual = basicCalc.exponentiation(-2., 2.); + Assert.assertEquals(assumed, actual); + } + + @Test + public void ExponentNegativeExponentPositiveNumber() { + Double assumed = 0.0625; + Double actual = basicCalc.exponentiation(4., -2.); + Assert.assertEquals(assumed, actual); + } + + @Test + public void ExponentNegativeExponentNegativeNumber() { + Double assumed = 0.0625; + Double actual = basicCalc.exponentiation(-4., -2.); + Assert.assertEquals(assumed, actual); + } + + @Test + public void ExponentZeroPositiveNumber() { + Double assumed = 1.; + Double actual = basicCalc.exponentiation(2., .0); + Assert.assertEquals(assumed, actual); + } + + @Test + public void ExponentZeroZeroNumber() { + Double assumed = 1.; + Double actual = basicCalc.exponentiation(0., .0); + Assert.assertEquals(assumed, actual); + } + } + + + From deded3334b47250da3e7a2984a762e50cbb19286 Mon Sep 17 00:00:00 2001 From: junior Date: Sat, 30 Oct 2021 15:33:05 -0400 Subject: [PATCH 19/29] added new fibonacci method and convert to Binary, Octal and Hex --- .../scientificcalculator/Fibonacci.java | 35 +++++---------- .../scientificcalculator/MainApplication.java | 2 +- .../scientificcalculator/ScFunction.java | 16 ++++++- .../TestScCalculator.java | 45 ++++++++++++------- 4 files changed, 57 insertions(+), 41 deletions(-) diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Fibonacci.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Fibonacci.java index 0b7e23fb..ec03d52d 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Fibonacci.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Fibonacci.java @@ -1,31 +1,18 @@ package com.zipcodewilmington.scientificcalculator; public class Fibonacci { - Double c=0.0, - i=0.0, - j=1.0, - next; - public Fibonacci(){ - } - public void fib(Double num1) - { - - - if(num1>0) - { - if(c<=1) - next=c; - else - { - next=i+j; - i=j; - j=next; - } - - System.out.print(Math.round(next)+" ");// Math.round removes decimal point from Double - c++; - fib(--num1); + public static String fibonacci(Double num1){ + Double maxNumber = num1; + Integer previousNumber = 0; + Integer nextNumber = 1; + String fiboString = ""; + for (int i = 1; i <= maxNumber; i++){ + fiboString += " " + previousNumber; + int sum = previousNumber + nextNumber; + previousNumber = nextNumber; + nextNumber = sum; } + return fiboString; } } diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java b/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java index 0cb28b0f..17f749bf 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java @@ -145,7 +145,7 @@ public static void main(String[] args) { Console.displayValue(currentValue); break; case 18: - fibo.fib(currentValue); + fibo.fibonacci(currentValue); currentValue = Console.getDoubleInput("Please enter the number"); Console.displayValue(currentValue); break; diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/ScFunction.java b/src/main/java/com/zipcodewilmington/scientificcalculator/ScFunction.java index 99cb03dd..07b0f17a 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/ScFunction.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/ScFunction.java @@ -81,7 +81,7 @@ public double inverseNaturalLog(double num1) { } - //Fibonacci + //Fibonacci has own class //Factorial @@ -91,6 +91,20 @@ public Double factorial(Double num1) { else return (num1 * factorial(num1 - 1)); } + // Converts to Octal + public String Octal(Double num1) { + return Integer.toHexString((int) Math.round(num1)); + } + // Converts to Binary + public String Binary(Double num1) { + + return Integer.toBinaryString((int) Math.round(num1)); + } + // Converts to Hexadecimal + public String Hexadecimal(Double num1){ + + return Integer.toHexString((int)Math.round(num1)); + } } diff --git a/src/test/java/com/zipcodewilmington/scientific_calculator/TestScCalculator.java b/src/test/java/com/zipcodewilmington/scientific_calculator/TestScCalculator.java index d6c4ff9c..d792f506 100644 --- a/src/test/java/com/zipcodewilmington/scientific_calculator/TestScCalculator.java +++ b/src/test/java/com/zipcodewilmington/scientific_calculator/TestScCalculator.java @@ -1,9 +1,8 @@ package com.zipcodewilmington.scientific_calculator; -======= - import com.zipcodewilmington.scientificcalculator.BasicCalculator; +import com.zipcodewilmington.scientificcalculator.Fibonacci; import com.zipcodewilmington.scientificcalculator.ScFunction; import org.junit.Assert; @@ -13,7 +12,7 @@ public class TestScCalculator { - + Fibonacci fibo = new Fibonacci(); ScFunction scientificCal = new ScFunction(); BasicCalculator basicCalc = new BasicCalculator(); @Test @@ -137,19 +136,7 @@ public void SquareReturnNegativeDecimal() { Assert.assertEquals(assumed, actual); } - /* @Test - public void ChangeSignFromPositive() { - Double assumed = -5.; - Double actual = ScFunction.changeSign(5.); - Assert.assertEquals(assumed, actual); - } - @Test - public void ChangeSignFromNegative() { - Double assumed = 5.; - Double actual = ScFunction.changeSign(-5.); - Assert.assertEquals(assumed, actual); - }*/ @Test public void SinePositiveNumber() { @@ -256,6 +243,34 @@ public void ExponentZeroZeroNumber() { Double actual = basicCalc.exponentiation(0., .0); Assert.assertEquals(assumed, actual); } + @Test + public void Factorial(){ + Double assumed = 120.; + Double actual = scientificCal.factorial(5.); + Assert.assertEquals(assumed, actual); + } + @Test + public void Fibonacci(){ + String assumed = " 0 1 1 2 3"; + String actual = fibo.fibonacci(5.); + Assert.assertEquals(assumed, actual); + + } + @Test + public void Fibonacci2(){ + String assumed = " 0 1 1 2 3 5 8"; + String actual = fibo.fibonacci(7.); + Assert.assertEquals(assumed, actual); + + } + @Test + public void Fibonacci3BigNumber(){ + String assumed = " 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811 514229 832040 1346269"; + String actual = fibo.fibonacci(32.); + Assert.assertEquals(assumed, actual); + + } + } From 7427d34303171c4ff9c5ab715787f9933269872a Mon Sep 17 00:00:00 2001 From: junior Date: Sat, 30 Oct 2021 16:03:29 -0400 Subject: [PATCH 20/29] added Test for binary, octal and Hex(Still not working) --- .../scientificcalculator/ScFunction.java | 2 +- .../TestScCalculator.java | 39 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/ScFunction.java b/src/main/java/com/zipcodewilmington/scientificcalculator/ScFunction.java index 07b0f17a..4cdc4db2 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/ScFunction.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/ScFunction.java @@ -94,7 +94,7 @@ public Double factorial(Double num1) { // Converts to Octal public String Octal(Double num1) { - return Integer.toHexString((int) Math.round(num1)); + return Integer.toOctalString((int) Math.round(num1)); } // Converts to Binary public String Binary(Double num1) { diff --git a/src/test/java/com/zipcodewilmington/scientific_calculator/TestScCalculator.java b/src/test/java/com/zipcodewilmington/scientific_calculator/TestScCalculator.java index d792f506..9725fe9f 100644 --- a/src/test/java/com/zipcodewilmington/scientific_calculator/TestScCalculator.java +++ b/src/test/java/com/zipcodewilmington/scientific_calculator/TestScCalculator.java @@ -270,7 +270,46 @@ public void Fibonacci3BigNumber(){ Assert.assertEquals(assumed, actual); } + @Test + public void ConvertToBinary(){ + String assumed = "10100"; + String actual = scientificCal.Binary(20.); + Assert.assertEquals(assumed, actual); + } + @Test + public void ConvertToBinary2(){ + String assumed = "11111010000"; + String actual = scientificCal.Binary(2000.); + Assert.assertEquals(assumed, actual); + } + + @Test + public void ConvertToOctal(){ + String assumed = "12"; + String actual = scientificCal.Octal(10.); + Assert.assertEquals(assumed, actual); + } + + @Test + public void ConvertToOctal2(){ + String assumed = "764"; + String actual = scientificCal.Octal(500.); + Assert.assertEquals(assumed, actual); + } + + @Test // Need to Fix this test + public void ConvertToHex(){ + String assumed = "B"; + String actual = scientificCal.Octal(11.); + Assert.assertEquals(assumed, actual); + } + @Test + public void ConvertToHex2(){ + String assumed = "9e"; + String actual = scientificCal.Octal(158.0); + Assert.assertEquals(assumed, actual); + } } From 0a5f6c0e0432b6d18ce79e2501f300307f55ca96 Mon Sep 17 00:00:00 2001 From: Keerthana Srinivasan Date: Sat, 30 Oct 2021 16:58:05 -0400 Subject: [PATCH 21/29] changes made in main application --- .../scientificcalculator/BasicCalculator.java | 22 +- .../scientificcalculator/MainApplication.java | 223 +++++++++--------- 2 files changed, 128 insertions(+), 117 deletions(-) diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/BasicCalculator.java b/src/main/java/com/zipcodewilmington/scientificcalculator/BasicCalculator.java index 69f1427c..3febabac 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/BasicCalculator.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/BasicCalculator.java @@ -2,58 +2,58 @@ public class BasicCalculator { - private Double result; + private double result; public BasicCalculator(){ result = 0.0; } - public Double add(double num1, double num2){ + public double add(double num1, double num2){ result = num1 + num2; return result; } - public Double subtract(double num1, double num2){ + public double subtract(double num1, double num2){ result = num1 - num2; return result; } - public Double multiply(double num1, double num2){ + public double multiply(double num1, double num2){ result = num1 * num2; return result; } - public Double divide(double num1, double num2){ + public double divide(double num1, double num2){ result = num1 / num2; return result; } - public Double squareRoot(Double num1) { + public double squareRoot(double num1) { result = Math.sqrt(num1); return result; } - public Double square(Double num1) { + public double square(double num1) { result = Math.pow(num1, 2); return result; } - public Double exponentiation(Double num1, Double num2) { + public double exponentiation(double num1, double num2) { result = Math.pow(num1, num2); return result; } - public Double inverse(Double num1) { + public double inverse(double num1) { result = 1 / num1; return result; } - public Double invertNumber(Double num1) { + public double invertNumber(double num1) { result = -1 * num1; return result; } - public Double percentage(Double num1) { + public double percentage(double num1) { result = num1 / 100; return result; } diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java b/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java index b862c70c..3903e359 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java @@ -33,68 +33,73 @@ public static void main(String[] args) { // Creating infinite loop until user choose to exit while (true) { - if (calculatorType == 1) { - - int choice = Console.getIntegerInput("Please select from the options below\n " + - "1) Add \n " + - "2) Subtract\n " + - "3) Multiply \n " + - "4) Divison\n " + - "5) Invert\n" + - "6) Percentage\n" + - "7) Switch to Scientific Calculator\n" + - "8) Clear\n" + - "9) Exit", 1, 9); - - if (choice >= 1 && choice <= 4) { - operandValue = Console.getDoubleInput("Please enter the operand number"); - } - - switch (choice) { - case 1: - currentValue = basicCalc.add(currentValue, operandValue); - Console.displayValue(currentValue); - break; - case 2: - currentValue = basicCalc.subtract(currentValue, operandValue); - Console.displayValue(currentValue); - break; - case 3: - currentValue = basicCalc.multiply(currentValue, operandValue); - Console.displayValue(currentValue); - break; - case 4: - currentValue = basicCalc.divide(currentValue, operandValue); - Console.displayValue(currentValue); - break; - case 5: - currentValue = basicCalc.invertNumber(currentValue); - Console.displayValue(currentValue); - break; - case 6: - currentValue = basicCalc.percentage(currentValue); - Console.displayValue(currentValue); - break; - case 7: - calculatorType = 2; - Console.println("Switching to Scientific Calculator"); - Console.displayValue(currentValue); - break; - case 8: - currentValue = 0.0; - Console.displayValue(currentValue); - currentValue = Console.getDoubleInput("Please enter the number"); - Console.displayValue(currentValue); - break; - } - - if (choice == 9) { - break; - } - } - else { - - int choice = Console.getIntegerInput("Please select from the options below\n " + + try { + if (calculatorType == 1) { + + int choice = Console.getIntegerInput("Please select from the options below\n " + + "1) Add \n " + + "2) Subtract\n " + + "3) Multiply \n " + + "4) Divison\n " + + "5) Invert\n" + + "6) Percentage\n" + + "7) Switch to Scientific Calculator\n" + + "8) Clear\n" + + "9) Exit", 1, 9); + + if (choice >= 1 && choice <= 4) { + operandValue = Console.getDoubleInput("Please enter the operand number"); + } + + switch (choice) { + case 1: + currentValue = basicCalc.add(currentValue, operandValue); + Console.displayValue(currentValue); + break; + case 2: + currentValue = basicCalc.subtract(currentValue, operandValue); + Console.displayValue(currentValue); + break; + case 3: + currentValue = basicCalc.multiply(currentValue, operandValue); + Console.displayValue(currentValue); + break; + case 4: + currentValue = basicCalc.divide(currentValue, operandValue); + if (Double.isInfinite(currentValue)) { + Console.println(">>>>>>>> Display Value: %s <<<<<<<<<< ", "Err"); + } + currentValue = Console.getDoubleInput("Please enter the number"); + Console.displayValue(currentValue); + break; + case 5: + currentValue = basicCalc.invertNumber(currentValue); + Console.displayValue(currentValue); + break; + case 6: + currentValue = basicCalc.percentage(currentValue); + Console.displayValue(currentValue); + break; + case 7: + calculatorType = 2; + Console.println("Switching to Scientific Calculator"); + Console.displayValue(currentValue); + break; + case 8: + currentValue = 0.0; + Console.displayValue(currentValue); + currentValue = Console.getDoubleInput("Please enter the number"); + Console.displayValue(currentValue); + break; + } + + // Break the while loop when user choose to exit + if (choice == 9) { + break; + } + } else { + + int choice = Console.getIntegerInput("Please select from the options below\n " + "1) degree\n" + "2) natural log\n" + "3) base log\n" + @@ -112,55 +117,61 @@ public static void main(String[] args) { "15) inverseSine\n" + "16) radian\n" + "17) factorial\n" + - "18) fibonacci\n"+ + "18) fibonacci\n" + "19) Switch to Scientific Calculator\n" + "20) Clear\n" + "21) Exit", 1, 21); - switch (choice) { - case 1: - // - break; - case 2: - // - break; - case 3: - // - break; - case 4: - // - break; - case 5: - currentValue = basicCalc.squareRoot(currentValue); - Console.displayValue(currentValue); - break; - case 6: - currentValue = basicCalc.inverse(currentValue); - Console.displayValue(currentValue); - break; - case 7: - currentValue = basicCalc.square(currentValue); - Console.displayValue(currentValue); - break; - case 18: - fibo.fib(currentValue); - currentValue = Console.getDoubleInput("Please enter the number"); - Console.displayValue(currentValue); - break; - case 19: - calculatorType = 1; - break; - case 20: - currentValue = 0.0; - Console.displayValue(currentValue); - currentValue = Console.getDoubleInput("Please enter the number"); - Console.displayValue(currentValue); - break; - } - - if (choice == 21) { - break; + switch (choice) { + case 1: + + case 2: + + break; + case 3: + // + break; + case 4: + // + break; + case 5: + currentValue = basicCalc.squareRoot(currentValue); + Console.displayValue(currentValue); + break; + case 6: + currentValue = basicCalc.inverse(currentValue); + Console.displayValue(currentValue); + break; + case 7: + currentValue = basicCalc.square(currentValue); + Console.displayValue(currentValue); + break; + case 18: + fibo.fib(currentValue); + currentValue = Console.getDoubleInput("\nPlease enter the number"); + Console.displayValue(currentValue); + break; + case 19: + calculatorType = 1; + break; + case 20: + currentValue = 0.0; + Console.displayValue(currentValue); + currentValue = Console.getDoubleInput("Please enter the number"); + Console.displayValue(currentValue); + break; + } + + if (choice == 21) { + break; + } } } + catch(Exception ex) { + // Display error whenever this catch block is called & start again + Console.println(">>>>>>>> Display Value: %s <<<<<<<<<< ", "Err"); + currentValue = Console.getDoubleInput("Please enter the number"); + Console.displayValue(currentValue); + } } Console.println("Exit! Thank you"); From d6a3a52bc77db413f61f533c86a6dcb93570925d Mon Sep 17 00:00:00 2001 From: Keerthana Srinivasan Date: Sat, 30 Oct 2021 17:09:21 -0400 Subject: [PATCH 22/29] Revert "changes made in main application" This reverts commit 0a5f6c0e0432b6d18ce79e2501f300307f55ca96. --- .../scientificcalculator/BasicCalculator.java | 22 +- .../scientificcalculator/MainApplication.java | 223 +++++++++--------- 2 files changed, 117 insertions(+), 128 deletions(-) diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/BasicCalculator.java b/src/main/java/com/zipcodewilmington/scientificcalculator/BasicCalculator.java index 3febabac..69f1427c 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/BasicCalculator.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/BasicCalculator.java @@ -2,58 +2,58 @@ public class BasicCalculator { - private double result; + private Double result; public BasicCalculator(){ result = 0.0; } - public double add(double num1, double num2){ + public Double add(double num1, double num2){ result = num1 + num2; return result; } - public double subtract(double num1, double num2){ + public Double subtract(double num1, double num2){ result = num1 - num2; return result; } - public double multiply(double num1, double num2){ + public Double multiply(double num1, double num2){ result = num1 * num2; return result; } - public double divide(double num1, double num2){ + public Double divide(double num1, double num2){ result = num1 / num2; return result; } - public double squareRoot(double num1) { + public Double squareRoot(Double num1) { result = Math.sqrt(num1); return result; } - public double square(double num1) { + public Double square(Double num1) { result = Math.pow(num1, 2); return result; } - public double exponentiation(double num1, double num2) { + public Double exponentiation(Double num1, Double num2) { result = Math.pow(num1, num2); return result; } - public double inverse(double num1) { + public Double inverse(Double num1) { result = 1 / num1; return result; } - public double invertNumber(double num1) { + public Double invertNumber(Double num1) { result = -1 * num1; return result; } - public double percentage(double num1) { + public Double percentage(Double num1) { result = num1 / 100; return result; } diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java b/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java index 3903e359..b862c70c 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java @@ -33,73 +33,68 @@ public static void main(String[] args) { // Creating infinite loop until user choose to exit while (true) { - try { - if (calculatorType == 1) { - - int choice = Console.getIntegerInput("Please select from the options below\n " + - "1) Add \n " + - "2) Subtract\n " + - "3) Multiply \n " + - "4) Divison\n " + - "5) Invert\n" + - "6) Percentage\n" + - "7) Switch to Scientific Calculator\n" + - "8) Clear\n" + - "9) Exit", 1, 9); - - if (choice >= 1 && choice <= 4) { - operandValue = Console.getDoubleInput("Please enter the operand number"); - } - - switch (choice) { - case 1: - currentValue = basicCalc.add(currentValue, operandValue); - Console.displayValue(currentValue); - break; - case 2: - currentValue = basicCalc.subtract(currentValue, operandValue); - Console.displayValue(currentValue); - break; - case 3: - currentValue = basicCalc.multiply(currentValue, operandValue); - Console.displayValue(currentValue); - break; - case 4: - currentValue = basicCalc.divide(currentValue, operandValue); - if (Double.isInfinite(currentValue)) { - Console.println(">>>>>>>> Display Value: %s <<<<<<<<<< ", "Err"); - } - currentValue = Console.getDoubleInput("Please enter the number"); - Console.displayValue(currentValue); - break; - case 5: - currentValue = basicCalc.invertNumber(currentValue); - Console.displayValue(currentValue); - break; - case 6: - currentValue = basicCalc.percentage(currentValue); - Console.displayValue(currentValue); - break; - case 7: - calculatorType = 2; - Console.println("Switching to Scientific Calculator"); - Console.displayValue(currentValue); - break; - case 8: - currentValue = 0.0; - Console.displayValue(currentValue); - currentValue = Console.getDoubleInput("Please enter the number"); - Console.displayValue(currentValue); - break; - } - - // Break the while loop when user choose to exit - if (choice == 9) { - break; - } - } else { - - int choice = Console.getIntegerInput("Please select from the options below\n " + + if (calculatorType == 1) { + + int choice = Console.getIntegerInput("Please select from the options below\n " + + "1) Add \n " + + "2) Subtract\n " + + "3) Multiply \n " + + "4) Divison\n " + + "5) Invert\n" + + "6) Percentage\n" + + "7) Switch to Scientific Calculator\n" + + "8) Clear\n" + + "9) Exit", 1, 9); + + if (choice >= 1 && choice <= 4) { + operandValue = Console.getDoubleInput("Please enter the operand number"); + } + + switch (choice) { + case 1: + currentValue = basicCalc.add(currentValue, operandValue); + Console.displayValue(currentValue); + break; + case 2: + currentValue = basicCalc.subtract(currentValue, operandValue); + Console.displayValue(currentValue); + break; + case 3: + currentValue = basicCalc.multiply(currentValue, operandValue); + Console.displayValue(currentValue); + break; + case 4: + currentValue = basicCalc.divide(currentValue, operandValue); + Console.displayValue(currentValue); + break; + case 5: + currentValue = basicCalc.invertNumber(currentValue); + Console.displayValue(currentValue); + break; + case 6: + currentValue = basicCalc.percentage(currentValue); + Console.displayValue(currentValue); + break; + case 7: + calculatorType = 2; + Console.println("Switching to Scientific Calculator"); + Console.displayValue(currentValue); + break; + case 8: + currentValue = 0.0; + Console.displayValue(currentValue); + currentValue = Console.getDoubleInput("Please enter the number"); + Console.displayValue(currentValue); + break; + } + + if (choice == 9) { + break; + } + } + else { + + int choice = Console.getIntegerInput("Please select from the options below\n " + "1) degree\n" + "2) natural log\n" + "3) base log\n" + @@ -117,60 +112,54 @@ public static void main(String[] args) { "15) inverseSine\n" + "16) radian\n" + "17) factorial\n" + - "18) fibonacci\n" + + "18) fibonacci\n"+ "19) Switch to Scientific Calculator\n" + "20) Clear\n" + "21) Exit", 1, 21); - switch (choice) { - case 1: - - case 2: - - break; - case 3: - // - break; - case 4: - // - break; - case 5: - currentValue = basicCalc.squareRoot(currentValue); - Console.displayValue(currentValue); - break; - case 6: - currentValue = basicCalc.inverse(currentValue); - Console.displayValue(currentValue); - break; - case 7: - currentValue = basicCalc.square(currentValue); - Console.displayValue(currentValue); - break; - case 18: - fibo.fib(currentValue); - currentValue = Console.getDoubleInput("\nPlease enter the number"); - Console.displayValue(currentValue); - break; - case 19: - calculatorType = 1; - break; - case 20: - currentValue = 0.0; - Console.displayValue(currentValue); - currentValue = Console.getDoubleInput("Please enter the number"); - Console.displayValue(currentValue); - break; - } - - if (choice == 21) { - break; - } + switch (choice) { + case 1: + // + break; + case 2: + // + break; + case 3: + // + break; + case 4: + // + break; + case 5: + currentValue = basicCalc.squareRoot(currentValue); + Console.displayValue(currentValue); + break; + case 6: + currentValue = basicCalc.inverse(currentValue); + Console.displayValue(currentValue); + break; + case 7: + currentValue = basicCalc.square(currentValue); + Console.displayValue(currentValue); + break; + case 18: + fibo.fib(currentValue); + currentValue = Console.getDoubleInput("Please enter the number"); + Console.displayValue(currentValue); + break; + case 19: + calculatorType = 1; + break; + case 20: + currentValue = 0.0; + Console.displayValue(currentValue); + currentValue = Console.getDoubleInput("Please enter the number"); + Console.displayValue(currentValue); + break; + } + + if (choice == 21) { + break; } - } - catch(Exception ex) { - // Display error whenever this catch block is called & start again - Console.println(">>>>>>>> Display Value: %s <<<<<<<<<< ", "Err"); - currentValue = Console.getDoubleInput("Please enter the number"); - Console.displayValue(currentValue); } } From 341568cc23c3a71b34c6de85fa4f21e8c72b0d47 Mon Sep 17 00:00:00 2001 From: junior Date: Sat, 30 Oct 2021 17:20:23 -0400 Subject: [PATCH 23/29] fixed Hex Test --- .../scientificcalculator/Fibonacci.java | 1 + .../scientificcalculator/ScFunction.java | 10 ++-------- .../scientific_calculator/TestScCalculator.java | 6 +++--- 3 files changed, 6 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Fibonacci.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Fibonacci.java index ec03d52d..5addbc18 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Fibonacci.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Fibonacci.java @@ -7,6 +7,7 @@ public static String fibonacci(Double num1){ Integer previousNumber = 0; Integer nextNumber = 1; String fiboString = ""; + for (int i = 1; i <= maxNumber; i++){ fiboString += " " + previousNumber; int sum = previousNumber + nextNumber; diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/ScFunction.java b/src/main/java/com/zipcodewilmington/scientificcalculator/ScFunction.java index 4cdc4db2..b9ebd8a8 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/ScFunction.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/ScFunction.java @@ -8,13 +8,6 @@ public ScFunction() { result = 0.0; // constructor to initialize variable } -// public switchDisplayMode() { -// -// } -// -// public switchDisplayMode(String mode) { -// -// } public double sin(double num1) { @@ -91,6 +84,7 @@ public Double factorial(Double num1) { else return (num1 * factorial(num1 - 1)); } + // Converts to Octal public String Octal(Double num1) { @@ -104,7 +98,7 @@ public String Binary(Double num1) { // Converts to Hexadecimal public String Hexadecimal(Double num1){ - return Integer.toHexString((int)Math.round(num1)); + return Integer.toHexString((int) Math.round(num1)); } } diff --git a/src/test/java/com/zipcodewilmington/scientific_calculator/TestScCalculator.java b/src/test/java/com/zipcodewilmington/scientific_calculator/TestScCalculator.java index 9725fe9f..bd3eada3 100644 --- a/src/test/java/com/zipcodewilmington/scientific_calculator/TestScCalculator.java +++ b/src/test/java/com/zipcodewilmington/scientific_calculator/TestScCalculator.java @@ -300,14 +300,14 @@ public void ConvertToOctal2(){ @Test // Need to Fix this test public void ConvertToHex(){ - String assumed = "B"; - String actual = scientificCal.Octal(11.); + String assumed = "b"; + String actual = scientificCal.Hexadecimal(11.); Assert.assertEquals(assumed, actual); } @Test public void ConvertToHex2(){ String assumed = "9e"; - String actual = scientificCal.Octal(158.0); + String actual = scientificCal.Hexadecimal(158.0); Assert.assertEquals(assumed, actual); } } From e498dbb508a586860b46a1daee3354195a603afc Mon Sep 17 00:00:00 2001 From: Rogelio Gamboa Jr <51083580+Roggam@users.noreply.github.com> Date: Sat, 30 Oct 2021 18:13:35 -0400 Subject: [PATCH 24/29] ScientificCalculatorUML --- Untitled Diagram.drawio | 1 + 1 file changed, 1 insertion(+) create mode 100644 Untitled Diagram.drawio diff --git a/Untitled Diagram.drawio b/Untitled Diagram.drawio new file mode 100644 index 00000000..60ac5229 --- /dev/null +++ b/Untitled Diagram.drawio @@ -0,0 +1 @@ +7ZbbjpswEIafBqm92Ipjml4Gsj1mq22zanvrBQfcNR5knE2yT98xDCGEjZSuFPWmEhL494yx5/sH4QRJuf2gWVXcQMal47vZ1gnmju97U9fFm1V2rTJ9F7VCrkVGQb2wFE+cRMrL1yLj9SDQAEgjqqGYglI8NQONaQ2bYdgK5PCtFcv5SFimTI7VnyIzBZ0icnv9Ixd50b3Z6w5csi6YhLpgGWwOpODaCRINYNqncptwaYvX1aXNe39idr8xzZU5JwFU/utLnH79PQvv3v749E189sMrWuWRyTUdOJGsrhUrOe3a7LpS1BtRSqZwFK9AmSXNeDhmUuQKn1PcC9coPHJtBFZxRhMGKlTTQshswXawtjuuDUsfulFcgBZPuCyTtCZOa0OG8CeDiKXNRNlFVfMaY267MnhH0g3bDgIXrDYkpCAlq2pxvz9GyXQuVAzGQElBVB88Dt+eLLy3x4l9wKHkRu8whBKCkBxALRB23t70hvImpBUHZppOyMfk4Xy/dI8ZH4j0X1D3R9QdP7bdIbhtj5ltsl01dgDWwDRkNDzwBCQg6rmC1hJCyiOpc4XkK3PSE3XFUqHyRRMzD3vlOxXCSoC5K9l0TyGyjCvLEwwzrIVnSVUglGkqFcV4YT0T903kRLjxBMdeP8bLhmuTgMKzMNFw5OiMDbfuOA/66X4aO4HI+5PzwPuXAh88A/6IsRQNu5Zx98nzXgS4RFSS90TvLPD5lTeiHoypB88Qluyey1uohRFg19dt7BH5fwU38s/s6guxDU80Na5eQPbKNvTr/719MfyTMz/qL+htHPa/Cc3cwc9WcP0H \ No newline at end of file From e8c1d0945ccf2f31beb19dc54ed0acae8933f15f Mon Sep 17 00:00:00 2001 From: Rogelio Gamboa Jr <51083580+Roggam@users.noreply.github.com> Date: Sat, 30 Oct 2021 19:16:53 -0400 Subject: [PATCH 25/29] Update Untitled Diagram.drawio --- Untitled Diagram.drawio | 116 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 115 insertions(+), 1 deletion(-) diff --git a/Untitled Diagram.drawio b/Untitled Diagram.drawio index 60ac5229..d53d2df8 100644 --- a/Untitled Diagram.drawio +++ b/Untitled Diagram.drawio @@ -1 +1,115 @@ -7ZbbjpswEIafBqm92Ipjml4Gsj1mq22zanvrBQfcNR5knE2yT98xDCGEjZSuFPWmEhL494yx5/sH4QRJuf2gWVXcQMal47vZ1gnmju97U9fFm1V2rTJ9F7VCrkVGQb2wFE+cRMrL1yLj9SDQAEgjqqGYglI8NQONaQ2bYdgK5PCtFcv5SFimTI7VnyIzBZ0icnv9Ixd50b3Z6w5csi6YhLpgGWwOpODaCRINYNqncptwaYvX1aXNe39idr8xzZU5JwFU/utLnH79PQvv3v749E189sMrWuWRyTUdOJGsrhUrOe3a7LpS1BtRSqZwFK9AmSXNeDhmUuQKn1PcC9coPHJtBFZxRhMGKlTTQshswXawtjuuDUsfulFcgBZPuCyTtCZOa0OG8CeDiKXNRNlFVfMaY267MnhH0g3bDgIXrDYkpCAlq2pxvz9GyXQuVAzGQElBVB88Dt+eLLy3x4l9wKHkRu8whBKCkBxALRB23t70hvImpBUHZppOyMfk4Xy/dI8ZH4j0X1D3R9QdP7bdIbhtj5ltsl01dgDWwDRkNDzwBCQg6rmC1hJCyiOpc4XkK3PSE3XFUqHyRRMzD3vlOxXCSoC5K9l0TyGyjCvLEwwzrIVnSVUglGkqFcV4YT0T903kRLjxBMdeP8bLhmuTgMKzMNFw5OiMDbfuOA/66X4aO4HI+5PzwPuXAh88A/6IsRQNu5Zx98nzXgS4RFSS90TvLPD5lTeiHoypB88Qluyey1uohRFg19dt7BH5fwU38s/s6guxDU80Na5eQPbKNvTr/719MfyTMz/qL+htHPa/Cc3cwc9WcP0H \ No newline at end of file + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From f82e4c07a909dc4bcee31657defc7d5c70874f8a Mon Sep 17 00:00:00 2001 From: Keerthana Srinivasan Date: Sun, 31 Oct 2021 09:27:56 -0400 Subject: [PATCH 26/29] completed --- .../scientificcalculator/Console.java | 77 +++++++- .../scientificcalculator/MainApplication.java | 174 ++++++++++++------ .../scientificcalculator/ScFunction.java | 62 +++++-- .../TestScCalculator.java | 22 +-- 4 files changed, 251 insertions(+), 84 deletions(-) diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Console.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Console.java index 962dfb82..63e90aa2 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Console.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Console.java @@ -7,6 +7,12 @@ */ public class Console { + private static int displayMode; + + public static void setDisplayMode(int givenDisplayMode) { + displayMode = givenDisplayMode; + } + public static void print(String output, Object... args) { System.out.printf(output, args); } @@ -16,7 +22,11 @@ public static void println(String output, Object... args) { } public static void displayValue(double currentValue) { - Console.println(">>>>>>>> Display Value: %s <<<<<<<<<< \n", currentValue); + Console.println(">>>>>>>> Display Value (in %s): %s <<<<<<<<<< \n", displayMode, applyDisplayMode(currentValue, displayMode)); + } + + public static void displayValue(double currentValue, String unitMode) { + Console.println(">>>>>>>> Display Value (Mode: %s): %s <<<<<<<<<< \n", unitMode, currentValue); } public static String getStringInput(String prompt) { @@ -55,20 +65,77 @@ public static Integer getIntegerInput(String prompt, int rangeStart, int rangeSt public static Double getDoubleInput(String prompt) { Scanner scanner = new Scanner(System.in); - println(prompt); + println(prompt + " (in %s): ", displayMode); Double inputValue = 0.0; while (true) { try { - inputValue = Double.valueOf(scanner.nextLine()); + inputValue = Double.valueOf(applyDisplayMode(scanner.nextLine(), displayMode)); break; } catch(Exception e) { - println("Invalid Double Number!"); - println(prompt); + println("Invalid Input!"); + println(prompt + "( in %s): ", displayMode); } } return inputValue; } + + public static String applyDisplayMode(double currentValue, int displayMode) { + String output = null; + switch (displayMode) { + case 1: + output = String.valueOf(currentValue); + break; + case 2: + output = Integer.toBinaryString((int) Math.round(currentValue)); + break; + case 3: + output = Integer.toOctalString((int) Math.round(currentValue)); + break; + case 4: + output = Integer.toHexString((int) Math.round(currentValue)); + break; + } + return output; + } + + public static Integer applyDisplayMode(String inputValue, int displayMode) { + Integer output = null; + switch (displayMode) { + case 1: + output = Integer.parseInt(inputValue, 10); + break; + case 2: + output = Integer.parseInt(inputValue, 2); + break; + case 3: + output = Integer.parseInt(inputValue, 8); + break; + case 4: + output = Integer.parseInt(inputValue, 16); + break; + } + return output; + } + + public static String getDisplayMode() { + String displayModeValue = null; + switch (displayMode) { + case 1: + displayModeValue = "Decimal"; + break; + case 2: + displayModeValue = "Binary"; + break; + case 3: + displayModeValue = "Octal"; + break; + case 4: + displayModeValue = "HexaDecimal"; + break; + } + return displayModeValue; + } } diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java b/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java index 17f749bf..d9260694 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java @@ -1,22 +1,33 @@ package com.zipcodewilmington.scientificcalculator; -import java.util.InputMismatchException; -import java.util.Scanner; - /** * Created by leon on 2/9/18. */ public class MainApplication { + private double currentValue; + private int calculatorType; + + // Initialize all required fields + private BasicCalculator basicCalc; + private ScFunction scientificCalc; + private Fibonacci fibo; + + public MainApplication() { + basicCalc = new BasicCalculator(); + scientificCalc = new ScFunction(); + fibo = new Fibonacci(); + currentValue = 0.0; + Console.setDisplayMode(1); + } + public static void main(String[] args) { - // Initialize all required fields - BasicCalculator basicCalc = new BasicCalculator(); - ScFunction scientificCalc = new ScFunction(); - Fibonacci fibo = new Fibonacci(); + MainApplication mainApplication = new MainApplication(); + mainApplication.runCalculator(); + } - double currentValue; - int calculatorType; + private void runCalculator() { // Print Welcome Note Console.println("Welcome to Our Calculator!"); @@ -40,17 +51,21 @@ public static void main(String[] args) { "2) Subtract\n " + "3) Multiply \n " + "4) Divison\n " + - "5) Invert\n" + - "6) Percentage\n" + - "7) Switch to Scientific Calculator\n" + - "8) Clear\n" + - "9) Exit", 1, 9); - - if (choice >= 1 && choice <= 4) { + "5) Exponential\n" + + "6) Square\n" + + "7) SquareRoot\n" + + "8) Inverse\n" + + "9) Invert\n" + + "10) Percentage\n" + + "11) Switch to Scientific Calculator\n" + + "12) Switch to Display Mode\n" + + "13) Clear\n" + + "14) Exit", 1, 13); + + if (choice >= 1 && choice <= 5) { operandValue = Console.getDoubleInput("Please enter the operand number"); } - switch (choice) { case 1: currentValue = basicCalc.add(currentValue, operandValue); @@ -69,19 +84,41 @@ public static void main(String[] args) { Console.displayValue(currentValue); break; case 5: - currentValue = basicCalc.invertNumber(currentValue); + currentValue = basicCalc.exponentiation(currentValue, operandValue); Console.displayValue(currentValue); break; case 6: - currentValue = basicCalc.percentage(currentValue); + currentValue = basicCalc.square(currentValue); Console.displayValue(currentValue); break; case 7: + currentValue = basicCalc.squareRoot(currentValue); + Console.displayValue(currentValue); + break; + case 8: + currentValue = basicCalc.inverse(currentValue); + Console.displayValue(currentValue); + break; + case 9: + currentValue = basicCalc.invertNumber(currentValue); + Console.displayValue(currentValue); + break; + case 10: + currentValue = basicCalc.percentage(currentValue); + Console.displayValue(currentValue); + break; + case 11: calculatorType = 2; Console.println("Switching to Scientific Calculator"); Console.displayValue(currentValue); break; - case 8: + case 12: + Console.println("Switching to Display Mode"); + int displayMode = Console.getIntegerInput("Please select display mode\n 1) Decimal\n 2) Binary\n 3) Octal\n 4) HexaDecimal", 1, 4); + Console.setDisplayMode(displayMode); + Console.displayValue(currentValue); + break; + case 13: currentValue = 0.0; Console.displayValue(currentValue); currentValue = Console.getDoubleInput("Please enter the number"); @@ -89,70 +126,94 @@ public static void main(String[] args) { break; } - if (choice == 9) { + if (choice == 14) { break; } } else { int choice = Console.getIntegerInput("Please select from the options below\n " + + "1) natural log\n" + + "2) base log\n" + + "3) inverse log\n" + + "4) change sign\n" + + "5) sine\n" + + "6) cos\n" + + "7) tan\n" + + "8) inverseCosine\n" + + "9) inverseTangent\n" + + "10) inverseSine\n" + + "11) factorial\n" + + "12) fibonacci\n"+ + "13) Switch Unit Mode\n" + + "14) Choose Unit Mode\n" + + "15) Switch to Scientific Calculator\n" + + "16) Clear\n" + + "17) Exit", 1, 21); - - "1) degree\n" + - "2) natural log\n" + - "3) base log\n" + - "4) inverse log\n" + - "5) square root\n" + - "6) inverse\n" + - "7) square\n" + - "8) exponent\n" + - "9) change sign\n" + - "10) sine\n" + - "11) cos\n" + - "12) tan\n" + - "13) inverseCosine\n" + - "14) inverseTangent\n" + - "15) inverseSine\n" + - "16) radian\n" + - "17) factorial\n" + - "18) fibonacci\n"+ - "19) Switch to Scientific Calculator\n" + - "20) Clear\n" + - "21) Exit", 1, 21); switch (choice) { case 1: - // + currentValue = scientificCalc.inverseNaturalLog(currentValue); + Console.displayValue(currentValue); break; case 2: - // + currentValue = scientificCalc.log(currentValue); + Console.displayValue(currentValue); break; case 3: - // + currentValue = scientificCalc.inverseLog(currentValue); + Console.displayValue(currentValue); break; case 4: - // + currentValue = scientificCalc.changesign(currentValue); + Console.displayValue(currentValue); break; case 5: - currentValue = basicCalc.squareRoot(currentValue); - Console.displayValue(currentValue); + currentValue = scientificCalc.sin(currentValue); + Console.displayValue(currentValue, scientificCalc.getUnitMode()); break; case 6: - currentValue = basicCalc.inverse(currentValue); - Console.displayValue(currentValue); + currentValue = scientificCalc.cosine(currentValue); + Console.displayValue(currentValue, scientificCalc.getUnitMode()); break; case 7: - currentValue = basicCalc.square(currentValue); + currentValue = scientificCalc.tangent(currentValue); + Console.displayValue(currentValue, scientificCalc.getUnitMode()); + break; + case 8: + currentValue = scientificCalc.inverseCosine(currentValue); + Console.displayValue(currentValue, scientificCalc.getUnitMode()); + break; + case 9: + currentValue = scientificCalc.inverseTangent(currentValue); + Console.displayValue(currentValue, scientificCalc.getUnitMode()); + break; + case 10: + currentValue = scientificCalc.inverseSin(currentValue); + Console.displayValue(currentValue, scientificCalc.getUnitMode()); + break; + case 11: + currentValue = scientificCalc.factorial(currentValue); Console.displayValue(currentValue); break; - case 18: + case 12: fibo.fibonacci(currentValue); currentValue = Console.getDoubleInput("Please enter the number"); Console.displayValue(currentValue); break; - case 19: + case 13: + scientificCalc.switchUnitMode(); + scientificCalc.applyUnitMode(currentValue); + Console.displayValue(currentValue, scientificCalc.getUnitMode()); + break; + case 14: + int choosenUnitMode = Console.getIntegerInput("Please select unit mode\n 1) Degree\n 2) Radian\n", 1, 2); + scientificCalc.setUnitMode(choosenUnitMode); + break; + case 15: calculatorType = 1; break; - case 20: + case 16: currentValue = 0.0; Console.displayValue(currentValue); currentValue = Console.getDoubleInput("Please enter the number"); @@ -160,12 +221,11 @@ public static void main(String[] args) { break; } - if (choice == 21) { + if (choice == 17) { break; } } } - Console.println("Exit! Thank you"); } } \ No newline at end of file diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/ScFunction.java b/src/main/java/com/zipcodewilmington/scientificcalculator/ScFunction.java index b9ebd8a8..dfa028c7 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/ScFunction.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/ScFunction.java @@ -4,20 +4,24 @@ public class ScFunction { private Double result; + // 1 for degree & 2 for radian + private int unitsMode; + public ScFunction() { result = 0.0; // constructor to initialize variable + unitsMode = 1; } - - public double sin(double num1) { result = Math.sin(num1); + result = applyUnitMode(result); return result; // this works } public double cosine(double num1) { result = Math.cos(num1); + result = applyUnitMode(result); return result; // this works } @@ -25,6 +29,7 @@ public double cosine(double num1) { public double tangent(double num1) { result = Math.tan(num1); + result = applyUnitMode(result); return result; // this works } @@ -32,6 +37,7 @@ public double tangent(double num1) { public double inverseSin(double num1) { result = Math.asin(num1); + result = applyUnitMode(result); return result; // this works // num1 NEEDS TO BE BETWEEN -1 AND 1 otherwise NaN. @@ -40,6 +46,7 @@ public double inverseSin(double num1) { public double inverseCosine(double num1) { result = Math.acos(num1); + result = applyUnitMode(result); return result; // this works // num1 NEEDS TO BE BETWEEN -1 AND 1 otherwise NaN @@ -48,6 +55,7 @@ public double inverseCosine(double num1) { public double inverseTangent(double num1) { result = Math.atan(num1); + result = applyUnitMode(result); return result; // this works // no input restrictions @@ -74,6 +82,21 @@ public double inverseNaturalLog(double num1) { } + public double changesign(double num1) { + // Implement change sign + return num1; + } + + public double rad(double num1){ + result = Math.toRadians(num1); + return result; + } + + public double degree(double num1){ + result = Math.toDegrees(num1); + return result; + } + //Fibonacci has own class @@ -85,20 +108,37 @@ public Double factorial(Double num1) { return (num1 * factorial(num1 - 1)); } - // Converts to Octal - public String Octal(Double num1) { + public Double applyUnitMode(Double result) { - return Integer.toOctalString((int) Math.round(num1)); + if (unitsMode == 2) { + result = Math.toRadians(result); + } + return result; } - // Converts to Binary - public String Binary(Double num1) { - return Integer.toBinaryString((int) Math.round(num1)); + public void switchUnitMode() { + + if (unitsMode == 1) { + unitsMode = 2; + Console.println("Switched Unit Mode from Degree to Radian"); + } else { + unitsMode = 1; + Console.println("Switched Unit Mode from Radian to Degree"); + } } - // Converts to Hexadecimal - public String Hexadecimal(Double num1){ - return Integer.toHexString((int) Math.round(num1)); + public void setUnitMode(int givenUnitMode) { + + unitsMode = givenUnitMode; } + public String getUnitMode() { + if (unitsMode == 1) { + return "Degree"; + } else { + return "Radian"; + } + } } + + diff --git a/src/test/java/com/zipcodewilmington/scientific_calculator/TestScCalculator.java b/src/test/java/com/zipcodewilmington/scientific_calculator/TestScCalculator.java index bd3eada3..615113a4 100644 --- a/src/test/java/com/zipcodewilmington/scientific_calculator/TestScCalculator.java +++ b/src/test/java/com/zipcodewilmington/scientific_calculator/TestScCalculator.java @@ -2,6 +2,7 @@ import com.zipcodewilmington.scientificcalculator.BasicCalculator; +import com.zipcodewilmington.scientificcalculator.Console; import com.zipcodewilmington.scientificcalculator.Fibonacci; import com.zipcodewilmington.scientificcalculator.ScFunction; import org.junit.Assert; @@ -270,44 +271,43 @@ public void Fibonacci3BigNumber(){ Assert.assertEquals(assumed, actual); } - @Test - public void ConvertToBinary(){ + @Test + public void ConvertToBinary() { String assumed = "10100"; - String actual = scientificCal.Binary(20.); + String actual = Console.applyDisplayMode(20, 2); Assert.assertEquals(assumed, actual); } - @Test - public void ConvertToBinary2(){ + @Test + public void ConvertToBinary2(){ String assumed = "11111010000"; - String actual = scientificCal.Binary(2000.); + String actual = Console.applyDisplayMode(2000, 2); Assert.assertEquals(assumed, actual); } @Test public void ConvertToOctal(){ String assumed = "12"; - String actual = scientificCal.Octal(10.); + String actual = Console.applyDisplayMode(10, 3); Assert.assertEquals(assumed, actual); } - @Test public void ConvertToOctal2(){ String assumed = "764"; - String actual = scientificCal.Octal(500.); + String actual = Console.applyDisplayMode(500, 3); Assert.assertEquals(assumed, actual); } @Test // Need to Fix this test public void ConvertToHex(){ String assumed = "b"; - String actual = scientificCal.Hexadecimal(11.); + String actual = Console.applyDisplayMode(11, 4); Assert.assertEquals(assumed, actual); } @Test public void ConvertToHex2(){ String assumed = "9e"; - String actual = scientificCal.Hexadecimal(158.0); + String actual = Console.applyDisplayMode(158.0, 4); Assert.assertEquals(assumed, actual); } } From cebe582329d8d82539a5fa00a4b7f101ff1800e4 Mon Sep 17 00:00:00 2001 From: Rogelio Gamboa Jr <51083580+Roggam@users.noreply.github.com> Date: Sun, 31 Oct 2021 12:18:34 -0400 Subject: [PATCH 27/29] Updated UML Diagram --- Untitled Diagram.drawio | 111 ++++++++++++++++++++++------------------ 1 file changed, 61 insertions(+), 50 deletions(-) diff --git a/Untitled Diagram.drawio b/Untitled Diagram.drawio index d53d2df8..4e21f2c8 100644 --- a/Untitled Diagram.drawio +++ b/Untitled Diagram.drawio @@ -1,114 +1,125 @@ - + - + - - - - - - - - - - + - + - + - + - - + + - - + + - - + + - - + + - + - + - + - + - + - + - + - + - + - - - - - - - - + + + - - - - + - + - + + + - + - + + - + + + - + + + + + + + + + + + + + + + + + + + + + + + + From 75a4082936d4ceb2ac8a9b88d488f29125f1b4b8 Mon Sep 17 00:00:00 2001 From: Keerthana Srinivasan Date: Sun, 31 Oct 2021 14:15:23 -0400 Subject: [PATCH 28/29] final output --- .../scientificcalculator/Console.java | 10 +- .../scientificcalculator/MainApplication.java | 382 ++++++++++-------- 2 files changed, 212 insertions(+), 180 deletions(-) diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Console.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Console.java index 63e90aa2..29f74846 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Console.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Console.java @@ -21,8 +21,12 @@ public static void println(String output, Object... args) { print(output + "\n", args); } + public static void displayValue(String currentValue) { + Console.println(">>>>>>>> Display Value: %s <<<<<<<<<< \n", currentValue); + } + public static void displayValue(double currentValue) { - Console.println(">>>>>>>> Display Value (in %s): %s <<<<<<<<<< \n", displayMode, applyDisplayMode(currentValue, displayMode)); + Console.println(">>>>>>>> Display Value (in %s): %s <<<<<<<<<< \n", getDisplayMode(), applyDisplayMode(currentValue, displayMode)); } public static void displayValue(double currentValue, String unitMode) { @@ -65,7 +69,7 @@ public static Integer getIntegerInput(String prompt, int rangeStart, int rangeSt public static Double getDoubleInput(String prompt) { Scanner scanner = new Scanner(System.in); - println(prompt + " (in %s): ", displayMode); + println(prompt + " (in %s): ", getDisplayMode()); Double inputValue = 0.0; while (true) { @@ -75,7 +79,7 @@ public static Double getDoubleInput(String prompt) { } catch(Exception e) { println("Invalid Input!"); - println(prompt + "( in %s): ", displayMode); + println(prompt + "( in %s): ", getDisplayMode()); } } diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java b/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java index d9260694..fda7bc8e 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java @@ -33,199 +33,227 @@ private void runCalculator() { Console.println("Welcome to Our Calculator!"); // Get Calculator Type + calculatorType = Console.getIntegerInput("Please select between 1) Basic Calculator and 2) Scientific Calculator", 1, 2); // Get First Number to start currentValue = Console.getDoubleInput("Please enter the number"); Console.displayValue(currentValue); - double operandValue = 0.0; - // Creating infinite loop until user choose to exit while (true) { - if (calculatorType == 1) { - - int choice = Console.getIntegerInput("Please select from the options below\n " + - "1) Add \n " + - "2) Subtract\n " + - "3) Multiply \n " + - "4) Divison\n " + - "5) Exponential\n" + - "6) Square\n" + - "7) SquareRoot\n" + - "8) Inverse\n" + - "9) Invert\n" + - "10) Percentage\n" + - "11) Switch to Scientific Calculator\n" + - "12) Switch to Display Mode\n" + - "13) Clear\n" + - "14) Exit", 1, 13); - - if (choice >= 1 && choice <= 5) { - operandValue = Console.getDoubleInput("Please enter the operand number"); - } + try { - switch (choice) { - case 1: - currentValue = basicCalc.add(currentValue, operandValue); - Console.displayValue(currentValue); - break; - case 2: - currentValue = basicCalc.subtract(currentValue, operandValue); - Console.displayValue(currentValue); - break; - case 3: - currentValue = basicCalc.multiply(currentValue, operandValue); - Console.displayValue(currentValue); - break; - case 4: - currentValue = basicCalc.divide(currentValue, operandValue); - Console.displayValue(currentValue); - break; - case 5: - currentValue = basicCalc.exponentiation(currentValue, operandValue); - Console.displayValue(currentValue); - break; - case 6: - currentValue = basicCalc.square(currentValue); - Console.displayValue(currentValue); - break; - case 7: - currentValue = basicCalc.squareRoot(currentValue); - Console.displayValue(currentValue); - break; - case 8: - currentValue = basicCalc.inverse(currentValue); - Console.displayValue(currentValue); - break; - case 9: - currentValue = basicCalc.invertNumber(currentValue); - Console.displayValue(currentValue); - break; - case 10: - currentValue = basicCalc.percentage(currentValue); - Console.displayValue(currentValue); - break; - case 11: - calculatorType = 2; - Console.println("Switching to Scientific Calculator"); - Console.displayValue(currentValue); + if (calculatorType == 1) { + int choice = runBasicCalculator(); + if (choice == 14) { break; - case 12: - Console.println("Switching to Display Mode"); - int displayMode = Console.getIntegerInput("Please select display mode\n 1) Decimal\n 2) Binary\n 3) Octal\n 4) HexaDecimal", 1, 4); - Console.setDisplayMode(displayMode); - Console.displayValue(currentValue); + } + } else { + int choice = runScientificCalculator(); + if (choice == 17) { break; - case 13: - currentValue = 0.0; - Console.displayValue(currentValue); - currentValue = Console.getDoubleInput("Please enter the number"); - Console.displayValue(currentValue); - break; - } - - if (choice == 14) { - break; + } } + } catch (Exception e) { + Console.displayValue("Err"); + currentValue = Console.getDoubleInput("Please enter the number"); + Console.displayValue(currentValue); } - else { - - int choice = Console.getIntegerInput("Please select from the options below\n " + - "1) natural log\n" + - "2) base log\n" + - "3) inverse log\n" + - "4) change sign\n" + - "5) sine\n" + - "6) cos\n" + - "7) tan\n" + - "8) inverseCosine\n" + - "9) inverseTangent\n" + - "10) inverseSine\n" + - "11) factorial\n" + - "12) fibonacci\n"+ - "13) Switch Unit Mode\n" + - "14) Choose Unit Mode\n" + - "15) Switch to Scientific Calculator\n" + - "16) Clear\n" + - "17) Exit", 1, 21); - - switch (choice) { - case 1: - currentValue = scientificCalc.inverseNaturalLog(currentValue); - Console.displayValue(currentValue); - break; - case 2: - currentValue = scientificCalc.log(currentValue); - Console.displayValue(currentValue); - break; - case 3: - currentValue = scientificCalc.inverseLog(currentValue); - Console.displayValue(currentValue); - break; - case 4: - currentValue = scientificCalc.changesign(currentValue); - Console.displayValue(currentValue); - break; - case 5: - currentValue = scientificCalc.sin(currentValue); - Console.displayValue(currentValue, scientificCalc.getUnitMode()); - break; - case 6: - currentValue = scientificCalc.cosine(currentValue); - Console.displayValue(currentValue, scientificCalc.getUnitMode()); - break; - case 7: - currentValue = scientificCalc.tangent(currentValue); - Console.displayValue(currentValue, scientificCalc.getUnitMode()); - break; - case 8: - currentValue = scientificCalc.inverseCosine(currentValue); - Console.displayValue(currentValue, scientificCalc.getUnitMode()); - break; - case 9: - currentValue = scientificCalc.inverseTangent(currentValue); - Console.displayValue(currentValue, scientificCalc.getUnitMode()); - break; - case 10: - currentValue = scientificCalc.inverseSin(currentValue); - Console.displayValue(currentValue, scientificCalc.getUnitMode()); - break; - case 11: - currentValue = scientificCalc.factorial(currentValue); - Console.displayValue(currentValue); - break; - case 12: - fibo.fibonacci(currentValue); - currentValue = Console.getDoubleInput("Please enter the number"); - Console.displayValue(currentValue); - break; - case 13: - scientificCalc.switchUnitMode(); - scientificCalc.applyUnitMode(currentValue); - Console.displayValue(currentValue, scientificCalc.getUnitMode()); - break; - case 14: - int choosenUnitMode = Console.getIntegerInput("Please select unit mode\n 1) Degree\n 2) Radian\n", 1, 2); - scientificCalc.setUnitMode(choosenUnitMode); - break; - case 15: - calculatorType = 1; - break; - case 16: - currentValue = 0.0; - Console.displayValue(currentValue); - currentValue = Console.getDoubleInput("Please enter the number"); - Console.displayValue(currentValue); - break; - } + } + Console.println("Exit! Thank you"); + } + + private int runBasicCalculator() { + + double operandValue = 0; + int choice = Console.getIntegerInput("Please select from the options below\n " + + "1) Add \n " + + "2) Subtract\n " + + "3) Multiply \n " + + "4) Division\n " + + "5) Exponential\n" + + "6) Square\n" + + "7) SquareRoot\n" + + "8) Inverse\n" + + "9) Invert\n" + + "10) Percentage\n" + + "11) Switch to Scientific Calculator\n" + + "12) Switch to Display Mode\n" + + "13) Clear\n" + + "14) Exit", 1, 14); - if (choice == 17) { - break; + if (choice >= 1 && choice <= 5) { + operandValue = Console.getDoubleInput("Please enter the operand number"); + } + + switch (choice) { + case 1: + currentValue = basicCalc.add(currentValue, operandValue); + Console.displayValue(currentValue); + break; + case 2: + currentValue = basicCalc.subtract(currentValue, operandValue); + Console.displayValue(currentValue); + break; + case 3: + currentValue = basicCalc.multiply(currentValue, operandValue); + Console.displayValue(currentValue); + break; + case 4: + currentValue = basicCalc.divide(currentValue, operandValue); + if (Double.isInfinite(currentValue)) { + Console.displayValue("Err"); + currentValue = Console.getDoubleInput("Please enter the number"); } - } + Console.displayValue(currentValue); + break; + case 5: + currentValue = basicCalc.exponentiation(currentValue, operandValue); + Console.displayValue(currentValue); + break; + case 6: + currentValue = basicCalc.square(currentValue); + Console.displayValue(currentValue); + break; + case 7: + currentValue = basicCalc.squareRoot(currentValue); + Console.displayValue(currentValue); + break; + case 8: + currentValue = basicCalc.inverse(currentValue); + Console.displayValue(currentValue); + break; + case 9: + currentValue = basicCalc.invertNumber(currentValue); + Console.displayValue(currentValue); + break; + case 10: + currentValue = basicCalc.percentage(currentValue); + Console.displayValue(currentValue); + break; + case 11: + calculatorType = 2; + Console.println("Switching to Scientific Calculator"); + Console.setDisplayMode(1); + Console.displayValue(currentValue); + break; + case 12: + Console.println("Switching to Display Mode"); + int displayMode = Console.getIntegerInput("Please select display mode\n 1) Decimal\n 2) Binary\n 3) Octal\n 4) HexaDecimal", 1, 4); + Console.setDisplayMode(displayMode); + Console.displayValue(currentValue); + break; + case 13: + currentValue = 0.0; + Console.displayValue(currentValue); + currentValue = Console.getDoubleInput("Please enter the number"); + Console.displayValue(currentValue); + break; } - Console.println("Exit! Thank you"); + + return choice; + } + + private int runScientificCalculator() { + + int choice = Console.getIntegerInput("Please select from the options below\n " + + "1) natural log\n" + + "2) base log\n" + + "3) inverse log\n" + + "4) change sign\n" + + "5) sine\n" + + "6) cos\n" + + "7) tan\n" + + "8) inverseCosine\n" + + "9) inverseTangent\n" + + "10) inverseSine\n" + + "11) factorial\n" + + "12) fibonacci\n" + + "13) Switch Unit Mode\n" + + "14) Choose Unit Mode\n" + + "15) Switch to Basic Calculator\n" + + "16) Enter New Number\n" + + "17) Exit", 1, 21); + + + switch (choice) { + case 1: + currentValue = scientificCalc.inverseNaturalLog(currentValue); + Console.displayValue(currentValue); + break; + case 2: + currentValue = scientificCalc.log(currentValue); + Console.displayValue(currentValue); + break; + case 3: + currentValue = scientificCalc.inverseLog(currentValue); + Console.displayValue(currentValue); + break; + case 4: + currentValue = scientificCalc.changesign(currentValue); + Console.displayValue(currentValue); + break; + case 5: + currentValue = scientificCalc.sin(currentValue); + Console.displayValue(currentValue, scientificCalc.getUnitMode()); + break; + case 6: + currentValue = scientificCalc.cosine(currentValue); + Console.displayValue(currentValue, scientificCalc.getUnitMode()); + break; + case 7: + currentValue = scientificCalc.tangent(currentValue); + Console.displayValue(currentValue, scientificCalc.getUnitMode()); + break; + case 8: + currentValue = scientificCalc.inverseCosine(currentValue); + Console.displayValue(currentValue, scientificCalc.getUnitMode()); + break; + case 9: + currentValue = scientificCalc.inverseTangent(currentValue); + Console.displayValue(currentValue, scientificCalc.getUnitMode()); + break; + case 10: + currentValue = scientificCalc.inverseSin(currentValue); + Console.displayValue(currentValue, scientificCalc.getUnitMode()); + break; + case 11: + currentValue = scientificCalc.factorial(currentValue); + Console.displayValue(currentValue); + break; + case 12: + String result = fibo.fibonacci(currentValue); + Console.displayValue(result); + currentValue = Console.getDoubleInput("Please enter the number"); + Console.displayValue(currentValue); + break; + case 13: + scientificCalc.switchUnitMode(); + scientificCalc.applyUnitMode(currentValue); + Console.displayValue(currentValue, scientificCalc.getUnitMode()); + currentValue = Console.getDoubleInput("Please enter the number"); + Console.displayValue(currentValue); + break; + case 14: + int choosenUnitMode = Console.getIntegerInput("Please select unit mode\n 1) Degree\n 2) Radian\n", 1, 2); + scientificCalc.setUnitMode(choosenUnitMode); + currentValue = Console.getDoubleInput("Please enter the number"); + Console.displayValue(currentValue); + break; + case 15: + calculatorType = 1; + Console.setDisplayMode(1); + currentValue = Console.getDoubleInput("Please enter the number"); + Console.displayValue(currentValue); + break; + case 16: + currentValue = Console.getDoubleInput("Please enter the number"); + Console.displayValue(currentValue); + break; + } + + return choice; } } \ No newline at end of file From 375e1f293105c77d3265dfd147bdafaaf0844dbd Mon Sep 17 00:00:00 2001 From: Rogelio Gamboa Jr <51083580+Roggam@users.noreply.github.com> Date: Sun, 31 Oct 2021 14:49:39 -0400 Subject: [PATCH 29/29] added more methods to UML Diagram --- Untitled Diagram.drawio | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/Untitled Diagram.drawio b/Untitled Diagram.drawio index 4e21f2c8..f94661ad 100644 --- a/Untitled Diagram.drawio +++ b/Untitled Diagram.drawio @@ -1,6 +1,6 @@ - + - + @@ -29,16 +29,16 @@ - + - - + + - + - - + + @@ -106,7 +106,7 @@ - +