From 2cbf5697feef3b0fd8f8d4d4af8a83d219848758 Mon Sep 17 00:00:00 2001 From: juhlgren Date: Thu, 11 Oct 2018 15:49:43 +0300 Subject: [PATCH 1/2] jooh --- src/RomanNumerals.java | 75 ++++++++++++++++++++++++++++++++++-- tests/TestRomanNumerals.java | 44 +++++++++++++++++++-- 2 files changed, 112 insertions(+), 7 deletions(-) diff --git a/src/RomanNumerals.java b/src/RomanNumerals.java index 20904f0..1c097e6 100644 --- a/src/RomanNumerals.java +++ b/src/RomanNumerals.java @@ -1,8 +1,77 @@ -public class RomanNumerals { - public int convertToInteger(String romanNum) { +/* +The symbols 'I', 'X', 'C', and 'M' can be repeated at most 3 times in a row. +The symbols 'V', 'L', and 'D' can never be repeated. +The '1' symbols ('I', 'X', and 'C') can only be subtracted from the 2 next highest values ('IV' and +'IX', 'XL' and 'XC', 'CD' and 'CM'). +Only one subtraction can be made per numeral ('XC' is allowed, 'XXC' is not). +The '5' symbols ('V', 'L', and 'D') can never be subtracted. + */ + +public class RomanNumerals +{ + + public int convertToInteger(String romanNum) + { + romanNum = romanNum.toUpperCase(); // in case someone sends small letters + // To be Implemented return 0; - } + } + + public int convertOne(char x) + { + switch (x) + { + case 'I' : return 1; + case 'V' : return 5; + case 'X' : return 10; + case 'L' : return 50; + case 'C' : return 100; + case 'D' : return 500; + case 'M' : return 1000; + } + return 0; + } + + public int sumUp(String rNum) + { + int sum=0; + for (int i=0; i 2) return 2; + } + + } + + return 0; + } + + } diff --git a/tests/TestRomanNumerals.java b/tests/TestRomanNumerals.java index 5d1de75..18459d4 100644 --- a/tests/TestRomanNumerals.java +++ b/tests/TestRomanNumerals.java @@ -2,11 +2,47 @@ import org.junit.Test; -public class TestRomanNumerals { +public class TestRomanNumerals +{ @Test - public void test() { - fail("Not yet implemented"); - } + public void test() + { + String year = "MCMLXXXIV"; + RomanNumerals x = new RomanNumerals(); + if (x.convertToInteger(year) != 1984) fail("Not yet implemented"); + } + @Test + public void testIndividualLetters() + { + String letters="IVXCLM"; + RomanNumerals x = new RomanNumerals(); + for (int i=0; i<6; i++) + { + // char letter = letters.charAt(i); + + if (x.convertOne(letters.charAt(i)) == 0) fail("Wrong result for individual letter convert test"); + } + } + + @Test + public void testSum_without_considering_the_places_in_string() + { + String year = "MCMLXXXIV"; + RomanNumerals x = new RomanNumerals(); + if (x.sumUp(year) != 2186) fail("Did not compute correctly"); + } + + @Test + public void test_are_there_two_VLD_or_more_than_three_IXCM_in_row() + { + String year = "MCMLXXXIV"; + RomanNumerals x = new RomanNumerals(); + if (x.threeRow(year) != 0) fail("Something went awry"); + } + + + + } From c3575321fa6aebcfb89eb5e87d67a9c017ef572e Mon Sep 17 00:00:00 2001 From: Sonofhades Date: Mon, 15 Oct 2018 14:21:26 +0300 Subject: [PATCH 2/2] Assignment Done --- src/RomanNumerals.java | 102 +++++++++++++++++++++++++---------- tests/TestRomanNumerals.java | 44 ++++++++++++--- 2 files changed, 110 insertions(+), 36 deletions(-) diff --git a/src/RomanNumerals.java b/src/RomanNumerals.java index 1c097e6..37200f0 100644 --- a/src/RomanNumerals.java +++ b/src/RomanNumerals.java @@ -13,11 +13,17 @@ public class RomanNumerals public int convertToInteger(String romanNum) { - romanNum = romanNum.toUpperCase(); // in case someone sends small letters - - // To be Implemented - return 0; - + // in case someone sends small letters that we use for + // other purposes in the program + romanNum = romanNum.toUpperCase(); + // checks that the 2 / 4 same letter combo is not present + if(!checkValidity(romanNum)) return -1; + // checks that IXC can be deducted only from two next letters + if(!illegalCombos(romanNum)) return -1; + // changes substracted IXC values of VXLDM to vxlcdm whcih stand for 4, 9, 40, 90, 400 and 900 + romanNum=cheatCalculation(romanNum); + // counts the total + return sumUp(romanNum); } public int convertOne(char x) @@ -25,11 +31,17 @@ public int convertOne(char x) switch (x) { case 'I' : return 1; + case 'v' : return 4; case 'V' : return 5; + case 'x' : return 9; case 'X' : return 10; - case 'L' : return 50; - case 'C' : return 100; + case 'l' : return 40; + case 'L' : return 50; + case 'c' : return 90; + case 'C' : return 100; + case 'd' : return 400; case 'D' : return 500; + case 'm' : return 900; case 'M' : return 1000; } return 0; @@ -45,33 +57,67 @@ public int sumUp(String rNum) return sum; } - public int threeRow (String x) + public String cheatCalculation(String x) { - char y; - int number=0; - for (int i=0; i 2) return 2; + target=target+tested+tested; + } + else + { + for (j=0; j<4; j++) + { + target=target+tested; } - + } + if (x.contains(target)) return false; + target=""; } - - return 0; + return true; } + public boolean illegalCombos(String x) + { + String rNumerals = "IXC"; + String [] comparison = new String[3]; + comparison[0]="IVX"; + comparison[1]="XILC"; + comparison[2]="CIXDM"; + + int i, j; + for (i=0; i