From aa3e0129c555d430af369b21b51775ebd5fb3a65 Mon Sep 17 00:00:00 2001 From: abrime Date: Thu, 29 Sep 2016 16:52:06 +0300 Subject: [PATCH 1/3] . --- src/RomanNumerals.java | 101 ++++++++++++++++++++++++++++++++++- tests/TestRomanNumerals.java | 68 ++++++++++++++++++++++- 2 files changed, 165 insertions(+), 4 deletions(-) diff --git a/src/RomanNumerals.java b/src/RomanNumerals.java index 20904f0..3c45c5b 100644 --- a/src/RomanNumerals.java +++ b/src/RomanNumerals.java @@ -1,8 +1,105 @@ public class RomanNumerals { - public int convertToInteger(String romanNum) { + + public static int convertToInteger(String romanNum) { // To be Implemented + if(romanNum == "I") return 1; + + else + + if(romanNum == "V") return 5; + + else + + if(romanNum == "X") return 10; + + else + + if(romanNum == "L") return 50; + + else + + if(romanNum == "C") return 100; + + else + + if(romanNum == "D") return 500; + + else + + if(romanNum == "M") return 1000; return 0; - } + //The symbols'I''X''C'and'M'can be repeated at most 3 times in a row. + public static boolean repeatLessThanThreeTimesI(String numI) { + + // TODO Auto-generated method stub + int repeat=0; + for (int i = 0; i < numI.length(); i++) { + if (numI.charAt(i)=='I') { + repeat++; + } + } + + if (repeat<4) { + return true; + }else{ + return false; + } + + } + + public static boolean repeatLessThanThreeTimesX(String numX) { + + // TODO Auto-generated method stub + int repeat=0; + for (int i = 0; i < numX.length(); i++) { + if (numX.charAt(i)=='I') { + repeat++; + } + } + + if (repeat<4) { + return true; + }else{ + return false; + } + + } + public static boolean repeatLessThanThreeTimesC(String numC) { + + // TODO Auto-generated method stub + int repeat=0; + for (int i = 0; i < numC.length(); i++) { + if (numC.charAt(i)=='I') { + repeat++; + } + } + + if (repeat<4) { + return true; + }else{ + return false; + } + + } + public static boolean repeatLessThanThreeTimesM(String numM) { + + // TODO Auto-generated method stub + int repeat=0; + for (int i = 0; i < numM.length(); i++) { + if (numM.charAt(i)=='I') { + repeat++; + } + } + + if (repeat<4) { + return true; + }else{ + return false; + } + + } + + } diff --git a/tests/TestRomanNumerals.java b/tests/TestRomanNumerals.java index 5d1de75..d269422 100644 --- a/tests/TestRomanNumerals.java +++ b/tests/TestRomanNumerals.java @@ -5,8 +5,72 @@ public class TestRomanNumerals { @Test - public void test() { - fail("Not yet implemented"); + public void test_I_1() { + int romanNum = RomanNumerals.convertToInteger("I"); + assertEquals(1, romanNum); + + } + + @Test + public void test_V_5() { + int romanNum = RomanNumerals.convertToInteger("V"); + assertEquals(5, romanNum); + + } + + @Test + public void test_X_10() { + int romanNum = RomanNumerals.convertToInteger("X"); + assertEquals(10, romanNum); + + } + + @Test + public void test_L_50() { + int romanNum = RomanNumerals.convertToInteger("L"); + assertEquals(50, romanNum); + + } + + @Test + public void test_C_100() { + int romanNum = RomanNumerals.convertToInteger("C"); + assertEquals(100, romanNum); + + } + @Test + public void test_D_500() { + int romanNum = RomanNumerals.convertToInteger("D"); + assertEquals(500, romanNum); + + } + + @Test + public void test_M_1000() { + int romanNum = RomanNumerals.convertToInteger("M"); + assertEquals(1000, romanNum); + + } + + @Test + public void test_RepeatLessThanThreeTimesI(){ + String numI = ("XIII"); + assertTrue(RomanNumerals.repeatLessThanThreeTimesI(numI)); + } + @Test + public void test_RepeatLessThanThreeTimesX(){ + String numX = ("XX"); + assertTrue(RomanNumerals.repeatLessThanThreeTimesX(numX)); + } + @Test + public void test_RepeatLessThanThreeTimesC(){ + String numC = ("C"); + assertTrue(RomanNumerals.repeatLessThanThreeTimesC(numC)); + } + @Test + public void test_RepeatLessThanThreeTimesM(){ + String numM = ("M"); + assertTrue(RomanNumerals.repeatLessThanThreeTimesM(numM)); } } From 7fbd8a35e7757321e367cbf2fe4de40bf649bac3 Mon Sep 17 00:00:00 2001 From: abrime Date: Thu, 6 Oct 2016 15:19:53 +0300 Subject: [PATCH 2/3] . --- src/RomanNumerals.java | 65 ++++++++++++++++++++++++++++++++++++ tests/TestRomanNumerals.java | 19 +++++++++++ 2 files changed, 84 insertions(+) diff --git a/src/RomanNumerals.java b/src/RomanNumerals.java index 3c45c5b..b1872b5 100644 --- a/src/RomanNumerals.java +++ b/src/RomanNumerals.java @@ -76,6 +76,7 @@ public static boolean repeatLessThanThreeTimesC(String numC) { } } + if (repeat<4) { return true; }else{ @@ -101,5 +102,69 @@ public static boolean repeatLessThanThreeTimesM(String numM) { } + public static boolean neverRepeatV(String numV){ + + int repeat = 0; + + for (int i = 0; i < numV.length(); i++) { + if(numV.charAt(i) == 'V'){ + repeat ++; + } + } + if(repeat == 1){ + return true; + }else{ + return false; + } + } + + public static boolean neverRepeatL(String numL){ + + int repeat = 0; + + for (int i = 0; i < numL.length(); i++) { + if(numL.charAt(i) == 'L'){ + repeat ++; + } + } + if(repeat == 1){ + return true; + }else{ + return false; + } + } + + public static boolean neverRepeatD(String numD){ + + int repeat = 0; + + for (int i = 0; i < numD.length(); i++) { + if(numD.charAt(i) == 'D'){ + repeat ++; + } + } + if(repeat == 1 || repeat == 0){ + return true; + }else{ + return false; + } + } + + public static void substracted(String oneSymbol){ + + int repeat = 0; + + for (int i = 0; i < oneSymbol.length(); i++) { + + if(oneSymbol.charAt(i) == 'I'){ + repeat ++; + }else{ + if(repeat != 0 && ( (oneSymbol.charAt(i) == 'V') || (oneSymbol.charAt(i) == 'X'))){ + // onesymbol pass to int and then oneSymbol.charAt(i)-repeat + } + } + + } + } } diff --git a/tests/TestRomanNumerals.java b/tests/TestRomanNumerals.java index d269422..e0601ce 100644 --- a/tests/TestRomanNumerals.java +++ b/tests/TestRomanNumerals.java @@ -72,5 +72,24 @@ public void test_RepeatLessThanThreeTimesM(){ String numM = ("M"); assertTrue(RomanNumerals.repeatLessThanThreeTimesM(numM)); } + + @Test + public void test_neverRepeatV(){ + String numV = ("V"); + assertTrue(RomanNumerals.neverRepeatV(numV)); + } + + @Test + public void test_neverRepeatL(){ + String numL = ("L"); + assertTrue(RomanNumerals.neverRepeatL(numL)); + } + + @Test + public void test_neverRepeatD(){ + String numD = ("D"); + assertTrue(RomanNumerals.neverRepeatD(numD)); + } + } From 082b2dfcceb9df36a70801ccb94e0de7fd7a7df4 Mon Sep 17 00:00:00 2001 From: abrime Date: Thu, 6 Oct 2016 16:54:19 +0300 Subject: [PATCH 3/3] . --- src/RomanNumerals.java | 72 ++++++++++++++++++++++++++++++++++-- tests/TestRomanNumerals.java | 6 ++- 2 files changed, 73 insertions(+), 5 deletions(-) diff --git a/src/RomanNumerals.java b/src/RomanNumerals.java index b1872b5..8c7a97e 100644 --- a/src/RomanNumerals.java +++ b/src/RomanNumerals.java @@ -150,8 +150,15 @@ public static boolean neverRepeatD(String numD){ } } - public static void substracted(String oneSymbol){ - + + public static String substracted(String oneSymbol){ + + char J = 3; + char K = 8; + char R = 80; + char Z = 30; + char W = 300; + char Y = 800; int repeat = 0; for (int i = 0; i < oneSymbol.length(); i++) { @@ -160,11 +167,68 @@ public static void substracted(String oneSymbol){ repeat ++; }else{ - if(repeat != 0 && ( (oneSymbol.charAt(i) == 'V') || (oneSymbol.charAt(i) == 'X'))){ - // onesymbol pass to int and then oneSymbol.charAt(i)-repeat + if(repeat == 1 && ( (oneSymbol.charAt(i) == 'V') || (oneSymbol.charAt(i) == 'X'))){ + + if((oneSymbol.charAt(i) == 'V')){ + + char[] l = oneSymbol.toCharArray(); + l[i] = J; + oneSymbol = l.toString(); + + } + }else if(oneSymbol.charAt(i) == 'X'){ + + char[] l = oneSymbol.toCharArray(); + l[i] = K; + oneSymbol = l.toString(); + } } + + if(oneSymbol.charAt(i) == 'X'){ + repeat ++; + }else{ + if(repeat == 1 && ( (oneSymbol.charAt(i) == 'C') || (oneSymbol.charAt(i) == 'L'))){ + + if((oneSymbol.charAt(i) == 'C')){ + + char[] l = oneSymbol.toCharArray(); + l[i] = R; + oneSymbol = l.toString(); + + } + }else if(oneSymbol.charAt(i) == 'L'){ + + char[] l = oneSymbol.toCharArray(); + l[i] = Z; + oneSymbol = l.toString(); + + } + } + + if(oneSymbol.charAt(i) == 'C'){ + repeat ++; + }else{ + + if(repeat == 1 && ( (oneSymbol.charAt(i) == 'D') || (oneSymbol.charAt(i) == 'M'))){ + + if((oneSymbol.charAt(i) == 'D')){ + + char[] l = oneSymbol.toCharArray(); + l[i] = W; + oneSymbol = l.toString(); + + } + }else if(oneSymbol.charAt(i) == 'M'){ + + char[] l = oneSymbol.toCharArray(); + l[i] = Y; + oneSymbol = l.toString(); + + } + } } + return oneSymbol; } } diff --git a/tests/TestRomanNumerals.java b/tests/TestRomanNumerals.java index e0601ce..92e0289 100644 --- a/tests/TestRomanNumerals.java +++ b/tests/TestRomanNumerals.java @@ -91,5 +91,9 @@ public void test_neverRepeatD(){ assertTrue(RomanNumerals.neverRepeatD(numD)); } - + @Test + public void test_substracted(){ + String oneSymbol = ("CD"); + assertEquals("CW", oneSymbol); + } }