diff --git a/Application.java b/Application.java new file mode 100644 index 0000000..7cf5332 --- /dev/null +++ b/Application.java @@ -0,0 +1,92 @@ +public class Application { + + public Application() { + + } + + public static void main(String[] args) { + run(); + } + + public static void run() { + IOConsole console = new IOConsole(); + Processor processor = new Processor(); + String result = ""; + String numConvert = ""; + + + boolean quit = false; + // Hello + console.println("Hi!"); + while(!quit) { + + // String input = console.getStringInput("Would you like to change the display?"); + // if (input.matches("yes")){ + // String viewDisplayinput = console.getStringInput("Would you like to view display options"); + // if(viewDisplayinput.matches("yes")) { + // processor.displayMode("display"); + // } else { + // String mode = console.getStringInput("Enter Mode:"); + // processor.displayMode(mode); + // Integer x = console.getIntegerInput("Enter x"); + // numConvert = processor.changeMode(mode, x); + // console.println(numConvert); + // // enter y + // Integer y = console.getIntegerInput("Enter y"); + // numConvert = processor.changeMode(mode, y); + // console.println(numConvert); + // } + // } + + + // enter operator + String operator = console.getStringInput("Enter operator"); + + boolean isValid = isValid(operator); + if(!isValid) { + run(); + } else { + if(operator.matches("inverse|square|invert|!|sqrt|sin|asin|cos|acos|tan|atan|")){ + Double x = console.getDoubleInput("Enter x"); + result = processor.arithmetic(operator, x, 0.0); + } else { + // enter x + Double x = console.getDoubleInput("Enter x"); + // enter y + Double y = console.getDoubleInput("Enter y"); + result = processor.arithmetic(operator, x, y); + } + + } + // print result + console.println(result); + + console.println("Would you like to do more Math????"); + console.println("Enter: yes to continue."); + String redo = console.getStringInput("Enter: no to quit."); + + // More math? + if(redo.equals("no")) { + quit = true; + } else { + console.clearScreen(); + quit = false; + } + + + } + // bye! + } + public static boolean isValid(String operation) { + String[] operations = {"-", "/", "*", "^", "+" , "inverse" , "square" , "invert" , "!" , "sqrt", "sin" , "asin" , "cos" , "acos" , "tan" , "atan"}; + for(int i= 0; i < operations.length-1; i++) { + if(operations[i].equals(operation)) { + return true; + } + } + return false; + } + + +} + diff --git a/ApplicationTest.java b/ApplicationTest.java new file mode 100644 index 0000000..4a25627 --- /dev/null +++ b/ApplicationTest.java @@ -0,0 +1,42 @@ + + +import static org.junit.Assert.*; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * The test class ApplicationTest. + * + * @author (your name) + * @version (a version number or a date) + */ +public class ApplicationTest +{ + /** + * Default constructor for test class ApplicationTest + */ + public ApplicationTest() + { + } + + /** + * Sets up the test fixture. + * + * Called before every test case method. + */ + @Before + public void setUp() + { + } + + /** + * Tears down the test fixture. + * + * Called after every test case method. + */ + @After + public void tearDown() + { + } +} diff --git a/Calculator.java b/Calculator.java new file mode 100644 index 0000000..e4a0bb0 --- /dev/null +++ b/Calculator.java @@ -0,0 +1,166 @@ + +/** + * Write a description of class Calculator here. + * + * @author (your name) + * @version (a version number or a date) + */ + +import java.lang.Math; + +public class Calculator +{ + // instance variables - replace the example below with your own + private Double x; + + /** + * Constructor for objects of class Calculator + */ + public Calculator() + { + // initialise instance variables + x = 0.0; + } + + + /** + * TODO Write comments + */ + public Double squareMethod(Double x) + { + // put your code here + return x * x; + + } + + public Double add(Double x,Double y) + { + return (x+y); + } + + public Double sub(Double x,Double y) + { + return x - y; + } + + public Double multiply(Double x,Double y) + { + return (x*y); + } + + public Double div(Double x,Double y) + { + return (Double)(x/y); + } + public Double squareRootMethod(Double x) + { + // put your code here + return Math.sqrt(x); + + } + + public Double invertMethod(Double x) + { + // put your code here + x=-x; + return x; + + } + public Double exponentiationMethod(Double x,Double y) + { + // put your code here + return Math.pow(x,y); + + } + + public Double inverseMethod(Double x) + { + // put your code here + return 1/(Double)x; + + } + + public Double sineMathMethod(Double x) + { + Double radians = Math.toRadians(x); + + + return (Math.sin(radians)); + + } + + public Double cosMathMethod(Double x) + + { + Double radians = Math.toRadians(x); + // put your code here + return (Math.cos(radians)); + + } + + public Double tanMathMethod(Double x) + { + Double radians = Math.toRadians(x); + // put your code here + return (Math.tan(radians)); + + } + public Double invSineMathMethod(Double x) + { + //Double radians = Math.toRadians(x); + + return (Math.asin(x)); + + } + + public Double invCosMathMethod(Double x) + { + Double radians = Math.toRadians(x); + // put your code here + return (Math.acos(radians)); + + } + + public Double invTanMathMethod(Double x) + { + //Double radians = Math.toRadians(x); + // put your code here + return (Math.atan(x)); + + } + + public long factorialMethod(Double x){ + + long result = 1; + + for (int factor = 2; factor <= x; factor++) { + result *= factor; + } + + return result; + } + + public String toBinary(int x) + { + String binary =Integer.toBinaryString( x ); + return binary; + } + + public String toOctal(int x) + { + String octal =Integer.toOctalString( x ); + return octal; + } + + /*public String tofloat (int x) + { + String fl =Integer.floatValue( x ); + return fl; + } +*/ + public String toHexadecimal(int x) + { + String hexa =Integer.toHexString( x ); + return hexa; + } +} diff --git a/CalculatorTest.java b/CalculatorTest.java new file mode 100644 index 0000000..b9df616 --- /dev/null +++ b/CalculatorTest.java @@ -0,0 +1,245 @@ + + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * The test class CalculatorTest. + * + * @author (your name) + * @version (a version number or a date) + */ +public class CalculatorTest +{ + private Calculator calculator; + /** + * Default constructor for test class CalculatorTest + */ + public CalculatorTest() + { + + } + + /** + * Sets up the test fixture. + * + * Called before every test case method. + */ + @Before + public void setUp(){ + calculator = new Calculator(); + } + + @Test + public void squareMethodTest(){ + //:Given + Double x = 5.0; + + //Expected + Double expected = 25.0; + Double actual = calculator.squareMethod( x ); + + //:Then + Assert.assertEquals("Values should be equal", expected, actual); + } + + @Test + public void addMethodTest(){ + //:Given + Double x = 5.0; + Double y = 8.0; + + //Expected + Double expected = 13.0; + Double actual = calculator.add(x , y); + + //:Then + Assert.assertEquals("Values should be equal", expected, actual); + } + + + @Test + public void subStartMethodTest(){ + //:Given + Double x = 8.0; + Double y = 5.0; + + //Expected + Double expected = 3.0; + Double actual = calculator.sub(x , y); + + //:Then + Assert.assertEquals("Values should be equal", expected, actual); + } + + @Test + public void multiStartMethodTest(){ + //:Given + Double x = 5.0; + Double y = 8.0; + + //Expected + Double expected = 40.0; + Double actual = calculator.multiply(x , y); + + //:Then + Assert.assertEquals("Values should be equal", expected, actual); + } + + + @Test + public void divStartMethodTest(){ + //:Given + Double x = 8.0; + Double y = 5.0; + + //Expected + Double expected = 1.6; + Double actual = calculator.div(x , y); + + //:Then + Assert.assertEquals( expected, actual , Double.NaN); + } + + + + @Test + public void squareRootMethodTest(){ + //:Given + Double x = 16.0; + + + //Expected + Double expected = 4.0; + Double actual = calculator.squareRootMethod(x); + + + + //:Then + Assert.assertEquals( expected, actual , Double.NaN); + } + + + public void exponentiationMethodTest(){ + //:Given + Double x = 3.0; + Double y = 2.0; + + + //Expected + Double expected = 9.0; + Double actual = calculator.exponentiationMethod( x , y ); + + + + //:Then + Assert.assertEquals( expected, actual , Double.NaN); + } + + public void invertMethodTest(){ + //:Given + Double x = 3.0; + + + + //Expected + Double expected = -3.0; + Double actual = calculator.invertMethod( x ); + + + + //:Then + Assert.assertEquals( expected, actual , Double.NaN); + } + @Test + public void inverseMethodTest(){ + //:Given + Double x = 2.0; + + //Expected + Double expected = 0.5; + Double actual = calculator.inverseMethod(x); + + //:Then + Assert.assertEquals( expected, actual , Double.NaN); + } + + @Test + public void sineMathMethodTest(){ + //:Given + Double x = 90.0; + + //Expected + Double expected = 1.0; + Double actual = calculator.sineMathMethod(x); + + //:Then + Assert.assertEquals( expected, actual , Double.NaN); + } + + @Test + public void tanMathMethodTest(){ + //:Given + Double x = 45.0; + + //Expected + Double expected = 0.9999999999999999; + Double actual = calculator.tanMathMethod(x); + + //:Then + Assert.assertEquals( expected, actual , Double.NaN); + } + + @Test + public void invSineMathMethodTest(){ + //:Given + Double x = 45.0; + + //Expected + Double expected = 0.9033391107665127; + Double actual = calculator.invSineMathMethod(x); + + //:Then + Assert.assertEquals( expected, actual , Double.NaN); + } + + @Test + public void invCosMathMethodTest(){ + //:Given + Double x = 45.0; + + //Expected + Double expected = 0.6674572160283838; + Double actual = calculator.invCosMathMethod(x); + + //:Then + Assert.assertEquals( expected, actual , Double.NaN); + } + + @Test + public void invTanMathMethodTest(){ + //:Given + Double x = 45.0; + + //Expected + Double expected = 0.6657737500283538; + Double actual = calculator.invTanMathMethod(x); + + //:Then + Assert.assertEquals( expected, actual , Double.NaN); + } + + @Test + public void factorialMethodTest(){ + //:Given + Double x = 4.0; + + //Expected + Long expected = 24L; + Long actual = calculator.factorialMethod(x); + + //:Then + Assert.assertEquals( expected, actual , Double.NaN); + } +} diff --git a/IOConsole.java b/IOConsole.java new file mode 100644 index 0000000..ed88845 --- /dev/null +++ b/IOConsole.java @@ -0,0 +1,83 @@ +import java.io.InputStream; +import java.io.OutputStream; +import java.io.PrintStream; +import java.util.Scanner; + +public class IOConsole { + private final Scanner scanner; + private final PrintStream out; + + public IOConsole() { + this(System.in, System.out); + } + + public IOConsole(InputStream inputStream, OutputStream outputStream) { + this(new Scanner(inputStream), new PrintStream(outputStream)); + } + + public IOConsole(Scanner scanner, PrintStream printStream) { + this.scanner = scanner; + this.out = printStream; + } + + /** + * @param val : text to display on console + * @param args : optional arguments to send for string formatting + */ + public void print(String val, Object... args) { + out.format(val, args); + } + + /** + * @param val : text to display on console + * @param args : optional arguments to send for string formatting + */ + public void println(String val, Object... args) { + out.format(val, args); + out.format("\n"); + } + + public void print(Double number) { + out.print(number); + } + + public void println(Double number) { + out.println(number); + } + + /** + * @param prompt : text to display to user + * @param args : optional arguments to send for string formatting + * @return user's input as String + */ + public String getStringInput(String prompt, Object... args) { + out.println(prompt); + return scanner.next(); + } + + /** + * @param prompt : text to display to user + * @param args : optional arguments to send for string formatting + * @return user's input as integer + */ + public Integer getIntegerInput(String prompt, Object... args) { + out.println(prompt); + Integer result = scanner.nextInt(); + return result; + } + + /** + * @param prompt : text to display to user + * @param args : optional arguments to send for string formatting + * @return user's input as double + */ + public Double getDoubleInput(String prompt, Object... args) { + out.println(prompt); + Double result = scanner.nextDouble(); + return result; + } + + public void clearScreen() { + out.print('\u000C'); + } +} diff --git a/MainApplication.java b/MainApplication.java index 7407110..688ec05 100644 --- a/MainApplication.java +++ b/MainApplication.java @@ -1,4 +1,22 @@ +import java.util.Scanner; public class MainApplication { + private IOConsole inputOutput; + public MainApplication(){ + + inputOutput = new IOConsole(); + } + public static void main(String[] args) { + + } + + public void start(){ + Double num1 = inputOutput.getDoubleInput("Give me a number"); + Double num2 = inputOutput.getDoubleInput("Give me another number"); + String operations = inputOutput.getStringInput("What operations would you like use?"); + + Double result = 0.0; + inputOutput.println(result); + } } -} + diff --git a/PrintTest.java b/PrintTest.java new file mode 100644 index 0000000..0ae5a8e --- /dev/null +++ b/PrintTest.java @@ -0,0 +1,200 @@ +import org.junit.Assert; +import org.junit.Test; + +import java.io.ByteArrayInputStream; +import java.util.NoSuchElementException; +import java.util.Scanner; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; + +/** + * @author leon on 03/02/2019. + */ +public class PrintTest { + private IOConsole console; + @Test + public void test1() { + String outputString = "The quick brown fox"; + test(outputString); + } + + @Test + public void test2() { + String outputString = "The quick %s fox"; + String[] stringArguments = {"brown"}; + test(outputString, stringArguments); + } + + @Test + public void test3() { + String outputString = "The %s %s fox"; + String[] stringArguments = {"quick", "brown"}; + test(outputString, stringArguments); + } + + @Test + public void test5() { + String outputString = "Over the lazy dog"; + test(outputString); + } + + @Test + public void test6() { + String outputString = "Over the %s dog."; + String[] stringArguments = {"lazy"}; + test(outputString, stringArguments); + } + + @Test + public void test7() { + String outputString = "Over the %s %s."; + String[] stringArguments = {"lazy", "dog"}; + test(outputString, stringArguments); + } + + @Test + public void test8() { + String outputString = "The quick brown fox"; + test(outputString); + } + + @Test + public void test9() { + String outputString = "The quick %s fox"; + String[] stringArguments = {"brown"}; + test(outputString, stringArguments); + } + + @Test + public void test10() { + String outputString = "The %s %s fox"; + String[] stringArguments = {"quick", "brown"}; + test(outputString, stringArguments); + } + + @Test + public void test11() { + String outputString = "Over the lazy dog"; + test(outputString); + } + + @Test + public void test12() { + String outputString = "Over the %s dog."; + String[] stringArguments = {"lazy"}; + test(outputString, stringArguments); + } + + @Test + public void test13() { + String outputString = "Over the %s %s."; + String[] stringArguments = {"lazy", "dog"}; + test(outputString, stringArguments); + } + + private void test(String outputString, String... stringArguments) { + // Given + String expected = String.format(outputString, stringArguments) + "\n"; + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + IOConsole console = new IOConsole(System.in, new PrintStream(outputStream)); + + // When + console.println(outputString, stringArguments); + String actual = outputStream.toString(); + + // Then + Assert.assertEquals(expected, actual); + } + @Test + public void test14() { + test("0"); + } + + @Test + public void test15() { + test("1"); + } + + @Test + public void test16() { + test("3.4028235E38"); + } + + @Test + public void test17() { + test("-9223372036854775808"); + } + + @Test + public void test18() { + test("1.1"); + } + + @Test + public void test19() { + test("_"); + } + + + private void test(String input) { + // Given + String expected = input; + this.console = getConsoleWithBufferedInput(input); + + // When + String actual = console.getStringInput(""); + + // Then + Assert.assertEquals(actual, expected); + } + + @Test + public void test20() { + IOConsole console = new IOConsole(); + + test("0.0", 0.0); + } + + @Test + public void test21() { + test("1.1", 1.1); + } + + @Test + public void test22() { + test("3.4028235E38", Double.MAX_VALUE); + } + + @Test + public void test23() { + test("1.4E-45", Double.MIN_VALUE); + } + + @Test + public void test24() { + test("1.0", 1.0); + } + + private void test(String input, Double expectedInput) { + // Given + this.console = getConsoleWithBufferedInput(input); + + // When + Double actual = console.getDoubleInput(""); + + // Then + Assert.assertEquals(actual, expectedInput); + } + + private IOConsole getConsoleWithBufferedInput(String inputString) { + byte[] inputBytes = inputString.getBytes(); + ByteArrayInputStream inputByteArray = new ByteArrayInputStream(inputBytes); + Scanner scanner = new Scanner(inputByteArray); + IOConsole console = new IOConsole(scanner, System.out); + return console; + } +} + + + diff --git a/Processor.java b/Processor.java new file mode 100644 index 0000000..3e9bfa5 --- /dev/null +++ b/Processor.java @@ -0,0 +1,95 @@ +public class Processor { + + private static Calculator operations; + private static IOConsole console; + + public Processor() { + operations = new Calculator(); + console = new IOConsole(System.in, System.out); + } + + // display method that ask the user what they want to do and shows possible operations including display mode which switches to binary octal decimal hexadecimal + public static String changeMode(String mode, int num) { + String result = ""; + if(mode.matches("binary")){ + Integer binaryNum = (Integer) num; + result += operations.toBinary(num); + } else if(mode.matches("octal")){ + Integer octalNum = (Integer) num; + result += operations.toOctal(num); + } else if(mode.matches("decimal")){ + Integer decimalNum = (Integer) num; + // result += operations.toDecimal(Num); + // result += operations.toDecimal(y); + } else if (mode.matches("hexadecimal")){ + Integer hexadecimalNum = (Integer) num; + result += operations.toOctal(num); + } + return result; + } + + public static void displayMode(String operator) { + if(operator.equals("display")) { + console.println("Display Mode:"); + console.println("binary"); + console.println("octal"); + console.println("decimal"); + console.println("hexadecimal"); + } + } + + public static String arithmetic(String operator, Double x, Double y) { + String result = ""; + switch(operator.toLowerCase()) { + + case "+": result+= operations.add(x,y); + break; + + case "-": result+= operations.sub(x,y); + break; + + case "/": result+= operations.div(x,y); + break; + + case "*": result+= operations.multiply(x,y); + break; + + case "^": result+= operations.exponentiationMethod(x,y); + break; + + case "inverse": result+= operations.inverseMethod(x); + break; + + case "square": result+= operations.squareMethod(x); + break; + + case "invert": result+= operations.invertMethod(x); + break; + + case "!": result+= operations.factorialMethod(x); + break; + + case "sqrt": result+= operations.squareRootMethod(x); + break; + + case "sin": result+= operations.sineMathMethod(x); + break; + + case "asin": result+= operations.invSineMathMethod(x); + break; + + case "cos": result+= operations.cosMathMethod(x); + break; + + case "acos": result+= operations.invCosMathMethod(x); + break; + + case "tan": result+= operations.tanMathMethod(x); + break; + + case "atan": result+= operations.invTanMathMethod(x); + break; + } + return result; + } +} \ No newline at end of file diff --git a/ProcessorTest.java b/ProcessorTest.java new file mode 100644 index 0000000..a4969be --- /dev/null +++ b/ProcessorTest.java @@ -0,0 +1,56 @@ + + +import static org.junit.Assert.*; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * The test class ProcessorTest. + * + * @author (your name) + * @version (a version number or a date) + */ +public class ProcessorTest +{ + private static Processor processor; + /** + * Default constructor for test class ProcessorTest + */ + public ProcessorTest() + { + processor = new Processor(); + } + + /** + * Sets up the test fixture. + * + * Called before every test case method. + */ + @Before + public void setUp() + { + } + + /** + * Tears down the test fixture. + * + * Called after every test case method. + */ + @After + public void tearDown() + { + } + + @Test + public static void testArithmetic() { + processor.arithmetic("+", 2.0, 3.0); + assertEquals("+", 2.0, 3.0); + } + + // @Test + // public static void testDisplay() { + // processor.arithmetic(""); + // assertEquals("+", 2.0, 3.0); + // } +} diff --git a/package.bluej b/package.bluej index d50647f..41283b5 100644 --- a/package.bluej +++ b/package.bluej @@ -1,23 +1,29 @@ #BlueJ package file -dependency1.from=MainApplication -dependency1.to=Console +dependency1.from=Processor +dependency1.to=Calculator dependency1.type=UsesDependency -editor.fx.0.height=850 -editor.fx.0.width=1142 -editor.fx.0.x=568 -editor.fx.0.y=-936 -objectbench.height=214 -objectbench.width=595 -package.divider.horizontal=0.6 -package.divider.vertical=0.6847360912981455 -package.editor.height=473 +dependency2.from=Application +dependency2.to=Processor +dependency2.type=UsesDependency +dependency3.from=ProcessorTest +dependency3.to=Processor +dependency3.type=UsesDependency +editor.fx.0.height=0 +editor.fx.0.width=0 +editor.fx.0.x=0 +editor.fx.0.y=0 +objectbench.height=198 +objectbench.width=352 +package.divider.horizontal=0.5993322203672788 +package.divider.vertical=0.685099846390169 +package.editor.height=439 package.editor.width=493 -package.editor.x=-109 -package.editor.y=-994 -package.frame.height=759 +package.editor.x=100 +package.editor.y=23 +package.frame.height=709 package.frame.width=619 -package.numDependencies=1 -package.numTargets=5 +package.numDependencies=3 +package.numTargets=6 package.showExtends=true package.showUses=true project.charset=UTF-8 @@ -27,39 +33,44 @@ readme.width=47 readme.x=10 readme.y=10 target1.height=50 -target1.name=MainApplicationTest +target1.name=Processor target1.showInterface=false -target1.type=UnitTestTargetJunit4 -target1.width=120 -target1.x=110 -target1.y=40 -target2.association=ConsoleTest +target1.type=ClassTarget +target1.width=90 +target1.x=70 +target1.y=180 target2.height=50 -target2.name=Console +target2.name=CalculatorTest target2.showInterface=false -target2.type=ClassTarget -target2.width=80 -target2.x=80 -target2.y=200 +target2.type=UnitTestTargetJunit4 +target2.width=120 +target2.x=190 +target2.y=10 target3.height=50 -target3.name=ConsoleTest +target3.name=Calculator target3.showInterface=false -target3.type=UnitTestTargetJunit4 -target3.width=80 -target3.x=110 -target3.y=170 +target3.type=ClassTarget +target3.width=90 +target3.x=10 +target3.y=110 target4.height=50 -target4.name=operator +target4.name=ApplicationTest target4.showInterface=false -target4.type=ClassTarget -target4.width=80 -target4.x=240 -target4.y=110 -target5.association=MainApplicationTest +target4.type=UnitTestTargetJunit4 +target4.width=120 +target4.x=256 +target4.y=268 target5.height=50 -target5.name=MainApplication +target5.name=Application target5.showInterface=false target5.type=ClassTarget -target5.width=120 -target5.x=80 -target5.y=70 +target5.width=90 +target5.x=230 +target5.y=100 +target6.height=50 +target6.name=ProcessorTest +target6.showInterface=false +target6.type=UnitTestTargetJunit4 +target6.width=120 +target6.x=50 +target6.y=270