From 9231f4874647d3ec973aff2c1b6ba927c0ef64bf Mon Sep 17 00:00:00 2001 From: Swathi Bikumalia Date: Sat, 9 Feb 2019 16:15:42 -0500 Subject: [PATCH 01/10] Caluculator --- Calculator.java | 103 ++++++++++++++++++++++++++++++++++++++++++++ CalculatorTest.java | 42 ++++++++++++++++++ package.bluej | 66 +++++++++------------------- 3 files changed, 165 insertions(+), 46 deletions(-) create mode 100644 Calculator.java create mode 100644 CalculatorTest.java diff --git a/Calculator.java b/Calculator.java new file mode 100644 index 0000000..6f24d68 --- /dev/null +++ b/Calculator.java @@ -0,0 +1,103 @@ + +/** + * 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 int x; + + /** + * Constructor for objects of class Calculator + */ + public Calculator() + { + // initialise instance variables + x = 0; + } + + + + + /** + * TODO Write comments + */ + public int squareMethod(int x) + { + // put your code here + return x * x; + + } + + public double squareRootMethod(int x) + { + // put your code here + return Math.sqrt(x); + + } + public double inverseMethod(int x) + { + // put your code here + return (1/x); + + } + + public double sineMathMethod(int x) + { + // put your code here + return (Math.sin(x)); + + } + + public double cosMathMethod(int x) + { + // put your code here + return (Math.cos(x)); + + } + + public double tanMathMethod(int x) + { + // put your code here + return (Math.tan(x)); + + } + public double invSineMathMethod(int x) + { + // put your code here + return (Math.asin(x)); + + } + + public double invCosMathMethod(int x) + { + // put your code here + return (Math.acos(x)); + + } + + public double invTanMathMethod(int x) + { + // put your code here + return (Math.atan(x)); + + } + + public long factorialMethod(int x){ + + long result = 1; + + for (int factor = 2; factor <= x; factor++) { + result *= factor; + } + + return result; + } + + +} diff --git a/CalculatorTest.java b/CalculatorTest.java new file mode 100644 index 0000000..a4b50bb --- /dev/null +++ b/CalculatorTest.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 CalculatorTest. + * + * @author (your name) + * @version (a version number or a date) + */ +public class CalculatorTest +{ + /** + * Default constructor for test class CalculatorTest + */ + public CalculatorTest() + { + } + + /** + * 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/package.bluej b/package.bluej index d50647f..1d098f7 100644 --- a/package.bluej +++ b/package.bluej @@ -1,23 +1,20 @@ #BlueJ package file -dependency1.from=MainApplication -dependency1.to=Console -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 +editor.fx.0.height=722 +editor.fx.0.width=800 +editor.fx.0.x=240 +editor.fx.0.y=23 +objectbench.height=202 objectbench.width=595 package.divider.horizontal=0.6 -package.divider.vertical=0.6847360912981455 -package.editor.height=473 +package.divider.vertical=0.6847662141779789 +package.editor.height=447 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=721 package.frame.width=619 -package.numDependencies=1 -package.numTargets=5 +package.numDependencies=0 +package.numTargets=2 package.showExtends=true package.showUses=true project.charset=UTF-8 @@ -27,39 +24,16 @@ readme.width=47 readme.x=10 readme.y=10 target1.height=50 -target1.name=MainApplicationTest +target1.name=Calculator 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=10 target2.height=50 -target2.name=Console +target2.name=MainApplication target2.showInterface=false target2.type=ClassTarget -target2.width=80 +target2.width=120 target2.x=80 -target2.y=200 -target3.height=50 -target3.name=ConsoleTest -target3.showInterface=false -target3.type=UnitTestTargetJunit4 -target3.width=80 -target3.x=110 -target3.y=170 -target4.height=50 -target4.name=operator -target4.showInterface=false -target4.type=ClassTarget -target4.width=80 -target4.x=240 -target4.y=110 -target5.association=MainApplicationTest -target5.height=50 -target5.name=MainApplication -target5.showInterface=false -target5.type=ClassTarget -target5.width=120 -target5.x=80 -target5.y=70 +target2.y=70 From ab34d171018239b73a3d520e3393c1027068a226 Mon Sep 17 00:00:00 2001 From: Julian Rios Date: Sat, 9 Feb 2019 19:02:28 -0500 Subject: [PATCH 02/10] added logic class may combine with console and run method to main --- Calculator.java | 4 +-- CalculatorTest.java | 4 +-- Logic.java | 73 ++++++++++++++++++++++++++++++++++++++++++++ MainApplication.java | 31 ++++++++++++++++++- package.bluej | 41 ++++++++++++++----------- 5 files changed, 131 insertions(+), 22 deletions(-) create mode 100644 Logic.java diff --git a/Calculator.java b/Calculator.java index 6f24d68..89fac16 100644 --- a/Calculator.java +++ b/Calculator.java @@ -7,7 +7,7 @@ */ import java.lang.Math; -public class Calculator +public class Operations { // instance variables - replace the example below with your own private int x; @@ -15,7 +15,7 @@ public class Calculator /** * Constructor for objects of class Calculator */ - public Calculator() + public Operations() { // initialise instance variables x = 0; diff --git a/CalculatorTest.java b/CalculatorTest.java index a4b50bb..8be4152 100644 --- a/CalculatorTest.java +++ b/CalculatorTest.java @@ -11,12 +11,12 @@ * @author (your name) * @version (a version number or a date) */ -public class CalculatorTest +public class OperationsTest { /** * Default constructor for test class CalculatorTest */ - public CalculatorTest() + public OperationsTest() { } diff --git a/Logic.java b/Logic.java new file mode 100644 index 0000000..c7074a6 --- /dev/null +++ b/Logic.java @@ -0,0 +1,73 @@ +public class Logic { + + private static Operations operator = new Operations(); + + public static void arithmetic(String operator, int x, int y) { + + if(operator.equals((binary)||(octal)||(decimal)||(hexadecimal)){ + switchDisplayMode(operation); + } else if(operation.equals(display)) { + switchDisplayMode(); + } + + switch(operator.toLowerCase()) { + + case "+": operations.add(x,y); + break; + + case "-": operations.subtract(x,y); + break; + + case "/": operations.divide(x,y); + break; + + case "%": operations.remainder(x,y); + break; + + case "*": operations.mulit(x,y); + break; + + case "^": operations.exponentiation(x,y); + break; + + case "inverse": operations.inverse(x); + break; + + case "square": operations.square(x); + break; + + case "invert": operations.invert(x,y); + break; + + case "factorial": operations.factorial(x); + break; + + case "switchSign": operations.switchSign(x); + break; + + case "sqrt": operations.sqrt(x); + break; + + case "sine": operations.sine(x,y); + break; + + case "asin": operations.asin(x,y); + break; + + case "cos": operations.cos(x,y); + break; + + case "acos": operations.acos(x,y); + break; + + case "tan": operations.tan(x,y); + break; + + case "atan": operations.atan(x,y); + break; + + case "quit": + break; + } + } +} \ No newline at end of file diff --git a/MainApplication.java b/MainApplication.java index 7407110..1e2ddf0 100644 --- a/MainApplication.java +++ b/MainApplication.java @@ -1,4 +1,33 @@ -public class MainApplication { +public class Calculator { + public static void main(String[] args) { + run(); } + + public static run() { + Console console = new Console(System.in, System.out); + Operations operations = new Operations(); + Logic processor = new Logic(); + + boolean quit = false; + // Hello + while(!quit) { + // enter x + int x = console.input(); + // enter operator + String operator = console.input(); + // enter y + int y = console.input(); + + processor.arithmetic(operator, x, y) + // print result + // More math? + String redo = console.input(); + if(redo.equals("no")) { + quit = true; + } + } + // bye! + } } + diff --git a/package.bluej b/package.bluej index 1d098f7..161680c 100644 --- a/package.bluej +++ b/package.bluej @@ -1,20 +1,20 @@ #BlueJ package file -editor.fx.0.height=722 -editor.fx.0.width=800 -editor.fx.0.x=240 -editor.fx.0.y=23 +editor.fx.0.height=0 +editor.fx.0.width=0 +editor.fx.0.x=0 +editor.fx.0.y=0 objectbench.height=202 -objectbench.width=595 -package.divider.horizontal=0.6 +objectbench.width=352 +package.divider.horizontal=0.5993322203672788 package.divider.vertical=0.6847662141779789 package.editor.height=447 package.editor.width=493 -package.editor.x=100 -package.editor.y=23 +package.editor.x=96 +package.editor.y=77 package.frame.height=721 package.frame.width=619 package.numDependencies=0 -package.numTargets=2 +package.numTargets=3 package.showExtends=true package.showUses=true project.charset=UTF-8 @@ -24,16 +24,23 @@ readme.width=47 readme.x=10 readme.y=10 target1.height=50 -target1.name=Calculator +target1.name=CalculatorTest target1.showInterface=false -target1.type=ClassTarget -target1.width=90 -target1.x=70 +target1.type=UnitTestTargetJunit4 +target1.width=120 +target1.x=170 target1.y=10 target2.height=50 -target2.name=MainApplication +target2.name=Calculator target2.showInterface=false target2.type=ClassTarget -target2.width=120 -target2.x=80 -target2.y=70 +target2.width=90 +target2.x=70 +target2.y=10 +target3.height=50 +target3.name=MainApplication +target3.showInterface=false +target3.type=ClassTarget +target3.width=120 +target3.x=80 +target3.y=70 From bd5bb9dc0b7130da952a8e8ddd25b0d4abac0003 Mon Sep 17 00:00:00 2001 From: Julian Rios Date: Sun, 10 Feb 2019 11:30:13 -0500 Subject: [PATCH 03/10] added processor functionality with console and calculator operations --- Application.java | 38 ++++++++++++++++++++++++++++++++++++ Calculator.java | 4 ++-- CalculatorTest.java | 4 ++-- MainApplication.java | 33 ------------------------------- Logic.java => Processor.java | 20 ++++++++++++------- 5 files changed, 55 insertions(+), 44 deletions(-) create mode 100644 Application.java delete mode 100644 MainApplication.java rename Logic.java => Processor.java (67%) diff --git a/Application.java b/Application.java new file mode 100644 index 0000000..6668434 --- /dev/null +++ b/Application.java @@ -0,0 +1,38 @@ +public class Application { + + public static void main(String[] args) { + run(); + } + + public static void run() { + IOConsole console = new IOConsole(System.in, System.out); + Processor processor = new Processor(); + + boolean quit = false; + + // Hello + console.println("Hello"); + while(!quit) { + // enter x + console.println("Enter x"); + Double x = console.getDoubleInput(); + // enter operator + console.println("Enter operator"); + String operator = console.getStringInput(); + // enter y + console.println("Enter y"); + Double y = console.getDoubleInput(); + + processor.arithmetic(operator, x, y); + // print result + // More math? + String redo = console.getStringInput(); + if(redo.equals("no")) { + quit = true; + } + } + console.println("bye!"); + // bye! + } +} + diff --git a/Calculator.java b/Calculator.java index 89fac16..6f24d68 100644 --- a/Calculator.java +++ b/Calculator.java @@ -7,7 +7,7 @@ */ import java.lang.Math; -public class Operations +public class Calculator { // instance variables - replace the example below with your own private int x; @@ -15,7 +15,7 @@ public class Operations /** * Constructor for objects of class Calculator */ - public Operations() + public Calculator() { // initialise instance variables x = 0; diff --git a/CalculatorTest.java b/CalculatorTest.java index 8be4152..a4b50bb 100644 --- a/CalculatorTest.java +++ b/CalculatorTest.java @@ -11,12 +11,12 @@ * @author (your name) * @version (a version number or a date) */ -public class OperationsTest +public class CalculatorTest { /** * Default constructor for test class CalculatorTest */ - public OperationsTest() + public CalculatorTest() { } diff --git a/MainApplication.java b/MainApplication.java deleted file mode 100644 index 1e2ddf0..0000000 --- a/MainApplication.java +++ /dev/null @@ -1,33 +0,0 @@ -public class Calculator { - - public static void main(String[] args) { - run(); - } - - public static run() { - Console console = new Console(System.in, System.out); - Operations operations = new Operations(); - Logic processor = new Logic(); - - boolean quit = false; - // Hello - while(!quit) { - // enter x - int x = console.input(); - // enter operator - String operator = console.input(); - // enter y - int y = console.input(); - - processor.arithmetic(operator, x, y) - // print result - // More math? - String redo = console.input(); - if(redo.equals("no")) { - quit = true; - } - } - // bye! - } -} - diff --git a/Logic.java b/Processor.java similarity index 67% rename from Logic.java rename to Processor.java index c7074a6..613cc2a 100644 --- a/Logic.java +++ b/Processor.java @@ -1,13 +1,19 @@ -public class Logic { +public class Processor { - private static Operations operator = new Operations(); + private static Calculator operations; + private static IOConsole console; - public static void arithmetic(String operator, int x, int y) { + public Processor() { + operations = new Calculator(); + console = new IOConsole(System.in, System.out); + } + + public static void arithmetic(String operator, Double x, Double y) { - if(operator.equals((binary)||(octal)||(decimal)||(hexadecimal)){ - switchDisplayMode(operation); - } else if(operation.equals(display)) { - switchDisplayMode(); + if(operator.matches("binary|octal|decimal|hexadecimal")){ + operations.switchDisplayMode(operator); + } else if(operator.equals("display")) { + operations.switchDisplayMode(); } switch(operator.toLowerCase()) { From 0147f4a1c6e554f20d03be38696e9eef83579ad1 Mon Sep 17 00:00:00 2001 From: Julian Rios Date: Sun, 10 Feb 2019 14:30:40 -0500 Subject: [PATCH 04/10] added changeMode feature to speak with calculator and displayMdde --- Application.java | 35 +++++++- ApplicationTest.java | 42 ++++++++++ Processor.java | 186 +++++++++++++++++++++++++------------------ ProcessorTest.java | 56 +++++++++++++ package.bluej | 55 ++++++++----- 5 files changed, 274 insertions(+), 100 deletions(-) create mode 100644 ApplicationTest.java create mode 100644 ProcessorTest.java diff --git a/Application.java b/Application.java index 6668434..1d1d904 100644 --- a/Application.java +++ b/Application.java @@ -7,12 +7,38 @@ public static void main(String[] args) { public static void run() { IOConsole console = new IOConsole(System.in, System.out); Processor processor = new Processor(); + String result = ""; + String numConvert = ""; + boolean quit = false; // Hello console.println("Hello"); while(!quit) { + + console.println("Would you like to change the display?"); + String input = console.getStringInput(); + if (input.matches("yes")){ + console.println("Would you like to view display options"); + String viewDisplayinput = console.getStringInput(); + if(viewDisplayinput.matches("yes")) { + processor.displayMode("display"); + } else { + console.println("Enter Mode:"); + String mode = console.getStringInput(); + processor.displayMode(mode); + console.println("Enter x"); + Double x = console.getDoubleInput(); + numConvert = processor.changeMode(mode, x); + console.println(numConvert); + // enter y + console.println("Enter y"); + Double y = console.getDoubleInput(); + numConvert = processor.changeMode(mode, y); + console.println(numConvert); + } + } // enter x console.println("Enter x"); Double x = console.getDoubleInput(); @@ -23,15 +49,20 @@ public static void run() { console.println("Enter y"); Double y = console.getDoubleInput(); - processor.arithmetic(operator, x, y); + result = processor.arithmetic(operator, x, y); // print result + console.log(result); + + console.println("Would you like to do more Math????"); + console.println("Enter: yes to continue."); + console.println("Enter: no to quit."); + // More math? String redo = console.getStringInput(); if(redo.equals("no")) { quit = true; } } - console.println("bye!"); // bye! } } 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/Processor.java b/Processor.java index 613cc2a..12208a4 100644 --- a/Processor.java +++ b/Processor.java @@ -1,79 +1,111 @@ public class Processor { - - private static Calculator operations; - private static IOConsole console; - - public Processor() { - operations = new Calculator(); - console = new IOConsole(System.in, System.out); - } - - public static void arithmetic(String operator, Double x, Double y) { - - if(operator.matches("binary|octal|decimal|hexadecimal")){ - operations.switchDisplayMode(operator); - } else if(operator.equals("display")) { - operations.switchDisplayMode(); - } - - switch(operator.toLowerCase()) { - - case "+": operations.add(x,y); - break; - - case "-": operations.subtract(x,y); - break; - - case "/": operations.divide(x,y); - break; - - case "%": operations.remainder(x,y); - break; - - case "*": operations.mulit(x,y); - break; - - case "^": operations.exponentiation(x,y); - break; - - case "inverse": operations.inverse(x); - break; - - case "square": operations.square(x); - break; - - case "invert": operations.invert(x,y); - break; - - case "factorial": operations.factorial(x); - break; - - case "switchSign": operations.switchSign(x); - break; - - case "sqrt": operations.sqrt(x); - break; - - case "sine": operations.sine(x,y); - break; - - case "asin": operations.asin(x,y); - break; - - case "cos": operations.cos(x,y); - break; - - case "acos": operations.acos(x,y); - break; - - case "tan": operations.tan(x,y); - break; - - case "atan": operations.atan(x,y); - break; - - case "quit": - break; - } - } + + 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, Double x, Double y) { + String result = ""; + if(mode.matches("binary")){ + Integer binaryX = (Integer) x; + Integer BinaryY = (Integer) y; + result += operations.toBinary(x); + result += operations.toBinary(y); + } else if(mode.matches("octal")){ + Integer octalX = (Integer) x; + Integer octalY = (Integer) y; + result += operations.toOctal(x,y); + result += operations.toOctal(y); + } else if(mode.matches("decimal")){ + Integer decimalX = (Integer) x; + Integer decimalY = (Integer) y; + result += operations.toDecimal(x); + result += operations.toDecimal(y); + } else if (mode.matches("hexadecimal")){ + Integer hexadecimalX = (Integer) x; + Integer hexadecimalY = (Integer) y; + result += operations.toHexadecimal(x,y); + result += operations.toHexadecimal(y); + } + 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.subtract(x,y); + break; + + case "/": result+= operations.divide(x,y); + break; + + case "%": result+= operations.remainder(x,y); + break; + + case "*": result+= operations.mulit(x,y); + break; + + case "^": result+= operations.exponentiation(x,y); + break; + + case "inverse": result+= operations.inverse(x); + break; + + case "square": result+= operations.square(x); + break; + + case "invert": result+= operations.invert(x,y); + break; + + case "factorial": result+= operations.factorial(x); + break; + + case "switchSign": result+= operations.switchSign(x); + break; + + case "sqrt": result+= operations.sqrt(x); + break; + + case "sine": result+= operations.sine(x,y); + break; + + case "asin": result+= operations.asin(x,y); + break; + + case "cos": result+= operations.cos(x,y); + break; + + case "acos": result+= operations.acos(x,y); + break; + + case "tan": result+= operations.tan(x,y); + break; + + case "atan": result+= operations.atan(x,y); + break; + + case "quit": result = "bye!"; + break; + } + return result; + } } \ No newline at end of file diff --git a/ProcessorTest.java b/ProcessorTest.java new file mode 100644 index 0000000..a8233b0 --- /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 161680c..c1908d2 100644 --- a/package.bluej +++ b/package.bluej @@ -1,20 +1,26 @@ #BlueJ package file +dependency1.from=Processor +dependency1.to=Calculator +dependency1.type=UsesDependency +dependency2.from=Application +dependency2.to=Processor +dependency2.type=UsesDependency editor.fx.0.height=0 editor.fx.0.width=0 editor.fx.0.x=0 editor.fx.0.y=0 -objectbench.height=202 +objectbench.height=198 objectbench.width=352 package.divider.horizontal=0.5993322203672788 -package.divider.vertical=0.6847662141779789 -package.editor.height=447 +package.divider.vertical=0.685099846390169 +package.editor.height=439 package.editor.width=493 -package.editor.x=96 -package.editor.y=77 -package.frame.height=721 +package.editor.x=661 +package.editor.y=23 +package.frame.height=709 package.frame.width=619 -package.numDependencies=0 -package.numTargets=3 +package.numDependencies=2 +package.numTargets=4 package.showExtends=true package.showUses=true project.charset=UTF-8 @@ -24,23 +30,30 @@ readme.width=47 readme.x=10 readme.y=10 target1.height=50 -target1.name=CalculatorTest +target1.name=Processor target1.showInterface=false -target1.type=UnitTestTargetJunit4 -target1.width=120 -target1.x=170 -target1.y=10 +target1.type=ClassTarget +target1.width=90 +target1.x=70 +target1.y=180 target2.height=50 -target2.name=Calculator +target2.name=CalculatorTest target2.showInterface=false -target2.type=ClassTarget -target2.width=90 -target2.x=70 +target2.type=UnitTestTargetJunit4 +target2.width=120 +target2.x=190 target2.y=10 target3.height=50 -target3.name=MainApplication +target3.name=Calculator target3.showInterface=false target3.type=ClassTarget -target3.width=120 -target3.x=80 -target3.y=70 +target3.width=90 +target3.x=10 +target3.y=110 +target4.height=50 +target4.name=Application +target4.showInterface=false +target4.type=ClassTarget +target4.width=90 +target4.x=220 +target4.y=100 From 7b9235626e4a7a5d0d7f47c76c125647db49256d Mon Sep 17 00:00:00 2001 From: Julian Rios Date: Sun, 10 Feb 2019 14:40:07 -0500 Subject: [PATCH 05/10] update app corrections --- Application.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Application.java b/Application.java index 1d1d904..3ee54b0 100644 --- a/Application.java +++ b/Application.java @@ -5,14 +5,13 @@ public static void main(String[] args) { } public static void run() { - IOConsole console = new IOConsole(System.in, System.out); + IOConsole console = new IOConsole(); Processor processor = new Processor(); String result = ""; String numConvert = ""; boolean quit = false; - // Hello console.println("Hello"); while(!quit) { From 8a580a3b087a663615e32e87ad034c2239873b97 Mon Sep 17 00:00:00 2001 From: Thao Nguyen Date: Sun, 10 Feb 2019 14:45:47 -0500 Subject: [PATCH 06/10] console and run feature --- Calculator.java | 25 ++++++ IOConsole.java | 79 +++++++++++++++++ MainApplication.java | 20 ++++- PrintTest.java | 200 +++++++++++++++++++++++++++++++++++++++++++ package.bluej | 69 +++++++-------- 5 files changed, 356 insertions(+), 37 deletions(-) create mode 100644 Calculator.java create mode 100644 IOConsole.java create mode 100644 PrintTest.java diff --git a/Calculator.java b/Calculator.java new file mode 100644 index 0000000..525d81d --- /dev/null +++ b/Calculator.java @@ -0,0 +1,25 @@ + +import java.util.Scanner; +/** + * Write a description of class Calculator here. + * + * @author (your name) + * @version (a version number or a date) + */ +public class Calculator +{ + private IOConsole inputOutput; + + public Calculator(){ + inputOutput = new IOConsole(); + } + + 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/IOConsole.java b/IOConsole.java new file mode 100644 index 0000000..38ef1d1 --- /dev/null +++ b/IOConsole.java @@ -0,0 +1,79 @@ +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; + } +} 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/package.bluej b/package.bluej index d50647f..ee36ab8 100644 --- a/package.bluej +++ b/package.bluej @@ -1,23 +1,29 @@ #BlueJ package file -dependency1.from=MainApplication -dependency1.to=Console +dependency1.from=Calculator +dependency1.to=IOConsole dependency1.type=UsesDependency -editor.fx.0.height=850 -editor.fx.0.width=1142 -editor.fx.0.x=568 -editor.fx.0.y=-936 +dependency2.from=PrintTest +dependency2.to=IOConsole +dependency2.type=UsesDependency +dependency3.from=MainApplication +dependency3.to=IOConsole +dependency3.type=UsesDependency +editor.fx.0.height=722 +editor.fx.0.width=800 +editor.fx.0.x=152 +editor.fx.0.y=222 objectbench.height=214 objectbench.width=595 package.divider.horizontal=0.6 package.divider.vertical=0.6847360912981455 package.editor.height=473 package.editor.width=493 -package.editor.x=-109 -package.editor.y=-994 +package.editor.x=710 +package.editor.y=115 package.frame.height=759 package.frame.width=619 -package.numDependencies=1 -package.numTargets=5 +package.numDependencies=3 +package.numTargets=4 package.showExtends=true package.showUses=true project.charset=UTF-8 @@ -27,39 +33,30 @@ readme.width=47 readme.x=10 readme.y=10 target1.height=50 -target1.name=MainApplicationTest +target1.name=Calculator 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=260 +target1.y=180 target2.height=50 -target2.name=Console +target2.name=PrintTest target2.showInterface=false -target2.type=ClassTarget +target2.type=UnitTestTargetJunit4 target2.width=80 -target2.x=80 -target2.y=200 +target2.x=180 +target2.y=10 target3.height=50 -target3.name=ConsoleTest +target3.name=IOConsole target3.showInterface=false -target3.type=UnitTestTargetJunit4 -target3.width=80 -target3.x=110 -target3.y=170 +target3.type=ClassTarget +target3.width=90 +target3.x=250 +target3.y=90 target4.height=50 -target4.name=operator +target4.name=MainApplication target4.showInterface=false target4.type=ClassTarget -target4.width=80 -target4.x=240 +target4.width=120 +target4.x=40 target4.y=110 -target5.association=MainApplicationTest -target5.height=50 -target5.name=MainApplication -target5.showInterface=false -target5.type=ClassTarget -target5.width=120 -target5.x=80 -target5.y=70 From ad23f6b3477785a2ddd866fbebf55b8124bf967f Mon Sep 17 00:00:00 2001 From: Thao Nguyen Date: Sun, 10 Feb 2019 14:53:47 -0500 Subject: [PATCH 07/10] changed name --- Calculator.java => Hello.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename Calculator.java => Hello.java (92%) diff --git a/Calculator.java b/Hello.java similarity index 92% rename from Calculator.java rename to Hello.java index 525d81d..036069e 100644 --- a/Calculator.java +++ b/Hello.java @@ -6,11 +6,11 @@ * @author (your name) * @version (a version number or a date) */ -public class Calculator +public class Hello { private IOConsole inputOutput; - public Calculator(){ + public Hello(){ inputOutput = new IOConsole(); } From e8131c5b6596be0da245e685199a7bf76aa9cb62 Mon Sep 17 00:00:00 2001 From: Swathi Bikumalia Date: Sun, 10 Feb 2019 15:09:41 -0500 Subject: [PATCH 08/10] merging calc --- Calculator.java | 104 ++++++++++++++++---- CalculatorTest.java | 229 +++++++++++++++++++++++++++++++++++++++++--- package.bluej | 36 ++++--- 3 files changed, 324 insertions(+), 45 deletions(-) diff --git a/Calculator.java b/Calculator.java index 6f24d68..4e62210 100644 --- a/Calculator.java +++ b/Calculator.java @@ -7,7 +7,8 @@ */ import java.lang.Math; -public class Calculator + +public class Calculator extends MainApplication { // instance variables - replace the example below with your own private int x; @@ -22,69 +23,112 @@ public Calculator() } - - /** * TODO Write comments */ - public int squareMethod(int x) + public double squareMethod(int 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) + { if(x>y) + { + return (x-y); + } + return 0; + } + + public double multiply(double x,double y) + { + return (x*y); + } + + public double div(double x,double y) + { + return (double)(x/y); + } public double squareRootMethod(int x) { // put your code here return Math.sqrt(x); } - public double inverseMethod(int x) + + public double invertMethod(int x) { // put your code here - return (1/x); + x=-x; + return x; + + } + public double exponentiationtMethod(int x,int y) + { + // put your code here + return Math.pow(x,y); } - public double sineMathMethod(int x) + public double inverseMethod(int x) { // put your code here - return (Math.sin(x)); + return 1/(double)x; } - public double cosMathMethod(int 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(x)); + return (Math.cos(radians)); } - public double tanMathMethod(int x) + public double tanMathMethod(double x) { + double radians = Math.toRadians(x); // put your code here - return (Math.tan(x)); + return (Math.tan(radians)); } - public double invSineMathMethod(int x) + public double invSineMathMethod(double x) { + double radians = Math.toRadians(x); // put your code here - return (Math.asin(x)); + return (Math.asin(radians)); } - public double invCosMathMethod(int x) + public double invCosMathMethod(double x) { + double radians = Math.toRadians(x); // put your code here - return (Math.acos(x)); + return (Math.acos(radians)); } - public double invTanMathMethod(int x) + public double invTanMathMethod(double x) { + double radians = Math.toRadians(x); // put your code here - return (Math.atan(x)); + return (Math.atan(radians)); } @@ -99,5 +143,27 @@ public long factorialMethod(int x){ 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 index a4b50bb..c8fd2b1 100644 --- a/CalculatorTest.java +++ b/CalculatorTest.java @@ -1,7 +1,6 @@ -import static org.junit.Assert.*; -import org.junit.After; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -13,11 +12,13 @@ */ public class CalculatorTest { + private Calculator calculator; /** * Default constructor for test class CalculatorTest */ public CalculatorTest() { + } /** @@ -25,18 +26,220 @@ public CalculatorTest() * * Called before every test case method. */ - @Before - public void setUp() - { + @Before + public void setUp(){ + calculator = new Calculator(); } - /** - * Tears down the test fixture. - * - * Called after every test case method. - */ - @After - public void tearDown() - { + @Test + public void squareMethodTest(){ + //:Given + int x = 5; + + //Expected + double expected = 25; + double actual = calculator.squareMethod( x ); + + //:Then + Assert.assertEquals("Values should be equal", expected, actual); + } + + @Test + public void addMethodTest(){ + //:Given + double x = 5; + double y = 8; + + //Expected + double expected = 13; + double actual = calculator.add(x , y); + + //:Then + Assert.assertEquals("Values should be equal", expected, actual); + } + + + @Test + public void subStartMethodTest(){ + //:Given + double x = 8; + double y = 5; + + //Expected + double expected = 3; + double actual = calculator.sub(x , y); + + //:Then + Assert.assertEquals("Values should be equal", expected, actual); + } + + @Test + public void multiStartMethodTest(){ + //:Given + double x = 5; + double y = 8; + + //Expected + double expected = 40; + double actual = calculator.multiply(x , y); + + //:Then + Assert.assertEquals("Values should be equal", expected, actual); + } + + + @Test + public void divStartMethodTest(){ + //:Given + double x = 8; + double y = 5; + + //Expected + double expected = 1.6; + double actual = calculator.div(x , y); + + //:Then + Assert.assertEquals( expected, actual , Double.NaN); + } + + + + @Test + public void squareRootMethodTest(){ + //:Given + int x = 16; + + + //Expected + double expected = 4.0; + double actual = calculator.squareRootMethod(x); + + + + //:Then + Assert.assertEquals( expected, actual , Double.NaN); + } + + + public void exponentiationMethodTest(){ + //:Given + int x = 3; + int y = 2; + + + //Expected + double expected = 9; + double actual = calculator.exponentiationtMethod( x , y ); + + + + //:Then + Assert.assertEquals( expected, actual , Double.NaN); + } + + public void invertMethodTest(){ + //:Given + int x = 3; + + + + //Expected + double expected = -3; + double actual = calculator.invertMethod( x ); + + + + //:Then + Assert.assertEquals( expected, actual , Double.NaN); + } + @Test + public void inverseMethodTest(){ + //:Given + int x = 2; + + //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 + int x = 4; + + //Expected + float expected = 24; + double actual = calculator.factorialMethod(x); + + //:Then + Assert.assertEquals( expected, actual , Double.NaN); } } diff --git a/package.bluej b/package.bluej index 1d098f7..3b9d291 100644 --- a/package.bluej +++ b/package.bluej @@ -1,7 +1,10 @@ #BlueJ package file +dependency1.from=CalculatorTest +dependency1.to=Calculator +dependency1.type=UsesDependency editor.fx.0.height=722 -editor.fx.0.width=800 -editor.fx.0.x=240 +editor.fx.0.width=1094 +editor.fx.0.x=345 editor.fx.0.y=23 objectbench.height=202 objectbench.width=595 @@ -13,8 +16,8 @@ package.editor.x=100 package.editor.y=23 package.frame.height=721 package.frame.width=619 -package.numDependencies=0 -package.numTargets=2 +package.numDependencies=1 +package.numTargets=3 package.showExtends=true package.showUses=true project.charset=UTF-8 @@ -24,16 +27,23 @@ readme.width=47 readme.x=10 readme.y=10 target1.height=50 -target1.name=Calculator +target1.name=CalculatorTest target1.showInterface=false -target1.type=ClassTarget -target1.width=90 -target1.x=70 -target1.y=10 +target1.type=UnitTestTargetJunit4 +target1.width=120 +target1.x=10 +target1.y=130 target2.height=50 -target2.name=MainApplication +target2.name=Calculator target2.showInterface=false target2.type=ClassTarget -target2.width=120 -target2.x=80 -target2.y=70 +target2.width=90 +target2.x=230 +target2.y=130 +target3.height=50 +target3.name=MainApplication +target3.showInterface=false +target3.type=ClassTarget +target3.width=120 +target3.x=200 +target3.y=30 From 1aeb1dfcce0fb98ec56552b789545585fb41a229 Mon Sep 17 00:00:00 2001 From: Julian Rios Date: Sun, 10 Feb 2019 15:37:29 -0500 Subject: [PATCH 09/10] package.bluej --- package.bluej | 131 ++++++++++++++++++++------------------------------ 1 file changed, 52 insertions(+), 79 deletions(-) diff --git a/package.bluej b/package.bluej index 4cf0c27..41283b5 100644 --- a/package.bluej +++ b/package.bluej @@ -1,45 +1,29 @@ #BlueJ package file -<<<<<<< HEAD -dependency1.from=CalculatorTest +dependency1.from=Processor dependency1.to=Calculator dependency1.type=UsesDependency -editor.fx.0.height=722 -editor.fx.0.width=1094 -editor.fx.0.x=186 -editor.fx.0.y=23 -objectbench.height=202 -objectbench.width=540 -package.divider.horizontal=0.6 -package.divider.vertical=0.6847662141779789 -package.editor.height=447 -package.editor.width=438 -package.editor.x=54 -package.editor.y=23 -package.frame.height=721 -package.frame.width=564 -package.numDependencies=1 -package.numTargets=2 -======= -dependency1.from=MainApplication -dependency1.to=Console -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 ->>>>>>> d98ffe9a86811f41e803af4fcb08e2a833626fd6 +package.numDependencies=3 +package.numTargets=6 package.showExtends=true package.showUses=true project.charset=UTF-8 @@ -49,55 +33,44 @@ readme.width=47 readme.x=10 readme.y=10 target1.height=50 -<<<<<<< HEAD -target1.name=CalculatorTest -target1.showInterface=false -target1.type=UnitTestTargetJunit4 -target1.width=120 -target1.x=10 -target1.y=130 -target2.height=50 -target2.name=Calculator -target2.showInterface=false -target2.type=ClassTarget -target2.width=90 -target2.x=230 -target2.y=130 -======= -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 ->>>>>>> d98ffe9a86811f41e803af4fcb08e2a833626fd6 +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 From 5273c5ccfab0af64d61806812279833746e8194a Mon Sep 17 00:00:00 2001 From: Julian Rios Date: Sun, 10 Feb 2019 19:20:33 -0500 Subject: [PATCH 10/10] final version --- Application.java | 98 +++++++++++++++++++++++++++---------------- Calculator.java | 69 +++++++++++++++--------------- CalculatorTest.java | 100 ++++++++++++++++++++++---------------------- Hello.java | 25 ----------- IOConsole.java | 4 ++ Processor.java | 66 +++++++++++------------------ ProcessorTest.java | 10 ++--- 7 files changed, 178 insertions(+), 194 deletions(-) delete mode 100644 Hello.java diff --git a/Application.java b/Application.java index 3ee54b0..7cf5332 100644 --- a/Application.java +++ b/Application.java @@ -1,5 +1,9 @@ public class Application { + public Application() { + + } + public static void main(String[] args) { run(); } @@ -13,56 +17,76 @@ public static void run() { boolean quit = false; // Hello - console.println("Hello"); + console.println("Hi!"); while(!quit) { - console.println("Would you like to change the display?"); - String input = console.getStringInput(); - if (input.matches("yes")){ - console.println("Would you like to view display options"); - String viewDisplayinput = console.getStringInput(); - if(viewDisplayinput.matches("yes")) { - processor.displayMode("display"); - } else { - console.println("Enter Mode:"); - String mode = console.getStringInput(); - processor.displayMode(mode); - console.println("Enter x"); - Double x = console.getDoubleInput(); - numConvert = processor.changeMode(mode, x); - console.println(numConvert); - // enter y - console.println("Enter y"); - Double y = console.getDoubleInput(); - numConvert = processor.changeMode(mode, y); - console.println(numConvert); - } - } - // enter x - console.println("Enter x"); - Double x = console.getDoubleInput(); + // 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 - console.println("Enter operator"); - String operator = console.getStringInput(); - // enter y - console.println("Enter y"); - Double y = console.getDoubleInput(); + String operator = console.getStringInput("Enter operator"); - result = processor.arithmetic(operator, x, y); - // print result - console.log(result); + 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."); - console.println("Enter: no to quit."); + String redo = console.getStringInput("Enter: no to quit."); // More math? - String redo = console.getStringInput(); 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/Calculator.java b/Calculator.java index 4e62210..e4a0bb0 100644 --- a/Calculator.java +++ b/Calculator.java @@ -8,10 +8,10 @@ import java.lang.Math; -public class Calculator extends MainApplication +public class Calculator { // instance variables - replace the example below with your own - private int x; + private Double x; /** * Constructor for objects of class Calculator @@ -19,120 +19,117 @@ public class Calculator extends MainApplication public Calculator() { // initialise instance variables - x = 0; + x = 0.0; } /** * TODO Write comments */ - public double squareMethod(int x) + public Double squareMethod(Double x) { // put your code here return x * x; } - public double add(double x,double y) + public Double add(Double x,Double y) { return (x+y); } - public double sub(double x,double y) - { if(x>y) - { - return (x-y); - } - return 0; + public Double sub(Double x,Double y) + { + return x - y; } - public double multiply(double x,double y) + public Double multiply(Double x,Double y) { return (x*y); } - public double div(double x,double y) + public Double div(Double x,Double y) { - return (double)(x/y); + return (Double)(x/y); } - public double squareRootMethod(int x) + public Double squareRootMethod(Double x) { // put your code here return Math.sqrt(x); } - public double invertMethod(int x) + public Double invertMethod(Double x) { // put your code here x=-x; return x; } - public double exponentiationtMethod(int x,int y) + public Double exponentiationMethod(Double x,Double y) { // put your code here return Math.pow(x,y); } - public double inverseMethod(int x) + public Double inverseMethod(Double x) { // put your code here - return 1/(double)x; + return 1/(Double)x; } - public double sineMathMethod(double x) + public Double sineMathMethod(Double x) { - double radians = Math.toRadians(x); + Double radians = Math.toRadians(x); - return Math.sin(radians); + return (Math.sin(radians)); } - public double cosMathMethod(double x) + public Double cosMathMethod(Double x) { - double radians = Math.toRadians(x); + Double radians = Math.toRadians(x); // put your code here return (Math.cos(radians)); } - public double tanMathMethod(double x) + public Double tanMathMethod(Double x) { - double radians = Math.toRadians(x); + Double radians = Math.toRadians(x); // put your code here return (Math.tan(radians)); } - public double invSineMathMethod(double x) + public Double invSineMathMethod(Double x) { - double radians = Math.toRadians(x); - // put your code here - return (Math.asin(radians)); + //Double radians = Math.toRadians(x); + + return (Math.asin(x)); } - public double invCosMathMethod(double x) + public Double invCosMathMethod(Double x) { - double radians = Math.toRadians(x); + Double radians = Math.toRadians(x); // put your code here return (Math.acos(radians)); } - public double invTanMathMethod(double x) + public Double invTanMathMethod(Double x) { - double radians = Math.toRadians(x); + //Double radians = Math.toRadians(x); // put your code here - return (Math.atan(radians)); + return (Math.atan(x)); } - public long factorialMethod(int x){ + public long factorialMethod(Double x){ long result = 1; diff --git a/CalculatorTest.java b/CalculatorTest.java index c8fd2b1..b9df616 100644 --- a/CalculatorTest.java +++ b/CalculatorTest.java @@ -34,11 +34,11 @@ public void setUp(){ @Test public void squareMethodTest(){ //:Given - int x = 5; + Double x = 5.0; //Expected - double expected = 25; - double actual = calculator.squareMethod( x ); + Double expected = 25.0; + Double actual = calculator.squareMethod( x ); //:Then Assert.assertEquals("Values should be equal", expected, actual); @@ -47,12 +47,12 @@ public void squareMethodTest(){ @Test public void addMethodTest(){ //:Given - double x = 5; - double y = 8; + Double x = 5.0; + Double y = 8.0; //Expected - double expected = 13; - double actual = calculator.add(x , y); + Double expected = 13.0; + Double actual = calculator.add(x , y); //:Then Assert.assertEquals("Values should be equal", expected, actual); @@ -62,12 +62,12 @@ public void addMethodTest(){ @Test public void subStartMethodTest(){ //:Given - double x = 8; - double y = 5; + Double x = 8.0; + Double y = 5.0; //Expected - double expected = 3; - double actual = calculator.sub(x , y); + Double expected = 3.0; + Double actual = calculator.sub(x , y); //:Then Assert.assertEquals("Values should be equal", expected, actual); @@ -76,12 +76,12 @@ public void subStartMethodTest(){ @Test public void multiStartMethodTest(){ //:Given - double x = 5; - double y = 8; + Double x = 5.0; + Double y = 8.0; //Expected - double expected = 40; - double actual = calculator.multiply(x , y); + Double expected = 40.0; + Double actual = calculator.multiply(x , y); //:Then Assert.assertEquals("Values should be equal", expected, actual); @@ -91,12 +91,12 @@ public void multiStartMethodTest(){ @Test public void divStartMethodTest(){ //:Given - double x = 8; - double y = 5; + Double x = 8.0; + Double y = 5.0; //Expected - double expected = 1.6; - double actual = calculator.div(x , y); + Double expected = 1.6; + Double actual = calculator.div(x , y); //:Then Assert.assertEquals( expected, actual , Double.NaN); @@ -107,12 +107,12 @@ public void divStartMethodTest(){ @Test public void squareRootMethodTest(){ //:Given - int x = 16; + Double x = 16.0; //Expected - double expected = 4.0; - double actual = calculator.squareRootMethod(x); + Double expected = 4.0; + Double actual = calculator.squareRootMethod(x); @@ -123,13 +123,13 @@ public void squareRootMethodTest(){ public void exponentiationMethodTest(){ //:Given - int x = 3; - int y = 2; + Double x = 3.0; + Double y = 2.0; //Expected - double expected = 9; - double actual = calculator.exponentiationtMethod( x , y ); + Double expected = 9.0; + Double actual = calculator.exponentiationMethod( x , y ); @@ -139,13 +139,13 @@ public void exponentiationMethodTest(){ public void invertMethodTest(){ //:Given - int x = 3; + Double x = 3.0; //Expected - double expected = -3; - double actual = calculator.invertMethod( x ); + Double expected = -3.0; + Double actual = calculator.invertMethod( x ); @@ -155,11 +155,11 @@ public void invertMethodTest(){ @Test public void inverseMethodTest(){ //:Given - int x = 2; + Double x = 2.0; //Expected - double expected = 0.5; - double actual = calculator.inverseMethod(x); + Double expected = 0.5; + Double actual = calculator.inverseMethod(x); //:Then Assert.assertEquals( expected, actual , Double.NaN); @@ -168,11 +168,11 @@ public void inverseMethodTest(){ @Test public void sineMathMethodTest(){ //:Given - double x = 90.0; + Double x = 90.0; //Expected - double expected = 1.0; - double actual = calculator.sineMathMethod(x); + Double expected = 1.0; + Double actual = calculator.sineMathMethod(x); //:Then Assert.assertEquals( expected, actual , Double.NaN); @@ -181,11 +181,11 @@ public void sineMathMethodTest(){ @Test public void tanMathMethodTest(){ //:Given - double x = 45.0; + Double x = 45.0; //Expected - double expected = 0.9999999999999999; - double actual = calculator.tanMathMethod(x); + Double expected = 0.9999999999999999; + Double actual = calculator.tanMathMethod(x); //:Then Assert.assertEquals( expected, actual , Double.NaN); @@ -194,11 +194,11 @@ public void tanMathMethodTest(){ @Test public void invSineMathMethodTest(){ //:Given - double x = 45.0; + Double x = 45.0; //Expected - double expected = 0.9033391107665127; - double actual = calculator.invSineMathMethod(x); + Double expected = 0.9033391107665127; + Double actual = calculator.invSineMathMethod(x); //:Then Assert.assertEquals( expected, actual , Double.NaN); @@ -207,11 +207,11 @@ public void invSineMathMethodTest(){ @Test public void invCosMathMethodTest(){ //:Given - double x = 45.0; + Double x = 45.0; //Expected - double expected = 0.6674572160283838; - double actual = calculator.invCosMathMethod(x); + Double expected = 0.6674572160283838; + Double actual = calculator.invCosMathMethod(x); //:Then Assert.assertEquals( expected, actual , Double.NaN); @@ -220,11 +220,11 @@ public void invCosMathMethodTest(){ @Test public void invTanMathMethodTest(){ //:Given - double x = 45.0; + Double x = 45.0; //Expected - double expected = 0.6657737500283538; - double actual = calculator.invTanMathMethod(x); + Double expected = 0.6657737500283538; + Double actual = calculator.invTanMathMethod(x); //:Then Assert.assertEquals( expected, actual , Double.NaN); @@ -233,11 +233,11 @@ public void invTanMathMethodTest(){ @Test public void factorialMethodTest(){ //:Given - int x = 4; + Double x = 4.0; //Expected - float expected = 24; - double actual = calculator.factorialMethod(x); + Long expected = 24L; + Long actual = calculator.factorialMethod(x); //:Then Assert.assertEquals( expected, actual , Double.NaN); diff --git a/Hello.java b/Hello.java deleted file mode 100644 index 036069e..0000000 --- a/Hello.java +++ /dev/null @@ -1,25 +0,0 @@ - -import java.util.Scanner; -/** - * Write a description of class Calculator here. - * - * @author (your name) - * @version (a version number or a date) - */ -public class Hello -{ - private IOConsole inputOutput; - - public Hello(){ - inputOutput = new IOConsole(); - } - - 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/IOConsole.java b/IOConsole.java index 38ef1d1..ed88845 100644 --- a/IOConsole.java +++ b/IOConsole.java @@ -76,4 +76,8 @@ public Double getDoubleInput(String prompt, Object... args) { Double result = scanner.nextDouble(); return result; } + + public void clearScreen() { + out.print('\u000C'); + } } diff --git a/Processor.java b/Processor.java index 12208a4..3e9bfa5 100644 --- a/Processor.java +++ b/Processor.java @@ -9,28 +9,21 @@ public Processor() { } // 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, Double x, Double y) { + public static String changeMode(String mode, int num) { String result = ""; if(mode.matches("binary")){ - Integer binaryX = (Integer) x; - Integer BinaryY = (Integer) y; - result += operations.toBinary(x); - result += operations.toBinary(y); + Integer binaryNum = (Integer) num; + result += operations.toBinary(num); } else if(mode.matches("octal")){ - Integer octalX = (Integer) x; - Integer octalY = (Integer) y; - result += operations.toOctal(x,y); - result += operations.toOctal(y); + Integer octalNum = (Integer) num; + result += operations.toOctal(num); } else if(mode.matches("decimal")){ - Integer decimalX = (Integer) x; - Integer decimalY = (Integer) y; - result += operations.toDecimal(x); - result += operations.toDecimal(y); + Integer decimalNum = (Integer) num; + // result += operations.toDecimal(Num); + // result += operations.toDecimal(y); } else if (mode.matches("hexadecimal")){ - Integer hexadecimalX = (Integer) x; - Integer hexadecimalY = (Integer) y; - result += operations.toHexadecimal(x,y); - result += operations.toHexadecimal(y); + Integer hexadecimalNum = (Integer) num; + result += operations.toOctal(num); } return result; } @@ -52,58 +45,49 @@ public static String arithmetic(String operator, Double x, Double y) { case "+": result+= operations.add(x,y); break; - case "-": result+= operations.subtract(x,y); + case "-": result+= operations.sub(x,y); break; - case "/": result+= operations.divide(x,y); + case "/": result+= operations.div(x,y); break; - case "%": result+= operations.remainder(x,y); + case "*": result+= operations.multiply(x,y); break; - case "*": result+= operations.mulit(x,y); - break; - - case "^": result+= operations.exponentiation(x,y); + case "^": result+= operations.exponentiationMethod(x,y); break; - case "inverse": result+= operations.inverse(x); + case "inverse": result+= operations.inverseMethod(x); break; - case "square": result+= operations.square(x); + case "square": result+= operations.squareMethod(x); break; - case "invert": result+= operations.invert(x,y); + case "invert": result+= operations.invertMethod(x); break; - case "factorial": result+= operations.factorial(x); - break; - - case "switchSign": result+= operations.switchSign(x); + case "!": result+= operations.factorialMethod(x); break; - case "sqrt": result+= operations.sqrt(x); + case "sqrt": result+= operations.squareRootMethod(x); break; - case "sine": result+= operations.sine(x,y); - break; - - case "asin": result+= operations.asin(x,y); + case "sin": result+= operations.sineMathMethod(x); break; - case "cos": result+= operations.cos(x,y); + case "asin": result+= operations.invSineMathMethod(x); break; - case "acos": result+= operations.acos(x,y); + case "cos": result+= operations.cosMathMethod(x); break; - case "tan": result+= operations.tan(x,y); + case "acos": result+= operations.invCosMathMethod(x); break; - case "atan": result+= operations.atan(x,y); + case "tan": result+= operations.tanMathMethod(x); break; - case "quit": result = "bye!"; + case "atan": result+= operations.invTanMathMethod(x); break; } return result; diff --git a/ProcessorTest.java b/ProcessorTest.java index a8233b0..a4969be 100644 --- a/ProcessorTest.java +++ b/ProcessorTest.java @@ -48,9 +48,9 @@ public static void testArithmetic() { assertEquals("+", 2.0, 3.0); } - @Test - public static void testDisplay() { - processor.arithmetic(""); - assertEquals("+", 2.0, 3.0); - } + // @Test + // public static void testDisplay() { + // processor.arithmetic(""); + // assertEquals("+", 2.0, 3.0); + // } }