From 519f0f3f57cef0edcc3a336bee1cbe48fcae5ffc Mon Sep 17 00:00:00 2001 From: james Date: Sun, 1 Mar 2026 12:23:52 -0500 Subject: [PATCH 1/2] Have a basic calc that works with common equations but only allows 2 numbers --- pom.xml | 10 ++- .../scientificcalculator/Console.java | 40 +++++++++- .../scientificcalculator/MainApplication.java | 75 ++++++++++++++++--- .../TestMainApplication.java | 32 ++++++++ 4 files changed, 144 insertions(+), 13 deletions(-) diff --git a/pom.xml b/pom.xml index b92b052..5abfe4b 100644 --- a/pom.xml +++ b/pom.xml @@ -1,7 +1,7 @@ + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 com.zipcodewilmington @@ -48,6 +48,12 @@ 4.12 test + + + eu.tortitas.utils + lib + 1.2.0 + diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Console.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Console.java index 83f0e97..f374443 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Console.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Console.java @@ -5,8 +5,36 @@ /** * Created by leon on 2/9/18. */ + + public class Console { + public static int add(int a, int b) { + int sum = a + b; + System.out.println(sum); + return sum; + } + + public static int subtract(int a, int b) { + int difference = a - b; + System.out.println(difference); + return difference; + } + + public static int multiply(int a, int b) { + int product = a * b; + System.out.println(product); + return product; + } + + public static int divide(int a, int b) { + int quotient = a / b; + System.out.println(quotient); + return quotient; + } + + + public static void print(String output, Object... args) { System.out.printf(output, args); } @@ -23,10 +51,18 @@ public static String getStringInput(String prompt) { } public static Integer getIntegerInput(String prompt) { - return null; + Scanner scanner = new Scanner(System.in); + println(prompt); + Integer userInput = scanner.nextInt(); + return userInput; } public static Double getDoubleInput(String prompt) { - return null; + Scanner scanner = new Scanner(System.in); + println(prompt); + Double userInput = scanner.nextDouble(); + return userInput; } } + + diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java b/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java index 5f42132..61d341f 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java @@ -1,17 +1,74 @@ package com.zipcodewilmington.scientificcalculator; +import java.lang.reflect.Array; +import java.util.Scanner; + +import utils.Prompt; + /** * Created by leon on 2/9/18. */ + + 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."); + + public static int calculate(int a, int b, String operator) { + int result = 5; + switch (operator) { + case "+": + result = Console.add(a, b); + break; + case "-": + result = Console.subtract(a, b); + break; + case "*": + result = Console.multiply(a, b); + break; + case "/": + result = Console.divide(a, b); + break; + default: + System.out.println("Invalid operator"); + } + return result; + } - 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(""); + // String s = Console.getStringInput("Enter a string"); + // Integer i = Console.getIntegerInput("Enter an integer"); + // Double d = Console.getDoubleInput("Enter a double"); + // } + + + // public static void main(String[] args, Object userInput, Object prompt, String operator) { + + // if (userInput instanceof String) { + // System.out.println(); + // String userInputStr = Console.getStringInput(prompt); + // Console.println(userInputStr); + // } else if (userInput instanceof Integer) { + // System.out.println(); + // Integer userInputInt = Console.getIntegerInput(prompt); + // Console.println(userInputInt); + // } else if (userInput instanceof Double) { + // System.out.println(); + // Double userInputDbl = Console.getDoubleInput(prompt); + // Console.println(userInputDbl); + // } + // else { + // System.out.println("Invalid input type"); + // } + // System.out.println(prompt); + // } + + public static void main(String[] args) { + String prompt = "Enter an operator (+, -, *, /): "; + String operatorInput = Console.getStringInput(prompt); + Integer num1Input = Console.getIntegerInput("Enter the first number: "); + Integer num2Input = Console.getIntegerInput("Enter the second number: "); + int result = calculate(num1Input, num2Input, operatorInput); + Console.println("Result: %d", result); + } -} +} \ No newline at end of file diff --git a/src/test/java/com/zipcodewilmington/scientific_calculator/TestMainApplication.java b/src/test/java/com/zipcodewilmington/scientific_calculator/TestMainApplication.java index 94e8d98..451aac9 100644 --- a/src/test/java/com/zipcodewilmington/scientific_calculator/TestMainApplication.java +++ b/src/test/java/com/zipcodewilmington/scientific_calculator/TestMainApplication.java @@ -3,5 +3,37 @@ /** * Created by leon on 2/9/18. */ + +class Prompt { + private String prompt; + private String userInput; + + public Prompt(String prompt, String userInput) { + this.prompt = prompt; + this.userInput = userInput; + } + + public String getPrompt() { + return prompt; + } + + public String getUserInput() { + return userInput; + } +} public class TestMainApplication { + + + public static int add(int a, int b) { + int sum = a + b; + System.out.println(sum); + return sum; + } + + public static int multiply(int a, int b) { + int product = a * b; + System.out.println(product); + return product; + } + } From 6467dbbba4ee31a4d8a020506f02b7fc639500af Mon Sep 17 00:00:00 2001 From: james Date: Sun, 1 Mar 2026 12:33:51 -0500 Subject: [PATCH 2/2] Basic Calc w Add, Subtract, Multiply and Divide --- .../scientificcalculator/MainApplication.java | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java b/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java index 61d341f..9d11de3 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java @@ -1,10 +1,5 @@ package com.zipcodewilmington.scientificcalculator; -import java.lang.reflect.Array; -import java.util.Scanner; - -import utils.Prompt; - /** * Created by leon on 2/9/18. */