From 8c528a21b489b6bb5b6bdd9284968abad32b1fe9 Mon Sep 17 00:00:00 2001 From: Hoshiko Oki Date: Wed, 11 Mar 2015 00:52:23 -0400 Subject: [PATCH 1/8] add test code Hopefully it will work --- src/nyc/c4q/ac21/romancalc/RomanNumerals.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/nyc/c4q/ac21/romancalc/RomanNumerals.java b/src/nyc/c4q/ac21/romancalc/RomanNumerals.java index 12c285e..217f946 100644 --- a/src/nyc/c4q/ac21/romancalc/RomanNumerals.java +++ b/src/nyc/c4q/ac21/romancalc/RomanNumerals.java @@ -36,5 +36,28 @@ public static int parse(String number) { public static void main(String[] argv) { // TODO: Group 3: Write this function! // It should test that format() and parse() work correctly. + System.out.print(format(18)); /* it should return XVIII*/ + System.out.print(format(29)); /* it should return XXIX*/ + System.out.print(format(34)); /* it should return XXXIV*/ + System.out.print(format(46)); /* it should return XLVI*/ + System.out.print(format(89)); /* it should return LXXXiX*/ + System.out.print(format(846)); /* it should return DCCCXLVI*/ + System.out.print(format(1999)); /* it should return MCMXCIX*/ + System.out.print(format(2420)); /* it should return MMCDXX*/ + System.out.print(format(3454)); /* it should return MMMCDLIV*/ + System.out.print(format(3999)); /* it should return MMMCMXCIX*/ + + + + System.out.print(parse("XVIII")); /* it should return 18*/ + System.out.print(parse("XXIX")); /* it should return 29*/ + System.out.print(parse("XXXIV")); /* it should return 34*/ + System.out.print(parse("XLVI")); /* it should return 46*/ + System.out.print(parse("LXXXiX")); /* it should return 89*/ + System.out.print(parse("DCCCXLVI")); /* it should return 846*/ + System.out.print(parse("MCMXCIX")); /* it should return 1999*/ + System.out.print(parse("MMCDXX")); /* it should return 2420*/ + System.out.print(parse("MMMCDLIV")); /* it should return 3454*/ + System.out.print(parse("MMMCMXCIX")); /* it should return 3999*/ } } From d7fe4cab6363b1f47d280a0500d84642ba2cf8bc Mon Sep 17 00:00:00 2001 From: John Gomez Date: Wed, 11 Mar 2015 15:08:26 -0400 Subject: [PATCH 2/8] added temp test class. --- .DS_Store | Bin 0 -> 6148 bytes Roman-calculator.iml | 11 +++++ src/nyc/c4q/ac21/romancalc/Calculator.java | 13 ++++++ src/nyc/c4q/ac21/romancalc/RomanNumerals.java | 42 ++++++++++++++++++ src/nyc/c4q/ac21/romancalc/test.java | 36 +++++++++++++++ 5 files changed, 102 insertions(+) create mode 100644 .DS_Store create mode 100644 Roman-calculator.iml create mode 100644 src/nyc/c4q/ac21/romancalc/test.java diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..24f515169a5fe1e3350634950b1d384141f7a373 GIT binary patch literal 6148 zcmeHKOG*Pl5Pf9=M%-lK!euXA3Ep6gGeSUJBo{E5Y=qGPHTyoG2kh;p#>aa!N^OLxFw$B5gS|aF5|72al(w2aVtC_pe8odTj~{F z>3L?A2l|_C9a;41jPSzPx)=4{i(=Qs8E^)i0cXG&_=N$U*(#G`L)XrLGvEyD7|{J8 zuqhT9qhZ=QsL~UFsMqKuwDr=gIU!?_F&ffC5lbans^lkzSUSf;jw>=oLraI`$A{#Z zzaB4=W@r77!XbsBYiGb2=rS;}?o`kJioeX@Bfp#CJ!ilf*fR!XTrSHQ7nRS}AIsCT vHn82Zsc2kOgF?G=31CO}k<;Wfe^8roMaF2TRdhV16a6BP2yx{M`~d@>@P$JM literal 0 HcmV?d00001 diff --git a/Roman-calculator.iml b/Roman-calculator.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/Roman-calculator.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/nyc/c4q/ac21/romancalc/Calculator.java b/src/nyc/c4q/ac21/romancalc/Calculator.java index 5cf65ac..70c9bf3 100644 --- a/src/nyc/c4q/ac21/romancalc/Calculator.java +++ b/src/nyc/c4q/ac21/romancalc/Calculator.java @@ -16,6 +16,7 @@ public class Calculator { * less than 1 or larger than 3999, prints a message indicating this * instead. * + * * @param leftNumber * The left operand, in Roman numerals. * @param operation @@ -31,6 +32,18 @@ public class Calculator { */ public static void calculate(String leftNumber, String operation, String rightNumber) { // TODO: Group 3: Write this function! + Scanner userInput = new Scanner(System.in); + + + leftNumber = leftNumber; + System.out.println("Welcome to the Roman calculator. Type in a roman numeral"); + userInput.nextInt(); + System.out.println("Please type an operator."); + userInput.nextInt(); + System.out.println("Please type a second roman numeral."); + + + } /** diff --git a/src/nyc/c4q/ac21/romancalc/RomanNumerals.java b/src/nyc/c4q/ac21/romancalc/RomanNumerals.java index 12c285e..6b4391b 100644 --- a/src/nyc/c4q/ac21/romancalc/RomanNumerals.java +++ b/src/nyc/c4q/ac21/romancalc/RomanNumerals.java @@ -1,8 +1,11 @@ package nyc.c4q.ac21.romancalc; +import java.util.Scanner; + /** * Code to convert to and from Roman numerals. */ + public class RomanNumerals { /** * Formats a number in Roman numerals. @@ -36,5 +39,44 @@ public static int parse(String number) { public static void main(String[] argv) { // TODO: Group 3: Write this function! // It should test that format() and parse() work correctly. + + Scanner userInput = new Scanner(System.in); + + + + + + System.out.println("Welcome to the Roman calculator. Type in a roman numeral"); + int leftNumber = userInput.nextInt(); + + System.out.println("Please type a second roman numeral."); + int rightNumber = userInput.nextInt(); + + System.out.println("Please type an operator."); + userInput.nextFloat(); + + String operation = userInput.next(); + if (operation.equals("+")) { + System.out.println(leftNumber+rightNumber); + } else if (operation.equals("-")) { + System.out.println(leftNumber-rightNumber); + } else if (operation.equals("/")) { + System.out.println(leftNumber/rightNumber); + } else if (operation.equals("*")) { + System.out.println(leftNumber*rightNumber); + } else if (operation.equals("%")) { + System.out.println(leftNumber%rightNumber); + } else { + System.out.println("Invalid operation."); + } + + + + + + + + + } } diff --git a/src/nyc/c4q/ac21/romancalc/test.java b/src/nyc/c4q/ac21/romancalc/test.java new file mode 100644 index 0000000..85037cc --- /dev/null +++ b/src/nyc/c4q/ac21/romancalc/test.java @@ -0,0 +1,36 @@ +package nyc.c4q.ac21.romancalc; + +import java.util.Scanner; + +/** + * Created by c4q-john on 3/10/15. + */ +public class test { + public static void main(String[] args) { + + Scanner userInput = new Scanner(System.in); + + System.out.println("Welcome to the Roman calculator. Type in a roman numeral"); + int leftNumber = userInput.nextInt(); + + System.out.println("Please type a second roman numeral."); + int rightNumber = userInput.nextInt(); + + System.out.println("Please type an operator."); + + String operation = userInput.next(); + if (operation.equals("+")) { + System.out.println(leftNumber + rightNumber); + } else if (operation.equals("-")) { + System.out.println(leftNumber - rightNumber); + } else if (operation.equals("/")) { + System.out.println(leftNumber / rightNumber); + } else if (operation.equals("*")) { + System.out.println(leftNumber * rightNumber); + } else if (operation.equals("%")) { + System.out.println(leftNumber % rightNumber); + } else { + System.out.println("Invalid operation."); + } + } +} From 05e77a2fd5cb72c0beb97f68ba546924ce7e8fe7 Mon Sep 17 00:00:00 2001 From: John Gomez Date: Wed, 11 Mar 2015 22:24:49 -0400 Subject: [PATCH 3/8] added calculator test --- src/nyc/c4q/ac21/romancalc/Calculator.java | 86 +++++++++++++++++-- src/nyc/c4q/ac21/romancalc/RomanNumerals.java | 46 +++------- src/nyc/c4q/ac21/romancalc/test.java | 36 -------- 3 files changed, 89 insertions(+), 79 deletions(-) delete mode 100644 src/nyc/c4q/ac21/romancalc/test.java diff --git a/src/nyc/c4q/ac21/romancalc/Calculator.java b/src/nyc/c4q/ac21/romancalc/Calculator.java index 70c9bf3..d36d948 100644 --- a/src/nyc/c4q/ac21/romancalc/Calculator.java +++ b/src/nyc/c4q/ac21/romancalc/Calculator.java @@ -1,5 +1,7 @@ package nyc.c4q.ac21.romancalc; +import com.sun.org.apache.xpath.internal.operations.Bool; + import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; @@ -7,6 +9,30 @@ import java.util.Scanner; public class Calculator { + + public static boolean validate(String num){ + + for(int index=0;index < num.length(); index=index+1){ + + String currentChar=String.valueOf(num.charAt(index)); + + System.out.println(currentChar); + + if ( currentChar.equals("M") + || currentChar.equals("D") + || currentChar.equals("C") + || currentChar.equals("L") + || currentChar.equals("X") + || currentChar.equals("V") + || currentChar.equals("I") ){ + } else { + return false; + } + } + + return true; + } + /** * Performs calculations on Roman numerals and prints the result. * @@ -32,17 +58,44 @@ public class Calculator { */ public static void calculate(String leftNumber, String operation, String rightNumber) { // TODO: Group 3: Write this function! - Scanner userInput = new Scanner(System.in); - leftNumber = leftNumber; - System.out.println("Welcome to the Roman calculator. Type in a roman numeral"); - userInput.nextInt(); - System.out.println("Please type an operator."); - userInput.nextInt(); - System.out.println("Please type a second roman numeral."); + int left = RomanNumerals.parse(leftNumber); + int right = RomanNumerals.parse(rightNumber); + int result = 0; + + if (left == -1 || right == -1) { + System.out.println("Not a roman numeral! "); + } + + if (operation.equals("+")) { + result = left+right; + } else if (operation.equals("-")) { + result = left - right; + } else if (operation.equals("/")) { + result = left/right; + } else if (operation.equals("*")) { + result = left*right; + } else if (operation.equals("%")) { + result = left % right; + } else { + System.out.println("Invalid operation."); + } + + + if (result < 1) { + System.out.println("Result is less than 1! It doesn't exist!"); + } + + if (result > 3999) { + System.out.println("Result is higher than 3999, Romans couldn't count that high!"); + } + + String output = RomanNumerals.format(result); + + System.out.println(output); } @@ -70,6 +123,7 @@ public static int parseDecimalNumber(String number) { * You do not need to understand how this function works. */ public static void main(String[] args) throws IOException { + final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); // Loop forever. while (true) { @@ -95,10 +149,26 @@ public static void main(String[] args) throws IOException { continue; } + + /*(! validate(leftNumber)){ + System.err.println("syntax error - LeftNumber not a roman numeral"); + System.out.println(); + continue; + // } + if(! validate(rightNumber)){ + System.err.println("syntax error - rightNumber not a roman numeral"); + System.out.println(); + continue; + } + System.out.println("Let's do cool stuff!");*/ + // Perform the calculation and show the result. calculate(leftNumber, operation, rightNumber); System.out.println(); - } + + + } + } } diff --git a/src/nyc/c4q/ac21/romancalc/RomanNumerals.java b/src/nyc/c4q/ac21/romancalc/RomanNumerals.java index 3262e2c..9be222a 100644 --- a/src/nyc/c4q/ac21/romancalc/RomanNumerals.java +++ b/src/nyc/c4q/ac21/romancalc/RomanNumerals.java @@ -16,6 +16,7 @@ public class RomanNumerals { */ static String format(int value) { // TODO: Group 1: Write this function! + return "???"; } @@ -39,47 +40,20 @@ public static int parse(String number) { public static void main(String[] argv) { // TODO: Group 3: Write this function! // It should test that format() and parse() work correctly. -<<<<<<< HEAD - - Scanner userInput = new Scanner(System.in); - - + String answer = format(234); - System.out.println("Welcome to the Roman calculator. Type in a roman numeral"); - int leftNumber = userInput.nextInt(); - - System.out.println("Please type a second roman numeral."); - int rightNumber = userInput.nextInt(); - - System.out.println("Please type an operator."); - userInput.nextFloat(); - - String operation = userInput.next(); - if (operation.equals("+")) { - System.out.println(leftNumber+rightNumber); - } else if (operation.equals("-")) { - System.out.println(leftNumber-rightNumber); - } else if (operation.equals("/")) { - System.out.println(leftNumber/rightNumber); - } else if (operation.equals("*")) { - System.out.println(leftNumber*rightNumber); - } else if (operation.equals("%")) { - System.out.println(leftNumber%rightNumber); - } else { - System.out.println("Invalid operation."); + //test + if ( ! format(5).equals("V") ) { + System.out.println("Error"); } + if ( parse("V") == 5) + System.out.println("'V' Success!"); - - - - - - -======= + //format: input int output string System.out.print(format(18)); /* it should return XVIII*/ System.out.print(format(29)); /* it should return XXIX*/ System.out.print(format(34)); /* it should return XXXIV*/ @@ -93,6 +67,8 @@ public static void main(String[] argv) { + + //parse: input string output int System.out.print(parse("XVIII")); /* it should return 18*/ System.out.print(parse("XXIX")); /* it should return 29*/ System.out.print(parse("XXXIV")); /* it should return 34*/ @@ -103,6 +79,6 @@ public static void main(String[] argv) { System.out.print(parse("MMCDXX")); /* it should return 2420*/ System.out.print(parse("MMMCDLIV")); /* it should return 3454*/ System.out.print(parse("MMMCMXCIX")); /* it should return 3999*/ ->>>>>>> 868ddef1ea1616a139091f630166965f2d074112 + } } diff --git a/src/nyc/c4q/ac21/romancalc/test.java b/src/nyc/c4q/ac21/romancalc/test.java deleted file mode 100644 index 85037cc..0000000 --- a/src/nyc/c4q/ac21/romancalc/test.java +++ /dev/null @@ -1,36 +0,0 @@ -package nyc.c4q.ac21.romancalc; - -import java.util.Scanner; - -/** - * Created by c4q-john on 3/10/15. - */ -public class test { - public static void main(String[] args) { - - Scanner userInput = new Scanner(System.in); - - System.out.println("Welcome to the Roman calculator. Type in a roman numeral"); - int leftNumber = userInput.nextInt(); - - System.out.println("Please type a second roman numeral."); - int rightNumber = userInput.nextInt(); - - System.out.println("Please type an operator."); - - String operation = userInput.next(); - if (operation.equals("+")) { - System.out.println(leftNumber + rightNumber); - } else if (operation.equals("-")) { - System.out.println(leftNumber - rightNumber); - } else if (operation.equals("/")) { - System.out.println(leftNumber / rightNumber); - } else if (operation.equals("*")) { - System.out.println(leftNumber * rightNumber); - } else if (operation.equals("%")) { - System.out.println(leftNumber % rightNumber); - } else { - System.out.println("Invalid operation."); - } - } -} From 87df96bb8c5342ff5faeb0bc0597262c92f03b68 Mon Sep 17 00:00:00 2001 From: charlyn23 Date: Thu, 12 Mar 2015 22:22:23 -0400 Subject: [PATCH 4/8] Parse update --- src/nyc/c4q/ac21/romancalc/RomanNumerals.java | 43 ++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/src/nyc/c4q/ac21/romancalc/RomanNumerals.java b/src/nyc/c4q/ac21/romancalc/RomanNumerals.java index 9be222a..7be203d 100644 --- a/src/nyc/c4q/ac21/romancalc/RomanNumerals.java +++ b/src/nyc/c4q/ac21/romancalc/RomanNumerals.java @@ -33,7 +33,48 @@ public static int parse(String number) { // You will need: // `number.length()` gives the length (number of characters) of the number // `number.charAt(i)` gives the i'th character - + Scanner input = new Scanner(System.in); + + int total = 0; + String romanNum = ""; + + + romanNum.trim(); + + for (int i = 0; i < romanNum.length(); i++) { + if (romanNum.charAt(i) == 'm') + total = total + 1000; + else if (romanNum.charAt(i) == 'd') + total = total + 500; + else if (romanNum.charAt(i) == 'c') + total = total + 100; + else if (romanNum.charAt(i) == 'l') + total = total + 50; + else if (romanNum.charAt(i) == 'x') + total = total + 10; + else if (romanNum.charAt(i) == 'v') + total = total + 5; + else if (romanNum.charAt(i) == 'i') + total = total + 1; + else + break; + } + + if (romanNum.indexOf("cm") > -1) + total = total - 200; + if (romanNum.indexOf("cd") > -1) + total = total - 200; + if (romanNum.indexOf("xc") > -1) + total = total - 20; + if (romanNum.indexOf("xl") > -1) + total = total - 20; + if (romanNum.indexOf("ix") > -1) + total = total - 2; + if (romanNum.indexOf("iv") > -1) + total = total - 2; + + Retun total; + } return -1; } From c7866cc50d3776af8ed06d1030d0fc3e4d6f3bc1 Mon Sep 17 00:00:00 2001 From: tashsmit Date: Fri, 13 Mar 2015 13:56:01 -0400 Subject: [PATCH 5/8] Update Calculator.java --- src/nyc/c4q/ac21/romancalc/Calculator.java | 45 ++++++++++------------ 1 file changed, 21 insertions(+), 24 deletions(-) diff --git a/src/nyc/c4q/ac21/romancalc/Calculator.java b/src/nyc/c4q/ac21/romancalc/Calculator.java index d36d948..e3a703e 100644 --- a/src/nyc/c4q/ac21/romancalc/Calculator.java +++ b/src/nyc/c4q/ac21/romancalc/Calculator.java @@ -10,7 +10,7 @@ public class Calculator { - public static boolean validate(String num){ + /*public static boolean validate(String num){ for(int index=0;index < num.length(); index=index+1){ @@ -66,37 +66,36 @@ public static void calculate(String leftNumber, String operation, String rightNu if (left == -1 || right == -1) { System.out.println("Not a roman numeral! "); - } - if (operation.equals("+")) { + } else if (operation.equals("+")) { - result = left+right; + result = left + right; } else if (operation.equals("-")) { result = left - right; } else if (operation.equals("/")) { - result = left/right; + result = left / right; } else if (operation.equals("*")) { - result = left*right; + result = left * right; } else if (operation.equals("%")) { result = left % right; } else { System.out.println("Invalid operation."); - } - if (result < 1) { - System.out.println("Result is less than 1! It doesn't exist!"); - } + if (result < 0) { + System.out.println("Result is less than 1! It doesn't exist!"); + } - if (result > 3999) { - System.out.println("Result is higher than 3999, Romans couldn't count that high!"); - } + if (result > 3999) { + System.out.println("Result is higher than 3999, Romans couldn't count that high!"); + } - String output = RomanNumerals.format(result); + String output = RomanNumerals.format(result); - System.out.println(output); + System.out.println(output); + } } /** @@ -150,25 +149,23 @@ public static void main(String[] args) throws IOException { } - /*(! validate(leftNumber)){ + /* if (! validate(leftNumber) || (! validate(rightNumber))) System.err.println("syntax error - LeftNumber not a roman numeral"); System.out.println(); continue; - // } - if(! validate(rightNumber)){ + + if (! validate(rightNumber)) System.err.println("syntax error - rightNumber not a roman numeral"); System.out.println(); continue; - } - System.out.println("Let's do cool stuff!");*/ - // Perform the calculation and show the result. - calculate(leftNumber, operation, rightNumber); + // Perform the calculation and show the result.*/ + calculate(leftNumber, operation, rightNumber); - System.out.println(); - } + } } } + From 5eb640e3109d1a62c670c373e7ee0502ef273f89 Mon Sep 17 00:00:00 2001 From: tashsmit Date: Fri, 13 Mar 2015 13:56:29 -0400 Subject: [PATCH 6/8] Update RomanNumerals.java --- src/nyc/c4q/ac21/romancalc/RomanNumerals.java | 150 ++++++++++-------- 1 file changed, 87 insertions(+), 63 deletions(-) diff --git a/src/nyc/c4q/ac21/romancalc/RomanNumerals.java b/src/nyc/c4q/ac21/romancalc/RomanNumerals.java index 7be203d..c612e53 100644 --- a/src/nyc/c4q/ac21/romancalc/RomanNumerals.java +++ b/src/nyc/c4q/ac21/romancalc/RomanNumerals.java @@ -14,10 +14,70 @@ public class RomanNumerals { * @return * The value in Roman numerals. */ - static String format(int value) { - // TODO: Group 1: Write this function! - return "???"; + public static String format(int value) { + if (value <= 0 || value > 3999) + return "Error: not a valid Roman Numeral"; + String num$ = ""; //represents integer # inputted. + + while (value >= 1000) { //Start with largest place. 1000's place. + num$ += "M"; //Print M for each multiple of 1000; + value = value - 1000; //Remove 1000 from number. ex: 1984 = 1000 + 900 + 80 + 4 + } //MCMLXXIV + if (value >= 900) { + num$ += "CM"; // //100 less than a 1000. ex: 984 = 900 (CM) + 80 + 4 + value = value - 900; + } + if (value >= 500) { + num$ += "D"; + value = value - 500; + } + if (value >= 400) { + num$ += "CD"; + value = value - 400; + } + while (value >= 100) { + num$ += "C"; + value -= 100; + } + if (value >= 90) { + num$ += "XC"; + value -= 90; + } + if (value >= 60) { + num$ += "LX"; + value -= 60; + } + if (value >= 50) { + num$ += "L"; + value -= 50; + } + if (value >= 40) { + num$ += "XL"; + value -= 40; + } + while (value >= 10) { + num$ += "X"; + value -= 10; + } + if (value == 9) { + num$ += "IX"; + value -= 9; + } + if (value >= 5) { + num$ += "V"; + value -= 5; + } + if (value == 4) { + num$ += "IV"; + value -= 4; + } + while (value >= 1) { + num$ += "I"; + value -= 1; + } + System.out.println(num$); + return num$; } /** @@ -27,99 +87,63 @@ static String format(int value) { * @return * The value, or -1 if the number isn't in Roman numerals. */ - public static int parse(String number) { + public static int parse(String romanNum) { // TODO: Group 2: Write this function! - + // You will need: // `number.length()` gives the length (number of characters) of the number // `number.charAt(i)` gives the i'th character - Scanner input = new Scanner(System.in); int total = 0; - String romanNum = ""; - romanNum.trim(); + //romanNum.trim(); for (int i = 0; i < romanNum.length(); i++) { - if (romanNum.charAt(i) == 'm') + if (romanNum.charAt(i) == 'M' || romanNum.charAt(i) == 'm') total = total + 1000; - else if (romanNum.charAt(i) == 'd') + else if (romanNum.charAt(i) == 'D' || romanNum.charAt(i) == 'd') total = total + 500; - else if (romanNum.charAt(i) == 'c') + else if (romanNum.charAt(i) == 'C' || romanNum.charAt(i) == 'c') total = total + 100; - else if (romanNum.charAt(i) == 'l') + else if (romanNum.charAt(i) == 'L' || romanNum.charAt(i) == 'l') total = total + 50; - else if (romanNum.charAt(i) == 'x') + else if (romanNum.charAt(i) == 'X' || romanNum.charAt(i) == 'x') total = total + 10; - else if (romanNum.charAt(i) == 'v') + else if (romanNum.charAt(i) == 'V' || romanNum.charAt(i) == 'v') total = total + 5; - else if (romanNum.charAt(i) == 'i') + else if (romanNum.charAt(i) == 'I' || romanNum.charAt(i) == 'i') total = total + 1; - else - break; + } - if (romanNum.indexOf("cm") > -1) + if (romanNum.indexOf("CM") > -1 ||romanNum.indexOf("cm") > -1 ) total = total - 200; - if (romanNum.indexOf("cd") > -1) + if (romanNum.indexOf("CD") > -1 || romanNum.indexOf("cd") > -1) total = total - 200; - if (romanNum.indexOf("xc") > -1) + if (romanNum.indexOf("XC") > -1 || romanNum.indexOf("xc") > -1) total = total - 20; - if (romanNum.indexOf("xl") > -1) + if (romanNum.indexOf("XL") > -1 || romanNum.indexOf("xl") > -1) total = total - 20; - if (romanNum.indexOf("ix") > -1) + if (romanNum.indexOf("IX") > -1 || romanNum.indexOf("ix") > -1) total = total - 2; - if (romanNum.indexOf("iv") > -1) + if (romanNum.indexOf("IV") > -1 || romanNum.indexOf("iv") > -1) total = total - 2; - Retun total; - } - return -1; + System.out.println(total); + return total; + + } + //return -1; + public static void main(String[] argv) { // TODO: Group 3: Write this function! // It should test that format() and parse() work correctly. - String answer = format(234); - - //test - if ( ! format(5).equals("V") ) { - System.out.println("Error"); - } - if ( parse("V") == 5) - System.out.println("'V' Success!"); - - - - //format: input int output string - System.out.print(format(18)); /* it should return XVIII*/ - System.out.print(format(29)); /* it should return XXIX*/ - System.out.print(format(34)); /* it should return XXXIV*/ - System.out.print(format(46)); /* it should return XLVI*/ - System.out.print(format(89)); /* it should return LXXXiX*/ - System.out.print(format(846)); /* it should return DCCCXLVI*/ - System.out.print(format(1999)); /* it should return MCMXCIX*/ - System.out.print(format(2420)); /* it should return MMCDXX*/ - System.out.print(format(3454)); /* it should return MMMCDLIV*/ - System.out.print(format(3999)); /* it should return MMMCMXCIX*/ - - - - - //parse: input string output int - System.out.print(parse("XVIII")); /* it should return 18*/ - System.out.print(parse("XXIX")); /* it should return 29*/ - System.out.print(parse("XXXIV")); /* it should return 34*/ - System.out.print(parse("XLVI")); /* it should return 46*/ - System.out.print(parse("LXXXiX")); /* it should return 89*/ - System.out.print(parse("DCCCXLVI")); /* it should return 846*/ - System.out.print(parse("MCMXCIX")); /* it should return 1999*/ - System.out.print(parse("MMCDXX")); /* it should return 2420*/ - System.out.print(parse("MMMCDLIV")); /* it should return 3454*/ - System.out.print(parse("MMMCMXCIX")); /* it should return 3999*/ } } + From 06443e2d67387d106ac4626ce69d0a17d9190943 Mon Sep 17 00:00:00 2001 From: tashsmit Date: Fri, 13 Mar 2015 15:39:38 -0400 Subject: [PATCH 7/8] Update RomanNumerals.java --- src/nyc/c4q/ac21/romancalc/RomanNumerals.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/nyc/c4q/ac21/romancalc/RomanNumerals.java b/src/nyc/c4q/ac21/romancalc/RomanNumerals.java index c612e53..5b775a6 100644 --- a/src/nyc/c4q/ac21/romancalc/RomanNumerals.java +++ b/src/nyc/c4q/ac21/romancalc/RomanNumerals.java @@ -76,7 +76,7 @@ public static String format(int value) { num$ += "I"; value -= 1; } - System.out.println(num$); + //System.out.println(num$); return num$; } @@ -130,7 +130,7 @@ else if (romanNum.charAt(i) == 'I' || romanNum.charAt(i) == 'i') if (romanNum.indexOf("IV") > -1 || romanNum.indexOf("iv") > -1) total = total - 2; - System.out.println(total); + //System.out.println(total); return total; From db9b3a78f6ccf71106750f4ec550a8bdbf3fc69e Mon Sep 17 00:00:00 2001 From: tashsmit Date: Fri, 13 Mar 2015 15:40:15 -0400 Subject: [PATCH 8/8] Update Calculator.java --- src/nyc/c4q/ac21/romancalc/Calculator.java | 76 +++++++--------------- 1 file changed, 24 insertions(+), 52 deletions(-) diff --git a/src/nyc/c4q/ac21/romancalc/Calculator.java b/src/nyc/c4q/ac21/romancalc/Calculator.java index e3a703e..9ebbc68 100644 --- a/src/nyc/c4q/ac21/romancalc/Calculator.java +++ b/src/nyc/c4q/ac21/romancalc/Calculator.java @@ -59,45 +59,31 @@ public class Calculator { public static void calculate(String leftNumber, String operation, String rightNumber) { // TODO: Group 3: Write this function! - - int left = RomanNumerals.parse(leftNumber); - int right = RomanNumerals.parse(rightNumber); + int leftInt = RomanNumerals.parse(leftNumber); + int rightInt = RomanNumerals.parse(rightNumber); int result = 0; - if (left == -1 || right == -1) { - System.out.println("Not a roman numeral! "); - - } else if (operation.equals("+")) { - - result = left + right; - - } else if (operation.equals("-")) { - result = left - right; - } else if (operation.equals("/")) { - result = left / right; - } else if (operation.equals("*")) { - result = left * right; - } else if (operation.equals("%")) { - result = left % right; - } else { - System.out.println("Invalid operation."); - - - if (result < 0) { - System.out.println("Result is less than 1! It doesn't exist!"); - } - - if (result > 3999) { - System.out.println("Result is higher than 3999, Romans couldn't count that high!"); - } - - String output = RomanNumerals.format(result); + if (operation.equals("+")){ + result = leftInt + rightInt; + } + else if (operation.equals("-")){ + result = leftInt - rightInt; + } + else if (operation.equals("*")){ + result = leftInt * rightInt; + } + else if (operation.equals("/")){ + result = leftInt / rightInt; + } + else if (operation.equals("#")) { + result = (leftInt + rightInt) / 2; + } - System.out.println(output); + System.out.println(RomanNumerals.format(result)); - } } + /** * Parses a decimal number. * @param number @@ -123,9 +109,10 @@ public static int parseDecimalNumber(String number) { */ public static void main(String[] args) throws IOException { + final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); // Loop forever. - while (true) { + while (true) { // Show the prompt. System.out.print("> "); // Read a line of input. @@ -147,25 +134,10 @@ public static void main(String[] args) throws IOException { System.out.println(); continue; } + calculate(leftNumber, operation, rightNumber); - - /* if (! validate(leftNumber) || (! validate(rightNumber))) - System.err.println("syntax error - LeftNumber not a roman numeral"); - System.out.println(); - continue; - - if (! validate(rightNumber)) - System.err.println("syntax error - rightNumber not a roman numeral"); - System.out.println(); - continue; - - // Perform the calculation and show the result.*/ - calculate(leftNumber, operation, rightNumber); - - - - } + System.out.println(); } -} +}}