From 1dc197a0d45027ebc7b13ea37c508a2754e001f4 Mon Sep 17 00:00:00 2001 From: ricky Date: Fri, 30 Jun 2023 19:54:15 -0400 Subject: [PATCH 01/10] I have added the first menu --- .../scientificcalculator/MainApplication.java | 49 +++++++++++++++---- 1 file changed, 40 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java b/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java index 5f42132..89a8136 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java @@ -1,17 +1,48 @@ package com.zipcodewilmington.scientificcalculator; - +import java.util.Scanner; /** * Created by leon on 2/9/18. */ public class MainApplication { + + + + Scanner scan = new Scanner(System.in); + public static void main(String[] args) { - Console.println("Welcome to my calculator!"); - String s = Console.getStringInput("Enter a string"); - Integer i = Console.getIntegerInput("Enter an integer"); - Double d = Console.getDoubleInput("Enter a double."); - - Console.println("The user input %s as a string", s); - Console.println("The user input %s as a integer", i); - Console.println("The user input %s as a d", d); + // Console.println("Welcome to my calculator!"); + // String 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); + + DisplayMenu(); + + + + + } + + public static void DisplayMenu() + { + StringBuilder welcome = new StringBuilder(); + welcome.append("|----------------------------------------------------------------------------------------------------|" + '\n'); + welcome.append("| Welcome Menu a) Addition |" + '\n'); + welcome.append("| s) Subtraction |" + '\n'); + welcome.append("| m) Multiplication |" + '\n'); + welcome.append("|Please enter the character of the arithmetic function you would like to use: d) Division |" + '\n'); + welcome.append("| q) Square |" + '\n'); + welcome.append("| r) Square Root |" + '\n'); + welcome.append("| i) Inverse |" + '\n'); + welcome.append("|----------------------------------------------------------------------------------------------------|"+ '\n'); + + String WelcomeMenu = welcome.toString(); + System.out.println(WelcomeMenu); + scan.next() } + + } From da6b0a17810a2fb29d056b27965b0702369087bb Mon Sep 17 00:00:00 2001 From: ricky Date: Sat, 1 Jul 2023 01:19:31 -0400 Subject: [PATCH 02/10] I have finished the menu system. --- .../scientificcalculator/Arithmetic.java | 355 ++++++++++++++++++ .../scientificcalculator/MainApplication.java | 51 +-- 2 files changed, 369 insertions(+), 37 deletions(-) create mode 100644 src/main/java/com/zipcodewilmington/scientificcalculator/Arithmetic.java diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Arithmetic.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Arithmetic.java new file mode 100644 index 0000000..2e49e68 --- /dev/null +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Arithmetic.java @@ -0,0 +1,355 @@ +package com.zipcodewilmington.scientificcalculator; + +import org.sonatype.guice.bean.containers.Main; + +import java.lang.invoke.SwitchPoint; +import java.util.Scanner; +public class Arithmetic { + String welcome = ("|----------------------------------------------------------------------------------------------------|" + '\n') + + ("| Welcome Menu a) Addition |" + '\n') + + ("| s) Subtraction |" + '\n') + + ("| m) Multiplication |" + '\n') + + ("|Please enter the character of the arithmetic function you would like to use: d) Division |" + '\n') + + ("| q) Square |" + '\n') + + ("| r) Square Root |" + '\n') + + ("| i) Inverse |" + '\n') + + ("|----------------------------------------------------------------------------------------------------|" + '\n'); + + public Arithmetic() { + } + + ; + + public void DisplayMenu(Scanner response2) { + + + System.out.println(welcome); + String response = response2.nextLine(); + char character = response.toLowerCase().charAt(0); + switch (character) { + case 'a': + add(run()); + break; + case 's': + sub(run()); + break; + case 'm': + mult(run()); + break; + case 'd': + div(run()); + break; + case 'q': + square(run()); + break; + case 'r': + sqrt(run()); + break; + case 'i': + inv(run()); + break; + default: + System.out.println(""); + System.out.println(""); + System.out.println("You have selected an invalid input."); + System.out.println("Waiting three seconds and restarting"); + try { + Thread.sleep(3000); // Pause for 5 seconds (5000 milliseconds) + } catch (InterruptedException e) { + e.printStackTrace(); + } + System.out.println("Program Resuming"); + try { + Thread.sleep(2000); // Pause for 5 seconds (5000 milliseconds) + } catch (InterruptedException e) { + e.printStackTrace(); + } + DisplayMenu(run()); + } + results(response2); + } + + public static double add(Scanner response) { + + String addi = ("|----------------------------------------------------------------------------------------------------|" + '\n') + + ("| Calculation Menu |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("|Please enter the number you would like to add first: |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("|----------------------------------------------------------------------------------------------------|" + '\n'); + System.out.println(addi); + double ret1 = response.nextDouble(); + String add2 = ("|----------------------------------------------------------------------------------------------------|" + '\n') + + ("| Calculation Menu |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("|Please enter the number you would like to add to: " + ret1 + " |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("|----------------------------------------------------------------------------------------------------|" + '\n'); + System.out.println(add2); + double ret = response.nextDouble(); + + double a = (ret1 + ret); + MainApplication.setval(a); + + return a; + + + } + + + public static double sub(Scanner response) { + String addi = ("|----------------------------------------------------------------------------------------------------|" + '\n') + + ("| Calculation Menu |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("|Please enter the number you would like to subtract from first: |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("|----------------------------------------------------------------------------------------------------|" + '\n'); + + System.out.println(addi); + double ret1 = response.nextDouble(); + String add2 = ("|----------------------------------------------------------------------------------------------------|" + '\n') + + ("| Calculation Menu |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("|Please enter the number you would like to subtract from: " + ret1 + " |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("|----------------------------------------------------------------------------------------------------|" + '\n'); + System.out.println(add2); + + + double ret2 = response.nextDouble(); + double fin = (ret1 - ret2); + MainApplication.setval(fin); + return fin; + + } + + + public static double mult(Scanner response) { + String addi = + ("|----------------------------------------------------------------------------------------------------|" + '\n') + + ("| Calculation Menu |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("|Please enter the number you would like to Multiply from first: |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("|----------------------------------------------------------------------------------------------------|" + '\n'); + System.out.println(addi); + double ret1 = response.nextDouble(); + String add2 = ("|----------------------------------------------------------------------------------------------------|" + '\n') + + ("| Calculation Menu |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("|Please enter the number you would like to multiply to: " + ret1 + " |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("|----------------------------------------------------------------------------------------------------|" + '\n'); + System.out.println(add2); + double ret2 = response.nextDouble(); + double f = (ret1 * ret2); + MainApplication.setval(f); + return f; + } + + + public static double div(Scanner response) { + String addi = + ("|----------------------------------------------------------------------------------------------------|" + '\n') + + ("| Calculation Menu |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("|Please enter the number you would like to divide from first: |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("|----------------------------------------------------------------------------------------------------|" + '\n'); + System.out.println(addi); + double ret1 = response.nextDouble(); + String add2 = ("|----------------------------------------------------------------------------------------------------|" + '\n') + + ("| Calculation Menu |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("|Please enter the number you would like to divide " + ret1 + " by |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("|----------------------------------------------------------------------------------------------------|" + '\n'); + System.out.println(add2); + double ret2 = response.nextDouble(); + double f = (ret1 / ret2); + MainApplication.setval(f); + return f; + + + } + + public static double square(Scanner response) { + String addi = + ("|----------------------------------------------------------------------------------------------------|" + '\n') + + ("| Calculation Menu |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("|Please enter the number you would like to square: |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("|----------------------------------------------------------------------------------------------------|" + '\n'); + + System.out.println(addi); + double ret2 = response.nextDouble(); + double f = (ret2 * ret2); + MainApplication.setval(f); + return f; + + + } + + + public static double sqrt(Scanner response) { + String addi = + ("|----------------------------------------------------------------------------------------------------|" + '\n') + + ("| Calculation Menu |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("|Please enter the number you would like to take the square root of |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("|----------------------------------------------------------------------------------------------------|" + '\n'); + + System.out.println(addi); + double ret2 = response.nextDouble(); + + double f = Math.sqrt(ret2); + MainApplication.setval(f); + return f; + } + + + public static double inv(Scanner response) { + String addi = + ("|----------------------------------------------------------------------------------------------------|" + '\n') + + ("| Calculation Menu |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("|Please enter the number you would like to take the inverse of: |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("|----------------------------------------------------------------------------------------------------|" + '\n'); + + System.out.println(addi); + double ret2 = response.nextDouble(); + + if (ret2 == 0) { + System.out.println("You cannot take the inverse of 0. Restarting Program"); + try { + Thread.sleep(3000); + } catch (InterruptedException e) { + throw new RuntimeException(e); + + + } + System.out.println("Restarting now"); + try { + Thread.sleep(3000); + } catch (InterruptedException e){ + throw new RuntimeException(e); + } + inv(run()); + + + } + double inv = 1/ret2; + MainApplication.setval(inv); + return (1 / ret2); + } + + public void results(Scanner response2) + { + + double res = MainApplication.getCurrentVal(); + + String addi = ("|----------------------------------------------------------------------------------------------------|" + '\n') + + ("| Results Menu |" + '\n') + + ("| a)Clear display and do a new calculation |" + '\n') + + ("| b)Store result in memory |" + '\n') + + ("|The result of your calculation is: " +res+ " c)Quit App |" + '\n') + + ("| |" + '\n') + + ("|Please select what you would like to do next |" + '\n') + + ("| |" + '\n') + + ("|----------------------------------------------------------------------------------------------------|" + '\n'); + System.out.println(addi); + String response = response2.nextLine(); + char character = response.toLowerCase().charAt(0); + switch (character) { + case 'a': + ClearDisplay(); + break; + case 'b': + // StoreMem; + break; + case 'c': + QuitApp(); + break; + default: + System.out.println(""); + System.out.println(""); + System.out.println("You have selected an invalid input."); + System.out.println("Waiting three seconds and restarting"); + try { + Thread.sleep(3000); // Pause for 5 seconds (5000 milliseconds) + } catch (InterruptedException e) { + e.printStackTrace(); + } + System.out.println("Program Resuming"); + try { + Thread.sleep(2000); // Pause for 5 seconds (5000 milliseconds) + } catch (InterruptedException e) { + e.printStackTrace(); + } + results(run()); + } + + } + + public void QuitApp() + { + System.exit(0); + } + + public void ClearDisplay() + { + MainApplication.setval(0); + DisplayMenu(run()); + } + + + + + public static Scanner run() + { + Scanner response = new Scanner(System.in); + return response; + } + + + + + + +} diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java b/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java index 89a8136..479a1e4 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java @@ -4,45 +4,22 @@ * Created by leon on 2/9/18. */ public class MainApplication { +private static double CurrentValue=0; + public MainApplication() + { - - - Scanner scan = new Scanner(System.in); - + } 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); - - DisplayMenu(); - - - - + Arithmetic menus = new Arithmetic(); + menus.DisplayMenu(menus.run()); } - - public static void DisplayMenu() + public static double getCurrentVal() { - StringBuilder welcome = new StringBuilder(); - welcome.append("|----------------------------------------------------------------------------------------------------|" + '\n'); - welcome.append("| Welcome Menu a) Addition |" + '\n'); - welcome.append("| s) Subtraction |" + '\n'); - welcome.append("| m) Multiplication |" + '\n'); - welcome.append("|Please enter the character of the arithmetic function you would like to use: d) Division |" + '\n'); - welcome.append("| q) Square |" + '\n'); - welcome.append("| r) Square Root |" + '\n'); - welcome.append("| i) Inverse |" + '\n'); - welcome.append("|----------------------------------------------------------------------------------------------------|"+ '\n'); - - String WelcomeMenu = welcome.toString(); - System.out.println(WelcomeMenu); - scan.next() + return CurrentValue; } - - -} + public static double setval(double f) + { + CurrentValue = f; + return f; + } +} \ No newline at end of file From 48b588bec87d3f3472e48d65192b708e5ba5461a Mon Sep 17 00:00:00 2001 From: Joe Date: Sat, 1 Jul 2023 16:04:04 -0400 Subject: [PATCH 03/10] science added --- .../scientificcalculator/Arithmetic.java | 110 ++++++++++++------ .../scientificcalculator/MainApplication.java | 11 ++ 2 files changed, 88 insertions(+), 33 deletions(-) diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Arithmetic.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Arithmetic.java index 2e49e68..aead7fc 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Arithmetic.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Arithmetic.java @@ -5,7 +5,8 @@ import java.lang.invoke.SwitchPoint; import java.util.Scanner; public class Arithmetic { - String welcome = ("|----------------------------------------------------------------------------------------------------|" + '\n') + + String welcome = + ("|----------------------------------------------------------------------------------------------------|" + '\n') + ("| Welcome Menu a) Addition |" + '\n') + ("| s) Subtraction |" + '\n') + ("| m) Multiplication |" + '\n') + @@ -13,7 +14,8 @@ public class Arithmetic { ("| q) Square |" + '\n') + ("| r) Square Root |" + '\n') + ("| i) Inverse |" + '\n') + - ("|----------------------------------------------------------------------------------------------------|" + '\n'); + ("| c) Check Memory |" + '\n') + + ("|----------------------------------------------------------------------------------------------------|"+'\n'); public Arithmetic() { } @@ -48,6 +50,8 @@ public void DisplayMenu(Scanner response2) { case 'i': inv(run()); break; + case 'c': + DisplayMemory(run()); default: System.out.println(""); System.out.println(""); @@ -72,14 +76,14 @@ public void DisplayMenu(Scanner response2) { public static double add(Scanner response) { String addi = ("|----------------------------------------------------------------------------------------------------|" + '\n') + - ("| Calculation Menu |" + '\n') + - ("| |" + '\n') + - ("| |" + '\n') + - ("|Please enter the number you would like to add first: |" + '\n') + - ("| |" + '\n') + - ("| |" + '\n') + - ("| |" + '\n') + - ("|----------------------------------------------------------------------------------------------------|" + '\n'); + ("| Calculation Menu |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("|Please enter the number you would like to add first: |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("|----------------------------------------------------------------------------------------------------|" + '\n'); System.out.println(addi); double ret1 = response.nextDouble(); String add2 = ("|----------------------------------------------------------------------------------------------------|" + '\n') + @@ -267,32 +271,31 @@ public static double inv(Scanner response) { System.out.println("Restarting now"); try { Thread.sleep(3000); - } catch (InterruptedException e){ + } catch (InterruptedException e) { throw new RuntimeException(e); } inv(run()); } - double inv = 1/ret2; + double inv = 1 / ret2; MainApplication.setval(inv); return (1 / ret2); } - public void results(Scanner response2) - { + public void results(Scanner response2) { - double res = MainApplication.getCurrentVal(); + double res = MainApplication.getCurrentVal(); String addi = ("|----------------------------------------------------------------------------------------------------|" + '\n') + - ("| Results Menu |" + '\n') + - ("| a)Clear display and do a new calculation |" + '\n') + - ("| b)Store result in memory |" + '\n') + - ("|The result of your calculation is: " +res+ " c)Quit App |" + '\n') + - ("| |" + '\n') + - ("|Please select what you would like to do next |" + '\n') + - ("| |" + '\n') + - ("|----------------------------------------------------------------------------------------------------|" + '\n'); + ("| Results Menu |" + '\n') + + ("| a)Clear display and do a new calculation |" + '\n') + + ("| b)Store result in memory |" + '\n') + + ("|The result of your calculation is: " + res + " c)Quit App |" + '\n') + + ("| |" + '\n') + + ("|Please select what you would like to do next |" + '\n') + + ("| |" + '\n') + + ("|----------------------------------------------------------------------------------------------------|" + '\n'); System.out.println(addi); String response = response2.nextLine(); char character = response.toLowerCase().charAt(0); @@ -301,7 +304,8 @@ public void results(Scanner response2) ClearDisplay(); break; case 'b': - // StoreMem; + StoreMemory(res); + DisplayMenu(run()); break; case 'c': QuitApp(); @@ -327,29 +331,69 @@ public void results(Scanner response2) } - public void QuitApp() - { + public void QuitApp() { System.exit(0); } - public void ClearDisplay() - { + public void ClearDisplay() { MainApplication.setval(0); DisplayMenu(run()); } - - - public static Scanner run() - { + public static Scanner run() { Scanner response = new Scanner(System.in); return response; } + public void StoreMemory(double CurrentValue) { + MainApplication.setMemory(CurrentValue); + } + public void DisplayMemory(Scanner scan) { + String menu = ("|----------------------------------------------------------------------------------------------------|" + '\n') + + ("| Memory Menu a)Go back to main menu |" + '\n') + + ("| b)Clear memory |" + '\n') + + ("| c)Quit App |" + '\n') + + ("|The current value of your memory is: " + MainApplication.getMemory() + " |" + '\n') + + ("| |" + '\n') + + ("|Please select what you would like to do next |" + '\n') + + ("| |" + '\n') + + ("|----------------------------------------------------------------------------------------------------|" + '\n'); + System.out.println(menu); + String scanf = scan.nextLine(); + char character = scanf.toLowerCase().charAt(0); + switch (character) { + case 'a': + DisplayMenu(run()); + break; + case 'b': + MainApplication.setMemory(0); + DisplayMenu(run()); + break; + case 'c': + QuitApp(); + break; + default: + System.out.println(""); + System.out.println(""); + System.out.println("You have selected an invalid input."); + System.out.println("Waiting three seconds and restarting"); + try { + Thread.sleep(3000); // Pause for 5 seconds (5000 milliseconds) + } catch (InterruptedException e) { + e.printStackTrace(); + } + System.out.println("Program Resuming"); + try { + Thread.sleep(2000); // Pause for 5 seconds (5000 milliseconds) + } catch (InterruptedException e) { + e.printStackTrace(); + } + DisplayMemory(run()); + } - + } } diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java b/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java index 479a1e4..c6fc32a 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java @@ -5,6 +5,11 @@ */ public class MainApplication { private static double CurrentValue=0; + + + + + private static double Memory = 0; public MainApplication() { @@ -17,6 +22,12 @@ public static double getCurrentVal() { return CurrentValue; } + public static double getMemory() { + return Memory; + } + public static void setMemory(double memory) { + Memory = memory; + } public static double setval(double f) { CurrentValue = f; From ddf2339de5b4ba7d0956583e43335bd47797bcad Mon Sep 17 00:00:00 2001 From: Christine Date: Sun, 2 Jul 2023 13:31:47 -0400 Subject: [PATCH 04/10] wrote some simple test cases for main application --- .../TestMainApplication.java | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/test/java/com/zipcodewilmington/scientific_calculator/TestMainApplication.java b/src/test/java/com/zipcodewilmington/scientific_calculator/TestMainApplication.java index 94e8d98..6365996 100644 --- a/src/test/java/com/zipcodewilmington/scientific_calculator/TestMainApplication.java +++ b/src/test/java/com/zipcodewilmington/scientific_calculator/TestMainApplication.java @@ -1,7 +1,37 @@ package com.zipcodewilmington.scientific_calculator; +import com.zipcodewilmington.scientificcalculator.MainApplication; +import org.junit.Assert; +import org.junit.Test; +import org.junit.Before; +import org.sonatype.guice.bean.containers.Main; + /** * Created by leon on 2/9/18. */ public class TestMainApplication { + private MainApplication mainApp; + + @Before + public void setup(){ + mainApp = new MainApplication(); + } + + @Test + public void testInitialMemoryValue(){ + Assert.assertEquals(MainApplication.getMemory(), 0.0); + } + + @Test + public void testSetMemoryValue(){ + MainApplication.setMemory(9.1); + Assert.assertEquals(MainApplication.getMemory(), 9.1); + } + + // Note to Christine: write these after you understand how the program works + @Test + public void testSetCurrentValue(){ + // MainApplication.setMemory(9.1); + // Assert.assertEquals(MainApplication.getMemory(), 9.1); + } } From c953199957fcc94216fe7d1dc69d96655b3edc7c Mon Sep 17 00:00:00 2001 From: Christine Date: Sun, 2 Jul 2023 13:55:12 -0400 Subject: [PATCH 05/10] added some simple test cases for add, sub, mult, div --- .../scientific_calculator/TestArithmetic.java | 94 +++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 src/test/java/com/zipcodewilmington/scientific_calculator/TestArithmetic.java diff --git a/src/test/java/com/zipcodewilmington/scientific_calculator/TestArithmetic.java b/src/test/java/com/zipcodewilmington/scientific_calculator/TestArithmetic.java new file mode 100644 index 0000000..ed61f04 --- /dev/null +++ b/src/test/java/com/zipcodewilmington/scientific_calculator/TestArithmetic.java @@ -0,0 +1,94 @@ +package com.zipcodewilmington.scientific_calculator; + +import com.zipcodewilmington.scientificcalculator.Arithmetic; +import com.zipcodewilmington.scientificcalculator.MainApplication; +import org.junit.Assert; +import org.junit.Test; +import org.junit.Before; +import java.util.Scanner; + +public class TestArithmetic { + private Arithmetic arithmetic; + private Scanner in; + + @Before + public void setup(){ + arithmetic = new Arithmetic(); + } + + /* + * Addition tests + */ + @Test + public void testAdditionInts(){ + in = new Scanner("17 26"); + + double result = arithmetic.add(in); + Assert.assertEquals(result, 17.0+26.0); + } + + @Test + public void testAdditionDoubles(){ + in = new Scanner("1.7 2.6"); + + double result = arithmetic.add(in); + Assert.assertEquals(result, 1.7+2.6); + } + + /* + * Subtraction tests + */ + @Test + public void testSubtractionInts(){ + in = new Scanner("17 26"); + + double result = arithmetic.sub(in); + Assert.assertEquals(result, -9.0); + } + + @Test + public void testSubtractionDoubles(){ + in = new Scanner("1.7 2.6"); + + double result = arithmetic.sub(in); + Assert.assertEquals(result, 1.7-2.6); + } + + /* + * Multiplication tests + */ + @Test + public void testMultiplicationInts(){ + in = new Scanner("17 26"); + + double result = arithmetic.mult(in); + Assert.assertEquals(result, 17.0*26.0); + } + + @Test + public void testMultiplicationDoubles(){ + in = new Scanner("1.7 2.6"); + + double result = arithmetic.mult(in); + Assert.assertEquals(result, 1.7*2.6); + } + + /* + * Division tests + */ + @Test + public void testDivisionInts(){ + in = new Scanner("17 26"); + + double result = arithmetic.div(in); + Assert.assertEquals(result, 17.0/26.0); + } + + @Test + public void testDivisionDoubles(){ + in = new Scanner("1.7 2.6"); + + double result = arithmetic.div(in); + Assert.assertEquals(result, 4.42); + } +} From 74c2112a1371f04504e700b0a70c6b4ccd2659a0 Mon Sep 17 00:00:00 2001 From: Christine Date: Sun, 2 Jul 2023 14:01:07 -0400 Subject: [PATCH 06/10] wrote some tests for square, squareroot, inverse --- .../scientific_calculator/TestArithmetic.java | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/src/test/java/com/zipcodewilmington/scientific_calculator/TestArithmetic.java b/src/test/java/com/zipcodewilmington/scientific_calculator/TestArithmetic.java index ed61f04..a88ae6b 100644 --- a/src/test/java/com/zipcodewilmington/scientific_calculator/TestArithmetic.java +++ b/src/test/java/com/zipcodewilmington/scientific_calculator/TestArithmetic.java @@ -91,4 +91,72 @@ public void testDivisionDoubles(){ double result = arithmetic.div(in); Assert.assertEquals(result, 4.42); } + /* + @Test + public void testDivisionBy0(){ + in = new Scanner("17 0"); + + double result = arithmetic.div(in); + // not sure what the assert statement should be at the moment + Assert.assertEquals(result, 4.42); + } + */ + /* + * Square tests + */ + @Test + public void testSquareInts(){ + in = new Scanner("17"); + + double result = arithmetic.square(in); + Assert.assertEquals(result, 17.0*17.0); + } + + @Test + public void testSquareDoubles(){ + in = new Scanner("1.7"); + + double result = arithmetic.square(in); + Assert.assertEquals(result, 1.7*1.7); + } + + /* + * Square Root tests + */ + @Test + public void testSquareRootInts(){ + in = new Scanner("169"); + + double result = arithmetic.sqrt(in); + Assert.assertEquals(result, 13.0); + } + + @Test + public void testSquareRootDoubles(){ + in = new Scanner("1.69"); + + double result = arithmetic.sqrt(in); + Assert.assertEquals(result, 1.3); + } + + /* + * Inverse tests + */ + @Test + public void testInverse(){ + in = new Scanner("17"); + + double result = arithmetic.inv(in); + Assert.assertEquals(result, 1.0/17.0); + } + /* + @Test + public void testInverseOf0(){ + in = new Scanner("0"); + + double result = arithmetic.div(in); + // not sure what the assert statement should be at the moment + Assert.assertEquals(result, 4.42); + } + */ } From cd8d5b2011c0ef5458fc9de01480e8c0b6cd451d Mon Sep 17 00:00:00 2001 From: Christine Date: Sun, 2 Jul 2023 14:25:17 -0400 Subject: [PATCH 07/10] had to do some weird sysout manipulation but i can now test sysout stuff? --- .../scientific_calculator/TestArithmetic.java | 74 ++++++++++++++++--- 1 file changed, 62 insertions(+), 12 deletions(-) diff --git a/src/test/java/com/zipcodewilmington/scientific_calculator/TestArithmetic.java b/src/test/java/com/zipcodewilmington/scientific_calculator/TestArithmetic.java index a88ae6b..7d098ca 100644 --- a/src/test/java/com/zipcodewilmington/scientific_calculator/TestArithmetic.java +++ b/src/test/java/com/zipcodewilmington/scientific_calculator/TestArithmetic.java @@ -1,19 +1,31 @@ package com.zipcodewilmington.scientific_calculator; -import com.zipcodewilmington.scientificcalculator.Arithmetic; -import com.zipcodewilmington.scientificcalculator.MainApplication; +import com.zipcodewilmington.scientificcalculator.*; import org.junit.Assert; import org.junit.Test; import org.junit.Before; +import org.junit.After; import java.util.Scanner; +import java.io.PrintStream; +import java.io.ByteArrayOutputStream; public class TestArithmetic { private Arithmetic arithmetic; + private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + private final ByteArrayOutputStream errContent = new ByteArrayOutputStream(); + private final PrintStream originalOut = System.out; + private Scanner in; @Before public void setup(){ arithmetic = new Arithmetic(); + System.setOut(new PrintStream(outContent)); + } + + @After + public void restoreStreams() { + System.setOut(originalOut); } /* @@ -91,16 +103,40 @@ public void testDivisionDoubles(){ double result = arithmetic.div(in); Assert.assertEquals(result, 4.42); } - /* + @Test public void testDivisionBy0(){ - in = new Scanner("17 0"); + in = new Scanner("0 0 17 26"); + StringBuilder sb = new StringBuilder(); + String divMenu1 = + ("|----------------------------------------------------------------------------------------------------|" + '\n') + + ("| Calculation Menu |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("|Please enter the number you would like to divide from first: |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("|----------------------------------------------------------------------------------------------------|" + '\n'); + String divMenu2 = + ("|----------------------------------------------------------------------------------------------------|" + '\n') + + ("| Calculation Menu |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("|Please enter the number you would like to divide " + ret1 + " by |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("|----------------------------------------------------------------------------------------------------|" + '\n'); + String divErrMessage = "You cannot divide by 0. Restarting Program\n"; + sb.append(divMenu1); + sb.append(divMenu2); + sb.append(divErrMessage); double result = arithmetic.div(in); - // not sure what the assert statement should be at the moment - Assert.assertEquals(result, 4.42); + Assert.assertEquals(outContent.toString(), sb.toString()); + Assert.assertEquals(result, 17.0/26.0); } - */ /* * Square tests */ @@ -149,14 +185,28 @@ public void testInverse(){ double result = arithmetic.inv(in); Assert.assertEquals(result, 1.0/17.0); } - /* + @Test public void testInverseOf0(){ - in = new Scanner("0"); + in = new Scanner("0 17"); + StringBuilder sb = new StringBuilder(); + String invMenu = + ("|----------------------------------------------------------------------------------------------------|" + '\n') + + ("| Calculation Menu |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("|Please enter the number you would like to take the inverse of: |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("|----------------------------------------------------------------------------------------------------|" + '\n'); + String invErrMessage = "You cannot take the inverse of 0. Restarting Program\n"; + sb.append(invMenu); + sb.append(invErrMessage); double result = arithmetic.div(in); - // not sure what the assert statement should be at the moment - Assert.assertEquals(result, 4.42); + Assert.assertEquals(outContent.toString(), sb.toString()); + Assert.assertEquals(result, 1.0/17.0); } - */ + } From da79a74bb0324b7935056b88bfb8b245b600adeb Mon Sep 17 00:00:00 2001 From: Christine Date: Sun, 2 Jul 2023 14:29:41 -0400 Subject: [PATCH 08/10] i believe i finished up the main application tests --- .../scientific_calculator/TestMainApplication.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/test/java/com/zipcodewilmington/scientific_calculator/TestMainApplication.java b/src/test/java/com/zipcodewilmington/scientific_calculator/TestMainApplication.java index 6365996..4cbea31 100644 --- a/src/test/java/com/zipcodewilmington/scientific_calculator/TestMainApplication.java +++ b/src/test/java/com/zipcodewilmington/scientific_calculator/TestMainApplication.java @@ -28,10 +28,14 @@ public void testSetMemoryValue(){ Assert.assertEquals(MainApplication.getMemory(), 9.1); } - // Note to Christine: write these after you understand how the program works + @Test + public void testInitialCurrentValue(){ + Assert.assertEquals(MainApplication.getCurrentVal(), 0.0); + } + @Test public void testSetCurrentValue(){ - // MainApplication.setMemory(9.1); - // Assert.assertEquals(MainApplication.getMemory(), 9.1); + MainApplication.setval(9.1); + Assert.assertEquals(MainApplication.getCurrentVal(), 9.1); } } From 1083720157b471129d5b0b28835778cc1b4f74fd Mon Sep 17 00:00:00 2001 From: Christine Date: Sun, 2 Jul 2023 14:52:19 -0400 Subject: [PATCH 09/10] fixed main application test bc i swapped the 'expected' and 'actual' parameters :( --- .../scientific_calculator/TestMainApplication.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/test/java/com/zipcodewilmington/scientific_calculator/TestMainApplication.java b/src/test/java/com/zipcodewilmington/scientific_calculator/TestMainApplication.java index 4cbea31..b30bdcc 100644 --- a/src/test/java/com/zipcodewilmington/scientific_calculator/TestMainApplication.java +++ b/src/test/java/com/zipcodewilmington/scientific_calculator/TestMainApplication.java @@ -19,23 +19,23 @@ public void setup(){ @Test public void testInitialMemoryValue(){ - Assert.assertEquals(MainApplication.getMemory(), 0.0); + Assert.assertEquals(0.0, MainApplication.getMemory()); } @Test public void testSetMemoryValue(){ MainApplication.setMemory(9.1); - Assert.assertEquals(MainApplication.getMemory(), 9.1); + Assert.assertEquals(9.1, MainApplication.getMemory()); } @Test public void testInitialCurrentValue(){ - Assert.assertEquals(MainApplication.getCurrentVal(), 0.0); + Assert.assertEquals(0.0, MainApplication.getCurrentVal()); } @Test public void testSetCurrentValue(){ MainApplication.setval(9.1); - Assert.assertEquals(MainApplication.getCurrentVal(), 9.1); + Assert.assertEquals(9.1, MainApplication.getCurrentVal()); } } From 3a2e1bb87d845b318dcb281820cbdb5dafbd3ec9 Mon Sep 17 00:00:00 2001 From: Christine Date: Sun, 2 Jul 2023 15:10:20 -0400 Subject: [PATCH 10/10] fixed the arithmetic tests, also added some funky error testing things ohoho --- .../scientific_calculator/TestArithmetic.java | 78 +++++-------------- 1 file changed, 19 insertions(+), 59 deletions(-) diff --git a/src/test/java/com/zipcodewilmington/scientific_calculator/TestArithmetic.java b/src/test/java/com/zipcodewilmington/scientific_calculator/TestArithmetic.java index 7d098ca..6b9658f 100644 --- a/src/test/java/com/zipcodewilmington/scientific_calculator/TestArithmetic.java +++ b/src/test/java/com/zipcodewilmington/scientific_calculator/TestArithmetic.java @@ -12,9 +12,7 @@ public class TestArithmetic { private Arithmetic arithmetic; private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); - private final ByteArrayOutputStream errContent = new ByteArrayOutputStream(); private final PrintStream originalOut = System.out; - private Scanner in; @Before @@ -36,7 +34,7 @@ public void testAdditionInts(){ in = new Scanner("17 26"); double result = arithmetic.add(in); - Assert.assertEquals(result, 17.0+26.0); + Assert.assertEquals(17.0+26.0, result); } @Test @@ -44,7 +42,7 @@ public void testAdditionDoubles(){ in = new Scanner("1.7 2.6"); double result = arithmetic.add(in); - Assert.assertEquals(result, 1.7+2.6); + Assert.assertEquals(1.7+2.6, result); } /* @@ -55,7 +53,7 @@ public void testSubtractionInts(){ in = new Scanner("17 26"); double result = arithmetic.sub(in); - Assert.assertEquals(result, -9.0); + Assert.assertEquals(-9.0, result); } @Test @@ -63,7 +61,7 @@ public void testSubtractionDoubles(){ in = new Scanner("1.7 2.6"); double result = arithmetic.sub(in); - Assert.assertEquals(result, 1.7-2.6); + Assert.assertEquals(1.7-2.6, result); } /* @@ -74,7 +72,7 @@ public void testMultiplicationInts(){ in = new Scanner("17 26"); double result = arithmetic.mult(in); - Assert.assertEquals(result, 17.0*26.0); + Assert.assertEquals(17.0*26.0, result); } @Test @@ -82,7 +80,7 @@ public void testMultiplicationDoubles(){ in = new Scanner("1.7 2.6"); double result = arithmetic.mult(in); - Assert.assertEquals(result, 1.7*2.6); + Assert.assertEquals(1.7*2.6, result); } /* @@ -93,7 +91,7 @@ public void testDivisionInts(){ in = new Scanner("17 26"); double result = arithmetic.div(in); - Assert.assertEquals(result, 17.0/26.0); + Assert.assertEquals(17.0/26.0, result); } @Test @@ -101,41 +99,17 @@ public void testDivisionDoubles(){ in = new Scanner("1.7 2.6"); double result = arithmetic.div(in); - Assert.assertEquals(result, 4.42); + Assert.assertEquals(4.42, result); } @Test public void testDivisionBy0(){ in = new Scanner("0 0 17 26"); - StringBuilder sb = new StringBuilder(); - String divMenu1 = - ("|----------------------------------------------------------------------------------------------------|" + '\n') + - ("| Calculation Menu |" + '\n') + - ("| |" + '\n') + - ("| |" + '\n') + - ("|Please enter the number you would like to divide from first: |" + '\n') + - ("| |" + '\n') + - ("| |" + '\n') + - ("| |" + '\n') + - ("|----------------------------------------------------------------------------------------------------|" + '\n'); - String divMenu2 = - ("|----------------------------------------------------------------------------------------------------|" + '\n') + - ("| Calculation Menu |" + '\n') + - ("| |" + '\n') + - ("| |" + '\n') + - ("|Please enter the number you would like to divide " + ret1 + " by |" + '\n') + - ("| |" + '\n') + - ("| |" + '\n') + - ("| |" + '\n') + - ("|----------------------------------------------------------------------------------------------------|" + '\n'); - String divErrMessage = "You cannot divide by 0. Restarting Program\n"; - sb.append(divMenu1); - sb.append(divMenu2); - sb.append(divErrMessage); + String divErrMessage = "You cannot divide by 0. Restarting Program"; double result = arithmetic.div(in); - Assert.assertEquals(outContent.toString(), sb.toString()); - Assert.assertEquals(result, 17.0/26.0); + Assert.assertTrue(outContent.toString().contains(divErrMessage)); + Assert.assertEquals(17.0/26.0, result); } /* * Square tests @@ -145,7 +119,7 @@ public void testSquareInts(){ in = new Scanner("17"); double result = arithmetic.square(in); - Assert.assertEquals(result, 17.0*17.0); + Assert.assertEquals(17.0*17.0, result); } @Test @@ -153,7 +127,7 @@ public void testSquareDoubles(){ in = new Scanner("1.7"); double result = arithmetic.square(in); - Assert.assertEquals(result, 1.7*1.7); + Assert.assertEquals(1.7*1.7, result); } /* @@ -164,7 +138,7 @@ public void testSquareRootInts(){ in = new Scanner("169"); double result = arithmetic.sqrt(in); - Assert.assertEquals(result, 13.0); + Assert.assertEquals(13.0, result); } @Test @@ -172,7 +146,7 @@ public void testSquareRootDoubles(){ in = new Scanner("1.69"); double result = arithmetic.sqrt(in); - Assert.assertEquals(result, 1.3); + Assert.assertEquals(1.3, result); } /* @@ -183,30 +157,16 @@ public void testInverse(){ in = new Scanner("17"); double result = arithmetic.inv(in); - Assert.assertEquals(result, 1.0/17.0); + Assert.assertEquals(1.0/17.0, result); } @Test public void testInverseOf0(){ in = new Scanner("0 17"); - StringBuilder sb = new StringBuilder(); - String invMenu = - ("|----------------------------------------------------------------------------------------------------|" + '\n') + - ("| Calculation Menu |" + '\n') + - ("| |" + '\n') + - ("| |" + '\n') + - ("|Please enter the number you would like to take the inverse of: |" + '\n') + - ("| |" + '\n') + - ("| |" + '\n') + - ("| |" + '\n') + - ("|----------------------------------------------------------------------------------------------------|" + '\n'); - String invErrMessage = "You cannot take the inverse of 0. Restarting Program\n"; - sb.append(invMenu); - sb.append(invErrMessage); + String invErrMessage = "You cannot take the inverse of 0. Restarting Program"; double result = arithmetic.div(in); - Assert.assertEquals(outContent.toString(), sb.toString()); - Assert.assertEquals(result, 1.0/17.0); + Assert.assertTrue(outContent.toString().contains(invErrMessage)); + Assert.assertEquals(1.0/17.0, result); } - }